This repository was archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub.go
139 lines (122 loc) · 3.84 KB
/
github.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
package connectors
import (
"context"
"fmt"
"io/ioutil"
"os"
gitv1 "github.com/flanksource/git-operator/api/v1"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/osfs"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-logr/logr"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/factory"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Github struct {
k8sCrd client.Client
logr.Logger
scm *scm.Client
repo *git.Repository
auth transport.AuthMethod
owner string
repoName string
repository string
}
func NewGithub(client client.Client, log logr.Logger, owner, repoName, githubToken string) (Connector, error) {
scmClient, err := factory.NewClient("github", "", githubToken)
if err != nil {
return nil, errors.Wrap(err, "failed to create github client")
}
github := &Github{
k8sCrd: client,
Logger: log.WithName("Github").WithName(owner + "/" + repoName),
scm: scmClient,
owner: owner,
repoName: repoName,
repository: owner + "/" + repoName,
auth: &http.BasicAuth{Password: githubToken, Username: githubToken},
}
return github, nil
}
func (g *Github) Push(ctx context.Context, branch string) error {
if g.repo == nil {
return errors.New("Need to clone first, before pushing ")
}
g.V(1).Info("Pushing", "branch", branch)
if err := g.repo.Push(&git.PushOptions{
Auth: g.auth,
Progress: os.Stdout,
}); err != nil {
return err
}
ref, _ := g.repo.Head()
g.Info("Pushed", "branch", branch, "ref", ref.String())
return nil
}
func (g *Github) OpenPullRequest(ctx context.Context, base string, head string, spec *gitv1.PullRequestTemplate) (int, error) {
if spec.Title == "" {
spec.Title = head
}
g.V(1).Info("Creating PR", "title", spec.Title, "head", head, "base", base)
pr, _, err := g.scm.PullRequests.Create(ctx, g.repository, &scm.PullRequestInput{
Title: spec.Title,
Body: spec.Body,
Head: head,
Base: base,
})
if err != nil {
return 0, errors.Wrapf(err, "failed to create pr repo=%s title=%s, head=%s base=%s", g.repository, spec.Title, head, base)
}
g.Info("PR created", "pr", pr.Number, "repository", g.repository)
if len(spec.Reviewers) > 0 {
g.Info("Requesting Reviews", "pr", pr.Number, "repository", g.repository, "reviewers", spec.Reviewers)
if _, err := g.scm.PullRequests.RequestReview(ctx, g.repository, pr.Number, spec.Reviewers); err != nil {
return 0, err
}
}
if len(spec.Assignees) > 0 {
g.Info("Assigning PR", "pr", pr.Number, "repository", g.repoName, "assignees", spec.Assignees)
if _, err := g.scm.PullRequests.AssignIssue(ctx, g.repository, pr.Number, spec.Assignees); err != nil {
return 0, err
}
}
return pr.Number, nil
}
func (g *Github) ClosePullRequest(ctx context.Context, id int) error {
if _, err := g.scm.PullRequests.Close(ctx, g.repository, id); err != nil {
return errors.Wrap(err, "failed to close github pull request")
}
return nil
}
func (g *Github) Clone(ctx context.Context, branch, local string) (billy.Filesystem, *git.Worktree, error) {
dir, _ := ioutil.TempDir("", "git-*")
url := fmt.Sprintf("https://github.com/%s/%s.git", g.owner, g.repoName)
g.Info("Cloning", "temp", dir)
repo, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
ReferenceName: plumbing.NewBranchReferenceName(branch),
URL: url,
Progress: os.Stdout,
Auth: g.auth,
})
if err != nil {
return nil, nil, err
}
g.repo = repo
work, err := repo.Worktree()
if err != nil {
return nil, nil, err
}
if branch != local {
// nolint: errcheck
work.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(local),
Create: true,
})
}
return osfs.New(dir), work, nil
}