@@ -491,32 +491,45 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
491491 }
492492 refMarked [key ] = true
493493
494- // only create comments for issues if user has permission for it
495- if perm .IsAdmin () || perm .IsOwner () || perm .CanWrite (UnitTypeIssues ) {
496- message := fmt .Sprintf (`<a href="%s/commit/%s">%s</a>` , repo .Link (), c .Sha1 , html .EscapeString (c .Message ))
497- if err = CreateRefComment (doer , refRepo , refIssue , message , c .Sha1 ); err != nil {
498- return err
499- }
494+ // FIXME: this kind of condition is all over the code, it should be consolidated in a single place
495+ canclose := perm .IsAdmin () || perm .IsOwner () || perm .CanWrite (UnitTypeIssues ) || refIssue .PosterID == doer .ID
496+ cancomment := canclose || perm .CanRead (UnitTypeIssues )
497+
498+ // Don't proceed if the user can't comment
499+ if ! cancomment {
500+ continue
500501 }
501502
502- // Process closing/reopening keywords
503- if ref .Action != references .XRefActionCloses && ref .Action != references .XRefActionReopens {
503+ message := fmt .Sprintf (`<a href="%s/commit/%s">%s</a>` , repo .Link (), c .Sha1 , html .EscapeString (c .Message ))
504+ if err = CreateRefComment (doer , refRepo , refIssue , message , c .Sha1 ); err != nil {
505+ return err
506+ }
507+
508+ // Only issues can be closed/reopened this way, and user needs the correct permissions
509+ if refIssue .IsPull || ! canclose {
504510 continue
505511 }
506512
507- // Change issue status only if the commit has been pushed to the default branch.
508- // and if the repo is configured to allow only that
509- // FIXME: we should be using Issue.ref if set instead of repo.DefaultBranch
510- if repo .DefaultBranch != branchName && ! repo .CloseIssuesViaCommitInAnyBranch {
513+ // Only process closing/reopening keywords
514+ if ref .Action != references .XRefActionCloses && ref .Action != references .XRefActionReopens {
511515 continue
512516 }
513517
514- // only close issues in another repo if user has push access
515- if perm .IsAdmin () || perm .IsOwner () || perm .CanWrite (UnitTypeCode ) {
516- if err := changeIssueStatus (refRepo , refIssue , doer , ref .Action == references .XRefActionCloses ); err != nil {
517- return err
518+ if ! repo .CloseIssuesViaCommitInAnyBranch {
519+ // If the issue was specified to be in a particular branch, don't allow commits in other branches to close it
520+ if refIssue .Ref != "" {
521+ if branchName != refIssue .Ref {
522+ continue
523+ }
524+ // Otherwise, only process commits to the default branch
525+ } else if branchName != repo .DefaultBranch {
526+ continue
518527 }
519528 }
529+
530+ if err := changeIssueStatus (refRepo , refIssue , doer , ref .Action == references .XRefActionCloses ); err != nil {
531+ return err
532+ }
520533 }
521534 }
522535 return nil
0 commit comments