-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
162 lines (138 loc) · 3.71 KB
/
git.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
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/v54/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,
})
}
// Create new pull request
func (ghApp *GitHubApp) NewPullRequest(source, target, title, body string) error {
ctx := context.Background()
// Create PR
newPR := &github.NewPullRequest{
Title: github.String(title),
Head: github.String(source), // source branch
Base: github.String(target), // target branch
Body: github.String(body),
MaintainerCanModify: github.Bool(true),
}
pr, _, err := ghApp.githubClient.PullRequests.Create(ctx, "kununu", ghApp.Config.repoName, newPR)
if err != nil {
return err
}
// Wait for checks to pass
err = waitForChecksToPass(ctx, ghApp.githubClient, "kununu", ghApp.Config.repoName, pr.GetNumber())
if err != nil {
return err
}
// Merge PR
return mergePullRequest(ctx, ghApp.githubClient, "kununu", ghApp.Config.repoName, pr.GetNumber())
}
func waitForChecksToPass(ctx context.Context, client *github.Client, owner, repo string, number int) error {
// Polling interval and timeout can be adjusted based on your requirements
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
timeout := time.After(10 * time.Minute)
for {
select {
case <-timeout:
return fmt.Errorf("timeout while waiting for checks to pass")
case <-ticker.C:
combined, _, err := client.Repositories.GetCombinedStatus(ctx, owner, repo, fmt.Sprintf("pull/%d/head", number), nil)
if err != nil {
return err
}
if combined.GetState() == "success" {
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
}