From 1cf2e663ae457bac40bcb9bb71cc4b193e93f9c7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 26 Sep 2025 10:10:07 -0700 Subject: [PATCH] improvements --- services/agit/agit.go | 2 +- services/pull/pull.go | 32 +++++++++++++------------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/services/agit/agit.go b/services/agit/agit.go index 203f1e38af..c5bd78bcd4 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -216,7 +216,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. } pr.HeadCommitID = opts.NewCommitIDs[i] - if err = pull_service.UpdatePullRequestAgitFlowHead(ctx, pr, pr.HeadCommitID); err != nil { + if err = pull_service.UpdatePullRequestHeadRef(ctx, pr); err != nil { return nil, fmt.Errorf("failed to update pull ref. Error: %w", err) } diff --git a/services/pull/pull.go b/services/pull/pull.go index f51f85e7aa..ad271d2912 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -123,12 +123,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error { issue.PullRequest = pr // update head commit id into git repository - if pr.Flow == issues_model.PullRequestFlowAGit { - err = UpdatePullRequestAgitFlowHead(ctx, pr, pr.HeadCommitID) - } else { - err = UpdatePullRequestGithubFlowHead(ctx, pr) - } - if err != nil { + if err = UpdatePullRequestHeadRef(ctx, pr); err != nil { return err } @@ -388,7 +383,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) { log.Trace("Updating PR[%d]: composing new test task", pr.ID) pr.HeadRepo = repo // avoid loading again if pr.Flow == issues_model.PullRequestFlowGithub { - if err := UpdatePullRequestGithubFlowHead(ctx, pr); err != nil { + if err := UpdatePullRequestHeadRef(ctx, pr); err != nil { log.Error("PushToBaseRepo: %v", err) continue } @@ -549,7 +544,7 @@ func UpdatePullsRefs(ctx context.Context, repo *repo_model.Repository, update *r for _, pr := range prs { log.Trace("Updating PR[%d]: composing new test task", pr.ID) if pr.Flow == issues_model.PullRequestFlowGithub { - if err := UpdatePullRequestGithubFlowHead(ctx, pr); err != nil { + if err := UpdatePullRequestHeadRef(ctx, pr); err != nil { log.Error("UpdatePullRequestHead: %v", err) } } @@ -557,31 +552,30 @@ func UpdatePullsRefs(ctx context.Context, repo *repo_model.Repository, update *r } } -func UpdatePullRequestAgitFlowHead(ctx context.Context, pr *issues_model.PullRequest, commitID string) error { - log.Trace("UpdateAgitPullRequestHead[%d]: update pull request head in base repo '%s'", pr.ID, pr.GetGitHeadRefName()) - - return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), commitID) -} - // UpdatePullRequestHeadRef updates the head reference of a pull request -func UpdatePullRequestGithubFlowHead(ctx context.Context, pr *issues_model.PullRequest) error { +func UpdatePullRequestHeadRef(ctx context.Context, pr *issues_model.PullRequest) error { log.Trace("UpdatePullRequestHeadRef[%d]: update pull request ref in base repo '%s'", pr.ID, pr.GetGitHeadRefName()) if err := pr.LoadBaseRepo(ctx); err != nil { return err } + if pr.Flow == issues_model.PullRequestFlowAGit { + if pr.HeadCommitID == "" { + return errors.New("head commit ID cannot be empty for agit flow") + } + return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID) + } + if !pr.IsSameRepo() { // for cross repository pull request if err := pr.LoadHeadRepo(ctx); err != nil { return err } - if err := gitrepo.FetchRemoteBranch(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadRepo, pr.HeadBranch); err != nil { - return err - } + return gitrepo.FetchRemoteBranch(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadRepo, pr.HeadBranch) } - return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID) + return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadBranch) } // retargetBranchPulls change target branch for all pull requests whose base branch is the branch