You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflows_test.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/modules/git"
  7. api "code.gitea.io/gitea/modules/structs"
  8. webhook_module "code.gitea.io/gitea/modules/webhook"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestDetectMatched(t *testing.T) {
  12. testCases := []struct {
  13. desc string
  14. commit *git.Commit
  15. triggedEvent webhook_module.HookEventType
  16. payload api.Payloader
  17. yamlOn string
  18. expected bool
  19. }{
  20. {
  21. desc: "HookEventCreate(create) matches GithubEventCreate(create)",
  22. triggedEvent: webhook_module.HookEventCreate,
  23. payload: nil,
  24. yamlOn: "on: create",
  25. expected: true,
  26. },
  27. {
  28. desc: "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
  29. triggedEvent: webhook_module.HookEventIssues,
  30. payload: &api.IssuePayload{Action: api.HookIssueOpened},
  31. yamlOn: "on: issues",
  32. expected: true,
  33. },
  34. {
  35. desc: "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
  36. triggedEvent: webhook_module.HookEventIssues,
  37. payload: &api.IssuePayload{Action: api.HookIssueMilestoned},
  38. yamlOn: "on: issues",
  39. expected: true,
  40. },
  41. {
  42. desc: "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
  43. triggedEvent: webhook_module.HookEventPullRequestSync,
  44. payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized},
  45. yamlOn: "on: pull_request",
  46. expected: true,
  47. },
  48. {
  49. desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
  50. triggedEvent: webhook_module.HookEventPullRequest,
  51. payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
  52. yamlOn: "on: pull_request",
  53. expected: false,
  54. },
  55. {
  56. desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
  57. triggedEvent: webhook_module.HookEventPullRequest,
  58. payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
  59. yamlOn: "on: pull_request",
  60. expected: false,
  61. },
  62. {
  63. desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
  64. triggedEvent: webhook_module.HookEventPullRequest,
  65. payload: &api.PullRequestPayload{
  66. Action: api.HookIssueClosed,
  67. PullRequest: &api.PullRequest{
  68. Base: &api.PRBranchInfo{},
  69. },
  70. },
  71. yamlOn: "on:\n pull_request:\n branches: [main]",
  72. expected: false,
  73. },
  74. {
  75. desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
  76. triggedEvent: webhook_module.HookEventPullRequest,
  77. payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
  78. yamlOn: "on:\n pull_request:\n types: [labeled]",
  79. expected: true,
  80. },
  81. {
  82. desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
  83. triggedEvent: webhook_module.HookEventPullRequestReviewComment,
  84. payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
  85. yamlOn: "on:\n pull_request_review_comment:\n types: [created]",
  86. expected: true,
  87. },
  88. {
  89. desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
  90. triggedEvent: webhook_module.HookEventPullRequestReviewRejected,
  91. payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
  92. yamlOn: "on:\n pull_request_review:\n types: [dismissed]",
  93. expected: false,
  94. },
  95. {
  96. desc: "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
  97. triggedEvent: webhook_module.HookEventRelease,
  98. payload: &api.ReleasePayload{Action: api.HookReleasePublished},
  99. yamlOn: "on:\n release:\n types: [published]",
  100. expected: true,
  101. },
  102. {
  103. desc: "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
  104. triggedEvent: webhook_module.HookEventPackage,
  105. payload: &api.PackagePayload{Action: api.HookPackageCreated},
  106. yamlOn: "on:\n registry_package:\n types: [updated]",
  107. expected: false,
  108. },
  109. {
  110. desc: "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
  111. triggedEvent: webhook_module.HookEventWiki,
  112. payload: nil,
  113. yamlOn: "on: gollum",
  114. expected: true,
  115. },
  116. {
  117. desc: "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)",
  118. triggedEvent: webhook_module.HookEventSchedule,
  119. payload: nil,
  120. yamlOn: "on: schedule",
  121. expected: true,
  122. },
  123. }
  124. for _, tc := range testCases {
  125. t.Run(tc.desc, func(t *testing.T) {
  126. evts, err := GetEventsFromContent([]byte(tc.yamlOn))
  127. assert.NoError(t, err)
  128. assert.Len(t, evts, 1)
  129. assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggedEvent, tc.payload, evts[0]))
  130. })
  131. }
  132. }