Skip to content

Commit 2871ea0

Browse files
lunnywolfogre
andauthored
Add more events details supports for actions (#22680)
#21937 implemented only basic events based on name because of `act`'s limitation. So I sent a PR to parse all possible events details in https://gitea.com/gitea/act/pulls/11 and it merged. The ref documentation is https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows This PR depends on that and make more detail responses for `push` events and `pull_request` events. And it lefts more events there for future PRs. --------- Co-authored-by: Jason Song <i@wolfogre.com>
1 parent 7fd5d38 commit 2871ea0

File tree

4 files changed

+154
-8
lines changed

4 files changed

+154
-8
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142
284284

285285
replace github.com/blevesearch/zapx/v15 v15.3.6 => github.com/zeripath/zapx/v15 v15.3.6-alignment-fix
286286

287-
replace github.com/nektos/act => gitea.com/gitea/act v0.234.0
287+
replace github.com/nektos/act => gitea.com/gitea/act v0.234.2-0.20230131074955-e46ede1b1744
288288

289289
exclude github.com/gofrs/uuid v3.2.0+incompatible
290290

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570/go.mod h1:IIAjsi
7070
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
7171
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:cliQ4HHsCo6xi2oWZYKWW4bly/Ory9FuTpFPRxj/mAg=
7272
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078/go.mod h1:g/V2Hjas6Z1UHUp4yIx6bATpNzJ7DYtD0FG3+xARWxs=
73-
gitea.com/gitea/act v0.234.0 h1:gWgMPMKdNcMrp/o2CF/SyVKiiJLBFl+xmzfvoHCpykU=
74-
gitea.com/gitea/act v0.234.0/go.mod h1:2C/WbTalu1VPNgbVaZJaZDzlOtAKqkXJhdOClxkMy14=
73+
gitea.com/gitea/act v0.234.2-0.20230131074955-e46ede1b1744 h1:cqzKmGlX0wynSXO04NILpL25eBGwogDrKpkkbwmIpj4=
74+
gitea.com/gitea/act v0.234.2-0.20230131074955-e46ede1b1744/go.mod h1:2C/WbTalu1VPNgbVaZJaZDzlOtAKqkXJhdOClxkMy14=
7575
gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681 h1:MMSPgnVULVwV9kEBgvyEUhC9v/uviZ55hPJEMjpbNR4=
7676
gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc=
7777
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0=

modules/actions/workflows.go

+150-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010

1111
"code.gitea.io/gitea/modules/git"
1212
"code.gitea.io/gitea/modules/log"
13+
api "code.gitea.io/gitea/modules/structs"
1314
webhook_module "code.gitea.io/gitea/modules/webhook"
1415

16+
"github.com/gobwas/glob"
17+
"github.com/nektos/act/pkg/jobparser"
1518
"github.com/nektos/act/pkg/model"
1619
)
1720

@@ -41,7 +44,7 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
4144
return ret, nil
4245
}
4346

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) {
4548
entries, err := ListWorkflows(commit)
4649
if err != nil {
4750
return nil, err
@@ -63,13 +66,156 @@ func DetectWorkflows(commit *git.Commit, event webhook_module.HookEventType) (ma
6366
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
6467
continue
6568
}
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) {
6880
workflows[entry.Name()] = content
69-
break
7081
}
7182
}
7283
}
7384

7485
return workflows, nil
7586
}
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+
}

services/actions/notifier_helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func notify(ctx context.Context, input *notifyInput) error {
137137
return fmt.Errorf("gitRepo.GetCommit: %w", err)
138138
}
139139

140-
workflows, err := actions_module.DetectWorkflows(commit, input.Event)
140+
workflows, err := actions_module.DetectWorkflows(commit, input.Event, input.Payload)
141141
if err != nil {
142142
return fmt.Errorf("DetectWorkflows: %w", err)
143143
}

0 commit comments

Comments
 (0)