-
Notifications
You must be signed in to change notification settings - Fork 12
/
squash_command_test.go
159 lines (132 loc) · 4.01 KB
/
squash_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
package main_test
import (
"errors"
"net/http"
"net/http/httptest"
"github.com/google/go-github/github"
"github.com/salemove/github-review-helper/git"
"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("!squash comment", func() {
var (
handle = context.Handle
headers = context.Headers
requestJSON = context.RequestJSON
responseRecorder *httptest.ResponseRecorder
pullRequests *mocks.PullRequests
)
BeforeEach(func() {
responseRecorder = *context.ResponseRecorder
pullRequests = *context.PullRequests
})
headers.Is(func() map[string]string {
return map[string]string{
"X-Github-Event": "issue_comment",
}
})
requestJSON.Is(func() string {
return IssueCommentEvent("!squash", arbitraryIssueAuthor)
})
ForCollaborator(context, repositoryOwner, repositoryName, arbitraryIssueAuthor, func() {
Context("with GitHub request failing", func() {
BeforeEach(func() {
pullRequests.
On("Get", anyContext, repositoryOwner, repositoryName, issueNumber).
Return(emptyResult, emptyResponse, errors.New("an error"))
})
It("fails with a gateway error", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusBadGateway))
})
})
Context("with GitHub request succeeding", func() {
pr := &github.PullRequest{
Number: github.Int(issueNumber),
Base: &github.PullRequestBranch{
SHA: github.String("1234"),
Ref: github.String("master"),
Repo: repository,
},
Head: &github.PullRequestBranch{
SHA: github.String("1235"),
Ref: github.String("feature"),
Repo: repository,
},
}
BeforeEach(func() {
pullRequests.
On("Get", anyContext, repositoryOwner, repositoryName, issueNumber).
Return(pr, emptyResponse, noError)
})
ItSquashesPR(context, pr)
})
})
})
})
var ItSquashesPR = func(context WebhookTestContext, pr *github.PullRequest) {
var (
handle = context.Handle
responseRecorder *httptest.ResponseRecorder
repositories *mocks.Repositories
gitRepos *mocks.Repos
gitRepo *mocks.Repo
baseRef = *pr.Base.Ref
headRef = *pr.Head.Ref
headSHA = *pr.Head.SHA
)
BeforeEach(func() {
responseRecorder = *context.ResponseRecorder
repositories = *context.Repositories
gitRepos = *context.GitRepos
gitRepo = new(mocks.Repo)
gitRepos.
On("GetUpdatedRepo", sshURL, repositoryOwner, repositoryName).
Return(gitRepo, noError)
})
AfterEach(func() {
gitRepo.AssertExpectations(GinkgoT())
})
Context("with autosquash and push failing due to a squash conflict", func() {
BeforeEach(func() {
squashErr := &git.ErrSquashConflict{Err: errors.New("merge conflict")}
gitRepo.
On("AutosquashAndPush", "origin/"+baseRef, headSHA, headRef).
Return(squashErr)
})
It("reports the failure", func() {
repositories.
On("CreateStatus", anyContext, repositoryOwner, repositoryName, headSHA, mock.MatchedBy(func(status *github.RepoStatus) bool {
return *status.State == "failure" && *status.Context == "review/squash"
})).
Return(emptyResult, emptyResponse, noError)
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
})
Context("with autosquash and push failing due to a reason other than a squash conflict", func() {
BeforeEach(func() {
gitRepo.
On("AutosquashAndPush", "origin/"+baseRef, headSHA, headRef).
Return(errors.New("other git error"))
})
It("responds with an internal server error", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusInternalServerError))
})
})
Context("with autosquash and push succeeding", func() {
BeforeEach(func() {
gitRepo.
On("AutosquashAndPush", "origin/"+baseRef, headSHA, headRef).
Return(noError)
})
It("returns 200 OK", func() {
handle()
Expect(responseRecorder.Code).To(Equal(http.StatusOK))
})
})
}