-
Notifications
You must be signed in to change notification settings - Fork 12
/
check_command_test.go
167 lines (146 loc) · 5.2 KB
/
check_command_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
package main_test
import (
"net/http"
"net/http/httptest"
"github.com/google/go-github/github"
"github.com/salemove/github-review-helper/mocks"
"github.com/stretchr/testify/mock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = TestWebhookHandler(func(context WebhookTestContext) {
Describe("!check comment", 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 commitRevision = "1235"
var headRepository = &github.Repository{
Owner: &github.User{
Login: github.String("other"),
},
Name: github.String("github-review-helper-fork"),
SSHURL: github.String("[email protected]:other/github-review-helper-fork.git"),
}
headers.Is(func() map[string]string {
return map[string]string{
"X-Github-Event": "issue_comment",
}
})
requestJSON.Is(func() string {
return IssueCommentEvent("!check", arbitraryIssueAuthor)
})
ForCollaborator(context, repositoryOwner, repositoryName, arbitraryIssueAuthor, func() {
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() {
resp, err := createGithubErrorResponse(http.StatusInternalServerError)
pullRequests.
On("ListCommits", anyContext, repositoryOwner, repositoryName, issueNumber, mock.AnythingOfType("*github.ListOptions")).
Return(emptyResult, resp, err)
})
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 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{commitRevision, "Another casual commit"},
), &github.Response{}, noError)
pullRequests.
On("Get", anyContext, repositoryOwner, repositoryName, issueNumber).
Return(&github.PullRequest{
Number: github.Int(issueNumber),
Head: &github.PullRequestBranch{
SHA: github.String(commitRevision),
Repo: headRepository,
},
Base: &github.PullRequestBranch{
Repo: repository,
},
}, emptyResponse, noError)
})
It("reports success status to GitHub", func() {
repositories.
On("CreateStatus", anyContext, *headRepository.Owner.Login, *headRepository.Name, commitRevision,
mock.MatchedBy(func(status *github.RepoStatus) bool {
return *status.State == "success" && *status.Context == "review/squash"
}),
).
Return(emptyResult, emptyResult, 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{commitRevision, "fixup! Changing things\n\nOopsie. Forgot a thing"},
)
mockListCommits(commits, perPage, repositoryOwner, repositoryName, issueNumber, pullRequests)
pullRequests.
On("Get", anyContext, repositoryOwner, repositoryName, issueNumber).
Return(&github.PullRequest{
Number: github.Int(issueNumber),
Head: &github.PullRequestBranch{
SHA: github.String(commitRevision),
Repo: headRepository,
},
Base: &github.PullRequestBranch{
Repo: repository,
},
}, emptyResponse, noError)
})
It("reports pending squash status to GitHub", func() {
repositories.
On("CreateStatus", anyContext, *headRepository.Owner.Login, *headRepository.Name, commitRevision,
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))
})
})
})
})
})