Fix compatible for webhook ref type (#25195)

Fix #25185 
Caused by #24634
This commit is contained in:
Lunny Xiao 2023-06-13 14:05:28 +08:00 committed by GitHub
parent 2126f712bf
commit 419804fd4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View file

@ -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)

View file

@ -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/<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.
"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.

View file

@ -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,

View file

@ -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)
}
}