-
Notifications
You must be signed in to change notification settings - Fork 0
/
githelper.go
352 lines (307 loc) · 9.4 KB
/
githelper.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"time"
"github.com/Khan/genqlient/graphql"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"go.uber.org/zap"
)
var (
hclPath = "envs/dev/terragrunt.hcl"
reExpression = `(github\.com/liatrio/dora-lambda-tf-module-demo\?ref=)v\d+\.\d+\.\d+`
upToDateReExpression = `(github\.com/liatrio/dora-lambda-tf-module-demo\?ref=)v0.6.2`
)
type authedTransport struct {
key string
wrapped http.RoundTripper
}
func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "bearer "+t.key)
return t.wrapped.RoundTrip(req)
}
type GitHubRepoContext struct {
gitHubDomain string
pat string
client graphql.Client
name string
org string
remoteRepoUrl string
logger *zap.Logger
// localDir string
// repo *git.Repository
}
func (ghc *GitHubRepoContext) generateClient(url string) graphql.Client {
httpClient := http.Client{
Transport: &authedTransport{
key: ghc.pat,
wrapped: http.DefaultTransport,
},
}
return graphql.NewClient(url, &httpClient)
}
func (ghc *GitHubRepoContext) CalculateRepoUrl() (string, error) {
url, err := url.JoinPath(ghc.gitHubDomain, ghc.org, ghc.name)
if err != nil {
return "", err
}
return url + ".git", nil
}
// If no deployments exist, return nil
func (ghrc *GitHubRepoContext) GetLastDeployment(ctx context.Context) (*getLatestDeploymentsRepositoryDeploymentsDeploymentConnectionNodesDeployment, error) {
recentDeployments, err := getLatestDeployments(ctx, ghrc.client, ghrc.org, ghrc.name)
if err != nil {
logger.Sugar().Errorf("Error getting latest deployments for %s/%s: %s", ghrc.org, ghrc.name, err)
return nil, err
}
if len(recentDeployments.Repository.Deployments.Nodes) == 0 {
logger.Sugar().Infof("No deployments found for %s/%s", ghrc.org, ghrc.name)
return nil, nil
}
// return the last deployment
return &recentDeployments.Repository.Deployments.Nodes[len(recentDeployments.Repository.Deployments.Nodes)-1], nil
}
// This function will wait for up to 10 minutes for the deployment to complete
func (ghrc *GitHubRepoContext) WaitForDeployment(ctx context.Context, sha string) error {
logger.Sugar().Infof("Waiting for Deploy workflow to complete for %s", sha)
timeout := time.After(10 * time.Minute)
tick := time.Tick(10 * time.Second)
for {
select {
case <-timeout:
return errors.New("Timed out after 10 minutes waiting for deployment")
case <-tick:
commitGitHubActionRuns, err := getCommitGitHubActionsRuns(ctx, ghrc.client, ghrc.org, ghrc.name, sha)
if err != nil {
return err
}
if commit, ok := commitGitHubActionRuns.Repository.Object.(*getCommitGitHubActionsRunsRepositoryObjectCommit); ok {
for _, node := range commit.StatusCheckRollup.Contexts.Nodes {
if cr, ok := node.(*GitHubActionCheckRun); ok {
if cr.Name != "deploy" {
continue
}
switch cr.Status {
case "COMPLETED":
if cr.Conclusion == "SUCCESS" {
return nil
} else {
return errors.New("Deployment failed")
}
case "IN_PROGRESS":
continue
case "QUEUED":
continue
case "REQUESTED":
continue
default:
return fmt.Errorf("Unknown deployment state: %s", cr.Status)
}
}
}
} else {
return errors.New("Error getting commit")
}
// switch deploymentAction {
// // case "SUCCESS":
// // return nil
// // case "FAILURE":
// // return errors.New("Deployment failed")
// // case "PENDING":
// // continue
// // case "ERROR":
// // return errors.New("Deployment errored")
// // case "EXPECTED":
// // continue
// default:
// return fmt.Errorf("Unknown deployment state: %s", deploymentAction)
// }
}
}
}
// This function will generate a deployment for the given repository
// Each call it will clone the remote repository to a temp directory.
// It will create a branch, make a change, commit the change, and push the
// branch to the remote. Create a Pull Request and Merge it.
// Workflows will then run to create a Deployment.
func (ghrc *GitHubRepoContext) GeneratePullRequest(ctx context.Context, logger *zap.Logger) (prId *createPullRequestResponse, err error) {
// Create a temp directory and clone the repository
dir, err := os.MkdirTemp("", "cloned-repo")
if err != nil {
logger.Sugar().Errorf("Error creating temp dir: %s", err)
return
}
logger.Sugar().Infof("Temp dir is: %v", dir)
defer os.RemoveAll(dir) // clean up
// Clones the repository into the given dir, just as a normal git clone does
repo, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: ghrc.remoteRepoUrl,
Auth: &githttp.BasicAuth{
Username: "dora-the-explorer",
Password: ghrc.pat,
},
})
if err != nil {
logger.Sugar().Errorf("Error cloning repository: %s", err)
return
}
head, err := repo.Head()
if err != nil {
logger.Sugar().Errorf("Error getting HEAD: %s", err)
return
}
baseRefName := head.Name().Short()
// Generate a remote branch with a change to the repo
branchName, err := GenerateChangeRemoteBranch(dir, ghrc, repo, logger)
// Create a Pull Request
repoIdResp, err := getRepoId(ctx, ghrc.client, ghrc.org, ghrc.name)
if err != nil {
logger.Sugar().Errorf("Error getting repo ID: %s", err)
return
}
prId, err = createPullRequest(ctx,
ghrc.client,
baseRefName,
"Generated by Dora the Explorer",
branchName,
repoIdResp.Repository.Id,
"fix: Change app version")
logger.Sugar().Infof("Created PR: %d", prId.CreatePullRequest.PullRequest.Number)
return prId, err
}
func (ghrc *GitHubRepoContext) UpdateBaseBranch() {
}
// This function will wait for up to 10 min for the status checks to complete
func (ghrc *GitHubRepoContext) WaitForStatusChecks(ctx context.Context, prNumber int) error {
logger.Sugar().Infof("Waiting for status checks for PR %d", prNumber)
timeout := time.After(10 * time.Minute)
tick := time.Tick(10 * time.Second)
for {
select {
case <-timeout:
return errors.New("Timed out after 10 minutes waiting for status checks")
case <-tick:
pr, err := getPullRequestStatusCheckRollup(ctx,
ghrc.client,
ghrc.org,
ghrc.name,
prNumber)
if err != nil {
return err
}
switch pr.Repository.PullRequest.StatusCheckRollup.State {
case "SUCCESS":
return nil
case "FAILURE":
return fmt.Errorf("PR %d failed status checks", prNumber)
case "PENDING":
continue
case "ERROR":
return fmt.Errorf("PR %d errored status checks", prNumber)
case "EXPECTED":
continue
default:
return fmt.Errorf("Unknown status check state: %s", pr.Repository.PullRequest.StatusCheckRollup.State)
}
}
}
}
func NeedsDowngrade(dir string) (bool, error) {
f := filepath.Join(dir, hclPath)
bb, err := os.ReadFile(f)
if err != nil {
logger.Sugar().Errorf("Error reading file: %s", err)
return false, err
}
re := regexp.MustCompile(upToDateReExpression)
return re.Match(bb), nil
}
func GenerateChangeRemoteBranch(
dir string,
ghrc *GitHubRepoContext,
repo *git.Repository,
logger *zap.Logger) (string, error) {
// Create a new branch
worktree, err := repo.Worktree()
if err != nil {
logger.Sugar().Errorf("Error getting worktree: %s", err)
return "", err
}
epochMilliseconds := time.Now().UnixMilli()
branchName := "dora-the-explorer-" + strconv.FormatInt(epochMilliseconds, 10)
newBranch := plumbing.NewBranchReferenceName(branchName)
err = worktree.Checkout(&git.CheckoutOptions{
Branch: newBranch,
Create: true,
})
if err != nil {
logger.Sugar().Errorf("Error creating new branch: %s", err)
return "", err
}
// Make changes (if any)
f := filepath.Join(dir, hclPath)
bb, err := os.ReadFile(f)
if err != nil {
logger.Sugar().Errorf("Error reading file: %s", err)
return "", err
}
changeString := "v0.6.2"
if needsDowngrade, err := NeedsDowngrade(dir); err == nil && needsDowngrade {
changeString = "v0.3.0"
} else if err != nil {
logger.Sugar().Errorf("Error checking for downgrade: %s", err)
return "", err
}
re := regexp.MustCompile(reExpression)
updatedContent := re.ReplaceAll(bb, []byte("${1}"+changeString))
err = os.WriteFile(f, updatedContent, 0600)
if err != nil {
logger.Sugar().Errorf("Error writing to file: %s", err)
return "", err
}
// Add the file to the staging area
_, err = worktree.Add("envs/dev/terragrunt.hcl")
if err != nil {
logger.Sugar().Errorf("Error adding file to staging area: %s", err)
return "", err
}
// Commit the changes
_, err = worktree.Commit("Updated version in terragrunt.hcl", &git.CommitOptions{
Author: &object.Signature{
Name: "Bill Murray",
Email: "[email protected]",
When: time.Now(),
},
})
if err != nil {
logger.Sugar().Errorf("Error committing changes: %s", err)
return "", err
}
// Push the new branch to the remote repository
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &githttp.BasicAuth{
Username: "trashpandas",
Password: ghrc.pat,
},
RefSpecs: []config.RefSpec{
config.RefSpec("refs/heads/" + branchName + ":refs/heads/" + branchName),
},
})
if err != nil {
logger.Sugar().Errorf("Error pushing to remote: %s", err)
return "", err
}
return branchName, nil
}