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.

actions_trigger_test.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/url"
  6. "strings"
  7. "testing"
  8. "time"
  9. actions_model "code.gitea.io/gitea/models/actions"
  10. "code.gitea.io/gitea/models/db"
  11. issues_model "code.gitea.io/gitea/models/issues"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. unit_model "code.gitea.io/gitea/models/unit"
  14. "code.gitea.io/gitea/models/unittest"
  15. user_model "code.gitea.io/gitea/models/user"
  16. actions_module "code.gitea.io/gitea/modules/actions"
  17. "code.gitea.io/gitea/modules/git"
  18. pull_service "code.gitea.io/gitea/services/pull"
  19. repo_service "code.gitea.io/gitea/services/repository"
  20. files_service "code.gitea.io/gitea/services/repository/files"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func TestPullRequestTargetEvent(t *testing.T) {
  24. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  25. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the base repo
  26. org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the forked repo
  27. // create the base repo
  28. baseRepo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
  29. Name: "repo-pull-request-target",
  30. Description: "test pull-request-target event",
  31. AutoInit: true,
  32. Gitignores: "Go",
  33. License: "MIT",
  34. Readme: "Default",
  35. DefaultBranch: "main",
  36. IsPrivate: false,
  37. })
  38. assert.NoError(t, err)
  39. assert.NotEmpty(t, baseRepo)
  40. // enable actions
  41. err = repo_service.UpdateRepositoryUnits(db.DefaultContext, baseRepo, []repo_model.RepoUnit{{
  42. RepoID: baseRepo.ID,
  43. Type: unit_model.TypeActions,
  44. }}, nil)
  45. assert.NoError(t, err)
  46. // create the forked repo
  47. forkedRepo, err := repo_service.ForkRepository(git.DefaultContext, user2, org3, repo_service.ForkRepoOptions{
  48. BaseRepo: baseRepo,
  49. Name: "forked-repo-pull-request-target",
  50. Description: "test pull-request-target event",
  51. })
  52. assert.NoError(t, err)
  53. assert.NotEmpty(t, forkedRepo)
  54. // add workflow file to the base repo
  55. addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, baseRepo, user2, &files_service.ChangeRepoFilesOptions{
  56. Files: []*files_service.ChangeRepoFile{
  57. {
  58. Operation: "create",
  59. TreePath: ".gitea/workflows/pr.yml",
  60. ContentReader: strings.NewReader("name: test\non:\n pull_request_target:\n paths:\n - 'file_*.txt'\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
  61. },
  62. },
  63. Message: "add workflow",
  64. OldBranch: "main",
  65. NewBranch: "main",
  66. Author: &files_service.IdentityOptions{
  67. Name: user2.Name,
  68. Email: user2.Email,
  69. },
  70. Committer: &files_service.IdentityOptions{
  71. Name: user2.Name,
  72. Email: user2.Email,
  73. },
  74. Dates: &files_service.CommitDateOptions{
  75. Author: time.Now(),
  76. Committer: time.Now(),
  77. },
  78. })
  79. assert.NoError(t, err)
  80. assert.NotEmpty(t, addWorkflowToBaseResp)
  81. // add a new file to the forked repo
  82. addFileToForkedResp, err := files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, org3, &files_service.ChangeRepoFilesOptions{
  83. Files: []*files_service.ChangeRepoFile{
  84. {
  85. Operation: "create",
  86. TreePath: "file_1.txt",
  87. ContentReader: strings.NewReader("file1"),
  88. },
  89. },
  90. Message: "add file1",
  91. OldBranch: "main",
  92. NewBranch: "fork-branch-1",
  93. Author: &files_service.IdentityOptions{
  94. Name: org3.Name,
  95. Email: org3.Email,
  96. },
  97. Committer: &files_service.IdentityOptions{
  98. Name: org3.Name,
  99. Email: org3.Email,
  100. },
  101. Dates: &files_service.CommitDateOptions{
  102. Author: time.Now(),
  103. Committer: time.Now(),
  104. },
  105. })
  106. assert.NoError(t, err)
  107. assert.NotEmpty(t, addFileToForkedResp)
  108. // create Pull
  109. pullIssue := &issues_model.Issue{
  110. RepoID: baseRepo.ID,
  111. Title: "Test pull-request-target-event",
  112. PosterID: org3.ID,
  113. Poster: org3,
  114. IsPull: true,
  115. }
  116. pullRequest := &issues_model.PullRequest{
  117. HeadRepoID: forkedRepo.ID,
  118. BaseRepoID: baseRepo.ID,
  119. HeadBranch: "fork-branch-1",
  120. BaseBranch: "main",
  121. HeadRepo: forkedRepo,
  122. BaseRepo: baseRepo,
  123. Type: issues_model.PullRequestGitea,
  124. }
  125. err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
  126. assert.NoError(t, err)
  127. // load and compare ActionRun
  128. assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
  129. actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID})
  130. assert.Equal(t, addFileToForkedResp.Commit.SHA, actionRun.CommitSHA)
  131. assert.Equal(t, actions_module.GithubEventPullRequestTarget, actionRun.TriggerEvent)
  132. // add another file whose name cannot match the specified path
  133. addFileToForkedResp, err = files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, org3, &files_service.ChangeRepoFilesOptions{
  134. Files: []*files_service.ChangeRepoFile{
  135. {
  136. Operation: "create",
  137. TreePath: "foo.txt",
  138. ContentReader: strings.NewReader("foo"),
  139. },
  140. },
  141. Message: "add foo.txt",
  142. OldBranch: "main",
  143. NewBranch: "fork-branch-2",
  144. Author: &files_service.IdentityOptions{
  145. Name: org3.Name,
  146. Email: org3.Email,
  147. },
  148. Committer: &files_service.IdentityOptions{
  149. Name: org3.Name,
  150. Email: org3.Email,
  151. },
  152. Dates: &files_service.CommitDateOptions{
  153. Author: time.Now(),
  154. Committer: time.Now(),
  155. },
  156. })
  157. assert.NoError(t, err)
  158. assert.NotEmpty(t, addFileToForkedResp)
  159. // create Pull
  160. pullIssue = &issues_model.Issue{
  161. RepoID: baseRepo.ID,
  162. Title: "A mismatched path cannot trigger pull-request-target-event",
  163. PosterID: org3.ID,
  164. Poster: org3,
  165. IsPull: true,
  166. }
  167. pullRequest = &issues_model.PullRequest{
  168. HeadRepoID: forkedRepo.ID,
  169. BaseRepoID: baseRepo.ID,
  170. HeadBranch: "fork-branch-2",
  171. BaseBranch: "main",
  172. HeadRepo: forkedRepo,
  173. BaseRepo: baseRepo,
  174. Type: issues_model.PullRequestGitea,
  175. }
  176. err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
  177. assert.NoError(t, err)
  178. // the new pull request cannot trigger actions, so there is still only 1 record
  179. assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: baseRepo.ID}))
  180. })
  181. }