Skip to content

Commit 950c93a

Browse files
authored
Actions: Use default branch as ref when a branch/tag delete occurs (#23910)
Currently using the tip of main (2c585d6) and when deleting a branch (and presumably tag, but not tested), no workflows with `on: [delete]` are being triggered. The runner isn't being notified about them. I see this in the gitea log: `2023/04/04 04:29:36 ...s/notifier_helper.go:102:Notify() [E] an error occurred while executing the NotifyDeleteRef actions method: gitRepo.GetCommit: object does not exist [id: test, rel_path: ]` Understandably the ref has already been deleted and so `GetCommit` fails. Currently at https://github.com/go-gitea/gitea/blob/main/services/actions/notifier_helper.go#L130, if the ref is an empty string it falls back to the default branch name. This PR also checks if it is a `HookEventDelete` and does the same. Currently `${{ github.ref }}` would be equivalent to the deleted branch (if `notify()` succeded), but this PR allows `notify()` to proceed and also aligns it with the GitHub Actions behavior at https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete: `$GITHUB_REF` / `${{ github.ref }}` => Default branch (main/master) `$GITHUB_SHA` / `${{ github.sha }}` => Last commit on default branch If the user needs the name of the deleted branch (or tag), it is available as `${{ github.event.ref }}`. There appears to be no way for the user to get the tip commit SHA of the deleted branch (GitHub does not do this either). N.B. there may be other conditions other than `HookEventDelete` where the default branch ref needs swapped in, but this was sufficient for my use case.
1 parent 6eb6783 commit 950c93a

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

services/actions/notifier_helper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func notify(ctx context.Context, input *notifyInput) error {
127127
defer gitRepo.Close()
128128

129129
ref := input.Ref
130+
if input.Event == webhook_module.HookEventDelete {
131+
// The event is deleting a reference, so it will fail to get the commit for a deleted reference.
132+
// Set ref to empty string to fall back to the default branch.
133+
ref = ""
134+
}
130135
if ref == "" {
131136
ref = input.Repo.DefaultBranch
132137
}

0 commit comments

Comments
 (0)