/
github_dependabot.go
226 lines (182 loc) · 7.59 KB
/
github_dependabot.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"fmt"
"log"
"github.com/cenkalti/backoff/v4"
"github.com/google/go-github/v61/github"
)
// GitHubDependabotSecrets for GitHub Dependabot secrets management.
type GitHubDependabotSecrets interface {
PutDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error
GetDependabotPublicKey(ctx context.Context, owner, repo string) (*github.PublicKey, *github.Response, error)
CreateOrUpdateDependabotSecret(ctx context.Context, owner, repo string, eSecret *github.DependabotEncryptedSecret) (*github.Response, error)
DeleteDependabotSecret(ctx context.Context, owner, repo, name string) (*github.Response, error)
ListDependabotSecrets(ctx context.Context, owner, repo string, opts *github.ListOptions) (*github.Secrets, *github.Response, error)
SyncDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error
}
func (api *gitHubAPI) GetDependabotPublicKey(ctx context.Context, owner, repo string) (*github.PublicKey, *github.Response, error) {
return api.client.Dependabot.GetRepoPublicKey(ctx, owner, repo)
}
func (api *gitHubAPI) CreateOrUpdateDependabotSecret(ctx context.Context, owner, repo string, eSecret *github.DependabotEncryptedSecret) (*github.Response, error) {
return api.client.Dependabot.CreateOrUpdateRepoSecret(ctx, owner, repo, eSecret)
}
func (api *gitHubAPI) DeleteDependabotSecret(ctx context.Context, owner, repo, name string) (*github.Response, error) {
return api.client.Dependabot.DeleteRepoSecret(ctx, owner, repo, name)
}
func (api *gitHubAPI) ListDependabotSecrets(ctx context.Context, owner, repo string, opts *github.ListOptions) (*github.Secrets, *github.Response, error) {
return api.client.Dependabot.ListRepoSecrets(ctx, owner, repo, opts)
}
func (api *gitHubAPI) PutDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
if api.dryRunEnabled {
log.Printf("Dry run: Putting Dependabot secrets for repo %s/%s", owner, repo)
for secretName := range mappings {
log.Printf("Dry run: Would put Dependabot secret '%s' in repo %s/%s", secretName, owner, repo)
}
return nil
}
publicKey, _, err := api.GetDependabotPublicKey(ctx, owner, repo)
if err != nil {
return err
}
for secretName, secretValue := range mappings {
encryptedSecret, err := encryptDependabotWithPublicKey(publicKey, secretName, secretValue)
if err != nil {
return err
}
_, err = api.CreateOrUpdateDependabotSecret(ctx, owner, repo, encryptedSecret)
if err != nil {
return err
}
}
return nil
}
func (api *gitHubAPI) SyncDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
if api.dryRunEnabled {
log.Printf("Dry run: Syncing Dependabot secrets for repo %s/%s", owner, repo)
opts := &github.ListOptions{PerPage: 100}
for {
secrets, resp, err := api.ListDependabotSecrets(ctx, owner, repo, opts)
if err != nil {
return fmt.Errorf("dry run: failed to list existing Dependabot secrets: %v", err)
}
for _, secret := range secrets.Secrets {
if _, exists := mappings[secret.Name]; !exists {
log.Printf("Dry run: Would delete Dependabot secret '%s' from repo %s/%s", secret.Name, owner, repo)
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for secretName := range mappings {
log.Printf("Dry run: Would add/update Dependabot secret '%s' in repo %s/%s", secretName, owner, repo)
}
return nil
}
existingMap := make(map[string]bool)
opts := &github.ListOptions{PerPage: 100}
for {
secrets, resp, err := api.ListDependabotSecrets(ctx, owner, repo, opts)
if err != nil {
return err
}
for _, secret := range secrets.Secrets {
existingMap[secret.Name] = true
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for secretName := range existingMap {
if _, exists := mappings[secretName]; !exists {
_, err := api.DeleteDependabotSecret(ctx, owner, repo, secretName)
if err != nil {
return err
}
}
}
return api.PutDependabotSecrets(ctx, owner, repo, mappings)
}
// Ratelimiting
func (r *rateLimitedGitHubAPI) PutDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
r.ensureRatelimits(ctx)
return r.client.PutDependabotSecrets(ctx, owner, repo, mappings)
}
func (r *rateLimitedGitHubAPI) GetDependabotPublicKey(ctx context.Context, owner, repo string) (*github.PublicKey, *github.Response, error) {
r.ensureRatelimits(ctx)
return r.client.GetDependabotPublicKey(ctx, owner, repo)
}
func (r *rateLimitedGitHubAPI) CreateOrUpdateDependabotSecret(ctx context.Context, owner, repo string, eSecret *github.DependabotEncryptedSecret) (*github.Response, error) {
r.ensureRatelimits(ctx)
return r.client.CreateOrUpdateDependabotSecret(ctx, owner, repo, eSecret)
}
func (r *rateLimitedGitHubAPI) DeleteDependabotSecret(ctx context.Context, owner, repo, name string) (*github.Response, error) {
r.ensureRatelimits(ctx)
return r.client.DeleteDependabotSecret(ctx, owner, repo, name)
}
func (r *rateLimitedGitHubAPI) ListDependabotSecrets(ctx context.Context, owner, repo string, opts *github.ListOptions) (*github.Secrets, *github.Response, error) {
r.ensureRatelimits(ctx)
return r.client.ListDependabotSecrets(ctx, owner, repo, opts)
}
func (r *rateLimitedGitHubAPI) SyncDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
r.ensureRatelimits(ctx)
return r.client.SyncDependabotSecrets(ctx, owner, repo, mappings)
}
// Retry
func (r *retryableGitHubAPI) GetDependabotPublicKey(ctx context.Context, owner, repo string) (*github.PublicKey, *github.Response, error) {
var publicKey *github.PublicKey
var resp *github.Response
var err error
retryFunc := func() error {
publicKey, resp, err = r.client.GetDependabotPublicKey(ctx, owner, repo)
return err
}
err = backoff.Retry(retryFunc, r.backoffOptions)
return publicKey, resp, err
}
func (r *retryableGitHubAPI) CreateOrUpdateDependabotSecret(ctx context.Context, owner, repo string, eSecret *github.DependabotEncryptedSecret) (*github.Response, error) {
var resp *github.Response
var err error
retryFunc := func() error {
resp, err = r.client.CreateOrUpdateDependabotSecret(ctx, owner, repo, eSecret)
return err
}
err = backoff.Retry(retryFunc, r.backoffOptions)
return resp, err
}
func (r *retryableGitHubAPI) DeleteDependabotSecret(ctx context.Context, owner, repo, name string) (*github.Response, error) {
var resp *github.Response
var err error
retryFunc := func() error {
resp, err = r.client.DeleteDependabotSecret(ctx, owner, repo, name)
return err
}
err = backoff.Retry(retryFunc, r.backoffOptions)
return resp, err
}
func (r *retryableGitHubAPI) ListDependabotSecrets(ctx context.Context, owner, repo string, opts *github.ListOptions) (*github.Secrets, *github.Response, error) {
var secrets *github.Secrets
var resp *github.Response
var err error
retryFunc := func() error {
secrets, resp, err = r.client.ListDependabotSecrets(ctx, owner, repo, opts)
return err
}
err = backoff.Retry(retryFunc, r.backoffOptions)
return secrets, resp, err
}
func (r *retryableGitHubAPI) SyncDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
retryFunc := func() error {
return r.client.SyncDependabotSecrets(ctx, owner, repo, mappings)
}
return backoff.Retry(retryFunc, r.backoffOptions)
}
func (r *retryableGitHubAPI) PutDependabotSecrets(ctx context.Context, owner, repo string, mappings map[string]string) error {
retryFunc := func() error {
return r.client.PutDependabotSecrets(ctx, owner, repo, mappings)
}
return backoff.Retry(retryFunc, r.backoffOptions)
}