@@ -72,9 +72,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
7272 continue
7373 }
7474 for _ , evt := range events {
75- if evt .Name != triggedEvent .Event () {
76- continue
77- }
75+ log .Trace ("detect workflow %q for event %#v matching %q" , entry .Name (), evt , triggedEvent )
7876 if detectMatched (commit , triggedEvent , payload , evt ) {
7977 workflows [entry .Name ()] = content
8078 }
@@ -85,138 +83,197 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
8583}
8684
8785func detectMatched (commit * git.Commit , triggedEvent webhook_module.HookEventType , payload api.Payloader , evt * jobparser.Event ) bool {
88- if len (evt . Acts ) == 0 {
89- return true
86+ if convertFromGithubEvent (evt ) != string ( triggedEvent ) {
87+ return false
9088 }
9189
9290 switch triggedEvent {
93- case webhook_module .HookEventCreate :
94- fallthrough
95- case webhook_module .HookEventDelete :
96- fallthrough
97- case webhook_module .HookEventFork :
98- log .Warn ("unsupported event %q" , triggedEvent .Event ())
99- return false
91+ case webhook_module .HookEventCreate ,
92+ webhook_module .HookEventDelete ,
93+ webhook_module .HookEventFork ,
94+ webhook_module .HookEventIssueAssign ,
95+ webhook_module .HookEventIssueLabel ,
96+ webhook_module .HookEventIssueMilestone ,
97+ webhook_module .HookEventPullRequestAssign ,
98+ webhook_module .HookEventPullRequestLabel ,
99+ webhook_module .HookEventPullRequestMilestone ,
100+ webhook_module .HookEventPullRequestComment ,
101+ webhook_module .HookEventPullRequestReviewApproved ,
102+ webhook_module .HookEventPullRequestReviewRejected ,
103+ webhook_module .HookEventPullRequestReviewComment ,
104+ webhook_module .HookEventWiki ,
105+ webhook_module .HookEventRepository ,
106+ webhook_module .HookEventRelease ,
107+ webhook_module .HookEventPackage :
108+ if len (evt .Acts ) != 0 {
109+ log .Warn ("Ignore unsupported %s event arguments %q" , triggedEvent , evt .Acts )
110+ }
111+ // no special filter parameters for these events, just return true if name matched
112+ return true
113+
100114 case webhook_module .HookEventPush :
101- pushPayload := payload .(* api.PushPayload )
102- matchTimes := 0
103- // all acts conditions should be satisfied
104- for cond , vals := range evt .Acts {
105- switch cond {
106- case "branches" , "tags" :
107- refShortName := git .RefName (pushPayload .Ref ).ShortName ()
115+ return matchPushEvent (commit , payload .(* api.PushPayload ), evt )
116+
117+ case webhook_module .HookEventIssues :
118+ return matchIssuesEvent (commit , payload .(* api.IssuePayload ), evt )
119+
120+ case webhook_module .HookEventPullRequest , webhook_module .HookEventPullRequestSync :
121+ return matchPullRequestEvent (commit , payload .(* api.PullRequestPayload ), evt )
122+
123+ case webhook_module .HookEventIssueComment :
124+ return matchIssueCommentEvent (commit , payload .(* api.IssueCommentPayload ), evt )
125+
126+ default :
127+ log .Warn ("unsupported event %q" , triggedEvent )
128+ return false
129+ }
130+ }
131+
132+ func matchPushEvent (commit * git.Commit , pushPayload * api.PushPayload , evt * jobparser.Event ) bool {
133+ // with no special filter parameters
134+ if len (evt .Acts ) == 0 {
135+ return true
136+ }
137+
138+ matchTimes := 0
139+ // all acts conditions should be satisfied
140+ for cond , vals := range evt .Acts {
141+ switch cond {
142+ case "branches" , "tags" :
143+ refShortName := git .RefName (pushPayload .Ref ).ShortName ()
144+ for _ , val := range vals {
145+ if glob .MustCompile (val , '/' ).Match (refShortName ) {
146+ matchTimes ++
147+ break
148+ }
149+ }
150+ case "paths" :
151+ filesChanged , err := commit .GetFilesChangedSinceCommit (pushPayload .Before )
152+ if err != nil {
153+ log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
154+ } else {
108155 for _ , val := range vals {
109- if glob .MustCompile (val , '/' ).Match (refShortName ) {
156+ matched := false
157+ for _ , file := range filesChanged {
158+ if glob .MustCompile (val , '/' ).Match (file ) {
159+ matched = true
160+ break
161+ }
162+ }
163+ if matched {
110164 matchTimes ++
111165 break
112166 }
113167 }
114- case "paths" :
115- filesChanged , err := commit .GetFilesChangedSinceCommit (pushPayload .Before )
116- if err != nil {
117- log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
118- } else {
119- for _ , val := range vals {
120- matched := false
121- for _ , file := range filesChanged {
122- if glob .MustCompile (val , '/' ).Match (file ) {
123- matched = true
124- break
125- }
126- }
127- if matched {
128- matchTimes ++
129- break
130- }
131- }
168+ }
169+ default :
170+ log .Warn ("push event unsupported condition %q" , cond )
171+ }
172+ }
173+ return matchTimes == len (evt .Acts )
174+ }
175+
176+ func matchIssuesEvent (commit * git.Commit , issuePayload * api.IssuePayload , evt * jobparser.Event ) bool {
177+ // with no special filter parameters
178+ if len (evt .Acts ) == 0 {
179+ return true
180+ }
181+
182+ matchTimes := 0
183+ // all acts conditions should be satisfied
184+ for cond , vals := range evt .Acts {
185+ switch cond {
186+ case "types" :
187+ for _ , val := range vals {
188+ if glob .MustCompile (val , '/' ).Match (string (issuePayload .Action )) {
189+ matchTimes ++
190+ break
132191 }
133- default :
134- log .Warn ("unsupported condition %q" , cond )
135192 }
193+ default :
194+ log .Warn ("issue event unsupported condition %q" , cond )
136195 }
137- return matchTimes == len (evt .Acts )
196+ }
197+ return matchTimes == len (evt .Acts )
198+ }
138199
139- case webhook_module .HookEventIssues :
140- fallthrough
141- case webhook_module .HookEventIssueAssign :
142- fallthrough
143- case webhook_module .HookEventIssueLabel :
144- fallthrough
145- case webhook_module .HookEventIssueMilestone :
146- fallthrough
147- case webhook_module .HookEventIssueComment :
148- fallthrough
149- case webhook_module .HookEventPullRequest :
150- prPayload := payload .(* api.PullRequestPayload )
151- matchTimes := 0
152- // all acts conditions should be satisfied
153- for cond , vals := range evt .Acts {
154- switch cond {
155- case "types" :
156- for _ , val := range vals {
157- if glob .MustCompile (val , '/' ).Match (string (prPayload .Action )) {
158- matchTimes ++
159- break
160- }
200+ func matchPullRequestEvent (commit * git.Commit , prPayload * api.PullRequestPayload , evt * jobparser.Event ) bool {
201+ // with no special filter parameters
202+ if len (evt .Acts ) == 0 {
203+ // defaultly, only pull request opened and synchronized will trigger workflow
204+ return prPayload .Action == api .HookIssueSynchronized || prPayload .Action == api .HookIssueOpened
205+ }
206+
207+ matchTimes := 0
208+ // all acts conditions should be satisfied
209+ for cond , vals := range evt .Acts {
210+ switch cond {
211+ case "types" :
212+ action := prPayload .Action
213+ if prPayload .Action == api .HookIssueSynchronized {
214+ action = "synchronize"
215+ }
216+ log .Trace ("matching pull_request %s with %v" , action , vals )
217+ for _ , val := range vals {
218+ if glob .MustCompile (val , '/' ).Match (string (action )) {
219+ matchTimes ++
220+ break
221+ }
222+ }
223+ case "branches" :
224+ refShortName := git .RefName (prPayload .PullRequest .Base .Ref ).ShortName ()
225+ for _ , val := range vals {
226+ if glob .MustCompile (val , '/' ).Match (refShortName ) {
227+ matchTimes ++
228+ break
161229 }
162- case "branches" :
163- refShortName := git .RefName (prPayload .PullRequest .Base .Ref ).ShortName ()
230+ }
231+ case "paths" :
232+ filesChanged , err := commit .GetFilesChangedSinceCommit (prPayload .PullRequest .Base .Ref )
233+ if err != nil {
234+ log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
235+ } else {
164236 for _ , val := range vals {
165- if glob .MustCompile (val , '/' ).Match (refShortName ) {
237+ matched := false
238+ for _ , file := range filesChanged {
239+ if glob .MustCompile (val , '/' ).Match (file ) {
240+ matched = true
241+ break
242+ }
243+ }
244+ if matched {
166245 matchTimes ++
167246 break
168247 }
169248 }
170- case "paths" :
171- filesChanged , err := commit .GetFilesChangedSinceCommit (prPayload .PullRequest .Base .Ref )
172- if err != nil {
173- log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
174- } else {
175- for _ , val := range vals {
176- matched := false
177- for _ , file := range filesChanged {
178- if glob .MustCompile (val , '/' ).Match (file ) {
179- matched = true
180- break
181- }
182- }
183- if matched {
184- matchTimes ++
185- break
186- }
187- }
249+ }
250+ default :
251+ log .Warn ("pull request event unsupported condition %q" , cond )
252+ }
253+ }
254+ return matchTimes == len (evt .Acts )
255+ }
256+
257+ func matchIssueCommentEvent (commit * git.Commit , issueCommentPayload * api.IssueCommentPayload , evt * jobparser.Event ) bool {
258+ // with no special filter parameters
259+ if len (evt .Acts ) == 0 {
260+ return true
261+ }
262+
263+ matchTimes := 0
264+ // all acts conditions should be satisfied
265+ for cond , vals := range evt .Acts {
266+ switch cond {
267+ case "types" :
268+ for _ , val := range vals {
269+ if glob .MustCompile (val , '/' ).Match (string (issueCommentPayload .Action )) {
270+ matchTimes ++
271+ break
188272 }
189- default :
190- log .Warn ("unsupported condition %q" , cond )
191273 }
274+ default :
275+ log .Warn ("issue comment unsupported condition %q" , cond )
192276 }
193- return matchTimes == len (evt .Acts )
194- case webhook_module .HookEventPullRequestAssign :
195- fallthrough
196- case webhook_module .HookEventPullRequestLabel :
197- fallthrough
198- case webhook_module .HookEventPullRequestMilestone :
199- fallthrough
200- case webhook_module .HookEventPullRequestComment :
201- fallthrough
202- case webhook_module .HookEventPullRequestReviewApproved :
203- fallthrough
204- case webhook_module .HookEventPullRequestReviewRejected :
205- fallthrough
206- case webhook_module .HookEventPullRequestReviewComment :
207- fallthrough
208- case webhook_module .HookEventPullRequestSync :
209- fallthrough
210- case webhook_module .HookEventWiki :
211- fallthrough
212- case webhook_module .HookEventRepository :
213- fallthrough
214- case webhook_module .HookEventRelease :
215- fallthrough
216- case webhook_module .HookEventPackage :
217- fallthrough
218- default :
219- log .Warn ("unsupported event %q" , triggedEvent .Event ())
220277 }
221- return false
278+ return matchTimes == len ( evt . Acts )
222279}
0 commit comments