mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-01 06:14:06 +00:00
Fast-forward-only creates no Gitea commit, so skip the "can Gitea sign" precheck for it. Pre-check head-commit verification for styles that preserve user commits on the target (merge, fast-forward-only) so a PR with unsigned commits surfaces a localized error instead of a 500 at the pre-receive hook. The dropdown still shows every configured style; the avatar and signing warning toggle per selection via data-pull-merge-style. Fixes #12272 **Note**: Admin force-merge does not bypass the new head-commits check. This matches the existing `isSignedIfRequired` behavior. Signed-off-by: Nikita Vakula <programmistov.programmist@gmail.com> Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
149 lines
5.1 KiB
Go
149 lines
5.1 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
git_model "code.gitea.io/gitea/models/git"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
"code.gitea.io/gitea/models/pull"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
"code.gitea.io/gitea/modules/queue"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/test"
|
|
"code.gitea.io/gitea/services/automergequeue"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPullRequest_AddToTaskQueue(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
idChan := make(chan int64, 10)
|
|
testHandler := func(items ...string) []string {
|
|
for _, s := range items {
|
|
id, _ := strconv.ParseInt(s, 10, 64)
|
|
idChan <- id
|
|
}
|
|
return nil
|
|
}
|
|
|
|
cfg, err := setting.GetQueueSettings(setting.CfgProvider, "pr_patch_checker")
|
|
assert.NoError(t, err)
|
|
prPatchCheckerQueue, err = queue.NewWorkerPoolQueueWithContext(t.Context(), "pr_patch_checker", cfg, testHandler, true)
|
|
assert.NoError(t, err)
|
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
StartPullRequestCheckImmediately(t.Context(), pr)
|
|
|
|
assert.Eventually(t, func() bool {
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
return pr.Status == issues_model.PullRequestStatusChecking
|
|
}, 1*time.Second, 100*time.Millisecond)
|
|
|
|
has, err := prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
|
assert.True(t, has)
|
|
assert.NoError(t, err)
|
|
|
|
go prPatchCheckerQueue.Run()
|
|
|
|
select {
|
|
case id := <-idChan:
|
|
assert.Equal(t, pr.ID, id)
|
|
case <-time.After(time.Second):
|
|
assert.FailNow(t, "Timeout: nothing was added to pullRequestQueue")
|
|
}
|
|
|
|
has, err = prPatchCheckerQueue.Has(strconv.FormatInt(pr.ID, 10))
|
|
assert.False(t, has)
|
|
assert.NoError(t, err)
|
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
assert.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
|
|
|
|
prPatchCheckerQueue.ShutdownWait(time.Second)
|
|
prPatchCheckerQueue = nil
|
|
}
|
|
|
|
func TestCheckSigningRequirementsHeadCommits(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
ctx := t.Context()
|
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
require.NoError(t, pr.LoadBaseRepo(ctx))
|
|
require.NoError(t, pr.LoadHeadRepo(ctx))
|
|
|
|
check := func() error {
|
|
return checkSigningRequirements(ctx, pr, nil, repo_model.MergeStyleFastForwardOnly)
|
|
}
|
|
|
|
// No protected branch rule on the base branch: the check must pass.
|
|
require.NoError(t, check())
|
|
|
|
// Protected branch without RequireSignedCommits: the check must still pass.
|
|
require.NoError(t, git_model.UpdateProtectBranch(ctx, pr.BaseRepo, &git_model.ProtectedBranch{
|
|
RepoID: pr.BaseRepoID,
|
|
RuleName: pr.BaseBranch,
|
|
RequireSignedCommits: false,
|
|
}, git_model.WhitelistOptions{}))
|
|
require.NoError(t, check())
|
|
|
|
// With RequireSignedCommits enabled: the test fixture commits have no signatures,
|
|
// so the check must report ErrHeadCommitsNotAllVerified.
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, pb)
|
|
pb.RequireSignedCommits = true
|
|
require.NoError(t, git_model.UpdateProtectBranch(ctx, pr.BaseRepo, pb, git_model.WhitelistOptions{}))
|
|
require.ErrorIs(t, check(), ErrHeadCommitsNotAllVerified)
|
|
}
|
|
|
|
func TestMarkPullRequestAsMergeable(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
prPatchCheckerQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_patch_checker", func(items ...string) []string { return nil })
|
|
go prPatchCheckerQueue.Run()
|
|
defer func() {
|
|
prPatchCheckerQueue.ShutdownWait(time.Second)
|
|
prPatchCheckerQueue = nil
|
|
}()
|
|
|
|
addToQueueShaChan := make(chan string, 1)
|
|
defer test.MockVariableValue(&automergequeue.AddToQueue, func(pr *issues_model.PullRequest, sha string) {
|
|
addToQueueShaChan <- sha
|
|
})()
|
|
ctx := t.Context()
|
|
_, _ = db.GetEngine(ctx).ID(2).Update(&issues_model.PullRequest{Status: issues_model.PullRequestStatusChecking})
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
require.False(t, pr.HasMerged)
|
|
require.Equal(t, issues_model.PullRequestStatusChecking, pr.Status)
|
|
|
|
err := pull.ScheduleAutoMerge(ctx, &user_model.User{ID: 99999}, pr.ID, repo_model.MergeStyleMerge, "test msg", true)
|
|
require.NoError(t, err)
|
|
|
|
exist, scheduleMerge, err := pull.GetScheduledMergeByPullID(ctx, pr.ID)
|
|
require.NoError(t, err)
|
|
assert.True(t, exist)
|
|
assert.True(t, scheduleMerge.Doer.IsGhost())
|
|
|
|
markPullRequestAsMergeable(ctx, pr)
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
require.Equal(t, issues_model.PullRequestStatusMergeable, pr.Status)
|
|
|
|
select {
|
|
case sha := <-addToQueueShaChan:
|
|
assert.Equal(t, "985f0301dba5e7b34be866819cd15ad3d8f508ee", sha) // ref: refs/pull/3/head
|
|
case <-time.After(1 * time.Second):
|
|
assert.FailNow(t, "Timeout: nothing was added to automergequeue")
|
|
}
|
|
}
|