-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
187 lines (160 loc) · 6.95 KB
/
main_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
package main
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"github.com/google/go-github/v42/github"
)
type dummyReactor struct {
events []string
}
func (r *dummyReactor) HandleBranchPush(ctx context.Context, org, repo string, branch string) error {
r.events = append(r.events, fmt.Sprintf("branch_push:%s/%s:%s", org, repo, branch))
return nil
}
func (r *dummyReactor) HandleTagPush(ctx context.Context, org, repo string, tag string) error {
r.events = append(r.events, fmt.Sprintf("tag_push:%s/%s:%s", org, repo, tag))
return nil
}
func (r *dummyReactor) HandleCheckSuiteRerequest(ctx context.Context, org, repo string, suite *github.CheckSuite) error {
var prs []string
for _, pr := range suite.PullRequests {
prs = append(prs, fmt.Sprintf("%d", pr.GetNumber()))
}
r.events = append(r.events, fmt.Sprintf("check_suite_rerequest:%s/%s:[%s]", org, repo, strings.Join(prs, ",")))
return nil
}
func (r *dummyReactor) HandleIssueCommentCreate(ctx context.Context, org, repo string, issue *github.Issue, comment *github.IssueComment) error {
r.events = append(r.events, fmt.Sprintf("issue_comment_create:%s/%s:%d:[%s]:[%s]", org, repo, issue.GetNumber(), issue.GetTitle(), comment.GetBody()))
return nil
}
func (r *dummyReactor) HandlePullRequestClose(ctx context.Context, org, repo string, pr *github.PullRequest) error {
r.events = append(r.events, fmt.Sprintf("pull_request_close:%s/%s:%d:[%s]", org, repo, pr.GetNumber(), pr.GetTitle()))
return nil
}
func (r *dummyReactor) HandlePullRequestCreate(ctx context.Context, org, repo string, pr *github.PullRequest) error {
r.events = append(r.events, fmt.Sprintf("pull_request_create:%s/%s:%d:[%s]", org, repo, pr.GetNumber(), pr.GetTitle()))
return nil
}
func (r *dummyReactor) HandlePullRequestEdit(ctx context.Context, org, repo string, pr *github.PullRequest) error {
r.events = append(r.events, fmt.Sprintf("pull_request_edit:%s/%s:%d:[%s]", org, repo, pr.GetNumber(), pr.GetTitle()))
return nil
}
func (r *dummyReactor) HandlePullRequestSynchronize(ctx context.Context, org, repo string, pr *github.PullRequest) error {
r.events = append(r.events, fmt.Sprintf("pull_request_synchronize:%s/%s:%d:[%s]", org, repo, pr.GetNumber(), pr.GetTitle()))
return nil
}
func TestPushEvent(t *testing.T) {
const pushEvent = `{"ref":"refs/heads/master","before":"5a1fa17a799800f09a9bf447a5c83e3b01bd3ef1","after":"2219d5aed22f28546df28fac4a4c7d0cc783f9d6","repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("push", pushEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"branch_push:quay/quay:master"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPushTagEvent(t *testing.T) {
const pushEvent = `{"ref":"refs/tags/v3.8.0","before":"5a1fa17a799800f09a9bf447a5c83e3b01bd3ef1","after":"2219d5aed22f28546df28fac4a4c7d0cc783f9d6","repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("push", pushEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"tag_push:quay/quay:v3.8.0"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestCheckSuiteRerequest(t *testing.T) {
const suiteEvent = `{"action":"rerequested","check_suite":{"pull_requests":[{"number":1}]},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("check_suite", suiteEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"check_suite_rerequest:quay/quay:[1]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPullRequestCommentRecheck(t *testing.T) {
const commentEvent = `{"action":"created","issue":{"number":1,"title":"chore: Test PR (PROJQUAY-1234)","state":"open","pull_request":{}},"comment":{"body":"/retest"},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("issue_comment", commentEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"issue_comment_create:quay/quay:1:[chore: Test PR (PROJQUAY-1234)]:[/retest]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPullRequestMerged(t *testing.T) {
const prEvent = `{"action":"closed","pull_request":{"number":1,"title":"chore: Test PR (PROJQUAY-1234)","state":"closed"},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("pull_request", prEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"pull_request_close:quay/quay:1:[chore: Test PR (PROJQUAY-1234)]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPullRequestCreate(t *testing.T) {
const prEvent = `{"action":"opened","pull_request":{"number":1,"title":"chore: Test PR (PROJQUAY-1234)","state":"open"},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("pull_request", prEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"pull_request_create:quay/quay:1:[chore: Test PR (PROJQUAY-1234)]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPullRequestEdit(t *testing.T) {
const prEvent = `{"action":"edited","pull_request":{"number":1,"title":"chore: Test PR (PROJQUAY-1234)","state":"open"},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("pull_request", prEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"pull_request_edit:quay/quay:1:[chore: Test PR (PROJQUAY-1234)]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}
func TestPullRequestSynchronize(t *testing.T) {
const prEvent = `{"action":"synchronize","pull_request":{"number":1,"title":"chore: Test PR (PROJQUAY-1234)","state":"open"},"repository":{"name":"quay","full_name":"quay/quay","private":false,"owner":{"name":"quay","login":"quay"}}}`
r := &dummyReactor{}
eh := &EventHandler{
reactor: r,
}
err := eh.HandleEvent("pull_request", prEvent)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !reflect.DeepEqual(r.events, []string{"pull_request_synchronize:quay/quay:1:[chore: Test PR (PROJQUAY-1234)]"}) {
t.Errorf("unexpected events: %v", r.events)
}
}