Skip to content

Commit ce2d488

Browse files
lunnysapk
authored andcommitted
Move PushToBaseRepo from models to services/pull (#9352)
1 parent 6715677 commit ce2d488

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed

models/pull.go

-44
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package models
77

88
import (
99
"fmt"
10-
"os"
11-
"path"
1210
"strings"
1311

1412
"code.gitea.io/gitea/modules/git"
@@ -631,48 +629,6 @@ func (pr *PullRequest) UpdateCols(cols ...string) error {
631629
return err
632630
}
633631

634-
// PushToBaseRepo pushes commits from branches of head repository to
635-
// corresponding branches of base repository.
636-
// FIXME: Only push branches that are actually updates?
637-
func (pr *PullRequest) PushToBaseRepo() (err error) {
638-
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
639-
640-
headRepoPath := pr.HeadRepo.RepoPath()
641-
headGitRepo, err := git.OpenRepository(headRepoPath)
642-
if err != nil {
643-
return fmt.Errorf("OpenRepository: %v", err)
644-
}
645-
defer headGitRepo.Close()
646-
647-
tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
648-
if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
649-
return fmt.Errorf("headGitRepo.AddRemote: %v", err)
650-
}
651-
// Make sure to remove the remote even if the push fails
652-
defer func() {
653-
if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
654-
log.Error("PushToBaseRepo: RemoveRemote: %s", err)
655-
}
656-
}()
657-
658-
headFile := pr.GetGitRefName()
659-
660-
// Remove head in case there is a conflict.
661-
file := path.Join(pr.BaseRepo.RepoPath(), headFile)
662-
663-
_ = os.Remove(file)
664-
665-
if err = git.Push(headRepoPath, git.PushOptions{
666-
Remote: tmpRemoteName,
667-
Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
668-
Force: true,
669-
}); err != nil {
670-
return fmt.Errorf("Push: %v", err)
671-
}
672-
673-
return nil
674-
}
675-
676632
// IsWorkInProgress determine if the Pull Request is a Work In Progress by its title
677633
func (pr *PullRequest) IsWorkInProgress() bool {
678634
if err := pr.LoadIssue(); err != nil {

models/pull_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ func TestPullRequest_UpdateCols(t *testing.T) {
190190
CheckConsistencyFor(t, pr)
191191
}
192192

193-
// TODO TestPullRequest_PushToBaseRepo
194-
195193
func TestPullRequestList_LoadAttributes(t *testing.T) {
196194
assert.NoError(t, PrepareTestDatabase())
197195

routers/api/v1/repo/pull.go

-3
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
307307
}
308308
ctx.Error(500, "NewPullRequest", err)
309309
return
310-
} else if err := pr.PushToBaseRepo(); err != nil {
311-
ctx.Error(500, "PushToBaseRepo", err)
312-
return
313310
}
314311

315312
notification.NotifyNewPullRequest(pr)

routers/repo/pull.go

-3
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,6 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
813813
}
814814
ctx.ServerError("NewPullRequest", err)
815815
return
816-
} else if err := pullRequest.PushToBaseRepo(); err != nil {
817-
ctx.ServerError("PushToBaseRepo", err)
818-
return
819816
}
820817

821818
notification.NotifyNewPullRequest(pullRequest)

services/pull/pull.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package pull
66

77
import (
88
"fmt"
9+
"os"
10+
"path"
911

1012
"code.gitea.io/gitea/models"
1113
"code.gitea.io/gitea/modules/git"
@@ -33,6 +35,10 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
3335
pr.Issue = pull
3436
pull.PullRequest = pr
3537

38+
if err := PushToBaseRepo(pr); err != nil {
39+
return err
40+
}
41+
3642
notification.NotifyNewPullRequest(pr)
3743

3844
return nil
@@ -60,7 +66,7 @@ func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *m
6066
func addHeadRepoTasks(prs []*models.PullRequest) {
6167
for _, pr := range prs {
6268
log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
63-
if err := pr.PushToBaseRepo(); err != nil {
69+
if err := PushToBaseRepo(pr); err != nil {
6470
log.Error("PushToBaseRepo: %v", err)
6571
continue
6672
}
@@ -107,3 +113,45 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
107113
AddToTaskQueue(pr)
108114
}
109115
}
116+
117+
// PushToBaseRepo pushes commits from branches of head repository to
118+
// corresponding branches of base repository.
119+
// FIXME: Only push branches that are actually updates?
120+
func PushToBaseRepo(pr *models.PullRequest) (err error) {
121+
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
122+
123+
headRepoPath := pr.HeadRepo.RepoPath()
124+
headGitRepo, err := git.OpenRepository(headRepoPath)
125+
if err != nil {
126+
return fmt.Errorf("OpenRepository: %v", err)
127+
}
128+
defer headGitRepo.Close()
129+
130+
tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
131+
if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
132+
return fmt.Errorf("headGitRepo.AddRemote: %v", err)
133+
}
134+
// Make sure to remove the remote even if the push fails
135+
defer func() {
136+
if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
137+
log.Error("PushToBaseRepo: RemoveRemote: %s", err)
138+
}
139+
}()
140+
141+
headFile := pr.GetGitRefName()
142+
143+
// Remove head in case there is a conflict.
144+
file := path.Join(pr.BaseRepo.RepoPath(), headFile)
145+
146+
_ = os.Remove(file)
147+
148+
if err = git.Push(headRepoPath, git.PushOptions{
149+
Remote: tmpRemoteName,
150+
Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
151+
Force: true,
152+
}); err != nil {
153+
return fmt.Errorf("Push: %v", err)
154+
}
155+
156+
return nil
157+
}

services/pull/pull_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2019 The Gitea Authors.
2+
// All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package pull
7+
8+
// TODO TestPullRequest_PushToBaseRepo

0 commit comments

Comments
 (0)