1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-22 16:44:26 +00:00

Fix actions notify bug (#31866)

Try to fix
https://github.com/go-gitea/gitea/issues/31757#issuecomment-2295131062
This commit is contained in:
Lunny Xiao 2024-08-20 00:25:41 +08:00 committed by GitHub
parent 36232b69db
commit 4f5c96627b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 13 deletions

View File

@ -386,7 +386,7 @@ func (n *actionsNotifier) ForkRepository(ctx context.Context, doer *user_model.U
// Add to hook queue for created repo after session commit. // Add to hook queue for created repo after session commit.
if u.IsOrganization() { if u.IsOrganization() {
newNotifyInput(repo, doer, webhook_module.HookEventRepository). newNotifyInput(repo, doer, webhook_module.HookEventRepository).
WithRef(oldRepo.DefaultBranch). WithRef(git.RefNameFromBranch(oldRepo.DefaultBranch).String()).
WithPayload(&api.RepositoryPayload{ WithPayload(&api.RepositoryPayload{
Action: api.HookRepoCreated, Action: api.HookRepoCreated,
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}), Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeOwner}),

View File

@ -65,7 +65,7 @@ type notifyInput struct {
Event webhook_module.HookEventType Event webhook_module.HookEventType
// optional // optional
Ref string Ref git.RefName
Payload api.Payloader Payload api.Payloader
PullRequest *issues_model.PullRequest PullRequest *issues_model.PullRequest
} }
@ -89,7 +89,7 @@ func (input *notifyInput) WithDoer(doer *user_model.User) *notifyInput {
} }
func (input *notifyInput) WithRef(ref string) *notifyInput { func (input *notifyInput) WithRef(ref string) *notifyInput {
input.Ref = ref input.Ref = git.RefName(ref)
return input return input
} }
@ -101,7 +101,7 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput { func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
input.PullRequest = pr input.PullRequest = pr
if input.Ref == "" { if input.Ref == "" {
input.Ref = pr.GetGitRefName() input.Ref = git.RefName(pr.GetGitRefName())
} }
return input return input
} }
@ -144,20 +144,25 @@ func notify(ctx context.Context, input *notifyInput) error {
defer gitRepo.Close() defer gitRepo.Close()
ref := input.Ref ref := input.Ref
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) { if ref.BranchName() != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
if ref != "" { if ref != "" {
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch", log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
input.Event, ref) input.Event, ref)
} }
ref = input.Repo.DefaultBranch ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
} }
if ref == "" { if ref == "" {
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event) log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
ref = input.Repo.DefaultBranch ref = git.RefNameFromBranch(input.Repo.DefaultBranch)
}
commitID, err := gitRepo.GetRefCommitID(ref.String())
if err != nil {
return fmt.Errorf("gitRepo.GetRefCommitID: %w", err)
} }
// Get the commit object for the ref // Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref) commit, err := gitRepo.GetCommit(commitID)
if err != nil { if err != nil {
return fmt.Errorf("gitRepo.GetCommit: %w", err) return fmt.Errorf("gitRepo.GetCommit: %w", err)
} }
@ -168,7 +173,7 @@ func notify(ctx context.Context, input *notifyInput) error {
var detectedWorkflows []*actions_module.DetectedWorkflow var detectedWorkflows []*actions_module.DetectedWorkflow
actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig() actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
shouldDetectSchedules := input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch shouldDetectSchedules := input.Event == webhook_module.HookEventPush && input.Ref.BranchName() == input.Repo.DefaultBranch
workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit,
input.Event, input.Event,
input.Payload, input.Payload,
@ -220,12 +225,12 @@ func notify(ctx context.Context, input *notifyInput) error {
} }
if shouldDetectSchedules { if shouldDetectSchedules {
if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil { if err := handleSchedules(ctx, schedules, commit, input, ref.String()); err != nil {
return err return err
} }
} }
return handleWorkflows(ctx, detectedWorkflows, commit, input, ref) return handleWorkflows(ctx, detectedWorkflows, commit, input, ref.String())
} }
func skipWorkflows(input *notifyInput, commit *git.Commit) bool { func skipWorkflows(input *notifyInput, commit *git.Commit) bool {

View File

@ -427,7 +427,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
Title: "add workflow", Title: "add workflow",
RepoID: repo.ID, RepoID: repo.ID,
Event: "delete", Event: "delete",
Ref: "main", Ref: "refs/heads/main",
WorkflowID: "createdelete.yml", WorkflowID: "createdelete.yml",
CommitSHA: branch.CommitID, CommitSHA: branch.CommitID,
}) })
@ -442,7 +442,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
Title: "add workflow", Title: "add workflow",
RepoID: repo.ID, RepoID: repo.ID,
Event: "delete", Event: "delete",
Ref: "main", Ref: "refs/heads/main",
WorkflowID: "createdelete.yml", WorkflowID: "createdelete.yml",
CommitSHA: branch.CommitID, CommitSHA: branch.CommitID,
}) })