mirror of https://github.com/go-gitea/gitea.git
Fix automerge will not work because of some events haven't been triggered (#30780)
Replace #25741 Close #24445 Close #30658 Close #20646 ~Depends on #30805~ Since #25741 has been rewritten totally, to make the contribution easier, I will continue the work in this PR. Thanks @6543 --------- Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
1007ce764e
commit
c6cf96d31d
|
@ -155,14 +155,14 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) {
|
||||||
if r.CodeComments != nil {
|
if r.CodeComments != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = r.loadIssue(ctx); err != nil {
|
if err = r.LoadIssue(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false)
|
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Review) loadIssue(ctx context.Context) (err error) {
|
func (r *Review) LoadIssue(ctx context.Context) (err error) {
|
||||||
if r.Issue != nil {
|
if r.Issue != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -199,7 +199,7 @@ func (r *Review) LoadReviewerTeam(ctx context.Context) (err error) {
|
||||||
|
|
||||||
// LoadAttributes loads all attributes except CodeComments
|
// LoadAttributes loads all attributes except CodeComments
|
||||||
func (r *Review) LoadAttributes(ctx context.Context) (err error) {
|
func (r *Review) LoadAttributes(ctx context.Context) (err error) {
|
||||||
if err = r.loadIssue(ctx); err != nil {
|
if err = r.LoadIssue(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = r.LoadCodeComments(ctx); err != nil {
|
if err = r.LoadCodeComments(ctx); err != nil {
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/process"
|
"code.gitea.io/gitea/modules/process"
|
||||||
"code.gitea.io/gitea/modules/queue"
|
"code.gitea.io/gitea/modules/queue"
|
||||||
|
notify_service "code.gitea.io/gitea/services/notify"
|
||||||
pull_service "code.gitea.io/gitea/services/pull"
|
pull_service "code.gitea.io/gitea/services/pull"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,6 +31,8 @@ var prAutoMergeQueue *queue.WorkerPoolQueue[string]
|
||||||
|
|
||||||
// Init runs the task queue to that handles auto merges
|
// Init runs the task queue to that handles auto merges
|
||||||
func Init() error {
|
func Init() error {
|
||||||
|
notify_service.RegisterNotifier(NewNotifier())
|
||||||
|
|
||||||
prAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler)
|
prAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler)
|
||||||
if prAutoMergeQueue == nil {
|
if prAutoMergeQueue == nil {
|
||||||
return fmt.Errorf("unable to create pr_auto_merge queue")
|
return fmt.Errorf("unable to create pr_auto_merge queue")
|
||||||
|
@ -47,7 +50,7 @@ func handler(items ...string) []string {
|
||||||
log.Error("could not parse data from pr_auto_merge queue (%v): %v", s, err)
|
log.Error("could not parse data from pr_auto_merge queue (%v): %v", s, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
handlePull(id, sha)
|
handlePullRequestAutoMerge(id, sha)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -62,16 +65,6 @@ func addToQueue(pr *issues_model.PullRequest, sha string) {
|
||||||
// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
|
// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
|
||||||
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
|
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
|
||||||
err = db.WithTx(ctx, func(ctx context.Context) error {
|
err = db.WithTx(ctx, func(ctx context.Context) error {
|
||||||
lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// we don't need to schedule
|
|
||||||
if lastCommitStatus.IsSuccess() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message); err != nil {
|
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -95,8 +88,8 @@ func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded
|
// StartPRCheckAndAutoMergeBySHA start an automerge check and auto merge task for all pull requests of repository and SHA
|
||||||
func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model.Repository) error {
|
func StartPRCheckAndAutoMergeBySHA(ctx context.Context, sha string, repo *repo_model.Repository) error {
|
||||||
pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool {
|
pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool {
|
||||||
return !pr.HasMerged && pr.CanAutoMerge()
|
return !pr.HasMerged && pr.CanAutoMerge()
|
||||||
})
|
})
|
||||||
|
@ -111,6 +104,32 @@ func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartPRCheckAndAutoMerge start an automerge check and auto merge task for a pull request
|
||||||
|
func StartPRCheckAndAutoMerge(ctx context.Context, pull *issues_model.PullRequest) {
|
||||||
|
if pull == nil || pull.HasMerged || !pull.CanAutoMerge() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := pull.LoadBaseRepo(ctx); err != nil {
|
||||||
|
log.Error("LoadBaseRepo: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
gitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("OpenRepository: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer gitRepo.Close()
|
||||||
|
commitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("GetRefCommitID: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
addToQueue(pull, commitID)
|
||||||
|
}
|
||||||
|
|
||||||
func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) {
|
func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) {
|
||||||
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -161,7 +180,8 @@ func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.
|
||||||
return pulls, nil
|
return pulls, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePull(pullID int64, sha string) {
|
// handlePullRequestAutoMerge merge the pull request if all checks are successful
|
||||||
|
func handlePullRequestAutoMerge(pullID int64, sha string) {
|
||||||
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(),
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(),
|
||||||
fmt.Sprintf("Handle AutoMerge of PR[%d] with sha[%s]", pullID, sha))
|
fmt.Sprintf("Handle AutoMerge of PR[%d] with sha[%s]", pullID, sha))
|
||||||
defer finished()
|
defer finished()
|
||||||
|
@ -182,24 +202,50 @@ func handlePull(pullID int64, sha string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = pr.LoadBaseRepo(ctx); err != nil {
|
||||||
|
log.Error("%-v LoadBaseRepo: %v", pr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the sha is the same as pull request head commit id
|
||||||
|
baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("OpenRepository: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer baseGitRepo.Close()
|
||||||
|
|
||||||
|
headCommitID, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("GetRefCommitID: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if headCommitID != sha {
|
||||||
|
log.Warn("Head commit id of auto merge %-v does not match sha [%s], it may means the head branch has been updated. Just ignore this request because a new request expected in the queue", pr, sha)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Get all checks for this pr
|
// Get all checks for this pr
|
||||||
// We get the latest sha commit hash again to handle the case where the check of a previous push
|
// We get the latest sha commit hash again to handle the case where the check of a previous push
|
||||||
// did not succeed or was not finished yet.
|
// did not succeed or was not finished yet.
|
||||||
|
|
||||||
if err = pr.LoadHeadRepo(ctx); err != nil {
|
if err = pr.LoadHeadRepo(ctx); err != nil {
|
||||||
log.Error("%-v LoadHeadRepo: %v", pr, err)
|
log.Error("%-v LoadHeadRepo: %v", pr, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
headGitRepo, err := gitrepo.OpenRepository(ctx, pr.HeadRepo)
|
var headGitRepo *git.Repository
|
||||||
if err != nil {
|
if pr.BaseRepoID == pr.HeadRepoID {
|
||||||
log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
|
headGitRepo = baseGitRepo
|
||||||
return
|
} else {
|
||||||
|
headGitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer headGitRepo.Close()
|
||||||
}
|
}
|
||||||
defer headGitRepo.Close()
|
|
||||||
|
|
||||||
headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch)
|
headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch)
|
||||||
|
|
||||||
if pr.HeadRepo == nil || !headBranchExist {
|
if pr.HeadRepo == nil || !headBranchExist {
|
||||||
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
|
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
|
||||||
return
|
return
|
||||||
|
@ -238,25 +284,11 @@ func handlePull(pullID int64, sha string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var baseGitRepo *git.Repository
|
|
||||||
if pr.BaseRepoID == pr.HeadRepoID {
|
|
||||||
baseGitRepo = headGitRepo
|
|
||||||
} else {
|
|
||||||
if err = pr.LoadBaseRepo(ctx); err != nil {
|
|
||||||
log.Error("%-v LoadBaseRepo: %v", pr, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("OpenRepository %-v: %v", pr.BaseRepo, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer baseGitRepo.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := pull_service.Merge(ctx, pr, doer, baseGitRepo, scheduledPRM.MergeStyle, "", scheduledPRM.Message, true); err != nil {
|
if err := pull_service.Merge(ctx, pr, doer, baseGitRepo, scheduledPRM.MergeStyle, "", scheduledPRM.Message, true); err != nil {
|
||||||
log.Error("pull_service.Merge: %v", err)
|
log.Error("pull_service.Merge: %v", err)
|
||||||
|
// FIXME: if merge failed, we should display some error message to the pull request page.
|
||||||
|
// The resolution is add a new column on automerge table named `error_message` to store the error message and displayed
|
||||||
|
// on the pull request page. But this should not be finished in a bug fix PR which will be backport to release branch.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package automerge
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
notify_service "code.gitea.io/gitea/services/notify"
|
||||||
|
)
|
||||||
|
|
||||||
|
type automergeNotifier struct {
|
||||||
|
notify_service.NullNotifier
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ notify_service.Notifier = &automergeNotifier{}
|
||||||
|
|
||||||
|
// NewNotifier create a new automergeNotifier notifier
|
||||||
|
func NewNotifier() notify_service.Notifier {
|
||||||
|
return &automergeNotifier{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *automergeNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
|
||||||
|
// as a missing / blocking reviews could have blocked a pending automerge let's recheck
|
||||||
|
if review.Type == issues_model.ReviewTypeApprove {
|
||||||
|
if err := StartPRCheckAndAutoMergeBySHA(ctx, review.CommitID, pr.BaseRepo); err != nil {
|
||||||
|
log.Error("StartPullRequestAutoMergeCheckBySHA: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *automergeNotifier) PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
|
||||||
|
if err := review.LoadIssue(ctx); err != nil {
|
||||||
|
log.Error("LoadIssue: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := review.Issue.LoadPullRequest(ctx); err != nil {
|
||||||
|
log.Error("LoadPullRequest: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// as reviews could have blocked a pending automerge let's recheck
|
||||||
|
StartPRCheckAndAutoMerge(ctx, review.Issue.PullRequest)
|
||||||
|
}
|
|
@ -115,7 +115,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.State.IsSuccess() {
|
if status.State.IsSuccess() {
|
||||||
if err := automerge.MergeScheduledPullRequest(ctx, sha, repo); err != nil {
|
if err := automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo); err != nil {
|
||||||
return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)
|
return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -19,27 +20,31 @@ import (
|
||||||
func TestCreateFile(t *testing.T) {
|
func TestCreateFile(t *testing.T) {
|
||||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||||
session := loginUser(t, "user2")
|
session := loginUser(t, "user2")
|
||||||
|
testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content")
|
||||||
// Request editor page
|
|
||||||
req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
|
|
||||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
||||||
|
|
||||||
doc := NewHTMLParser(t, resp.Body)
|
|
||||||
lastCommit := doc.GetInputValueByName("last_commit")
|
|
||||||
assert.NotEmpty(t, lastCommit)
|
|
||||||
|
|
||||||
// Save new file to master branch
|
|
||||||
req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
|
|
||||||
"_csrf": doc.GetCSRF(),
|
|
||||||
"last_commit": lastCommit,
|
|
||||||
"tree_path": "test.txt",
|
|
||||||
"content": "Content",
|
|
||||||
"commit_choice": "direct",
|
|
||||||
})
|
|
||||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) *httptest.ResponseRecorder {
|
||||||
|
// Request editor page
|
||||||
|
newURL := fmt.Sprintf("/%s/%s/_new/%s/", user, repo, branch)
|
||||||
|
req := NewRequest(t, "GET", newURL)
|
||||||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
doc := NewHTMLParser(t, resp.Body)
|
||||||
|
lastCommit := doc.GetInputValueByName("last_commit")
|
||||||
|
assert.NotEmpty(t, lastCommit)
|
||||||
|
|
||||||
|
// Save new file to master branch
|
||||||
|
req = NewRequestWithValues(t, "POST", newURL, map[string]string{
|
||||||
|
"_csrf": doc.GetCSRF(),
|
||||||
|
"last_commit": lastCommit,
|
||||||
|
"tree_path": filePath,
|
||||||
|
"content": content,
|
||||||
|
"commit_choice": "direct",
|
||||||
|
})
|
||||||
|
return session.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateFileOnProtectedBranch(t *testing.T) {
|
func TestCreateFileOnProtectedBranch(t *testing.T) {
|
||||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||||
session := loginUser(t, "user2")
|
session := loginUser(t, "user2")
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -19,7 +21,9 @@ import (
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
auth_model "code.gitea.io/gitea/models/auth"
|
auth_model "code.gitea.io/gitea/models/auth"
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
git_model "code.gitea.io/gitea/models/git"
|
||||||
issues_model "code.gitea.io/gitea/models/issues"
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
|
pull_model "code.gitea.io/gitea/models/pull"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
@ -30,8 +34,10 @@ import (
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/test"
|
"code.gitea.io/gitea/modules/test"
|
||||||
"code.gitea.io/gitea/modules/translation"
|
"code.gitea.io/gitea/modules/translation"
|
||||||
|
"code.gitea.io/gitea/services/automerge"
|
||||||
"code.gitea.io/gitea/services/pull"
|
"code.gitea.io/gitea/services/pull"
|
||||||
repo_service "code.gitea.io/gitea/services/repository"
|
repo_service "code.gitea.io/gitea/services/repository"
|
||||||
|
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
|
||||||
files_service "code.gitea.io/gitea/services/repository/files"
|
files_service "code.gitea.io/gitea/services/repository/files"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -648,3 +654,195 @@ func TestPullMergeIndexerNotifier(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testResetRepo(t *testing.T, repoPath, branch, commitID string) {
|
||||||
|
f, err := os.OpenFile(filepath.Join(repoPath, "refs", "heads", branch), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
_, err = f.WriteString(commitID + "\n")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
repo, err := git.OpenRepository(context.Background(), repoPath)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer repo.Close()
|
||||||
|
id, err := repo.GetBranchCommitID(branch)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, commitID, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPullAutoMergeAfterCommitStatusSucceed(t *testing.T) {
|
||||||
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||||||
|
// create a pull request
|
||||||
|
session := loginUser(t, "user1")
|
||||||
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
|
forkedName := "repo1-1"
|
||||||
|
testRepoFork(t, session, "user2", "repo1", "user1", forkedName)
|
||||||
|
defer func() {
|
||||||
|
testDeleteRepository(t, session, "user1", forkedName)
|
||||||
|
}()
|
||||||
|
testEditFile(t, session, "user1", forkedName, "master", "README.md", "Hello, World (Edited)\n")
|
||||||
|
testPullCreate(t, session, "user1", forkedName, false, "master", "master", "Indexer notifier test pull")
|
||||||
|
|
||||||
|
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
|
||||||
|
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkedName})
|
||||||
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
|
||||||
|
BaseRepoID: baseRepo.ID,
|
||||||
|
BaseBranch: "master",
|
||||||
|
HeadRepoID: forkedRepo.ID,
|
||||||
|
HeadBranch: "master",
|
||||||
|
})
|
||||||
|
|
||||||
|
// add protected branch for commit status
|
||||||
|
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
|
||||||
|
// Change master branch to protected
|
||||||
|
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
|
||||||
|
"_csrf": csrf,
|
||||||
|
"rule_name": "master",
|
||||||
|
"enable_push": "true",
|
||||||
|
"enable_status_check": "true",
|
||||||
|
"status_check_contexts": "gitea/actions",
|
||||||
|
})
|
||||||
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
|
||||||
|
// first time insert automerge record, return true
|
||||||
|
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, scheduled)
|
||||||
|
|
||||||
|
// second time insert automerge record, return false because it does exist
|
||||||
|
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.False(t, scheduled)
|
||||||
|
|
||||||
|
// reload pr again
|
||||||
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
||||||
|
assert.False(t, pr.HasMerged)
|
||||||
|
assert.Empty(t, pr.MergedCommitID)
|
||||||
|
|
||||||
|
// update commit status to success, then it should be merged automatically
|
||||||
|
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
|
||||||
|
assert.NoError(t, err)
|
||||||
|
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
branches, _, err := baseGitRepo.GetBranchNames(0, 100)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.ElementsMatch(t, []string{"sub-home-md-img-check", "home-md-img-check", "pr-to-update", "branch2", "DefaultBranch", "develop", "feature/1", "master"}, branches)
|
||||||
|
baseGitRepo.Close()
|
||||||
|
defer func() {
|
||||||
|
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
|
||||||
|
State: api.CommitStatusSuccess,
|
||||||
|
TargetURL: "https://gitea.com",
|
||||||
|
Context: "gitea/actions",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
// realod pr again
|
||||||
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
||||||
|
assert.True(t, pr.HasMerged)
|
||||||
|
assert.NotEmpty(t, pr.MergedCommitID)
|
||||||
|
|
||||||
|
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPullAutoMergeAfterCommitStatusSucceedAndApproval(t *testing.T) {
|
||||||
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||||||
|
// create a pull request
|
||||||
|
session := loginUser(t, "user1")
|
||||||
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
|
forkedName := "repo1-2"
|
||||||
|
testRepoFork(t, session, "user2", "repo1", "user1", forkedName)
|
||||||
|
defer func() {
|
||||||
|
testDeleteRepository(t, session, "user1", forkedName)
|
||||||
|
}()
|
||||||
|
testEditFile(t, session, "user1", forkedName, "master", "README.md", "Hello, World (Edited)\n")
|
||||||
|
testPullCreate(t, session, "user1", forkedName, false, "master", "master", "Indexer notifier test pull")
|
||||||
|
|
||||||
|
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
|
||||||
|
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkedName})
|
||||||
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
|
||||||
|
BaseRepoID: baseRepo.ID,
|
||||||
|
BaseBranch: "master",
|
||||||
|
HeadRepoID: forkedRepo.ID,
|
||||||
|
HeadBranch: "master",
|
||||||
|
})
|
||||||
|
|
||||||
|
// add protected branch for commit status
|
||||||
|
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
|
||||||
|
// Change master branch to protected
|
||||||
|
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
|
||||||
|
"_csrf": csrf,
|
||||||
|
"rule_name": "master",
|
||||||
|
"enable_push": "true",
|
||||||
|
"enable_status_check": "true",
|
||||||
|
"status_check_contexts": "gitea/actions",
|
||||||
|
"required_approvals": "1",
|
||||||
|
})
|
||||||
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
|
||||||
|
// first time insert automerge record, return true
|
||||||
|
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, scheduled)
|
||||||
|
|
||||||
|
// second time insert automerge record, return false because it does exist
|
||||||
|
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.False(t, scheduled)
|
||||||
|
|
||||||
|
// reload pr again
|
||||||
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
||||||
|
assert.False(t, pr.HasMerged)
|
||||||
|
assert.Empty(t, pr.MergedCommitID)
|
||||||
|
|
||||||
|
// update commit status to success, then it should be merged automatically
|
||||||
|
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
|
||||||
|
assert.NoError(t, err)
|
||||||
|
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
baseGitRepo.Close()
|
||||||
|
defer func() {
|
||||||
|
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
|
||||||
|
State: api.CommitStatusSuccess,
|
||||||
|
TargetURL: "https://gitea.com",
|
||||||
|
Context: "gitea/actions",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
// reload pr again
|
||||||
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
||||||
|
assert.False(t, pr.HasMerged)
|
||||||
|
assert.Empty(t, pr.MergedCommitID)
|
||||||
|
|
||||||
|
// approve the PR from non-author
|
||||||
|
approveSession := loginUser(t, "user2")
|
||||||
|
req = NewRequest(t, "GET", fmt.Sprintf("/user2/repo1/pulls/%d", pr.Index))
|
||||||
|
resp := approveSession.MakeRequest(t, req, http.StatusOK)
|
||||||
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||||
|
testSubmitReview(t, approveSession, htmlDoc.GetCSRF(), "user2", "repo1", strconv.Itoa(int(pr.Index)), sha, "approve", http.StatusOK)
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
// realod pr again
|
||||||
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
||||||
|
assert.True(t, pr.HasMerged)
|
||||||
|
assert.NotEmpty(t, pr.MergedCommitID)
|
||||||
|
|
||||||
|
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -202,10 +202,10 @@ func TestPullView_GivenApproveOrRejectReviewOnClosedPR(t *testing.T) {
|
||||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||||
|
|
||||||
// Submit an approve review on the PR.
|
// Submit an approve review on the PR.
|
||||||
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "approve", http.StatusUnprocessableEntity)
|
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "", "approve", http.StatusUnprocessableEntity)
|
||||||
|
|
||||||
// Submit a reject review on the PR.
|
// Submit a reject review on the PR.
|
||||||
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "reject", http.StatusUnprocessableEntity)
|
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "", "reject", http.StatusUnprocessableEntity)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Submit approve/reject review on closed PR", func(t *testing.T) {
|
t.Run("Submit approve/reject review on closed PR", func(t *testing.T) {
|
||||||
|
@ -222,18 +222,18 @@ func TestPullView_GivenApproveOrRejectReviewOnClosedPR(t *testing.T) {
|
||||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||||
|
|
||||||
// Submit an approve review on the PR.
|
// Submit an approve review on the PR.
|
||||||
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "approve", http.StatusUnprocessableEntity)
|
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "", "approve", http.StatusUnprocessableEntity)
|
||||||
|
|
||||||
// Submit a reject review on the PR.
|
// Submit a reject review on the PR.
|
||||||
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "reject", http.StatusUnprocessableEntity)
|
testSubmitReview(t, user2Session, htmlDoc.GetCSRF(), "user2", "repo1", elem[4], "", "reject", http.StatusUnprocessableEntity)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSubmitReview(t *testing.T, session *TestSession, csrf, owner, repo, pullNumber, reviewType string, expectedSubmitStatus int) *httptest.ResponseRecorder {
|
func testSubmitReview(t *testing.T, session *TestSession, csrf, owner, repo, pullNumber, commitID, reviewType string, expectedSubmitStatus int) *httptest.ResponseRecorder {
|
||||||
options := map[string]string{
|
options := map[string]string{
|
||||||
"_csrf": csrf,
|
"_csrf": csrf,
|
||||||
"commit_id": "",
|
"commit_id": commitID,
|
||||||
"content": "test",
|
"content": "test",
|
||||||
"type": reviewType,
|
"type": reviewType,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue