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
Show file tree
Hide file tree
Changes from 6 commits
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
1 change: 1 addition & 0 deletions modules/actions/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent
case githubEventPullRequest, githubEventPullRequestTarget:
switch triggedEvent {
case webhook_module.HookEventPullRequest,
webhook_module.HookEventPullRequestTarget,
webhook_module.HookEventPullRequestSync,
webhook_module.HookEventPullRequestAssign,
webhook_module.HookEventPullRequestLabel:
Expand Down
1 change: 1 addition & 0 deletions modules/actions/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType

case // pull_request
webhook_module.HookEventPullRequest,
webhook_module.HookEventPullRequestTarget,
webhook_module.HookEventPullRequestSync,
webhook_module.HookEventPullRequestAssign,
webhook_module.HookEventPullRequestLabel:
Expand Down
3 changes: 3 additions & 0 deletions modules/webhook/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment"
HookEventPullRequestSync HookEventType = "pull_request_sync"
HookEventPullRequestReviewRequest HookEventType = "pull_request_review_request"
HookEventPullRequestTarget HookEventType = "pull_request_target"
HookEventWiki HookEventType = "wiki"
HookEventRepository HookEventType = "repository"
HookEventRelease HookEventType = "release"
Expand Down Expand Up @@ -57,6 +58,8 @@ func (h HookEventType) Event() string {
return "pull_request_rejected"
case HookEventPullRequestReviewComment:
return "pull_request_comment"
case HookEventPullRequestTarget:
return "pull_request_target"
case HookEventWiki:
return "wiki"
case HookEventRepository:
Expand Down
3 changes: 2 additions & 1 deletion routers/api/actions/runner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/log"
secret_module "code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/actions"

runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
Expand Down Expand Up @@ -53,7 +54,7 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv

func getSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string {
secrets := map[string]string{}
if task.Job.Run.IsForkPullRequest {
if task.Job.Run.IsForkPullRequest && task.Job.Run.Event != webhook_module.HookEventPullRequestTarget {
// ignore secrets for fork pull request
return secrets
}
Expand Down
12 changes: 6 additions & 6 deletions services/actions/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (n *actionsNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use
WithDoer(doer).
WithPayload(apiPullRequest).
WithPullRequest(issue.PullRequest).
Notify(ctx)
NotifyPullRequest(ctx)
return
}
apiIssue := &api.IssuePayload{
Expand Down Expand Up @@ -138,7 +138,7 @@ func (n *actionsNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use
Sender: convert.ToUser(ctx, doer, nil),
}).
WithPullRequest(issue.PullRequest).
Notify(ctx)
NotifyPullRequest(ctx)
return
}
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel).
Expand Down Expand Up @@ -220,7 +220,7 @@ func (n *actionsNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues
Sender: convert.ToUser(ctx, pull.Issue.Poster, nil),
}).
WithPullRequest(pull).
Notify(ctx)
NotifyPullRequest(ctx)
}

func (n *actionsNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (*actionsNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m
WithRef(pr.MergedCommitID).
WithPayload(apiPullRequest).
WithPullRequest(pr).
Notify(ctx)
NotifyPullRequest(ctx)
}

func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
Expand Down Expand Up @@ -498,7 +498,7 @@ func (n *actionsNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe
Sender: convert.ToUser(ctx, doer, nil),
}).
WithPullRequest(pr).
Notify(ctx)
NotifyPullRequest(ctx)
}

func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
Expand Down Expand Up @@ -529,7 +529,7 @@ func (n *actionsNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex
Sender: convert.ToUser(ctx, doer, nil),
}).
WithPullRequest(pr).
Notify(ctx)
NotifyPullRequest(ctx)
}

func (n *actionsNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
Expand Down
16 changes: 14 additions & 2 deletions services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyI
return input
}

func (input *notifyInput) NotifyPullRequest(ctx context.Context) {
// notify with the original `pull_request` related events
input.Notify(ctx)

// notify with the `pull_request_target` event
input.Event = webhook_module.HookEventPullRequestTarget
input.Ref = git.BranchPrefix + input.PullRequest.BaseBranch
input.Notify(ctx)
}

func (input *notifyInput) Notify(ctx context.Context) {
log.Trace("execute %v for event %v whose doer is %v", getMethod(ctx), input.Event, input.Doer.Name)

Expand Down Expand Up @@ -259,8 +269,10 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo
}

func ifNeedApproval(ctx context.Context, run *actions_model.ActionRun, repo *repo_model.Repository, user *user_model.User) (bool, error) {
// don't need approval if it's not a fork PR
if !run.IsForkPullRequest {
// 1. don't need approval if it's not a fork PR
// 2. don't need approval if the event is `pull_request_target` since the workflow will run in the context of base branch
// see https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks#about-workflow-runs-from-public-forks
if !run.IsForkPullRequest || run.Event == webhook_module.HookEventPullRequestTarget {
return false, nil
}

Expand Down