Skip to content

Commit 4f5c966

Browse files
authored
Fix actions notify bug (#31866)
Try to fix #31757 (comment)
1 parent 36232b6 commit 4f5c966

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
@@ -386,7 +386,7 @@ func (n *actionsNotifier) ForkRepository(ctx context.Context, doer *user_model.U
386386
// Add to hook queue for created repo after session commit.
387387
if u.IsOrganization() {
388388
newNotifyInput(repo, doer, webhook_module.HookEventRepository).
389-
WithRef(oldRepo.DefaultBranch).
389+
WithRef(git.RefNameFromBranch(oldRepo.DefaultBranch).String()).
390390
WithPayload(&api.RepositoryPayload{
391391
Action: api.HookRepoCreated,
392392
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
@@ -65,7 +65,7 @@ type notifyInput struct {
6565
Event webhook_module.HookEventType
6666

6767
// optional
68-
Ref string
68+
Ref git.RefName
6969
Payload api.Payloader
7070
PullRequest *issues_model.PullRequest
7171
}
@@ -89,7 +89,7 @@ func (input *notifyInput) WithDoer(doer *user_model.User) *notifyInput {
8989
}
9090

9191
func (input *notifyInput) WithRef(ref string) *notifyInput {
92-
input.Ref = ref
92+
input.Ref = git.RefName(ref)
9393
return input
9494
}
9595

@@ -101,7 +101,7 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
101101
func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
102102
input.PullRequest = pr
103103
if input.Ref == "" {
104-
input.Ref = pr.GetGitRefName()
104+
input.Ref = git.RefName(pr.GetGitRefName())
105105
}
106106
return input
107107
}
@@ -144,20 +144,25 @@ func notify(ctx context.Context, input *notifyInput) error {
144144
defer gitRepo.Close()
145145

146146
ref := input.Ref
147-
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
147+
if ref.BranchName() != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
148148
if ref != "" {
149149
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
150150
input.Event, ref)
151151
}
152-
ref = input.Repo.DefaultBranch
152+
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
153153
}
154154
if ref == "" {
155155
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
156-
ref = input.Repo.DefaultBranch
156+
ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
157+
}
158+
159+
commitID, err := gitRepo.GetRefCommitID(ref.String())
160+
if err != nil {
161+
return fmt.Errorf("gitRepo.GetRefCommitID: %w", err)
157162
}
158163

159164
// Get the commit object for the ref
160-
commit, err := gitRepo.GetCommit(ref)
165+
commit, err := gitRepo.GetCommit(commitID)
161166
if err != nil {
162167
return fmt.Errorf("gitRepo.GetCommit: %w", err)
163168
}
@@ -168,7 +173,7 @@ func notify(ctx context.Context, input *notifyInput) error {
168173

169174
var detectedWorkflows []*actions_module.DetectedWorkflow
170175
actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
171-
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch
176+
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && input.Ref.BranchName() == input.Repo.DefaultBranch
172177
workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit,
173178
input.Event,
174179
input.Payload,
@@ -220,12 +225,12 @@ func notify(ctx context.Context, input *notifyInput) error {
220225
}
221226

222227
if shouldDetectSchedules {
223-
if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil {
228+
if err := handleSchedules(ctx, schedules, commit, input, ref.String()); err != nil {
224229
return err
225230
}
226231
}
227232

228-
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
233+
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref.String())
229234
}
230235

231236
func skipWorkflows(input *notifyInput, commit *git.Commit) bool {

tests/integration/actions_trigger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
427427
Title: "add workflow",
428428
RepoID: repo.ID,
429429
Event: "delete",
430-
Ref: "main",
430+
Ref: "refs/heads/main",
431431
WorkflowID: "createdelete.yml",
432432
CommitSHA: branch.CommitID,
433433
})
@@ -442,7 +442,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
442442
Title: "add workflow",
443443
RepoID: repo.ID,
444444
Event: "delete",
445-
Ref: "main",
445+
Ref: "refs/heads/main",
446446
WorkflowID: "createdelete.yml",
447447
CommitSHA: branch.CommitID,
448448
})

0 commit comments

Comments
 (0)