From 506c70884a122f92fc722f297f3d775f5d36ba4f Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 13 Jun 2023 02:51:50 -0400 Subject: [PATCH] Fix compatible for webhook ref type (#25195) (#25223) Backport #25195 by @lunny Fix #25185 Caused by #24634 Co-authored-by: Lunny Xiao --- modules/git/ref.go | 14 ++++++++++++++ routers/api/actions/runner/utils.go | 4 ++-- services/actions/notifier.go | 4 ++-- services/webhook/notifier.go | 6 +++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/git/ref.go b/modules/git/ref.go index 73095f8dbee3..ad251515e7db 100644 --- a/modules/git/ref.go +++ b/modules/git/ref.go @@ -163,6 +163,7 @@ func (ref RefName) ShortName() string { } // RefGroup returns the group type of the reference +// Using the name of the directory under .git/refs func (ref RefName) RefGroup() string { if ref.IsBranch() { return "heads" @@ -182,6 +183,19 @@ func (ref RefName) RefGroup() string { return "" } +// RefType returns the simple ref type of the reference, e.g. branch, tag +// It's differrent from RefGroup, which is using the name of the directory under .git/refs +// Here we using branch but not heads, using tag but not tags +func (ref RefName) RefType() string { + var refType string + if ref.IsBranch() { + refType = "branch" + } else if ref.IsTag() { + refType = "tag" + } + return refType +} + // RefURL returns the absolute URL for a ref in a repository func RefURL(repoURL, ref string) string { refFullName := RefName(ref) diff --git a/routers/api/actions/runner/utils.go b/routers/api/actions/runner/utils.go index e90213964fd7..e1d54134c613 100644 --- a/routers/api/actions/runner/utils.go +++ b/routers/api/actions/runner/utils.go @@ -98,8 +98,8 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct { baseRef = pullPayload.PullRequest.Base.Ref headRef = pullPayload.PullRequest.Head.Ref } + refName := git.RefName(t.Job.Run.Ref) - refType := refName.RefGroup() taskContext, err := structpb.NewStruct(map[string]interface{}{ // standard contexts, see https://docs.github.com/en/actions/learn-github-actions/contexts#github-context @@ -121,7 +121,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct { "ref": t.Job.Run.Ref, // string, The fully-formed ref of the branch or tag that triggered the workflow run. For workflows triggered by push, this is the branch or tag ref that was pushed. For workflows triggered by pull_request, this is the pull request merge branch. For workflows triggered by release, this is the release tag created. For other triggers, this is the branch or tag ref that triggered the workflow run. This is only set if a branch or tag is available for the event type. The ref given is fully-formed, meaning that for branches the format is refs/heads/, for pull requests it is refs/pull//merge, and for tags it is refs/tags/. For example, refs/heads/feature-branch-1. "ref_name": refName.String(), // string, The short ref name of the branch or tag that triggered the workflow run. This value matches the branch or tag name shown on GitHub. For example, feature-branch-1. "ref_protected": false, // boolean, true if branch protections are configured for the ref that triggered the workflow run. - "ref_type": refType, // string, The type of ref that triggered the workflow run. Valid values are branch or tag. + "ref_type": refName.RefType(), // string, The type of ref that triggered the workflow run. Valid values are branch or tag. "path": "", // string, Path on the runner to the file that sets system PATH variables from workflow commands. This file is unique to the current step and is a different file for each step in a job. For more information, see "Workflow commands for GitHub Actions." "repository": t.Job.Run.Repo.OwnerName + "/" + t.Job.Run.Repo.Name, // string, The owner and repository name. For example, Codertocat/Hello-World. "repository_owner": t.Job.Run.Repo.OwnerName, // string, The repository owner's name. For example, Codertocat. diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 536b430b4189..da870bb84c8f 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -384,7 +384,7 @@ func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode WithPayload(&api.CreatePayload{ Ref: refFullName.ShortName(), Sha: refID, - RefType: refFullName.RefGroup(), + RefType: refFullName.RefType(), Repo: apiRepo, Sender: apiPusher, }). @@ -401,7 +401,7 @@ func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name WithPayload(&api.DeletePayload{ Ref: refFullName.ShortName(), - RefType: refFullName.RefGroup(), + RefType: refFullName.RefType(), PusherType: api.PusherTypeUser, Repo: apiRepo, Sender: apiPusher, diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index e6d1875b2996..bccd477852b7 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -755,7 +755,7 @@ func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{ Ref: refName, // FIXME: should it be a full ref name? Sha: refID, - RefType: refFullName.RefGroup(), + RefType: refFullName.RefType(), Repo: apiRepo, Sender: apiPusher, }); err != nil { @@ -791,12 +791,12 @@ func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{ Ref: refName, // FIXME: should it be a full ref name? - RefType: refFullName.RefGroup(), + RefType: refFullName.RefType(), PusherType: api.PusherTypeUser, Repo: apiRepo, Sender: apiPusher, }); err != nil { - log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefGroup(), err) + log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefType(), err) } }