gitea源码

github.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. webhook_module "code.gitea.io/gitea/modules/webhook"
  6. )
  7. const (
  8. GithubEventPullRequest = "pull_request"
  9. GithubEventPullRequestTarget = "pull_request_target"
  10. GithubEventPullRequestReviewComment = "pull_request_review_comment"
  11. GithubEventPullRequestReview = "pull_request_review"
  12. GithubEventRegistryPackage = "registry_package"
  13. GithubEventCreate = "create"
  14. GithubEventDelete = "delete"
  15. GithubEventFork = "fork"
  16. GithubEventPush = "push"
  17. GithubEventIssues = "issues"
  18. GithubEventIssueComment = "issue_comment"
  19. GithubEventRelease = "release"
  20. GithubEventPullRequestComment = "pull_request_comment"
  21. GithubEventGollum = "gollum"
  22. GithubEventSchedule = "schedule"
  23. )
  24. // IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch
  25. func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
  26. switch triggedEvent {
  27. case webhook_module.HookEventDelete:
  28. // GitHub "delete" event
  29. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete
  30. return true
  31. case webhook_module.HookEventFork:
  32. // GitHub "fork" event
  33. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#fork
  34. return true
  35. case webhook_module.HookEventIssueComment:
  36. // GitHub "issue_comment" event
  37. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
  38. return true
  39. case webhook_module.HookEventPullRequestComment:
  40. // GitHub "pull_request_comment" event
  41. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
  42. return true
  43. case webhook_module.HookEventWiki:
  44. // GitHub "gollum" event
  45. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
  46. return true
  47. case webhook_module.HookEventSchedule:
  48. // GitHub "schedule" event
  49. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
  50. return true
  51. case webhook_module.HookEventIssues,
  52. webhook_module.HookEventIssueAssign,
  53. webhook_module.HookEventIssueLabel,
  54. webhook_module.HookEventIssueMilestone:
  55. // Github "issues" event
  56. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
  57. return true
  58. }
  59. return false
  60. }
  61. // canGithubEventMatch check if the input Github event can match any Gitea event.
  62. func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
  63. switch eventName {
  64. case GithubEventRegistryPackage:
  65. return triggedEvent == webhook_module.HookEventPackage
  66. // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
  67. case GithubEventGollum:
  68. return triggedEvent == webhook_module.HookEventWiki
  69. case GithubEventIssues:
  70. switch triggedEvent {
  71. case webhook_module.HookEventIssues,
  72. webhook_module.HookEventIssueAssign,
  73. webhook_module.HookEventIssueLabel,
  74. webhook_module.HookEventIssueMilestone:
  75. return true
  76. default:
  77. return false
  78. }
  79. case GithubEventPullRequest, GithubEventPullRequestTarget:
  80. switch triggedEvent {
  81. case webhook_module.HookEventPullRequest,
  82. webhook_module.HookEventPullRequestSync,
  83. webhook_module.HookEventPullRequestAssign,
  84. webhook_module.HookEventPullRequestLabel,
  85. webhook_module.HookEventPullRequestReviewRequest,
  86. webhook_module.HookEventPullRequestMilestone:
  87. return true
  88. default:
  89. return false
  90. }
  91. case GithubEventPullRequestReview:
  92. switch triggedEvent {
  93. case webhook_module.HookEventPullRequestReviewApproved,
  94. webhook_module.HookEventPullRequestReviewComment,
  95. webhook_module.HookEventPullRequestReviewRejected:
  96. return true
  97. default:
  98. return false
  99. }
  100. case GithubEventSchedule:
  101. return triggedEvent == webhook_module.HookEventSchedule
  102. case GithubEventIssueComment:
  103. // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
  104. return triggedEvent == webhook_module.HookEventIssueComment ||
  105. triggedEvent == webhook_module.HookEventPullRequestComment
  106. default:
  107. return eventName == string(triggedEvent)
  108. }
  109. }