@@ -21,6 +21,8 @@ const (
21
21
githubStatusPeerReviewContext = "review/peer"
22
22
)
23
23
24
+ type retryGithubOperation func (func () asyncResponse ) MaybeSyncResponse
25
+
24
26
func main () {
25
27
conf := NewConfig ()
26
28
githubClient := initGithubClient (conf .AccessToken )
@@ -48,8 +50,13 @@ func main() {
48
50
asyncOperationWg .Wait ()
49
51
}
50
52
51
- func CreateHandler (conf Config , gitRepos git.Repos , asyncOperationWg * sync.WaitGroup , pullRequests PullRequests ,
52
- repositories Repositories , issues Issues , search Search ) Handler {
53
+ func CreateHandler (conf Config , gitRepos git.Repos , asyncOperationWg * sync.WaitGroup ,
54
+ pullRequests PullRequests , repositories Repositories , issues Issues , search Search ) Handler {
55
+
56
+ retry := func (operation func () asyncResponse ) MaybeSyncResponse {
57
+ return delayWithRetries (conf .GithubAPITryDeltas , operation , asyncOperationWg )
58
+ }
59
+
53
60
return func (w http.ResponseWriter , r * http.Request ) Response {
54
61
body , err := ioutil .ReadAll (r .Body )
55
62
if err != nil {
@@ -61,19 +68,18 @@ func CreateHandler(conf Config, gitRepos git.Repos, asyncOperationWg *sync.WaitG
61
68
eventType := r .Header .Get ("X-Github-Event" )
62
69
switch eventType {
63
70
case "issue_comment" :
64
- return handleIssueComment (body , conf , asyncOperationWg , gitRepos , pullRequests , repositories , issues )
71
+ return handleIssueComment (body , retry , gitRepos , pullRequests , repositories , issues )
65
72
case "pull_request" :
66
- return handlePullRequestEvent (body , conf , asyncOperationWg , pullRequests , repositories )
73
+ return handlePullRequestEvent (body , retry , pullRequests , repositories )
67
74
case "status" :
68
- return handleStatusEvent (body , conf , asyncOperationWg , gitRepos , search , issues , pullRequests )
75
+ return handleStatusEvent (body , retry , gitRepos , search , issues , pullRequests )
69
76
}
70
77
return SuccessResponse {"Not an event I understand. Ignoring." }
71
78
}
72
79
}
73
80
74
- func handleIssueComment (body []byte , conf Config , asyncOperationWg * sync.WaitGroup ,
75
- gitRepos git.Repos , pullRequests PullRequests , repositories Repositories ,
76
- issues Issues ) Response {
81
+ func handleIssueComment (body []byte , retry retryGithubOperation , gitRepos git.Repos ,
82
+ pullRequests PullRequests , repositories Repositories , issues Issues ) Response {
77
83
78
84
issueComment , err := parseIssueComment (body )
79
85
if err != nil {
@@ -97,35 +103,36 @@ func handleIssueComment(body []byte, conf Config, asyncOperationWg *sync.WaitGro
97
103
case mergeCommand :
98
104
return handleMergeCommand (issueComment , issues , pullRequests , repositories , gitRepos )
99
105
case checkCommand :
100
- return checkForFixupCommitsOnIssueComment (issueComment , pullRequests , repositories , conf , asyncOperationWg )
106
+ return checkForFixupCommitsOnIssueComment (issueComment , pullRequests , repositories , retry )
101
107
}
102
108
return ErrorResponse {
103
109
Code : http .StatusInternalServerError ,
104
110
ErrorMessage : fmt .Sprintf ("Unhandled comment type: %v" , commentCategory ),
105
111
}
106
112
}
107
113
108
- func handlePullRequestEvent (body []byte , conf Config , asyncOperationWg * sync. WaitGroup ,
109
- pullRequests PullRequests , repositories Repositories ) Response {
114
+ func handlePullRequestEvent (body []byte , retry retryGithubOperation , pullRequests PullRequests ,
115
+ repositories Repositories ) Response {
110
116
111
117
pullRequestEvent , err := parsePullRequestEvent (body )
112
118
if err != nil {
113
119
return ErrorResponse {err , http .StatusInternalServerError , "Failed to parse the request's body" }
114
120
} else if ! (pullRequestEvent .Action == "opened" || pullRequestEvent .Action == "synchronize" ) {
115
121
return SuccessResponse {"PR not opened or synchronized. Ignoring." }
116
122
}
117
- return checkForFixupCommitsOnPREvent (pullRequestEvent , pullRequests , repositories , conf , asyncOperationWg )
123
+ return checkForFixupCommitsOnPREvent (pullRequestEvent , pullRequests , repositories , retry )
118
124
}
119
125
120
- func handleStatusEvent (body []byte , conf Config , asyncOperationWg * sync. WaitGroup , gitRepos git.Repos , search Search ,
126
+ func handleStatusEvent (body []byte , retry retryGithubOperation , gitRepos git.Repos , search Search ,
121
127
issues Issues , pullRequests PullRequests ) Response {
128
+
122
129
statusEvent , err := parseStatusEvent (body )
123
130
if err != nil {
124
131
return ErrorResponse {err , http .StatusInternalServerError , "Failed to parse the request's body" }
125
132
} else if newPullRequestsPossiblyReadyForMerging (statusEvent ) {
126
- maybeSyncResponse := delayWithRetries ( conf . GithubAPITryDeltas , func () asyncResponse {
133
+ maybeSyncResponse := retry ( func () asyncResponse {
127
134
return mergePullRequestsReadyForMerging (statusEvent , gitRepos , search , issues , pullRequests )
128
- }, asyncOperationWg )
135
+ })
129
136
if maybeSyncResponse .OperationFinishedSynchronously {
130
137
return maybeSyncResponse .Response
131
138
}
0 commit comments