@@ -10,8 +10,11 @@ import (
10
10
11
11
"code.gitea.io/gitea/modules/git"
12
12
"code.gitea.io/gitea/modules/log"
13
+ api "code.gitea.io/gitea/modules/structs"
13
14
webhook_module "code.gitea.io/gitea/modules/webhook"
14
15
16
+ "github.com/gobwas/glob"
17
+ "github.com/nektos/act/pkg/jobparser"
15
18
"github.com/nektos/act/pkg/model"
16
19
)
17
20
@@ -41,7 +44,7 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
41
44
return ret , nil
42
45
}
43
46
44
- func DetectWorkflows (commit * git.Commit , event webhook_module.HookEventType ) (map [string ][]byte , error ) {
47
+ func DetectWorkflows (commit * git.Commit , triggedEvent webhook_module.HookEventType , payload api. Payloader ) (map [string ][]byte , error ) {
45
48
entries , err := ListWorkflows (commit )
46
49
if err != nil {
47
50
return nil , err
@@ -63,13 +66,156 @@ func DetectWorkflows(commit *git.Commit, event webhook_module.HookEventType) (ma
63
66
log .Warn ("ignore invalid workflow %q: %v" , entry .Name (), err )
64
67
continue
65
68
}
66
- for _ , e := range workflow .On () {
67
- if e == event .Event () {
69
+ events , err := jobparser .ParseRawOn (& workflow .RawOn )
70
+ if err != nil {
71
+ log .Warn ("ignore invalid workflow %q: %v" , entry .Name (), err )
72
+ continue
73
+ }
74
+ for _ , evt := range events {
75
+ if evt .Name != triggedEvent .Event () {
76
+ continue
77
+ }
78
+
79
+ if detectMatched (commit , triggedEvent , payload , evt ) {
68
80
workflows [entry .Name ()] = content
69
- break
70
81
}
71
82
}
72
83
}
73
84
74
85
return workflows , nil
75
86
}
87
+
88
+ func detectMatched (commit * git.Commit , triggedEvent webhook_module.HookEventType , payload api.Payloader , evt * jobparser.Event ) bool {
89
+ if len (evt .Acts ) == 0 {
90
+ return true
91
+ }
92
+
93
+ switch triggedEvent {
94
+ case webhook_module .HookEventCreate :
95
+ fallthrough
96
+ case webhook_module .HookEventDelete :
97
+ fallthrough
98
+ case webhook_module .HookEventFork :
99
+ log .Warn ("unsupported event %q" , triggedEvent .Event ())
100
+ return false
101
+ case webhook_module .HookEventPush :
102
+ pushPayload := payload .(* api.PushPayload )
103
+ matchTimes := 0
104
+ // all acts conditions should be satisfied
105
+ for cond , vals := range evt .Acts {
106
+ switch cond {
107
+ case "branches" , "tags" :
108
+ for _ , val := range vals {
109
+ if glob .MustCompile (val , '/' ).Match (pushPayload .Ref ) {
110
+ matchTimes ++
111
+ break
112
+ }
113
+ }
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
+ }
132
+ }
133
+ default :
134
+ log .Warn ("unsupported condition %q" , cond )
135
+ }
136
+ }
137
+ return matchTimes == len (evt .Acts )
138
+
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
+ }
161
+ }
162
+ case "branches" :
163
+ for _ , val := range vals {
164
+ if glob .MustCompile (val , '/' ).Match (prPayload .PullRequest .Base .Ref ) {
165
+ matchTimes ++
166
+ break
167
+ }
168
+ }
169
+ case "paths" :
170
+ filesChanged , err := commit .GetFilesChangedSinceCommit (prPayload .PullRequest .Base .Ref )
171
+ if err != nil {
172
+ log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
173
+ } else {
174
+ for _ , val := range vals {
175
+ matched := false
176
+ for _ , file := range filesChanged {
177
+ if glob .MustCompile (val , '/' ).Match (file ) {
178
+ matched = true
179
+ break
180
+ }
181
+ }
182
+ if matched {
183
+ matchTimes ++
184
+ break
185
+ }
186
+ }
187
+ }
188
+ default :
189
+ log .Warn ("unsupported condition %q" , cond )
190
+ }
191
+ }
192
+ return matchTimes == len (evt .Acts )
193
+ case webhook_module .HookEventPullRequestAssign :
194
+ fallthrough
195
+ case webhook_module .HookEventPullRequestLabel :
196
+ fallthrough
197
+ case webhook_module .HookEventPullRequestMilestone :
198
+ fallthrough
199
+ case webhook_module .HookEventPullRequestComment :
200
+ fallthrough
201
+ case webhook_module .HookEventPullRequestReviewApproved :
202
+ fallthrough
203
+ case webhook_module .HookEventPullRequestReviewRejected :
204
+ fallthrough
205
+ case webhook_module .HookEventPullRequestReviewComment :
206
+ fallthrough
207
+ case webhook_module .HookEventPullRequestSync :
208
+ fallthrough
209
+ case webhook_module .HookEventWiki :
210
+ fallthrough
211
+ case webhook_module .HookEventRepository :
212
+ fallthrough
213
+ case webhook_module .HookEventRelease :
214
+ fallthrough
215
+ case webhook_module .HookEventPackage :
216
+ fallthrough
217
+ default :
218
+ log .Warn ("unsupported event %q" , triggedEvent .Event ())
219
+ }
220
+ return false
221
+ }
0 commit comments