/
pull_request.go
100 lines (83 loc) · 2.58 KB
/
pull_request.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
package github
import (
"bytes"
"context"
"errors"
"strings"
"text/template"
"github.com/andygrunwald/gotrap/gerrit"
"github.com/google/go-github/github"
)
// createPullRequestForPatchset will create a new Pull Request at Github
// All information (like base and target branch) are received by the message by Gerrit
func (c GithubClient) CreatePullRequestForPatchset(m *gerrit.Message) (*github.PullRequest, error) {
// Remove "refs/" from the patchset reference,
// because if this patchset is synced to Github
// the branch is named without "refs/" as prefix.
baseRef := m.Patchset.Ref
if strings.HasPrefix(baseRef, "refs/") {
baseRef = baseRef[5:]
}
// Start polling until the branch is synced
// We have to wait, because after this we are able to continue
err := c.waitUntilBranchisSynced(baseRef)
if err != nil {
return nil, errors.New("Max loop reached for branch")
}
// Build title for Pull Request
titleBuffer := new(bytes.Buffer)
var titleTemplate = template.Must(template.New("pull-request-title").Parse(c.Conf.PRTemplate.Title))
err = titleTemplate.Execute(titleBuffer, m)
if err != nil {
return nil, err
}
title := titleBuffer.String()
// Build body for Pull Request
bodyString := strings.Join(c.Conf.PRTemplate.Body, "\n")
bodyBuffer := new(bytes.Buffer)
var bodyTemplate = template.Must(template.New("pull-request-body").Parse(bodyString))
err = bodyTemplate.Execute(bodyBuffer, m)
if err != nil {
return nil, err
}
body := bodyBuffer.String()
// Create the pull request itself
pr := &github.NewPullRequest{
Title: &title,
Head: &baseRef,
Base: &m.Change.Branch,
Body: &body,
}
ctx := context.Background()
prResult, resp, err := c.Client.PullRequests.Create(ctx, c.Conf.Organisation, c.Conf.Repository, pr)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return prResult, nil
}
func (c GithubClient) AddCommentToPullRequest(pr *github.PullRequest, message string) (bool, error) {
comment := &github.IssueComment{
Body: &message,
}
ctx := context.Background()
_, resp, err := c.Client.Issues.CreateComment(ctx, c.Conf.Organisation, c.Conf.Repository, *pr.Number, comment)
if err != nil {
return false, err
}
defer resp.Body.Close()
return true, nil
}
func (c GithubClient) ClosePullRequest(pr *github.PullRequest) (bool, error) {
state := "closed"
updatePr := &github.PullRequest{
State: &state,
}
ctx := context.Background()
_, resp, err := c.Client.PullRequests.Edit(ctx, c.Conf.Organisation, c.Conf.Repository, *pr.Number, updatePr)
if err != nil {
return false, err
}
defer resp.Body.Close()
return true, nil
}