diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-08 20:32:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 20:32:45 +0800 |
commit | 4344a6410788f30848e5153f6356dcdd0774bebc (patch) | |
tree | 6eba1230f1ac672f0eb0a36f148949470dd50048 /models | |
parent | 5ca224a789394d00cc1efb5afcae7b87aa7d1e49 (diff) | |
download | gitea-4344a6410788f30848e5153f6356dcdd0774bebc.tar.gz gitea-4344a6410788f30848e5153f6356dcdd0774bebc.zip |
Allow custom default merge message with .gitea/default_merge_message/<merge_style>_TEMPLATE.md (#18177)
* Allow custom default merge message with .gitea/MERGE_MESSAGE_<merge_style>_TEMPLATE.md
* Some improvements
* Follow some advices
* Fix bug
* Fix bug
* Fix lint
* Fix close comment
* Fix test
* Fix and docs
* Improve codes
* Update docs and remove unnecessary variables
* return error for GetDefaultMergeMessage
* Fix test
* improve code
* ignore unknow unit type
* return error for GetDefaultMergeMessage
* Update services/pull/merge.go
* Some improvements
* Follow some advices
* Fix bug
* Fix lint
* Improve codes
* Update docs and remove unnecessary variables
* return error for GetDefaultMergeMessage
* improve code
* Handle deleted HeadRepo in GetDefaultMergeMessage
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Fix test
* Fix test
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r-- | models/pull.go | 43 | ||||
-rw-r--r-- | models/pull_test.go | 53 | ||||
-rw-r--r-- | models/repo/repo_unit.go | 6 |
3 files changed, 3 insertions, 99 deletions
diff --git a/models/pull.go b/models/pull.go index 0fa3bdf14f..fc5c0d61b3 100644 --- a/models/pull.go +++ b/models/pull.go @@ -13,7 +13,6 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -228,34 +227,6 @@ func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) { return } -// GetDefaultMergeMessage returns default message used when merging pull request -func (pr *PullRequest) GetDefaultMergeMessage(ctx context.Context) (string, error) { - if pr.HeadRepo == nil { - var err error - pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID) - if err != nil { - return "", fmt.Errorf("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) - } - } - if err := pr.LoadIssueCtx(ctx); err != nil { - return "", fmt.Errorf("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err) - } - if err := pr.LoadBaseRepoCtx(ctx); err != nil { - return "", fmt.Errorf("LoadBaseRepo: %v", err) - } - - issueReference := "#" - if pr.BaseRepo.UnitEnabledCtx(ctx, unit.TypeExternalTracker) { - issueReference = "!" - } - - if pr.BaseRepoID == pr.HeadRepoID { - return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadBranch, pr.BaseBranch), nil - } - - return fmt.Sprintf("Merge pull request '%s' (%s%d) from %s:%s into %s", pr.Issue.Title, issueReference, pr.Issue.Index, pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseBranch), nil -} - // ReviewCount represents a count of Reviews type ReviewCount struct { IssueID int64 @@ -338,20 +309,6 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error { return committer.Commit() } -// GetDefaultSquashMessage returns default message used when squash and merging pull request -func (pr *PullRequest) GetDefaultSquashMessage(ctx context.Context) (string, error) { - if err := pr.LoadIssueCtx(ctx); err != nil { - return "", fmt.Errorf("LoadIssue: %v", err) - } - if err := pr.LoadBaseRepoCtx(ctx); err != nil { - return "", fmt.Errorf("LoadBaseRepo: %v", err) - } - if pr.BaseRepo.UnitEnabledCtx(ctx, unit.TypeExternalTracker) { - return fmt.Sprintf("%s (!%d)", pr.Issue.Title, pr.Issue.Index), nil - } - return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index), nil -} - // GetGitRefName returns git ref for hidden pull request branch func (pr *PullRequest) GetGitRefName() string { return fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index) diff --git a/models/pull_test.go b/models/pull_test.go index 92cf9a6524..6119bca692 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -8,10 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" - repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unittest" - user_model "code.gitea.io/gitea/models/user" "github.com/stretchr/testify/assert" ) @@ -256,53 +253,3 @@ func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { pr.Issue.Title = "[wip] " + original assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix()) } - -func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) - - msg, err := pr.GetDefaultMergeMessage(db.DefaultContext) - assert.NoError(t, err) - assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", msg) - - pr.BaseRepoID = 1 - pr.HeadRepoID = 2 - msg, err = pr.GetDefaultMergeMessage(db.DefaultContext) - assert.NoError(t, err) - assert.Equal(t, "Merge pull request 'issue3' (#3) from user2/repo1:branch2 into master", msg) -} - -func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - externalTracker := repo_model.RepoUnit{ - Type: unit.TypeExternalTracker, - Config: &repo_model.ExternalTrackerConfig{ - ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}", - }, - } - baseRepo := &repo_model.Repository{Name: "testRepo", ID: 1} - baseRepo.Owner = &user_model.User{Name: "testOwner"} - baseRepo.Units = []*repo_model.RepoUnit{&externalTracker} - - pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest) - - msg, err := pr.GetDefaultMergeMessage(db.DefaultContext) - assert.NoError(t, err) - assert.Equal(t, "Merge pull request 'issue3' (!3) from branch2 into master", msg) - - pr.BaseRepoID = 1 - pr.HeadRepoID = 2 - msg, err = pr.GetDefaultMergeMessage(db.DefaultContext) - assert.NoError(t, err) - assert.Equal(t, "Merge pull request 'issue3' (!3) from user2/repo1:branch2 into master", msg) -} - -func TestPullRequest_GetDefaultSquashMessage(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) - - msg, err := pr.GetDefaultSquashMessage(db.DefaultContext) - assert.NoError(t, err) - assert.Equal(t, "issue3 (#3)", msg) -} diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index 37f1c70545..4c87f017b3 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -173,8 +173,6 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { switch colName { case "type": switch unit.Type(db.Cell2Int64(val)) { - case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects: - r.Config = new(UnitConfig) case unit.TypeExternalWiki: r.Config = new(ExternalWikiConfig) case unit.TypeExternalTracker: @@ -183,8 +181,10 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { r.Config = new(PullRequestsConfig) case unit.TypeIssues: r.Config = new(IssuesConfig) + case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects: + fallthrough default: - panic(fmt.Sprintf("unrecognized repo unit type: %v", *val)) + r.Config = new(UnitConfig) } } } |