Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

workflows.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "bytes"
  6. "io"
  7. "strings"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. api "code.gitea.io/gitea/modules/structs"
  11. webhook_module "code.gitea.io/gitea/modules/webhook"
  12. "github.com/gobwas/glob"
  13. "github.com/nektos/act/pkg/jobparser"
  14. "github.com/nektos/act/pkg/model"
  15. "github.com/nektos/act/pkg/workflowpattern"
  16. "gopkg.in/yaml.v3"
  17. )
  18. type DetectedWorkflow struct {
  19. EntryName string
  20. TriggerEvent *jobparser.Event
  21. Content []byte
  22. }
  23. func init() {
  24. model.OnDecodeNodeError = func(node yaml.Node, out any, err error) {
  25. // Log the error instead of panic or fatal.
  26. // It will be a big job to refactor act/pkg/model to return decode error,
  27. // so we just log the error and return empty value, and improve it later.
  28. log.Error("Failed to decode node %v into %T: %v", node, out, err)
  29. }
  30. }
  31. func IsWorkflow(path string) bool {
  32. if (!strings.HasSuffix(path, ".yaml")) && (!strings.HasSuffix(path, ".yml")) {
  33. return false
  34. }
  35. return strings.HasPrefix(path, ".gitea/workflows") || strings.HasPrefix(path, ".github/workflows")
  36. }
  37. func ListWorkflows(commit *git.Commit) (git.Entries, error) {
  38. tree, err := commit.SubTree(".gitea/workflows")
  39. if _, ok := err.(git.ErrNotExist); ok {
  40. tree, err = commit.SubTree(".github/workflows")
  41. }
  42. if _, ok := err.(git.ErrNotExist); ok {
  43. return nil, nil
  44. }
  45. if err != nil {
  46. return nil, err
  47. }
  48. entries, err := tree.ListEntriesRecursiveFast()
  49. if err != nil {
  50. return nil, err
  51. }
  52. ret := make(git.Entries, 0, len(entries))
  53. for _, entry := range entries {
  54. if strings.HasSuffix(entry.Name(), ".yml") || strings.HasSuffix(entry.Name(), ".yaml") {
  55. ret = append(ret, entry)
  56. }
  57. }
  58. return ret, nil
  59. }
  60. func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
  61. f, err := entry.Blob().DataAsync()
  62. if err != nil {
  63. return nil, err
  64. }
  65. content, err := io.ReadAll(f)
  66. _ = f.Close()
  67. if err != nil {
  68. return nil, err
  69. }
  70. return content, nil
  71. }
  72. func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
  73. workflow, err := model.ReadWorkflow(bytes.NewReader(content))
  74. if err != nil {
  75. return nil, err
  76. }
  77. events, err := jobparser.ParseRawOn(&workflow.RawOn)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return events, nil
  82. }
  83. func DetectWorkflows(
  84. gitRepo *git.Repository,
  85. commit *git.Commit,
  86. triggedEvent webhook_module.HookEventType,
  87. payload api.Payloader,
  88. detectSchedule bool,
  89. ) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
  90. entries, err := ListWorkflows(commit)
  91. if err != nil {
  92. return nil, nil, err
  93. }
  94. workflows := make([]*DetectedWorkflow, 0, len(entries))
  95. schedules := make([]*DetectedWorkflow, 0, len(entries))
  96. for _, entry := range entries {
  97. content, err := GetContentFromEntry(entry)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. // one workflow may have multiple events
  102. events, err := GetEventsFromContent(content)
  103. if err != nil {
  104. log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
  105. continue
  106. }
  107. for _, evt := range events {
  108. log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent)
  109. if evt.IsSchedule() {
  110. if detectSchedule {
  111. dwf := &DetectedWorkflow{
  112. EntryName: entry.Name(),
  113. TriggerEvent: evt,
  114. Content: content,
  115. }
  116. schedules = append(schedules, dwf)
  117. }
  118. } else if detectMatched(gitRepo, commit, triggedEvent, payload, evt) {
  119. dwf := &DetectedWorkflow{
  120. EntryName: entry.Name(),
  121. TriggerEvent: evt,
  122. Content: content,
  123. }
  124. workflows = append(workflows, dwf)
  125. }
  126. }
  127. }
  128. return workflows, schedules, nil
  129. }
  130. func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
  131. entries, err := ListWorkflows(commit)
  132. if err != nil {
  133. return nil, err
  134. }
  135. wfs := make([]*DetectedWorkflow, 0, len(entries))
  136. for _, entry := range entries {
  137. content, err := GetContentFromEntry(entry)
  138. if err != nil {
  139. return nil, err
  140. }
  141. // one workflow may have multiple events
  142. events, err := GetEventsFromContent(content)
  143. if err != nil {
  144. log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
  145. continue
  146. }
  147. for _, evt := range events {
  148. if evt.IsSchedule() {
  149. log.Trace("detect scheduled workflow: %q", entry.Name())
  150. dwf := &DetectedWorkflow{
  151. EntryName: entry.Name(),
  152. TriggerEvent: evt,
  153. Content: content,
  154. }
  155. wfs = append(wfs, dwf)
  156. }
  157. }
  158. }
  159. return wfs, nil
  160. }
  161. func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool {
  162. if !canGithubEventMatch(evt.Name, triggedEvent) {
  163. return false
  164. }
  165. switch triggedEvent {
  166. case // events with no activity types
  167. webhook_module.HookEventCreate,
  168. webhook_module.HookEventDelete,
  169. webhook_module.HookEventFork,
  170. webhook_module.HookEventWiki,
  171. webhook_module.HookEventSchedule:
  172. if len(evt.Acts()) != 0 {
  173. log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts())
  174. }
  175. // no special filter parameters for these events, just return true if name matched
  176. return true
  177. case // push
  178. webhook_module.HookEventPush:
  179. return matchPushEvent(commit, payload.(*api.PushPayload), evt)
  180. case // issues
  181. webhook_module.HookEventIssues,
  182. webhook_module.HookEventIssueAssign,
  183. webhook_module.HookEventIssueLabel,
  184. webhook_module.HookEventIssueMilestone:
  185. return matchIssuesEvent(payload.(*api.IssuePayload), evt)
  186. case // issue_comment
  187. webhook_module.HookEventIssueComment,
  188. // `pull_request_comment` is same as `issue_comment`
  189. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
  190. webhook_module.HookEventPullRequestComment:
  191. return matchIssueCommentEvent(payload.(*api.IssueCommentPayload), evt)
  192. case // pull_request
  193. webhook_module.HookEventPullRequest,
  194. webhook_module.HookEventPullRequestSync,
  195. webhook_module.HookEventPullRequestAssign,
  196. webhook_module.HookEventPullRequestLabel,
  197. webhook_module.HookEventPullRequestReviewRequest,
  198. webhook_module.HookEventPullRequestMilestone:
  199. return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt)
  200. case // pull_request_review
  201. webhook_module.HookEventPullRequestReviewApproved,
  202. webhook_module.HookEventPullRequestReviewRejected:
  203. return matchPullRequestReviewEvent(payload.(*api.PullRequestPayload), evt)
  204. case // pull_request_review_comment
  205. webhook_module.HookEventPullRequestReviewComment:
  206. return matchPullRequestReviewCommentEvent(payload.(*api.PullRequestPayload), evt)
  207. case // release
  208. webhook_module.HookEventRelease:
  209. return matchReleaseEvent(payload.(*api.ReleasePayload), evt)
  210. case // registry_package
  211. webhook_module.HookEventPackage:
  212. return matchPackageEvent(payload.(*api.PackagePayload), evt)
  213. default:
  214. log.Warn("unsupported event %q", triggedEvent)
  215. return false
  216. }
  217. }
  218. func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobparser.Event) bool {
  219. // with no special filter parameters
  220. if len(evt.Acts()) == 0 {
  221. return true
  222. }
  223. matchTimes := 0
  224. hasBranchFilter := false
  225. hasTagFilter := false
  226. refName := git.RefName(pushPayload.Ref)
  227. // all acts conditions should be satisfied
  228. for cond, vals := range evt.Acts() {
  229. switch cond {
  230. case "branches":
  231. hasBranchFilter = true
  232. if !refName.IsBranch() {
  233. break
  234. }
  235. patterns, err := workflowpattern.CompilePatterns(vals...)
  236. if err != nil {
  237. break
  238. }
  239. if !workflowpattern.Skip(patterns, []string{refName.BranchName()}, &workflowpattern.EmptyTraceWriter{}) {
  240. matchTimes++
  241. }
  242. case "branches-ignore":
  243. hasBranchFilter = true
  244. if !refName.IsBranch() {
  245. break
  246. }
  247. patterns, err := workflowpattern.CompilePatterns(vals...)
  248. if err != nil {
  249. break
  250. }
  251. if !workflowpattern.Filter(patterns, []string{refName.BranchName()}, &workflowpattern.EmptyTraceWriter{}) {
  252. matchTimes++
  253. }
  254. case "tags":
  255. hasTagFilter = true
  256. if !refName.IsTag() {
  257. break
  258. }
  259. patterns, err := workflowpattern.CompilePatterns(vals...)
  260. if err != nil {
  261. break
  262. }
  263. if !workflowpattern.Skip(patterns, []string{refName.TagName()}, &workflowpattern.EmptyTraceWriter{}) {
  264. matchTimes++
  265. }
  266. case "tags-ignore":
  267. hasTagFilter = true
  268. if !refName.IsTag() {
  269. break
  270. }
  271. patterns, err := workflowpattern.CompilePatterns(vals...)
  272. if err != nil {
  273. break
  274. }
  275. if !workflowpattern.Filter(patterns, []string{refName.TagName()}, &workflowpattern.EmptyTraceWriter{}) {
  276. matchTimes++
  277. }
  278. case "paths":
  279. filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before)
  280. if err != nil {
  281. log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err)
  282. } else {
  283. patterns, err := workflowpattern.CompilePatterns(vals...)
  284. if err != nil {
  285. break
  286. }
  287. if !workflowpattern.Skip(patterns, filesChanged, &workflowpattern.EmptyTraceWriter{}) {
  288. matchTimes++
  289. }
  290. }
  291. case "paths-ignore":
  292. filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before)
  293. if err != nil {
  294. log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err)
  295. } else {
  296. patterns, err := workflowpattern.CompilePatterns(vals...)
  297. if err != nil {
  298. break
  299. }
  300. if !workflowpattern.Filter(patterns, filesChanged, &workflowpattern.EmptyTraceWriter{}) {
  301. matchTimes++
  302. }
  303. }
  304. default:
  305. log.Warn("push event unsupported condition %q", cond)
  306. }
  307. }
  308. // if both branch and tag filter are defined in the workflow only one needs to match
  309. if hasBranchFilter && hasTagFilter {
  310. matchTimes++
  311. }
  312. return matchTimes == len(evt.Acts())
  313. }
  314. func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool {
  315. // with no special filter parameters
  316. if len(evt.Acts()) == 0 {
  317. return true
  318. }
  319. matchTimes := 0
  320. // all acts conditions should be satisfied
  321. for cond, vals := range evt.Acts() {
  322. switch cond {
  323. case "types":
  324. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
  325. // Actions with the same name:
  326. // opened, edited, closed, reopened, assigned, unassigned, milestoned, demilestoned
  327. // Actions need to be converted:
  328. // label_updated -> labeled
  329. // label_cleared -> unlabeled
  330. // Unsupported activity types:
  331. // deleted, transferred, pinned, unpinned, locked, unlocked
  332. action := issuePayload.Action
  333. switch action {
  334. case api.HookIssueLabelUpdated:
  335. action = "labeled"
  336. case api.HookIssueLabelCleared:
  337. action = "unlabeled"
  338. }
  339. for _, val := range vals {
  340. if glob.MustCompile(val, '/').Match(string(action)) {
  341. matchTimes++
  342. break
  343. }
  344. }
  345. default:
  346. log.Warn("issue event unsupported condition %q", cond)
  347. }
  348. }
  349. return matchTimes == len(evt.Acts())
  350. }
  351. func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
  352. acts := evt.Acts()
  353. activityTypeMatched := false
  354. matchTimes := 0
  355. if vals, ok := acts["types"]; !ok {
  356. // defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow
  357. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
  358. activityTypeMatched = prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
  359. } else {
  360. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
  361. // Actions with the same name:
  362. // opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed, milestoned, demilestoned
  363. // Actions need to be converted:
  364. // synchronized -> synchronize
  365. // label_updated -> labeled
  366. // label_cleared -> unlabeled
  367. // Unsupported activity types:
  368. // converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled, enqueued, dequeued
  369. action := prPayload.Action
  370. switch action {
  371. case api.HookIssueSynchronized:
  372. action = "synchronize"
  373. case api.HookIssueLabelUpdated:
  374. action = "labeled"
  375. case api.HookIssueLabelCleared:
  376. action = "unlabeled"
  377. }
  378. log.Trace("matching pull_request %s with %v", action, vals)
  379. for _, val := range vals {
  380. if glob.MustCompile(val, '/').Match(string(action)) {
  381. activityTypeMatched = true
  382. matchTimes++
  383. break
  384. }
  385. }
  386. }
  387. var (
  388. headCommit = commit
  389. err error
  390. )
  391. if evt.Name == GithubEventPullRequestTarget && (len(acts["paths"]) > 0 || len(acts["paths-ignore"]) > 0) {
  392. headCommit, err = gitRepo.GetCommit(prPayload.PullRequest.Head.Sha)
  393. if err != nil {
  394. log.Error("GetCommit [ref: %s]: %v", prPayload.PullRequest.Head.Sha, err)
  395. return false
  396. }
  397. }
  398. // all acts conditions should be satisfied
  399. for cond, vals := range acts {
  400. switch cond {
  401. case "types":
  402. // types have been checked
  403. continue
  404. case "branches":
  405. refName := git.RefName(prPayload.PullRequest.Base.Ref)
  406. patterns, err := workflowpattern.CompilePatterns(vals...)
  407. if err != nil {
  408. break
  409. }
  410. if !workflowpattern.Skip(patterns, []string{refName.ShortName()}, &workflowpattern.EmptyTraceWriter{}) {
  411. matchTimes++
  412. }
  413. case "branches-ignore":
  414. refName := git.RefName(prPayload.PullRequest.Base.Ref)
  415. patterns, err := workflowpattern.CompilePatterns(vals...)
  416. if err != nil {
  417. break
  418. }
  419. if !workflowpattern.Filter(patterns, []string{refName.ShortName()}, &workflowpattern.EmptyTraceWriter{}) {
  420. matchTimes++
  421. }
  422. case "paths":
  423. filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref)
  424. if err != nil {
  425. log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", headCommit.ID.String(), err)
  426. } else {
  427. patterns, err := workflowpattern.CompilePatterns(vals...)
  428. if err != nil {
  429. break
  430. }
  431. if !workflowpattern.Skip(patterns, filesChanged, &workflowpattern.EmptyTraceWriter{}) {
  432. matchTimes++
  433. }
  434. }
  435. case "paths-ignore":
  436. filesChanged, err := headCommit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref)
  437. if err != nil {
  438. log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", headCommit.ID.String(), err)
  439. } else {
  440. patterns, err := workflowpattern.CompilePatterns(vals...)
  441. if err != nil {
  442. break
  443. }
  444. if !workflowpattern.Filter(patterns, filesChanged, &workflowpattern.EmptyTraceWriter{}) {
  445. matchTimes++
  446. }
  447. }
  448. default:
  449. log.Warn("pull request event unsupported condition %q", cond)
  450. }
  451. }
  452. return activityTypeMatched && matchTimes == len(evt.Acts())
  453. }
  454. func matchIssueCommentEvent(issueCommentPayload *api.IssueCommentPayload, evt *jobparser.Event) bool {
  455. // with no special filter parameters
  456. if len(evt.Acts()) == 0 {
  457. return true
  458. }
  459. matchTimes := 0
  460. // all acts conditions should be satisfied
  461. for cond, vals := range evt.Acts() {
  462. switch cond {
  463. case "types":
  464. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
  465. // Actions with the same name:
  466. // created, edited, deleted
  467. // Actions need to be converted:
  468. // NONE
  469. // Unsupported activity types:
  470. // NONE
  471. for _, val := range vals {
  472. if glob.MustCompile(val, '/').Match(string(issueCommentPayload.Action)) {
  473. matchTimes++
  474. break
  475. }
  476. }
  477. default:
  478. log.Warn("issue comment event unsupported condition %q", cond)
  479. }
  480. }
  481. return matchTimes == len(evt.Acts())
  482. }
  483. func matchPullRequestReviewEvent(prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
  484. // with no special filter parameters
  485. if len(evt.Acts()) == 0 {
  486. return true
  487. }
  488. matchTimes := 0
  489. // all acts conditions should be satisfied
  490. for cond, vals := range evt.Acts() {
  491. switch cond {
  492. case "types":
  493. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review
  494. // Activity types with the same name:
  495. // NONE
  496. // Activity types need to be converted:
  497. // reviewed -> submitted
  498. // reviewed -> edited
  499. // Unsupported activity types:
  500. // dismissed
  501. actions := make([]string, 0)
  502. if prPayload.Action == api.HookIssueReviewed {
  503. // the `reviewed` HookIssueAction can match the two activity types: `submitted` and `edited`
  504. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review
  505. actions = append(actions, "submitted", "edited")
  506. }
  507. matched := false
  508. for _, val := range vals {
  509. for _, action := range actions {
  510. if glob.MustCompile(val, '/').Match(action) {
  511. matched = true
  512. break
  513. }
  514. }
  515. if matched {
  516. break
  517. }
  518. }
  519. if matched {
  520. matchTimes++
  521. }
  522. default:
  523. log.Warn("pull request review event unsupported condition %q", cond)
  524. }
  525. }
  526. return matchTimes == len(evt.Acts())
  527. }
  528. func matchPullRequestReviewCommentEvent(prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
  529. // with no special filter parameters
  530. if len(evt.Acts()) == 0 {
  531. return true
  532. }
  533. matchTimes := 0
  534. // all acts conditions should be satisfied
  535. for cond, vals := range evt.Acts() {
  536. switch cond {
  537. case "types":
  538. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment
  539. // Activity types with the same name:
  540. // NONE
  541. // Activity types need to be converted:
  542. // reviewed -> created
  543. // reviewed -> edited
  544. // Unsupported activity types:
  545. // deleted
  546. actions := make([]string, 0)
  547. if prPayload.Action == api.HookIssueReviewed {
  548. // the `reviewed` HookIssueAction can match the two activity types: `created` and `edited`
  549. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment
  550. actions = append(actions, "created", "edited")
  551. }
  552. matched := false
  553. for _, val := range vals {
  554. for _, action := range actions {
  555. if glob.MustCompile(val, '/').Match(action) {
  556. matched = true
  557. break
  558. }
  559. }
  560. if matched {
  561. break
  562. }
  563. }
  564. if matched {
  565. matchTimes++
  566. }
  567. default:
  568. log.Warn("pull request review comment event unsupported condition %q", cond)
  569. }
  570. }
  571. return matchTimes == len(evt.Acts())
  572. }
  573. func matchReleaseEvent(payload *api.ReleasePayload, evt *jobparser.Event) bool {
  574. // with no special filter parameters
  575. if len(evt.Acts()) == 0 {
  576. return true
  577. }
  578. matchTimes := 0
  579. // all acts conditions should be satisfied
  580. for cond, vals := range evt.Acts() {
  581. switch cond {
  582. case "types":
  583. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#release
  584. // Activity types with the same name:
  585. // published
  586. // Activity types need to be converted:
  587. // updated -> edited
  588. // Unsupported activity types:
  589. // unpublished, created, deleted, prereleased, released
  590. action := payload.Action
  591. switch action {
  592. case api.HookReleaseUpdated:
  593. action = "edited"
  594. }
  595. for _, val := range vals {
  596. if glob.MustCompile(val, '/').Match(string(action)) {
  597. matchTimes++
  598. break
  599. }
  600. }
  601. default:
  602. log.Warn("release event unsupported condition %q", cond)
  603. }
  604. }
  605. return matchTimes == len(evt.Acts())
  606. }
  607. func matchPackageEvent(payload *api.PackagePayload, evt *jobparser.Event) bool {
  608. // with no special filter parameters
  609. if len(evt.Acts()) == 0 {
  610. return true
  611. }
  612. matchTimes := 0
  613. // all acts conditions should be satisfied
  614. for cond, vals := range evt.Acts() {
  615. switch cond {
  616. case "types":
  617. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#registry_package
  618. // Activity types with the same name:
  619. // NONE
  620. // Activity types need to be converted:
  621. // created -> published
  622. // Unsupported activity types:
  623. // updated
  624. action := payload.Action
  625. switch action {
  626. case api.HookPackageCreated:
  627. action = "published"
  628. }
  629. for _, val := range vals {
  630. if glob.MustCompile(val, '/').Match(string(action)) {
  631. matchTimes++
  632. break
  633. }
  634. }
  635. default:
  636. log.Warn("package event unsupported condition %q", cond)
  637. }
  638. }
  639. return matchTimes == len(evt.Acts())
  640. }