Skip to content

Commit be97293

Browse files
committed
Refactor dependency management for retriable operations
Passing around WaitGroups everywhere isn't all that nice.
1 parent b92d894 commit be97293

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

main.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
githubStatusPeerReviewContext = "review/peer"
2222
)
2323

24+
type retryGithubOperation func(func() asyncResponse) MaybeSyncResponse
25+
2426
func main() {
2527
conf := NewConfig()
2628
githubClient := initGithubClient(conf.AccessToken)
@@ -48,8 +50,13 @@ func main() {
4850
asyncOperationWg.Wait()
4951
}
5052

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+
5360
return func(w http.ResponseWriter, r *http.Request) Response {
5461
body, err := ioutil.ReadAll(r.Body)
5562
if err != nil {
@@ -61,19 +68,18 @@ func CreateHandler(conf Config, gitRepos git.Repos, asyncOperationWg *sync.WaitG
6168
eventType := r.Header.Get("X-Github-Event")
6269
switch eventType {
6370
case "issue_comment":
64-
return handleIssueComment(body, conf, asyncOperationWg, gitRepos, pullRequests, repositories, issues)
71+
return handleIssueComment(body, retry, gitRepos, pullRequests, repositories, issues)
6572
case "pull_request":
66-
return handlePullRequestEvent(body, conf, asyncOperationWg, pullRequests, repositories)
73+
return handlePullRequestEvent(body, retry, pullRequests, repositories)
6774
case "status":
68-
return handleStatusEvent(body, conf, asyncOperationWg, gitRepos, search, issues, pullRequests)
75+
return handleStatusEvent(body, retry, gitRepos, search, issues, pullRequests)
6976
}
7077
return SuccessResponse{"Not an event I understand. Ignoring."}
7178
}
7279
}
7380

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 {
7783

7884
issueComment, err := parseIssueComment(body)
7985
if err != nil {
@@ -97,35 +103,36 @@ func handleIssueComment(body []byte, conf Config, asyncOperationWg *sync.WaitGro
97103
case mergeCommand:
98104
return handleMergeCommand(issueComment, issues, pullRequests, repositories, gitRepos)
99105
case checkCommand:
100-
return checkForFixupCommitsOnIssueComment(issueComment, pullRequests, repositories, conf, asyncOperationWg)
106+
return checkForFixupCommitsOnIssueComment(issueComment, pullRequests, repositories, retry)
101107
}
102108
return ErrorResponse{
103109
Code: http.StatusInternalServerError,
104110
ErrorMessage: fmt.Sprintf("Unhandled comment type: %v", commentCategory),
105111
}
106112
}
107113

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 {
110116

111117
pullRequestEvent, err := parsePullRequestEvent(body)
112118
if err != nil {
113119
return ErrorResponse{err, http.StatusInternalServerError, "Failed to parse the request's body"}
114120
} else if !(pullRequestEvent.Action == "opened" || pullRequestEvent.Action == "synchronize") {
115121
return SuccessResponse{"PR not opened or synchronized. Ignoring."}
116122
}
117-
return checkForFixupCommitsOnPREvent(pullRequestEvent, pullRequests, repositories, conf, asyncOperationWg)
123+
return checkForFixupCommitsOnPREvent(pullRequestEvent, pullRequests, repositories, retry)
118124
}
119125

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,
121127
issues Issues, pullRequests PullRequests) Response {
128+
122129
statusEvent, err := parseStatusEvent(body)
123130
if err != nil {
124131
return ErrorResponse{err, http.StatusInternalServerError, "Failed to parse the request's body"}
125132
} else if newPullRequestsPossiblyReadyForMerging(statusEvent) {
126-
maybeSyncResponse := delayWithRetries(conf.GithubAPITryDeltas, func() asyncResponse {
133+
maybeSyncResponse := retry(func() asyncResponse {
127134
return mergePullRequestsReadyForMerging(statusEvent, gitRepos, search, issues, pullRequests)
128-
}, asyncOperationWg)
135+
})
129136
if maybeSyncResponse.OperationFinishedSynchronously {
130137
return maybeSyncResponse.Response
131138
}

squash.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77
"net/http"
88
"strings"
9-
"sync"
109

1110
"github.com/google/go-github/github"
1211
"github.com/salemove/github-review-helper/git"
@@ -31,19 +30,19 @@ func handleSquashCommand(issueComment IssueComment, gitRepos git.Repos, pullRequ
3130
}
3231

3332
func checkForFixupCommitsOnPREvent(pullRequestEvent PullRequestEvent, pullRequests PullRequests,
34-
repositories Repositories, conf Config, asyncOperationWg *sync.WaitGroup) Response {
33+
repositories Repositories, retry retryGithubOperation) Response {
3534

3635
isExpectedHead := func(head string) bool {
3736
return head == pullRequestEvent.Head.SHA
3837
}
3938
setStatus := func(status *github.RepoStatus) *ErrorResponse {
4039
return setStatusForPREvent(pullRequestEvent, status, repositories)
4140
}
42-
return checkForFixupCommits(pullRequestEvent, isExpectedHead, setStatus, pullRequests, conf, asyncOperationWg)
41+
return checkForFixupCommits(pullRequestEvent, isExpectedHead, setStatus, pullRequests, retry)
4342
}
4443

4544
func checkForFixupCommitsOnIssueComment(issueComment IssueComment, pullRequests PullRequests,
46-
repositories Repositories, conf Config, asyncOperationWg *sync.WaitGroup) Response {
45+
repositories Repositories, retry retryGithubOperation) Response {
4746

4847
isExpectedHead := func(string) bool { return true }
4948
setStatus := func(status *github.RepoStatus) *ErrorResponse {
@@ -53,15 +52,15 @@ func checkForFixupCommitsOnIssueComment(issueComment IssueComment, pullRequests
5352
}
5453
return setStatusForPR(pr, status, repositories)
5554
}
56-
return checkForFixupCommits(issueComment, isExpectedHead, setStatus, pullRequests, conf, asyncOperationWg)
55+
return checkForFixupCommits(issueComment, isExpectedHead, setStatus, pullRequests, retry)
5756
}
5857

5958
func checkForFixupCommits(issueable Issueable, isExpectedHead func(string) bool,
6059
setStatus func(*github.RepoStatus) *ErrorResponse, pullRequests PullRequests,
61-
conf Config, asyncOperationWg *sync.WaitGroup) Response {
60+
retry retryGithubOperation) Response {
6261

6362
log.Printf("Checking for fixup commits for PR %s.\n", issueable.Issue().FullName())
64-
maybeSyncResponse := delayWithRetries(conf.GithubAPITryDeltas, func() asyncResponse {
63+
maybeSyncResponse := retry(func() asyncResponse {
6564
commits, asyncErrResp := getCommits(issueable, isExpectedHead, pullRequests)
6665
if asyncErrResp != nil {
6766
return asyncErrResp.toAsyncResponse()
@@ -78,7 +77,7 @@ func checkForFixupCommits(issueable Issueable, isExpectedHead func(string) bool,
7877
return nonRetriable(errResp)
7978
}
8079
return nonRetriable(SuccessResponse{})
81-
}, asyncOperationWg)
80+
})
8281
if maybeSyncResponse.OperationFinishedSynchronously {
8382
return maybeSyncResponse.Response
8483
}

0 commit comments

Comments
 (0)