Skip to content

Commit 506c708

Browse files
GiteaBotlunny
andauthored
Fix compatible for webhook ref type (#25195) (#25223)
Backport #25195 by @lunny Fix #25185 Caused by #24634 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent f64f549 commit 506c708

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

modules/git/ref.go

+14
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func (ref RefName) ShortName() string {
163163
}
164164

165165
// RefGroup returns the group type of the reference
166+
// Using the name of the directory under .git/refs
166167
func (ref RefName) RefGroup() string {
167168
if ref.IsBranch() {
168169
return "heads"
@@ -182,6 +183,19 @@ func (ref RefName) RefGroup() string {
182183
return ""
183184
}
184185

186+
// RefType returns the simple ref type of the reference, e.g. branch, tag
187+
// It's differrent from RefGroup, which is using the name of the directory under .git/refs
188+
// Here we using branch but not heads, using tag but not tags
189+
func (ref RefName) RefType() string {
190+
var refType string
191+
if ref.IsBranch() {
192+
refType = "branch"
193+
} else if ref.IsTag() {
194+
refType = "tag"
195+
}
196+
return refType
197+
}
198+
185199
// RefURL returns the absolute URL for a ref in a repository
186200
func RefURL(repoURL, ref string) string {
187201
refFullName := RefName(ref)

routers/api/actions/runner/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
9898
baseRef = pullPayload.PullRequest.Base.Ref
9999
headRef = pullPayload.PullRequest.Head.Ref
100100
}
101+
101102
refName := git.RefName(t.Job.Run.Ref)
102-
refType := refName.RefGroup()
103103

104104
taskContext, err := structpb.NewStruct(map[string]interface{}{
105105
// 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 {
121121
"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/<branch_name>, for pull requests it is refs/pull/<pr_number>/merge, and for tags it is refs/tags/<tag_name>. For example, refs/heads/feature-branch-1.
122122
"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.
123123
"ref_protected": false, // boolean, true if branch protections are configured for the ref that triggered the workflow run.
124-
"ref_type": refType, // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
124+
"ref_type": refName.RefType(), // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
125125
"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."
126126
"repository": t.Job.Run.Repo.OwnerName + "/" + t.Job.Run.Repo.Name, // string, The owner and repository name. For example, Codertocat/Hello-World.
127127
"repository_owner": t.Job.Run.Repo.OwnerName, // string, The repository owner's name. For example, Codertocat.

services/actions/notifier.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode
384384
WithPayload(&api.CreatePayload{
385385
Ref: refFullName.ShortName(),
386386
Sha: refID,
387-
RefType: refFullName.RefGroup(),
387+
RefType: refFullName.RefType(),
388388
Repo: apiRepo,
389389
Sender: apiPusher,
390390
}).
@@ -401,7 +401,7 @@ func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode
401401
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
402402
WithPayload(&api.DeletePayload{
403403
Ref: refFullName.ShortName(),
404-
RefType: refFullName.RefGroup(),
404+
RefType: refFullName.RefType(),
405405
PusherType: api.PusherTypeUser,
406406
Repo: apiRepo,
407407
Sender: apiPusher,

services/webhook/notifier.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_mode
755755
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{
756756
Ref: refName, // FIXME: should it be a full ref name?
757757
Sha: refID,
758-
RefType: refFullName.RefGroup(),
758+
RefType: refFullName.RefType(),
759759
Repo: apiRepo,
760760
Sender: apiPusher,
761761
}); err != nil {
@@ -791,12 +791,12 @@ func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_mode
791791

792792
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{
793793
Ref: refName, // FIXME: should it be a full ref name?
794-
RefType: refFullName.RefGroup(),
794+
RefType: refFullName.RefType(),
795795
PusherType: api.PusherTypeUser,
796796
Repo: apiRepo,
797797
Sender: apiPusher,
798798
}); err != nil {
799-
log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefGroup(), err)
799+
log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefType(), err)
800800
}
801801
}
802802

0 commit comments

Comments
 (0)