loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Convert GitHub event on actions and fix some pull_request events. (#23037)

Follow #22680

Partially Fix #22958, on pull_request, `opened`, `reopened`,
`synchronize` supported, `edited` hasn't been supported yet because
Gitea doesn't trigger that events.

---------

Co-authored-by: yp05327 <576951401@qq.com>

authored by

Lunny Xiao
yp05327
and committed by
GitHub
8e406347 c492e863

+212 -114
+41
modules/actions/github.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package actions 5 + 6 + import ( 7 + webhook_module "code.gitea.io/gitea/modules/webhook" 8 + 9 + "github.com/nektos/act/pkg/jobparser" 10 + ) 11 + 12 + const ( 13 + githubEventPullRequest = "pull_request" 14 + githubEventPullRequestTarget = "pull_request_target" 15 + githubEventPullRequestReviewComment = "pull_request_review_comment" 16 + githubEventPullRequestReview = "pull_request_review" 17 + githubEventRegistryPackage = "registry_package" 18 + githubEventCreate = "create" 19 + githubEventDelete = "delete" 20 + githubEventFork = "fork" 21 + githubEventPush = "push" 22 + githubEventIssues = "issues" 23 + githubEventIssueComment = "issue_comment" 24 + githubEventRelease = "release" 25 + githubEventPullRequestComment = "pull_request_comment" 26 + ) 27 + 28 + func convertFromGithubEvent(evt *jobparser.Event) string { 29 + switch evt.Name { 30 + case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview, 31 + githubEventPullRequestReviewComment: 32 + return string(webhook_module.HookEventPullRequest) 33 + case githubEventRegistryPackage: 34 + return string(webhook_module.HookEventPackage) 35 + case githubEventCreate, githubEventDelete, githubEventFork, githubEventPush, 36 + githubEventIssues, githubEventIssueComment, githubEventRelease, githubEventPullRequestComment: 37 + fallthrough 38 + default: 39 + return evt.Name 40 + } 41 + }
+171 -114
modules/actions/workflows.go
··· 88 88 continue 89 89 } 90 90 for _, evt := range events { 91 - if evt.Name != triggedEvent.Event() { 92 - continue 93 - } 91 + log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent) 94 92 if detectMatched(commit, triggedEvent, payload, evt) { 95 93 workflows[entry.Name()] = content 96 94 } ··· 101 99 } 102 100 103 101 func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { 102 + if convertFromGithubEvent(evt) != string(triggedEvent) { 103 + return false 104 + } 105 + 106 + switch triggedEvent { 107 + case webhook_module.HookEventCreate, 108 + webhook_module.HookEventDelete, 109 + webhook_module.HookEventFork, 110 + webhook_module.HookEventIssueAssign, 111 + webhook_module.HookEventIssueLabel, 112 + webhook_module.HookEventIssueMilestone, 113 + webhook_module.HookEventPullRequestAssign, 114 + webhook_module.HookEventPullRequestLabel, 115 + webhook_module.HookEventPullRequestMilestone, 116 + webhook_module.HookEventPullRequestComment, 117 + webhook_module.HookEventPullRequestReviewApproved, 118 + webhook_module.HookEventPullRequestReviewRejected, 119 + webhook_module.HookEventPullRequestReviewComment, 120 + webhook_module.HookEventWiki, 121 + webhook_module.HookEventRepository, 122 + webhook_module.HookEventRelease, 123 + webhook_module.HookEventPackage: 124 + if len(evt.Acts) != 0 { 125 + log.Warn("Ignore unsupported %s event arguments %q", triggedEvent, evt.Acts) 126 + } 127 + // no special filter parameters for these events, just return true if name matched 128 + return true 129 + 130 + case webhook_module.HookEventPush: 131 + return matchPushEvent(commit, payload.(*api.PushPayload), evt) 132 + 133 + case webhook_module.HookEventIssues: 134 + return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) 135 + 136 + case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync: 137 + return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) 138 + 139 + case webhook_module.HookEventIssueComment: 140 + return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) 141 + 142 + default: 143 + log.Warn("unsupported event %q", triggedEvent) 144 + return false 145 + } 146 + } 147 + 148 + func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobparser.Event) bool { 149 + // with no special filter parameters 104 150 if len(evt.Acts) == 0 { 105 151 return true 106 152 } 107 153 108 - switch triggedEvent { 109 - case webhook_module.HookEventCreate: 110 - fallthrough 111 - case webhook_module.HookEventDelete: 112 - fallthrough 113 - case webhook_module.HookEventFork: 114 - log.Warn("unsupported event %q", triggedEvent.Event()) 115 - return false 116 - case webhook_module.HookEventPush: 117 - pushPayload := payload.(*api.PushPayload) 118 - matchTimes := 0 119 - // all acts conditions should be satisfied 120 - for cond, vals := range evt.Acts { 121 - switch cond { 122 - case "branches", "tags": 123 - refShortName := git.RefName(pushPayload.Ref).ShortName() 154 + matchTimes := 0 155 + // all acts conditions should be satisfied 156 + for cond, vals := range evt.Acts { 157 + switch cond { 158 + case "branches", "tags": 159 + refShortName := git.RefName(pushPayload.Ref).ShortName() 160 + for _, val := range vals { 161 + if glob.MustCompile(val, '/').Match(refShortName) { 162 + matchTimes++ 163 + break 164 + } 165 + } 166 + case "paths": 167 + filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) 168 + if err != nil { 169 + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) 170 + } else { 124 171 for _, val := range vals { 125 - if glob.MustCompile(val, '/').Match(refShortName) { 172 + matched := false 173 + for _, file := range filesChanged { 174 + if glob.MustCompile(val, '/').Match(file) { 175 + matched = true 176 + break 177 + } 178 + } 179 + if matched { 126 180 matchTimes++ 127 181 break 128 182 } 129 183 } 130 - case "paths": 131 - filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) 132 - if err != nil { 133 - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) 134 - } else { 135 - for _, val := range vals { 136 - matched := false 137 - for _, file := range filesChanged { 138 - if glob.MustCompile(val, '/').Match(file) { 139 - matched = true 140 - break 141 - } 142 - } 143 - if matched { 144 - matchTimes++ 145 - break 146 - } 147 - } 184 + } 185 + default: 186 + log.Warn("push event unsupported condition %q", cond) 187 + } 188 + } 189 + return matchTimes == len(evt.Acts) 190 + } 191 + 192 + func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *jobparser.Event) bool { 193 + // with no special filter parameters 194 + if len(evt.Acts) == 0 { 195 + return true 196 + } 197 + 198 + matchTimes := 0 199 + // all acts conditions should be satisfied 200 + for cond, vals := range evt.Acts { 201 + switch cond { 202 + case "types": 203 + for _, val := range vals { 204 + if glob.MustCompile(val, '/').Match(string(issuePayload.Action)) { 205 + matchTimes++ 206 + break 148 207 } 149 - default: 150 - log.Warn("unsupported condition %q", cond) 151 208 } 209 + default: 210 + log.Warn("issue event unsupported condition %q", cond) 152 211 } 153 - return matchTimes == len(evt.Acts) 212 + } 213 + return matchTimes == len(evt.Acts) 214 + } 215 + 216 + func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { 217 + // with no special filter parameters 218 + if len(evt.Acts) == 0 { 219 + // defaultly, only pull request opened and synchronized will trigger workflow 220 + return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened 221 + } 154 222 155 - case webhook_module.HookEventIssues: 156 - fallthrough 157 - case webhook_module.HookEventIssueAssign: 158 - fallthrough 159 - case webhook_module.HookEventIssueLabel: 160 - fallthrough 161 - case webhook_module.HookEventIssueMilestone: 162 - fallthrough 163 - case webhook_module.HookEventIssueComment: 164 - fallthrough 165 - case webhook_module.HookEventPullRequest: 166 - prPayload := payload.(*api.PullRequestPayload) 167 - matchTimes := 0 168 - // all acts conditions should be satisfied 169 - for cond, vals := range evt.Acts { 170 - switch cond { 171 - case "types": 223 + matchTimes := 0 224 + // all acts conditions should be satisfied 225 + for cond, vals := range evt.Acts { 226 + switch cond { 227 + case "types": 228 + action := prPayload.Action 229 + if prPayload.Action == api.HookIssueSynchronized { 230 + action = "synchronize" 231 + } 232 + log.Trace("matching pull_request %s with %v", action, vals) 233 + for _, val := range vals { 234 + if glob.MustCompile(val, '/').Match(string(action)) { 235 + matchTimes++ 236 + break 237 + } 238 + } 239 + case "branches": 240 + refShortName := git.RefName(prPayload.PullRequest.Base.Ref).ShortName() 241 + for _, val := range vals { 242 + if glob.MustCompile(val, '/').Match(refShortName) { 243 + matchTimes++ 244 + break 245 + } 246 + } 247 + case "paths": 248 + filesChanged, err := commit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) 249 + if err != nil { 250 + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) 251 + } else { 172 252 for _, val := range vals { 173 - if glob.MustCompile(val, '/').Match(string(prPayload.Action)) { 174 - matchTimes++ 175 - break 253 + matched := false 254 + for _, file := range filesChanged { 255 + if glob.MustCompile(val, '/').Match(file) { 256 + matched = true 257 + break 258 + } 176 259 } 177 - } 178 - case "branches": 179 - refShortName := git.RefName(prPayload.PullRequest.Base.Ref).ShortName() 180 - for _, val := range vals { 181 - if glob.MustCompile(val, '/').Match(refShortName) { 260 + if matched { 182 261 matchTimes++ 183 262 break 184 263 } 185 264 } 186 - case "paths": 187 - filesChanged, err := commit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) 188 - if err != nil { 189 - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) 190 - } else { 191 - for _, val := range vals { 192 - matched := false 193 - for _, file := range filesChanged { 194 - if glob.MustCompile(val, '/').Match(file) { 195 - matched = true 196 - break 197 - } 198 - } 199 - if matched { 200 - matchTimes++ 201 - break 202 - } 203 - } 265 + } 266 + default: 267 + log.Warn("pull request event unsupported condition %q", cond) 268 + } 269 + } 270 + return matchTimes == len(evt.Acts) 271 + } 272 + 273 + func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCommentPayload, evt *jobparser.Event) bool { 274 + // with no special filter parameters 275 + if len(evt.Acts) == 0 { 276 + return true 277 + } 278 + 279 + matchTimes := 0 280 + // all acts conditions should be satisfied 281 + for cond, vals := range evt.Acts { 282 + switch cond { 283 + case "types": 284 + for _, val := range vals { 285 + if glob.MustCompile(val, '/').Match(string(issueCommentPayload.Action)) { 286 + matchTimes++ 287 + break 204 288 } 205 - default: 206 - log.Warn("unsupported condition %q", cond) 207 289 } 290 + default: 291 + log.Warn("issue comment unsupported condition %q", cond) 208 292 } 209 - return matchTimes == len(evt.Acts) 210 - case webhook_module.HookEventPullRequestAssign: 211 - fallthrough 212 - case webhook_module.HookEventPullRequestLabel: 213 - fallthrough 214 - case webhook_module.HookEventPullRequestMilestone: 215 - fallthrough 216 - case webhook_module.HookEventPullRequestComment: 217 - fallthrough 218 - case webhook_module.HookEventPullRequestReviewApproved: 219 - fallthrough 220 - case webhook_module.HookEventPullRequestReviewRejected: 221 - fallthrough 222 - case webhook_module.HookEventPullRequestReviewComment: 223 - fallthrough 224 - case webhook_module.HookEventPullRequestSync: 225 - fallthrough 226 - case webhook_module.HookEventWiki: 227 - fallthrough 228 - case webhook_module.HookEventRepository: 229 - fallthrough 230 - case webhook_module.HookEventRelease: 231 - fallthrough 232 - case webhook_module.HookEventPackage: 233 - fallthrough 234 - default: 235 - log.Warn("unsupported event %q", triggedEvent.Event()) 236 293 } 237 - return false 294 + return matchTimes == len(evt.Acts) 238 295 }