-
Notifications
You must be signed in to change notification settings - Fork 18
/
prmirror.go
175 lines (147 loc) · 5.15 KB
/
prmirror.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
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
"github.com/google/go-github/github"
)
// MirroredPR contains the upstream and downstream PR ids
type MirroredPR struct {
DownstreamID int
UpstreamID int
}
// PRMirror contains various different variables
type PRMirror struct {
GitHubClient *github.Client
Context *context.Context
Configuration *Config
Database *Database
GitLock *SpinLock
}
// GitHubEventMonitor passes in an instance of the PRMirror struct to all HTTP calls to the webhook listener
type GitHubEventMonitor struct {
Mirrorer PRMirror
}
// HandleEvent handles github events and acts like an event handler
func (p PRMirror) HandleEvent(event *github.Event) {
seenEvent, _ := p.Database.SeenEvent(event.GetID())
if seenEvent {
return
}
eventType := event.GetType()
if eventType != "PullRequestEvent" {
return
}
prEvent := github.PullRequestEvent{}
err := json.Unmarshal(event.GetRawPayload(), &prEvent)
if err != nil {
panic(err)
}
p.HandlePREvent(&prEvent)
p.Database.AddEvent(event.GetID())
}
func (p PRMirror) HandlePREvent(prEvent *github.PullRequestEvent) {
//repoName := prEvent.Repo.GetName()
//repoOwner := prEvent.Repo.Owner.GetName()
prEventURL := prEvent.PullRequest.GetURL()
//if p.Configuration.UseWebhook repoName != p.Configuration.DownstreamRepo {
// log.Warningf("Ignoring PR Event: %s because %s != %s\n", prEventURL, repoName, p.Configuration.UpstreamRepo)
// return
//} //else if repoOwner != p.Configuration.DownstreamOwner {
//log.Warningf("Ignoring PR Event: %s because %s != %s\n", prEventURL, repoOwner, p.Configuration.UpstreamOwner)
//return
//}
log.Debugf("Handling PR Event: %s\n", prEventURL)
prAction := prEvent.GetAction()
if prAction == "closed" && prEvent.PullRequest.GetMerged() == true && prEvent.PullRequest.Base.GetRef() == "master" {
prID, err := p.MirrorPR(prEvent.PullRequest)
if err != nil {
log.Errorf("Error while creating a new PR: %s\n", err.Error())
} else {
p.AddLabels(prID, []string{"Upstream PR Merged"})
p.Database.StoreMirror(prID, prEvent.PullRequest.GetNumber())
}
}
}
// RunEventScraper runs the GitHub repo event API scraper
func (p PRMirror) RunEventScraper() {
for {
events, pollInterval, err := p.GetRepoEvents()
if err == nil {
for _, event := range events {
p.HandleEvent(event)
}
}
log.Debugf("Sleeping for %d as specified by GitHub\n", pollInterval)
time.Sleep(time.Duration(pollInterval) * time.Second)
}
}
// ServeHTTP handles HTTP requests to the webhook endpoint
func (s GitHubEventMonitor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(s.Mirrorer.Configuration.WebhookSecret))
if err != nil {
log.Errorf("Error validating the payload\n")
return
}
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
log.Errorf("Error parsing the payload\n")
}
switch event := event.(type) {
case *github.PullRequestEvent:
s.Mirrorer.HandlePREvent(event)
}
}
// RunWebhookListener acts a webhook listener which GitHub will call with events
func (p PRMirror) RunWebhookListener() {
server := GitHubEventMonitor{Mirrorer: p}
err := http.ListenAndServe(fmt.Sprintf(":%d", p.Configuration.WebhookPort), server)
log.Fatal(err)
}
// MirrorPR will mirror a PR from an upstream to the downstream
func (p PRMirror) MirrorPR(pr *github.PullRequest) (int, error) {
p.GitLock.Lock()
defer p.GitLock.Unlock()
downstreamID, err := p.Database.GetDownstreamID(pr.GetNumber())
if downstreamID != 0 {
log.Warningf("Refusing to mirror already existing PR: %s - %s\n", pr.GetTitle(), pr.GetNumber())
return 0, errors.New("prmirror: tried to mirror a PR which has already been mirrored")
}
log.Infof("Mirroring PR [%d]: %s from %s\n", pr.GetNumber(), pr.GetTitle(), pr.User.GetLogin())
cmd := exec.Command(fmt.Sprintf("%s%s", p.Configuration.RepoPath, p.Configuration.ToolPath), strconv.Itoa(pr.GetNumber()), pr.GetTitle())
cmd.Dir = p.Configuration.RepoPath
cmdoutput, err := cmd.CombinedOutput()
if err != nil {
log.Criticalf("Error while mirroring %d: %s\n", pr.GetNumber(), err)
return 0, err
}
logpath := fmt.Sprintf("./logs/upstream-merge-%d.log", pr.GetNumber())
ioutil.WriteFile(logpath, cmdoutput, 0600)
log.Debugf("Wrote log to %s\n", logpath)
base := "master"
head := fmt.Sprintf("upstream-merge-%d", pr.GetNumber())
maintainerCanModify := true // We are the owner of the PR so we can specify this as true
title := fmt.Sprintf("[MIRROR] %s", pr.GetTitle())
body := fmt.Sprintf("Original PR: %s\n--------------------\n%s", pr.GetHTMLURL(), strings.Replace(pr.GetBody(), "@", "@ ", -1))
newPR := github.NewPullRequest{}
newPR.Title = &title
newPR.Body = &body
newPR.Base = &base
newPR.Head = &head
newPR.MaintainerCanModify = &maintainerCanModify
pr, _, err = p.GitHubClient.PullRequests.Create(*p.Context, p.Configuration.DownstreamOwner, p.Configuration.DownstreamRepo, &newPR)
if err != nil {
return 0, err
}
if strings.Contains(string(cmdoutput), "Rejected hunk") {
p.AddLabels(pr.GetNumber(), []string{"Auto Merge Rejections"})
}
return pr.GetNumber(), nil
}