-
Notifications
You must be signed in to change notification settings - Fork 12
/
pull_request_event_test.go
213 lines (180 loc) · 6.74 KB
/
pull_request_event_test.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main_test
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"github.com/google/go-github/github"
grh "github.com/salemove/github-review-helper"
"github.com/salemove/github-review-helper/mocks"
"github.com/stretchr/testify/mock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var createGithubErrorResponse = func(statusCode int) (*github.Response, *github.ErrorResponse) {
httpResponse := &http.Response{
StatusCode: statusCode,
Request: &http.Request{},
}
return &github.Response{Response: httpResponse}, &github.ErrorResponse{Response: httpResponse}
}
var _ = TestWebhookHandler(func(context WebhookTestContext) {
Describe("pull_request event", func() {
var (
handle = context.Handle
headers = context.Headers
requestJSON = context.RequestJSON
responseRecorder *httptest.ResponseRecorder
pullRequests *mocks.PullRequests
repositories *mocks.Repositories
)
BeforeEach(func() {
responseRecorder = *context.ResponseRecorder
pullRequests = *context.PullRequests
repositories = *context.Repositories
})
var pullRequestHeadSHA = "1235"
var headRepository = grh.Repository{
Owner: "other",
Name: "github-review-helper-fork",
URL: "[email protected]:other/github-review-helper-fork.git",
}
headers.Is(func() map[string]string {
return map[string]string{
"X-Github-Event": "pull_request",
}
})
Context("with the PR being closed", func() {
requestJSON.Is(func() string {
return PullRequestEvent("closed", pullRequestHeadSHA, headRepository)
})
It("succeeds with 'ignored' response", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
Expect(responseRecorder.Body.String()).To(ContainSubstring("Ignoring"))
})
})
Context("with the PR being synchronized", func() {
requestJSON.Is(func() string {
return PullRequestEvent("synchronize", pullRequestHeadSHA, headRepository)
})
Context("with GitHub request to list commits failing", func() {
Context("with a 404", func() {
BeforeEach(func() {
resp, err := createGithubErrorResponse(http.StatusNotFound)
pullRequests.
On("ListCommits", anyContext, repositoryOwner, repositoryName, issueNumber, mock.AnythingOfType("*github.ListOptions")).
Return(emptyResult, resp, err)
})
// Responds with 200, because the operation will be retries asynchronously
It("responds with 200 OK", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
It("tries the configured amount of times", func() {
handle()
pullRequests.AssertNumberOfCalls(GinkgoT(), "ListCommits", numberOfGithubTries)
})
})
Context("with a different error", func() {
BeforeEach(func() {
pullRequests.
On("ListCommits", anyContext, repositoryOwner, repositoryName, issueNumber, mock.AnythingOfType("*github.ListOptions")).
Return(emptyResult, emptyResponse, errors.New("an error"))
})
It("fails with a gateway error", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusBadGateway))
})
It("tries once", func() {
handle()
pullRequests.AssertNumberOfCalls(GinkgoT(), "ListCommits", 1)
})
})
})
Context("with the head commit differing from the head SHA in the event", func() {
headCommitRevision := strings.Replace(pullRequestHeadSHA, "123", "223", 1)
BeforeEach(func() {
pullRequests.
On("ListCommits", anyContext, repositoryOwner, repositoryName, issueNumber, mock.AnythingOfType("*github.ListOptions")).
Return(githubCommits(
commit{arbitrarySHA, "Changing things"},
commit{headCommitRevision, "Another casual commit"},
), emptyResponse, noError)
})
// Responds with 200, because the operation will be retries asynchronously
It("responds with 200 OK", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
It("tries the configured amount of times", func() {
handle()
pullRequests.AssertNumberOfCalls(GinkgoT(), "ListCommits", numberOfGithubTries)
})
})
Context("with list of commits from GitHub NOT including fixup commits", func() {
BeforeEach(func() {
pullRequests.
On("ListCommits", anyContext, repositoryOwner, repositoryName, issueNumber, mock.AnythingOfType("*github.ListOptions")).
Return(githubCommits(
commit{arbitrarySHA, "Changing things"},
commit{pullRequestHeadSHA, "Another casual commit"},
), emptyResponse, noError)
})
It("reports success status to GitHub", func() {
repositories.
On("CreateStatus", anyContext, headRepository.Owner, headRepository.Name, pullRequestHeadSHA,
mock.MatchedBy(func(status *github.RepoStatus) bool {
return *status.State == "success" && *status.Context == "review/squash"
}),
).
Return(emptyResult, emptyResponse, noError)
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
})
Context("with paged list of commits from GitHub including fixup commits", func() {
BeforeEach(func() {
perPage := 1
commits := githubCommits(
commit{arbitrarySHA, "Changing things"},
commit{pullRequestHeadSHA, "fixup! Changing things\n\nOopsie. Forgot a thing"},
)
mockListCommits(commits, perPage, repositoryOwner, repositoryName, issueNumber, pullRequests)
})
It("reports pending squash status to GitHub", func() {
repositories.
On("CreateStatus", anyContext, headRepository.Owner, headRepository.Name, pullRequestHeadSHA,
mock.MatchedBy(func(status *github.RepoStatus) bool {
return *status.State == "pending" && *status.Context == "review/squash"
}),
).
Return(emptyResult, emptyResponse, noError)
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
})
Context("with paged list of commits in mixed order from GitHub including fixup commits", func() {
BeforeEach(func() {
perPage := 1
commits := githubCommitsInMixedOrder(
commit{arbitrarySHA, "Changing things"},
commit{pullRequestHeadSHA, "fixup! Changing things\n\nOopsie. Forgot a thing"},
)
mockListCommits(commits, perPage, repositoryOwner, repositoryName, issueNumber, pullRequests)
})
It("reports pending squash status to GitHub", func() {
repositories.
On("CreateStatus", anyContext, headRepository.Owner, headRepository.Name, pullRequestHeadSHA,
mock.MatchedBy(func(status *github.RepoStatus) bool {
return *status.State == "pending" && *status.Context == "review/squash"
}),
).
Return(emptyResult, emptyResponse, noError)
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
})
})
})
})