/
github.go
146 lines (115 loc) · 3.26 KB
/
github.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package github
import (
"context"
"fmt"
"log"
"time"
"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/google/go-github/v50/github"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type Repository github.Repository
type Workflow github.Workflow
const githubDelay = 720 * time.Millisecond
type BranchProtectionRule struct {
Nodes []struct {
Id string
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
}
// type structure for the graphql query GetOrganizationsInEnterprise
type Organization struct {
Nodes []struct {
Login string
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
}
var (
ctx context.Context
clientV3 *github.Client
clientV4 *githubv4.Client
accessToken string
)
func checkClients(token string, url string) error {
// Sleep to avoid hitting the API rate limit.
time.Sleep(githubDelay)
if clientV3 == nil || clientV4 == nil || token != accessToken {
accessToken = token
ctx = context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(tc.Transport)
if err != nil {
return err
}
clientV3, err = github.NewEnterpriseClient(url, url, rateLimiter)
if err != nil {
return err
}
var query string
if url == "https://api.github.com" {
query = fmt.Sprintf("%s/graphql", url)
} else {
query = fmt.Sprintf("%s/api/graphql", url)
}
clientV4 = githubv4.NewEnterpriseClient(query, rateLimiter)
}
return nil
}
func logTokenRateLimit(response *github.Response) {
log.Printf("Quota remaining: %d, Limit: %d, Reset: %s", response.Rate.Remaining, response.Rate.Limit, response.Rate.Reset)
}
func ChangeGHASOrgSettings(organization string, activate bool, token string, url string) error {
checkClients(token, url)
//create new organization object
newOrgSettings := github.Organization{
AdvancedSecurityEnabledForNewRepos: &activate,
SecretScanningPushProtectionEnabledForNewRepos: &activate,
SecretScanningEnabledForNewRepos: &activate,
}
// Update the organization
_, response, err := clientV3.Organizations.Edit(ctx, organization, &newOrgSettings)
logTokenRateLimit(response)
if err != nil {
log.Println("Error updating organization settings: ", err)
}
return err
}
func GetOrganizationsInEnterprise(enterprise string, token string, url string) ([]string, error) {
checkClients(token, url)
var query struct {
Enterprise struct {
Organizations Organization `graphql:"organizations(first: 100, after: $cursor)"`
} `graphql:"enterprise(slug: $enterprise)"`
}
variables := map[string]interface{}{
"enterprise": githubv4.String(enterprise),
"cursor": (*githubv4.String)(nil),
}
fmt.Println(query)
results := make([]string, 0)
for {
err := clientV4.Query(ctx, &query, variables)
if err != nil {
log.Println("Error querying organizations: ", err)
return nil, err
}
for _, organization := range query.Enterprise.Organizations.Nodes {
results = append(results, organization.Login)
}
variables["cursor"] = query.Enterprise.Organizations.PageInfo.EndCursor
if !query.Enterprise.Organizations.PageInfo.HasNextPage {
break
}
}
return results, nil
}