Skip to content

Commit 1d65f8c

Browse files
issue: Don't close issues via commits on non-default branch.
Adds a small check to close the issues only if the referencing commits are on the default branch. Fixes: #2314.
1 parent 0de57fd commit 1d65f8c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

models/action.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func getIssueFromRef(repo *Repository, ref string) (*Issue, error) {
477477
}
478478

479479
// UpdateIssuesCommit checks if issues are manipulated by commit message.
480-
func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) error {
480+
func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, branchName string) error {
481481
// Commits are appended in the reverse order.
482482
for i := len(commits) - 1; i >= 0; i-- {
483483
c := commits[i]
@@ -500,6 +500,11 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
500500
}
501501
}
502502

503+
// Change issue status only if the commit has been pushed to the default branch.
504+
if repo.DefaultBranch != branchName {
505+
continue
506+
}
507+
503508
refMarked = make(map[int64]bool)
504509
// FIXME: can merge this one and next one to a common function.
505510
for _, ref := range issueCloseKeywordsPat.FindAllString(c.Message, -1) {
@@ -609,7 +614,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
609614
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
610615
}
611616

612-
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
617+
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
613618
log.Error(4, "updateIssuesCommit: %v", err)
614619
}
615620
}

models/action_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,37 @@ func TestUpdateIssuesCommit(t *testing.T) {
227227

228228
AssertNotExistsBean(t, commentBean)
229229
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
230-
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
230+
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
231231
AssertExistsAndLoadBean(t, commentBean)
232232
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
233233
CheckConsistencyFor(t, &Action{})
234+
235+
// Test that push to a non-default branch closes no issue.
236+
pushCommits = []*PushCommit{
237+
{
238+
Sha1: "abcdef1",
239+
CommitterEmail: "[email protected]",
240+
CommitterName: "User Two",
241+
AuthorEmail: "[email protected]",
242+
AuthorName: "User Four",
243+
Message: "close #1",
244+
},
245+
}
246+
repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
247+
commentBean = &Comment{
248+
Type: CommentTypeCommitRef,
249+
CommitSHA: "abcdef1",
250+
PosterID: user.ID,
251+
IssueID: 6,
252+
}
253+
issueBean = &Issue{RepoID: repo.ID, Index: 1}
254+
255+
AssertNotExistsBean(t, commentBean)
256+
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
257+
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
258+
AssertExistsAndLoadBean(t, commentBean)
259+
AssertNotExistsBean(t, issueBean, "is_closed=1")
260+
CheckConsistencyFor(t, &Action{})
234261
}
235262

236263
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {

0 commit comments

Comments
 (0)