-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
190 lines (160 loc) · 4.3 KB
/
Copy pathgit.go
File metadata and controls
190 lines (160 loc) · 4.3 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
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
package github
import (
"context"
"fmt"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v70/github"
)
// User information
type UserInfo struct {
Name string
Email string
}
// PullRequest information
type PullRequest struct {
SourceBranch string
TargetBranch string
}
// Clones the repository
func (ghApp *GitHubApp) Clone() error {
var err error
ghApp.gitClient, err = git.PlainClone(ghApp.Config.LocalPath, false, &git.CloneOptions{
URL: ghApp.Config.RepoURL,
Auth: &http.BasicAuth{
Username: "github",
Password: ghApp.Auth.Token,
},
})
if err != nil {
return err
}
ghApp.worktree, err = ghApp.gitClient.Worktree()
if err != nil {
return err
}
return nil
}
// Check if repo has local changes
func (ghApp *GitHubApp) HasChanges() bool {
status, err := ghApp.worktree.Status()
if err != nil {
return false
}
return !status.IsClean()
}
// Adds files to the repository
func (ghApp *GitHubApp) Add(path string) error {
_, err := ghApp.worktree.Add(path)
return err
}
// Commits the changes
func (ghApp *GitHubApp) Commit(msg string, user UserInfo) error {
_, err := ghApp.worktree.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: user.Name,
Email: user.Email,
When: time.Now(),
},
})
if err != nil {
return err
}
return nil
}
// Pushes the changes
func (ghApp *GitHubApp) Push() error {
err := ghApp.gitClient.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: "github",
Password: ghApp.Auth.Token,
},
})
if err != nil {
return err
}
return nil
}
// Create a new branch
func (ghApp *GitHubApp) NewBranch(name string, checkout bool) error {
return ghApp.worktree.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", name)),
Create: checkout,
})
}
// Polling configuration for waiting on a PR's CI checks. Declared as vars
// (rather than consts) so tests can shrink them to keep polling loops fast.
var (
checksPollInterval = 10 * time.Second
checksWaitTimeout = 10 * time.Minute
)
// Create new pull request
func (ghApp *GitHubApp) NewPullRequest(source, target, title, body string, merge bool) error {
ctx := context.Background()
// Create PR
newPR := &github.NewPullRequest{
Title: github.Ptr(title),
Head: github.Ptr(source), // source branch
Base: github.Ptr(target), // target branch
Body: github.Ptr(body),
MaintainerCanModify: github.Ptr(true),
}
pr, _, err := ghApp.githubClient.PullRequests.Create(ctx, "kununu", ghApp.Config.repoName, newPR)
if err != nil {
return err
}
if merge {
if ghApp.Config.WaitForChecksToPass {
err = waitForChecksToPass(ctx, ghApp.githubClient, "kununu", ghApp.Config.repoName, pr.GetHead().GetSHA())
if err != nil {
return err
}
}
// Merge PR
return mergePullRequest(ctx, ghApp.githubClient, "kununu", ghApp.Config.repoName, pr.GetNumber())
}
return nil
}
func waitForChecksToPass(ctx context.Context, client *github.Client, owner, repo, ref string) error {
ctx, cancel := context.WithTimeout(ctx, checksWaitTimeout)
defer cancel()
ticker := time.NewTicker(checksPollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return fmt.Errorf("timeout while waiting for checks to pass: %w", ctx.Err())
case <-ticker.C:
}
result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repo, ref, nil)
if err != nil {
return fmt.Errorf("listing check runs for %s: %w", ref, err)
}
pending := false
for _, run := range result.CheckRuns {
if run.GetStatus() != "completed" {
pending = true
continue
}
switch run.GetConclusion() {
case "success", "skipped", "neutral":
default:
return fmt.Errorf("check %q did not pass: %s", run.GetName(), run.GetConclusion())
}
}
if !pending && result.GetTotal() > 0 {
return nil
}
}
}
func mergePullRequest(ctx context.Context, client *github.Client, owner, repo string, number int) error {
opts := &github.PullRequestOptions{
MergeMethod: "squash", // or "merge" or "rebase"
}
_, _, err := client.PullRequests.Merge(ctx, owner, repo, number, "Automated merge by bot", opts)
return err
}