summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLanre Adelowo <adelowomailbox@gmail.com>2019-02-10 20:27:19 +0100
committerLauris BH <lauris@nix.lv>2019-02-10 21:27:19 +0200
commit9d8178b3ac5f86a64f67ab7b5dea99347a5afe72 (patch)
treeed560142815927211f7d52db859728b88f0d9734 /models
parentc0adb5ea8bd0b2551a8c414b54ef5bc67882de55 (diff)
downloadgitea-9d8178b3ac5f86a64f67ab7b5dea99347a5afe72.tar.gz
gitea-9d8178b3ac5f86a64f67ab7b5dea99347a5afe72.zip
Add option to close issues via commit on a non master branch (#5992)
* fixes #5957 * add tests to make sure config option is respected * use already defined struct * - use migration to make the flag repo wide not for the entire gitea instance Also note that the config value can still be set so as to be able to control the value for new repositories that are to be created - fix copy/paste error in copyright header year and rearrange import - use repo config instead of server config value to determine if a commit should close an issue - update testsuite * use global config only when creating a new repository * allow repo admin toggle feature via UI * fix typo and improve testcase * fix fixtures * add DEFAULT prefix to config value * fix test
Diffstat (limited to 'models')
-rw-r--r--models/action.go3
-rw-r--r--models/action_test.go34
-rw-r--r--models/fixtures/issue.yml13
-rw-r--r--models/fixtures/repository.yml3
-rw-r--r--models/issue_test.go2
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v79.go27
-rw-r--r--models/repo.go30
8 files changed, 97 insertions, 17 deletions
diff --git a/models/action.go b/models/action.go
index c886408b2f..ca6bfaf666 100644
--- a/models/action.go
+++ b/models/action.go
@@ -539,7 +539,8 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
}
// Change issue status only if the commit has been pushed to the default branch.
- if repo.DefaultBranch != branchName {
+ // and if the repo is configured to allow only that
+ if repo.DefaultBranch != branchName && !repo.CloseIssuesViaCommitInAnyBranch {
continue
}
diff --git a/models/action_test.go b/models/action_test.go
index 0310b0ad5d..96d6ddb6dd 100644
--- a/models/action_test.go
+++ b/models/action_test.go
@@ -260,6 +260,40 @@ func TestUpdateIssuesCommit(t *testing.T) {
CheckConsistencyFor(t, &Action{})
}
+func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+
+ // Test that push to a non-default branch closes an issue.
+ pushCommits := []*PushCommit{
+ {
+ Sha1: "abcdef1",
+ CommitterEmail: "user2@example.com",
+ CommitterName: "User Two",
+ AuthorEmail: "user4@example.com",
+ AuthorName: "User Four",
+ Message: "close #2",
+ },
+ }
+
+ repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
+ commentBean := &Comment{
+ Type: CommentTypeCommitRef,
+ CommitSHA: "abcdef1",
+ PosterID: user.ID,
+ IssueID: 7,
+ }
+
+ issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
+
+ AssertNotExistsBean(t, commentBean)
+ AssertNotExistsBean(t, issueBean, "is_closed=1")
+ assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
+ AssertExistsAndLoadBean(t, commentBean)
+ AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
+ CheckConsistencyFor(t, &Action{})
+}
+
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
AssertNotExistsBean(t, actionBean)
assert.NoError(t, CommitRepoAction(opts))
diff --git a/models/fixtures/issue.yml b/models/fixtures/issue.yml
index 4de8c4fa7e..01bd8b86f6 100644
--- a/models/fixtures/issue.yml
+++ b/models/fixtures/issue.yml
@@ -73,3 +73,16 @@
num_comments: 0
created_unix: 946684850
updated_unix: 978307200
+
+-
+ id: 7
+ repo_id: 2
+ index: 2
+ poster_id: 2
+ name: issue7
+ content: content for the seventh issue
+ is_closed: false
+ is_pull: false
+ created_unix: 946684830
+ updated_unix: 978307200
+
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml
index d412e52d8e..f415761650 100644
--- a/models/fixtures/repository.yml
+++ b/models/fixtures/repository.yml
@@ -17,11 +17,12 @@
lower_name: repo2
name: repo2
is_private: true
- num_issues: 1
+ num_issues: 2
num_closed_issues: 1
num_pulls: 0
num_closed_pulls: 0
num_stars: 1
+ close_issues_via_commit_in_any_branch: true
-
id: 3
diff --git a/models/issue_test.go b/models/issue_test.go
index 3bda3304c3..cec7e8b478 100644
--- a/models/issue_test.go
+++ b/models/issue_test.go
@@ -275,7 +275,7 @@ func TestGetUserIssueStats(t *testing.T) {
YourRepositoriesCount: 2,
AssignCount: 0,
CreateCount: 2,
- OpenCount: 1,
+ OpenCount: 2,
ClosedCount: 2,
},
},
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index 533ff90738..174e7b5156 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -210,6 +210,8 @@ var migrations = []Migration{
NewMigration("add theme to users", addUserDefaultTheme),
// v78 -> v79
NewMigration("rename repo is_bare to repo is_empty", renameRepoIsBareToIsEmpty),
+ // v79 -> v80
+ NewMigration("add can close issues via commit in any branch", addCanCloseIssuesViaCommitInAnyBranch),
}
// Migrate database to current version
diff --git a/models/migrations/v79.go b/models/migrations/v79.go
new file mode 100644
index 0000000000..e246393957
--- /dev/null
+++ b/models/migrations/v79.go
@@ -0,0 +1,27 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/go-xorm/xorm"
+)
+
+func addCanCloseIssuesViaCommitInAnyBranch(x *xorm.Engine) error {
+
+ type Repository struct {
+ ID int64 `xorm:"pk autoincr"`
+ CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
+ }
+
+ if err := x.Sync2(new(Repository)); err != nil {
+ return err
+ }
+
+ _, err := x.Exec("UPDATE repository SET close_issues_via_commit_in_any_branch = ?",
+ setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch)
+ return err
+}
diff --git a/models/repo.go b/models/repo.go
index c0e69b0e38..848a76fe88 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -197,13 +197,14 @@ type Repository struct {
ExternalMetas map[string]string `xorm:"-"`
Units []*RepoUnit `xorm:"-"`
- IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
- ForkID int64 `xorm:"INDEX"`
- BaseRepo *Repository `xorm:"-"`
- Size int64 `xorm:"NOT NULL DEFAULT 0"`
- IndexerStatus *RepoIndexerStatus `xorm:"-"`
- IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
- Topics []string `xorm:"TEXT JSON"`
+ IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
+ ForkID int64 `xorm:"INDEX"`
+ BaseRepo *Repository `xorm:"-"`
+ Size int64 `xorm:"NOT NULL DEFAULT 0"`
+ IndexerStatus *RepoIndexerStatus `xorm:"-"`
+ IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
+ CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
+ Topics []string `xorm:"TEXT JSON"`
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
@@ -1373,13 +1374,14 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
}
repo := &Repository{
- OwnerID: u.ID,
- Owner: u,
- Name: opts.Name,
- LowerName: strings.ToLower(opts.Name),
- Description: opts.Description,
- IsPrivate: opts.IsPrivate,
- IsFsckEnabled: !opts.IsMirror,
+ OwnerID: u.ID,
+ Owner: u,
+ Name: opts.Name,
+ LowerName: strings.ToLower(opts.Name),
+ Description: opts.Description,
+ IsPrivate: opts.IsPrivate,
+ IsFsckEnabled: !opts.IsMirror,
+ CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
}
sess := x.NewSession()