Skip to content

Commit bdf477f

Browse files
lunnyalgernon
authored andcommitted
Fix actions notify bug (#31866)
Try to fix go-gitea/gitea#31757 (comment) (cherry picked from commit 4f5c96627b4622d64593db2d436b1f3befa5f3c3)
1 parent d34d8ec commit bdf477f

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

services/actions/notifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (n *actionsNotifier) ForkRepository(ctx context.Context, doer *user_model.U
396396
// Add to hook queue for created repo after session commit.
397397
if u.IsOrganization() {
398398
newNotifyInput(repo, doer, webhook_module.HookEventRepository).
399-
WithRef(oldRepo.DefaultBranch).
399+
WithRef(git.RefNameFromBranch(oldRepo.DefaultBranch).String()).
400400
WithPayload(&api.RepositoryPayload{
401401
Action: api.HookRepoCreated,
402402
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),

services/actions/notifier_helper.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type notifyInput struct {
6969
Event webhook_module.HookEventType
7070

7171
// optional
72-
Ref string
72+
Ref git.RefName
7373
Payload api.Payloader
7474
PullRequest *issues_model.PullRequest
7575
}
@@ -93,7 +93,7 @@ func (input *notifyInput) WithDoer(doer *user_model.User) *notifyInput {
9393
}
9494

9595
func (input *notifyInput) WithRef(ref string) *notifyInput {
96-
input.Ref = ref
96+
input.Ref = git.RefName(ref)
9797
return input
9898
}
9999

@@ -105,7 +105,7 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
105105
func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
106106
input.PullRequest = pr
107107
if input.Ref == "" {
108-
input.Ref = pr.GetGitRefName()
108+
input.Ref = git.RefName(pr.GetGitRefName())
109109
}
110110
return input
111111
}
@@ -148,20 +148,25 @@ func notify(ctx context.Context, input *notifyInput) error {
148148
defer gitRepo.Close()
149149

150150
ref := input.Ref
151-
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
151+
if ref.BranchName() != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
152152
if ref != "" {
153153
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
154154
input.Event, ref)
155155
}
156-
ref = input.Repo.DefaultBranch
156+
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
157157
}
158158
if ref == "" {
159159
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
160-
ref = input.Repo.DefaultBranch
160+
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
161+
}
162+
163+
commitID, err := gitRepo.GetRefCommitID(ref.String())
164+
if err != nil {
165+
return fmt.Errorf("gitRepo.GetRefCommitID: %w", err)
161166
}
162167

163168
// Get the commit object for the ref
164-
commit, err := gitRepo.GetCommit(ref)
169+
commit, err := gitRepo.GetCommit(commitID)
165170
if err != nil {
166171
return fmt.Errorf("gitRepo.GetCommit: %w", err)
167172
}
@@ -177,7 +182,7 @@ func notify(ctx context.Context, input *notifyInput) error {
177182

178183
var detectedWorkflows []*actions_module.DetectedWorkflow
179184
actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
180-
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch
185+
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && input.Ref.BranchName() == input.Repo.DefaultBranch
181186
workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit,
182187
input.Event,
183188
input.Payload,
@@ -235,12 +240,12 @@ func notify(ctx context.Context, input *notifyInput) error {
235240
}
236241

237242
if shouldDetectSchedules {
238-
if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil {
243+
if err := handleSchedules(ctx, schedules, commit, input, ref.String()); err != nil {
239244
return err
240245
}
241246
}
242247

243-
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
248+
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref.String())
244249
}
245250

246251
func SkipPullRequestEvent(ctx context.Context, event webhook_module.HookEventType, repoID int64, commitSHA string) bool {

tests/integration/actions_trigger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
375375
Title: "add workflow",
376376
RepoID: repo.ID,
377377
Event: "delete",
378-
Ref: "main",
378+
Ref: "refs/heads/main",
379379
WorkflowID: "createdelete.yml",
380380
CommitSHA: branch.CommitID,
381381
})
@@ -390,7 +390,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
390390
Title: "add workflow",
391391
RepoID: repo.ID,
392392
Event: "delete",
393-
Ref: "main",
393+
Ref: "refs/heads/main",
394394
WorkflowID: "createdelete.yml",
395395
CommitSHA: branch.CommitID,
396396
})

0 commit comments

Comments
 (0)