Skip to content

Commit 1cf2e66

Browse files
committed
improvements
1 parent c33886f commit 1cf2e66

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

services/agit/agit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
216216
}
217217

218218
pr.HeadCommitID = opts.NewCommitIDs[i]
219-
if err = pull_service.UpdatePullRequestAgitFlowHead(ctx, pr, pr.HeadCommitID); err != nil {
219+
if err = pull_service.UpdatePullRequestHeadRef(ctx, pr); err != nil {
220220
return nil, fmt.Errorf("failed to update pull ref. Error: %w", err)
221221
}
222222

services/pull/pull.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
123123
issue.PullRequest = pr
124124

125125
// update head commit id into git repository
126-
if pr.Flow == issues_model.PullRequestFlowAGit {
127-
err = UpdatePullRequestAgitFlowHead(ctx, pr, pr.HeadCommitID)
128-
} else {
129-
err = UpdatePullRequestGithubFlowHead(ctx, pr)
130-
}
131-
if err != nil {
126+
if err = UpdatePullRequestHeadRef(ctx, pr); err != nil {
132127
return err
133128
}
134129

@@ -388,7 +383,7 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
388383
log.Trace("Updating PR[%d]: composing new test task", pr.ID)
389384
pr.HeadRepo = repo // avoid loading again
390385
if pr.Flow == issues_model.PullRequestFlowGithub {
391-
if err := UpdatePullRequestGithubFlowHead(ctx, pr); err != nil {
386+
if err := UpdatePullRequestHeadRef(ctx, pr); err != nil {
392387
log.Error("PushToBaseRepo: %v", err)
393388
continue
394389
}
@@ -549,39 +544,38 @@ func UpdatePullsRefs(ctx context.Context, repo *repo_model.Repository, update *r
549544
for _, pr := range prs {
550545
log.Trace("Updating PR[%d]: composing new test task", pr.ID)
551546
if pr.Flow == issues_model.PullRequestFlowGithub {
552-
if err := UpdatePullRequestGithubFlowHead(ctx, pr); err != nil {
547+
if err := UpdatePullRequestHeadRef(ctx, pr); err != nil {
553548
log.Error("UpdatePullRequestHead: %v", err)
554549
}
555550
}
556551
}
557552
}
558553
}
559554

560-
func UpdatePullRequestAgitFlowHead(ctx context.Context, pr *issues_model.PullRequest, commitID string) error {
561-
log.Trace("UpdateAgitPullRequestHead[%d]: update pull request head in base repo '%s'", pr.ID, pr.GetGitHeadRefName())
562-
563-
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), commitID)
564-
}
565-
566555
// UpdatePullRequestHeadRef updates the head reference of a pull request
567-
func UpdatePullRequestGithubFlowHead(ctx context.Context, pr *issues_model.PullRequest) error {
556+
func UpdatePullRequestHeadRef(ctx context.Context, pr *issues_model.PullRequest) error {
568557
log.Trace("UpdatePullRequestHeadRef[%d]: update pull request ref in base repo '%s'", pr.ID, pr.GetGitHeadRefName())
569558

570559
if err := pr.LoadBaseRepo(ctx); err != nil {
571560
return err
572561
}
573562

563+
if pr.Flow == issues_model.PullRequestFlowAGit {
564+
if pr.HeadCommitID == "" {
565+
return errors.New("head commit ID cannot be empty for agit flow")
566+
}
567+
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID)
568+
}
569+
574570
if !pr.IsSameRepo() { // for cross repository pull request
575571
if err := pr.LoadHeadRepo(ctx); err != nil {
576572
return err
577573
}
578574

579-
if err := gitrepo.FetchRemoteBranch(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadRepo, pr.HeadBranch); err != nil {
580-
return err
581-
}
575+
return gitrepo.FetchRemoteBranch(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadRepo, pr.HeadBranch)
582576
}
583577

584-
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID)
578+
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadBranch)
585579
}
586580

587581
// retargetBranchPulls change target branch for all pull requests whose base branch is the branch

0 commit comments

Comments
 (0)