Skip to content

Commit 8660b18

Browse files
refactor: Extract changeIssueStatus() function.
1 parent 1d65f8c commit 8660b18

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

models/action.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,32 @@ 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.
480506
func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, branchName string) error {
481507
// Commits are appended in the reverse order.
@@ -506,50 +532,15 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
506532
}
507533

508534
refMarked = make(map[int64]bool)
509-
// FIXME: can merge this one and next one to a common function.
510535
for _, ref := range issueCloseKeywordsPat.FindAllString(c.Message, -1) {
511-
issue, err := getIssueFromRef(repo, ref)
512-
if err != nil {
513-
return err
514-
}
515-
516-
if issue == nil || refMarked[issue.ID] {
517-
continue
518-
}
519-
refMarked[issue.ID] = true
520-
521-
if issue.RepoID != repo.ID || issue.IsClosed {
522-
continue
523-
}
524-
525-
issue.Repo = repo
526-
if err = issue.ChangeStatus(doer, true); err != nil {
527-
// Don't return an error when dependencies are open as this would let the push fail
528-
if IsErrDependenciesLeft(err) {
529-
return nil
530-
}
536+
if err := changeIssueStatus(repo, doer, ref, refMarked, true); err != nil {
531537
return err
532538
}
533539
}
534540

535541
// It is conflict to have close and reopen at same time, so refsMarked doesn't need to reinit here.
536542
for _, ref := range issueReopenKeywordsPat.FindAllString(c.Message, -1) {
537-
issue, err := getIssueFromRef(repo, ref)
538-
if err != nil {
539-
return err
540-
}
541-
542-
if issue == nil || refMarked[issue.ID] {
543-
continue
544-
}
545-
refMarked[issue.ID] = true
546-
547-
if issue.RepoID != repo.ID || !issue.IsClosed {
548-
continue
549-
}
550-
551-
issue.Repo = repo
552-
if err = issue.ChangeStatus(doer, false); err != nil {
543+
if err := changeIssueStatus(repo, doer, ref, refMarked, false); err != nil {
553544
return err
554545
}
555546
}

0 commit comments

Comments
 (0)