Skip to content

Support pull_request_target event #25229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jun 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
adf0ecd
add DetectedWorkflow struct
Zettat123 Jun 13, 2023
4b75014
add HookEventPullRequestTarget
Zettat123 Jun 14, 2023
34af97c
check pull_request_target in getSecretsOfTask
Zettat123 Jun 14, 2023
d04133b
replace Notify with NotifyPullRequest
Zettat123 Jun 14, 2023
a696d08
remove useless struct
Zettat123 Jun 14, 2023
e44e8b4
improve ifNeedApproval for pull_request_target
Zettat123 Jun 14, 2023
5ed6287
remove HookEventPullRequestTarget and rename GithubEvents
Zettat123 Jun 15, 2023
16af993
Merge branch 'main' into pull_request_target
Zettat123 Jun 16, 2023
b6ba2d7
refactor DetectWorkflows
Zettat123 Jun 16, 2023
35de43e
remove commented code
Zettat123 Jun 19, 2023
6e54ac7
Merge branch 'main' into pull_request_target
Zettat123 Jun 19, 2023
dbf8b71
add TriggerEvent
Zettat123 Jun 19, 2023
82bc8e9
Merge branch 'main' into pull_request_target
Zettat123 Jun 19, 2023
505a076
fix DetectWorkflows
Zettat123 Jun 19, 2023
d1e50c4
Merge branch 'main' into pull_request_target
Zettat123 Jun 20, 2023
db642aa
fix review
Zettat123 Jun 20, 2023
bf96468
improve DetectWorkflows
Zettat123 Jun 20, 2023
a557f64
Merge branch 'main' into pull_request_target
Zettat123 Jun 21, 2023
0f43f56
Merge branch 'main' into pull_request_target
Zettat123 Jun 25, 2023
96c5acb
add TestPullRequestTargetEvent
Zettat123 Jun 25, 2023
bb15a67
put TestPullRequestTargetEvent in unit tests
Zettat123 Jun 25, 2023
bf89124
add main
Zettat123 Jun 25, 2023
083a25b
fix path
Zettat123 Jun 25, 2023
396ed46
fix test
Zettat123 Jun 25, 2023
a6e0232
Merge branch 'main' into pull_request_target
Zettat123 Jun 25, 2023
c272a77
move TestPullRequestTargetEvent to integration tests
Zettat123 Jun 26, 2023
fbc753f
Merge branch 'main' into pull_request_target
GiteaBot Jun 26, 2023
7ba405d
Merge branch 'main' into pull_request_target
GiteaBot Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions tests/integration/actions_trigger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
"net/url"
"testing"
"time"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
actions_module "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/git"
repo_module "code.gitea.io/gitea/modules/repository"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/stretchr/testify/assert"
)

func TestPullRequestTargetEvent(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the base repo
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the forked repo

// create the base repo
baseRepo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_module.CreateRepoOptions{
Name: "repo-pull-request-target",
Description: "test pull-request-target event",
AutoInit: true,
Gitignores: "Go",
License: "MIT",
Readme: "Default",
DefaultBranch: "main",
IsPrivate: false,
})
assert.NoError(t, err)
assert.NotEmpty(t, baseRepo)

// enable actions
err = repo_model.UpdateRepositoryUnits(baseRepo, []repo_model.RepoUnit{{
RepoID: baseRepo.ID,
Type: unit_model.TypeActions,
}}, nil)
assert.NoError(t, err)

// create the forked repo
forkedRepo, err := repo_service.ForkRepository(git.DefaultContext, user2, user3, repo_service.ForkRepoOptions{
BaseRepo: baseRepo,
Name: "forked-repo-pull-request-target",
Description: "test pull-request-target event",
})
assert.NoError(t, err)
assert.NotEmpty(t, forkedRepo)

// add workflow file to the base repo
addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, baseRepo, user2, &files_service.ChangeRepoFilesOptions{
Files: []*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: ".gitea/workflows/pr.yml",
Content: "name: test\non: pull_request_target\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n",
},
},
Message: "add workflow",
OldBranch: "main",
NewBranch: "main",
Author: &files_service.IdentityOptions{
Name: user2.Name,
Email: user2.Email,
},
Committer: &files_service.IdentityOptions{
Name: user2.Name,
Email: user2.Email,
},
Dates: &files_service.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
})
assert.NoError(t, err)
assert.NotEmpty(t, addWorkflowToBaseResp)

// add a new file to the forked repo
addFileToForkedResp, err := files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, user3, &files_service.ChangeRepoFilesOptions{
Files: []*files_service.ChangeRepoFile{
{
Operation: "create",
TreePath: "file_1.txt",
Content: "file1",
},
},
Message: "add file1",
OldBranch: "main",
NewBranch: "fork-branch-1",
Author: &files_service.IdentityOptions{
Name: user3.Name,
Email: user3.Email,
},
Committer: &files_service.IdentityOptions{
Name: user3.Name,
Email: user3.Email,
},
Dates: &files_service.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
})
assert.NoError(t, err)
assert.NotEmpty(t, addFileToForkedResp)

// create Pull
pullIssue := &issues_model.Issue{
RepoID: baseRepo.ID,
Title: "Test pull-request-target-event",
PosterID: user3.ID,
Poster: user3,
IsPull: true,
}
pullRequest := &issues_model.PullRequest{
HeadRepoID: forkedRepo.ID,
BaseRepoID: baseRepo.ID,
HeadBranch: "fork-branch-1",
BaseBranch: "main",
HeadRepo: forkedRepo,
BaseRepo: baseRepo,
Type: issues_model.PullRequestGitea,
}
err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
assert.NoError(t, err)

// load and compare ActionRun
actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID})
assert.Equal(t, addWorkflowToBaseResp.Commit.SHA, actionRun.CommitSHA)
assert.Equal(t, actions_module.GithubEventPullRequestTarget, actionRun.TriggerEvent)
})
}