summaryrefslogtreecommitdiffstats
path: root/modules/actions/workflows.go
blob: c1c8e71f53b5ca026500b2d931c43e557f83884c (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
	"bytes"
	"io"
	"strings"

	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/log"
	api "code.gitea.io/gitea/modules/structs"
	webhook_module "code.gitea.io/gitea/modules/webhook"

	"github.com/gobwas/glob"
	"github.com/nektos/act/pkg/jobparser"
	"github.com/nektos/act/pkg/model"
)

func ListWorkflows(commit *git.Commit) (git.Entries, error) {
	tree, err := commit.SubTree(".gitea/workflows")
	if _, ok := err.(git.ErrNotExist); ok {
		tree, err = commit.SubTree(".github/workflows")
	}
	if _, ok := err.(git.ErrNotExist); ok {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	entries, err := tree.ListEntriesRecursiveFast()
	if err != nil {
		return nil, err
	}

	ret := make(git.Entries, 0, len(entries))
	for _, entry := range entries {
		if strings.HasSuffix(entry.Name(), ".yml") || strings.HasSuffix(entry.Name(), ".yaml") {
			ret = append(ret, entry)
		}
	}
	return ret, nil
}

func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader) (map[string][]byte, error) {
	entries, err := ListWorkflows(commit)
	if err != nil {
		return nil, err
	}

	workflows := make(map[string][]byte, len(entries))
	for _, entry := range entries {
		f, err := entry.Blob().DataAsync()
		if err != nil {
			return nil, err
		}
		content, err := io.ReadAll(f)
		_ = f.Close()
		if err != nil {
			return nil, err
		}
		workflow, err := model.ReadWorkflow(bytes.NewReader(content))
		if err != nil {
			log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
			continue
		}
		events, err := jobparser.ParseRawOn(&workflow.RawOn)
		if err != nil {
			log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
			continue
		}
		for _, evt := range events {
			if evt.Name != triggedEvent.Event() {
				continue
			}

			if detectMatched(commit, triggedEvent, payload, evt) {
				workflows[entry.Name()] = content
			}
		}
	}

	return workflows, nil
}

func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool {
	if len(evt.Acts) == 0 {
		return true
	}

	switch triggedEvent {
	case webhook_module.HookEventCreate:
		fallthrough
	case webhook_module.HookEventDelete:
		fallthrough
	case webhook_module.HookEventFork:
		log.Warn("unsupported event %q", triggedEvent.Event())
		return false
	case webhook_module.HookEventPush:
		pushPayload := payload.(*api.PushPayload)
		matchTimes := 0
		// all acts conditions should be satisfied
		for cond, vals := range evt.Acts {
			switch cond {
			case "branches", "tags":
				for _, val := range vals {
					if glob.MustCompile(val, '/').Match(pushPayload.Ref) {
						matchTimes++
						break
					}
				}
			case "paths":
				filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before)
				if err != nil {
					log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err)
				} else {
					for _, val := range vals {
						matched := false
						for _, file := range filesChanged {
							if glob.MustCompile(val, '/').Match(file) {
								matched = true
								break
							}
						}
						if matched {
							matchTimes++
							break
						}
					}
				}
			default:
				log.Warn("unsupported condition %q", cond)
			}
		}
		return matchTimes == len(evt.Acts)

	case webhook_module.HookEventIssues:
		fallthrough
	case webhook_module.HookEventIssueAssign:
		fallthrough
	case webhook_module.HookEventIssueLabel:
		fallthrough
	case webhook_module.HookEventIssueMilestone:
		fallthrough
	case webhook_module.HookEventIssueComment:
		fallthrough
	case webhook_module.HookEventPullRequest:
		prPayload := payload.(*api.PullRequestPayload)
		matchTimes := 0
		// all acts conditions should be satisfied
		for cond, vals := range evt.Acts {
			switch cond {
			case "types":
				for _, val := range vals {
					if glob.MustCompile(val, '/').Match(string(prPayload.Action)) {
						matchTimes++
						break
					}
				}
			case "branches":
				for _, val := range vals {
					if glob.MustCompile(val, '/').Match(prPayload.PullRequest.Base.Ref) {
						matchTimes++
						break
					}
				}
			case "paths":
				filesChanged, err := commit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref)
				if err != nil {
					log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err)
				} else {
					for _, val := range vals {
						matched := false
						for _, file := range filesChanged {
							if glob.MustCompile(val, '/').Match(file) {
								matched = true
								break
							}
						}
						if matched {
							matchTimes++
							break
						}
					}
				}
			default:
				log.Warn("unsupported condition %q", cond)
			}
		}
		return matchTimes == len(evt.Acts)
	case webhook_module.HookEventPullRequestAssign:
		fallthrough
	case webhook_module.HookEventPullRequestLabel:
		fallthrough
	case webhook_module.HookEventPullRequestMilestone:
		fallthrough
	case webhook_module.HookEventPullRequestComment:
		fallthrough
	case webhook_module.HookEventPullRequestReviewApproved:
		fallthrough
	case webhook_module.HookEventPullRequestReviewRejected:
		fallthrough
	case webhook_module.HookEventPullRequestReviewComment:
		fallthrough
	case webhook_module.HookEventPullRequestSync:
		fallthrough
	case webhook_module.HookEventWiki:
		fallthrough
	case webhook_module.HookEventRepository:
		fallthrough
	case webhook_module.HookEventRelease:
		fallthrough
	case webhook_module.HookEventPackage:
		fallthrough
	default:
		log.Warn("unsupported event %q", triggedEvent.Event())
	}
	return false
}