forked from desbo/merge-pr-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
131 lines (102 loc) · 2.7 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
package main
import (
"context"
"errors"
"fmt"
"log"
"math"
"net/http"
"strings"
"time"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)
const maxRefetches = 4
var ErrNotMergeable = errors.New("PR not mergeable")
var ErrConflict = errors.New("PR has conflicts")
var ErrBehind = errors.New("PR is behind base branch")
type authenticatedGitHubClient struct {
ctx context.Context
client *github.Client
}
func newAuthenticatedClient(token string) *authenticatedGitHubClient {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return &authenticatedGitHubClient{ctx, client}
}
func (c *authenticatedGitHubClient) updatePRBranch(pr *github.PullRequest) error {
_, response, err := c.client.PullRequests.UpdateBranch(
c.ctx,
pr.Base.Repo.Owner.GetLogin(),
pr.Base.Repo.GetName(),
pr.GetNumber(),
&github.PullRequestBranchUpdateOptions{},
)
if err != nil {
return err
}
if response.StatusCode != http.StatusAccepted {
return fmt.Errorf("status %v when updating branch %v", response.Status, pr.Head.Label)
}
return nil
}
func (c *authenticatedGitHubClient) refetchPR(pr *github.PullRequest) (*github.PullRequest, error) {
pr, _, err := c.client.PullRequests.Get(
c.ctx,
pr.Base.Repo.Owner.GetLogin(),
pr.Base.Repo.GetName(),
pr.GetNumber(),
)
if err != nil {
return nil, fmt.Errorf("error refetching PR: %w", err)
}
return pr, nil
}
func (c *authenticatedGitHubClient) mergePR(pr *github.PullRequest, mergeMethod string, attempt int) error {
state := pr.GetMergeableState()
if strings.EqualFold(state, "dirty") {
return ErrConflict
}
if strings.EqualFold(state, "behind") {
return ErrBehind
}
if strings.EqualFold(state, "unknown") {
log.Println("PR mergable state unknown, refetching")
if attempt+1 == maxRefetches {
return fmt.Errorf("%w, state: %v. giving up after %v retries", ErrNotMergeable, state, attempt)
}
updatedPR, err := c.refetchPR(pr)
if err != nil {
return err
}
delay := time.Duration(math.Pow(2, float64(attempt))) * time.Second
time.Sleep(delay)
return c.mergePR(updatedPR, mergeMethod, attempt+1)
}
if !pr.GetMergeable() {
return fmt.Errorf("%w, state: %v", ErrNotMergeable, state)
}
options := &github.PullRequestOptions{
MergeMethod: strings.ToLower(mergeMethod),
}
result, _, err := c.client.PullRequests.Merge(
c.ctx,
pr.Base.Repo.Owner.GetLogin(),
pr.Base.Repo.GetName(),
pr.GetNumber(),
"",
options,
)
if err != nil {
return err
}
if !result.GetMerged() {
return fmt.Errorf("PR was not merged: %v", result.GetMessage())
}
log.Print(result.GetMessage())
return nil
}