fix(actions): run pull_request workflows on the merge commit (#40076)

`pull_request` workflow runs checked out the PR head, so a PR could pass
CI and still break the base branch once merged.

Gitea now maintains `refs/pull/<index>/merge`, a test merge of the head
into the base branch, for open pull requests. It is updated on creation
and mergeability checks and removed on close or merge.

- `pull_request` runs read their workflows from the test merge and
expose it as `github.ref`, `github.sha` and
`github.event.pull_request.merge_commit_sha`. Commit statuses, the API's
`head_sha`, `workflow_run` payloads and `paths` filters stay on the
head.
- Runs fall back to the head when the merge conflicts, the pull request
is closed or git is older than 2.40.
- Hidden refs keep each run's test merge (`refs/gitea/run/<id>`) and
merged commits (`refs/gitea/pull/<index>/merged`) from `git gc`, for
reruns and after the base branch is deleted or force-pushed.

Fixes: https://github.com/go-gitea/gitea/issues/27039
Fixes: https://github.com/go-gitea/gitea/issues/13134
Fixes: https://github.com/go-gitea/gitea/issues/30972
Docs: https://gitea.com/gitea/docs/pulls/617

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
bircniandsilverwind authored and GitHub committed 2026-11-09 12:42:40 +00:00
1 parent 0ab0bfc375
commit e0b2f3425e
18 files changed
+240 -25

No files matched your search

+4
View File
@@ -131,6 +131,10 @@ func syncGitConfig(ctx context.Context) (err error) {
}
}
if err := configAddNonExist(ctx, "transfer.hideRefs", hiddenRefPrefix); err != nil { // after the user's options, which replace all values
return err
}
GlobalConfig = &GlobalConfigStruct{}
// HINT: GIT-DIFF-TREE-UI-CONFIG: Git's bug: git-diff-tree loads config with /* no "diff" UI options */ (since 20 years ago).
// https://github.com/git/git/blame/5d2e7709234afea1b6ddb25cd4f60d3d5fb3c200/builtin/diff-tree.c#L127
+5 -3
View File
@@ -18,9 +18,11 @@ const MaxConflictedDetectFiles = 10
// It returns the resulting tree hash, a list of conflicted files (if any), and an error if the operation fails.
// If there are no conflicts, the list of conflicted files will be nil.
func MergeTree(ctx context.Context, repo RepositoryFacade, baseRef, headRef, mergeBase string) (treeID string, isErrHasConflicts bool, conflictFiles []string, _ error) {
cmd := gitcmd.NewCommand("merge-tree", "--write-tree", "-z", "--name-only", "--no-messages").
AddOptionFormat("--merge-base=%s", mergeBase).
AddDynamicArguments(baseRef, headRef)
cmd := gitcmd.NewCommand("merge-tree", "--write-tree", "-z", "--name-only", "--no-messages")
if mergeBase != "" {
cmd.AddOptionFormat("--merge-base=%s", mergeBase)
}
cmd.AddDynamicArguments(baseRef, headRef)
stdout, stdoutClose := cmd.MakeStdoutPipe()
defer stdoutClose()
+17
View File
@@ -19,6 +19,8 @@ const (
// PullPrefix is the base directory of the pull information of git.
PullPrefix = "refs/pull/"
pullSuffix = "/head"
hiddenRefPrefix = "refs/gitea"
)
// IsValidRefPattern ensures that the provided string could be a valid reference
@@ -84,6 +86,21 @@ func RefNameFromPullIndex(prIndex int64) RefName {
return RefName(PullPrefix + strconv.FormatInt(prIndex, 10) + pullSuffix)
}
// MergeRefNameFromPullIndex is the test merge of an open pull request
func MergeRefNameFromPullIndex(prIndex int64) RefName {
return RefName(PullPrefix + strconv.FormatInt(prIndex, 10) + "/merge")
}
// MergedRefNameFromPullIndex keeps the merged commit after the base branch is deleted or rewritten
func MergedRefNameFromPullIndex(prIndex int64) RefName {
return RefName(hiddenRefPrefix + "/pull/" + strconv.FormatInt(prIndex, 10) + "/merged")
}
// RunRefNameFromRunID keeps a run's test merge after refs/pull/<index>/merge moved on
func RunRefNameFromRunID(runID int64) RefName {
return RefName(hiddenRefPrefix + "/run/" + strconv.FormatInt(runID, 10))
}
func (ref RefName) String() string {
return string(ref)
}
+1 -1
View File
@@ -141,7 +141,7 @@ func (repo *Repository) GetRefsFiltered(ctx context.Context, pattern string) ([]
refName = refName[:len(refName)-1]
// refName cannot be HEAD but can be remotes or stash
if strings.HasPrefix(refName, RemotePrefix) || refName == "/refs/stash" {
if strings.HasPrefix(refName, RemotePrefix) || strings.HasPrefix(refName, hiddenRefPrefix+"/") || refName == "/refs/stash" {
continue
}