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