forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_branch_protection_rules.go
More file actions
87 lines (76 loc) · 2.12 KB
/
data_source_github_branch_protection_rules.go
File metadata and controls
87 lines (76 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package github
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/shurcooL/githubv4"
)
func dataSourceGithubBranchProtectionRules() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubBranchProtectionRulesRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"rules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pattern": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubBranchProtectionRulesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
var query struct {
Repository struct {
ID githubv4.String
BranchProtectionRules struct {
Nodes []struct {
Pattern githubv4.String
}
PageInfo PageInfo
} `graphql:"branchProtectionRules(first:$first, after:$cursor)"`
} `graphql:"repository(name: $name, owner: $owner)"`
}
variables := map[string]any{
"first": githubv4.Int(100),
"name": githubv4.String(repoName),
"owner": githubv4.String(orgName),
"cursor": (*githubv4.String)(nil),
}
var rules []any
for {
err := client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return diag.FromErr(err)
}
additionalRules := make([]any, len(query.Repository.BranchProtectionRules.Nodes))
for i, rule := range query.Repository.BranchProtectionRules.Nodes {
r := make(map[string]any)
r["pattern"] = rule.Pattern
additionalRules[i] = r
}
rules = append(rules, additionalRules...)
if !query.Repository.BranchProtectionRules.PageInfo.HasNextPage {
break
}
variables["cursor"] = new(query.Repository.BranchProtectionRules.PageInfo.EndCursor)
}
d.SetId(string(query.Repository.ID))
err := d.Set("rules", rules)
if err != nil {
return diag.FromErr(err)
}
return nil
}