Skip to content

Commit 9f476b8

Browse files
HarshitOnGitHublafriks
authored andcommitted
Don't close issues via commits on non-default branch. (#5622)
Adds a small check to close the issues only if the referencing commits are on the default branch. Fixes: #2314.
1 parent 0de57fd commit 9f476b8

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
lines changed

models/action.go

+35-39
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,34 @@ func getIssueFromRef(repo *Repository, ref string) (*Issue, error) {
476476
return issue, nil
477477
}
478478

479+
func changeIssueStatus(repo *Repository, doer *User, ref string, refMarked map[int64]bool, status bool) error {
480+
issue, err := getIssueFromRef(repo, ref)
481+
if err != nil {
482+
return err
483+
}
484+
485+
if issue == nil || refMarked[issue.ID] {
486+
return nil
487+
}
488+
refMarked[issue.ID] = true
489+
490+
if issue.RepoID != repo.ID || issue.IsClosed == status {
491+
return nil
492+
}
493+
494+
issue.Repo = repo
495+
if err = issue.ChangeStatus(doer, status); err != nil {
496+
// Don't return an error when dependencies are open as this would let the push fail
497+
if IsErrDependenciesLeft(err) {
498+
return nil
499+
}
500+
return err
501+
}
502+
return nil
503+
}
504+
479505
// UpdateIssuesCommit checks if issues are manipulated by commit message.
480-
func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) error {
506+
func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, branchName string) error {
481507
// Commits are appended in the reverse order.
482508
for i := len(commits) - 1; i >= 0; i-- {
483509
c := commits[i]
@@ -500,51 +526,21 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
500526
}
501527
}
502528

529+
// Change issue status only if the commit has been pushed to the default branch.
530+
if repo.DefaultBranch != branchName {
531+
continue
532+
}
533+
503534
refMarked = make(map[int64]bool)
504-
// FIXME: can merge this one and next one to a common function.
505535
for _, ref := range issueCloseKeywordsPat.FindAllString(c.Message, -1) {
506-
issue, err := getIssueFromRef(repo, ref)
507-
if err != nil {
508-
return err
509-
}
510-
511-
if issue == nil || refMarked[issue.ID] {
512-
continue
513-
}
514-
refMarked[issue.ID] = true
515-
516-
if issue.RepoID != repo.ID || issue.IsClosed {
517-
continue
518-
}
519-
520-
issue.Repo = repo
521-
if err = issue.ChangeStatus(doer, true); err != nil {
522-
// Don't return an error when dependencies are open as this would let the push fail
523-
if IsErrDependenciesLeft(err) {
524-
return nil
525-
}
536+
if err := changeIssueStatus(repo, doer, ref, refMarked, true); err != nil {
526537
return err
527538
}
528539
}
529540

530541
// It is conflict to have close and reopen at same time, so refsMarked doesn't need to reinit here.
531542
for _, ref := range issueReopenKeywordsPat.FindAllString(c.Message, -1) {
532-
issue, err := getIssueFromRef(repo, ref)
533-
if err != nil {
534-
return err
535-
}
536-
537-
if issue == nil || refMarked[issue.ID] {
538-
continue
539-
}
540-
refMarked[issue.ID] = true
541-
542-
if issue.RepoID != repo.ID || !issue.IsClosed {
543-
continue
544-
}
545-
546-
issue.Repo = repo
547-
if err = issue.ChangeStatus(doer, false); err != nil {
543+
if err := changeIssueStatus(repo, doer, ref, refMarked, false); err != nil {
548544
return err
549545
}
550546
}
@@ -609,7 +605,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
609605
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
610606
}
611607

612-
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits); err != nil {
608+
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
613609
log.Error(4, "updateIssuesCommit: %v", err)
614610
}
615611
}

models/action_test.go

+28-1
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)