-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
122 lines (105 loc) · 2.54 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
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 string, target string, title string, body string) error {
// 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),
}
ctx := context.Background()
_, _, err := ghApp.githubClient.PullRequests.Create(ctx, "kununu", ghApp.Config.repoName, newPR)
if err != nil {
return err
}
return nil
}