Skip to content

Commit a884256

Browse files
committed
small improvements
1 parent a1b9228 commit a884256

File tree

1 file changed

+161
-154
lines changed

1 file changed

+161
-154
lines changed

routers/web/repo/issue_view.go

Lines changed: 161 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ func prepareIssueViewSidebarDependency(ctx *context.Context, issue *issues_model
443443
}
444444

445445
func preparePullViewSigning(ctx *context.Context, issue *issues_model.Issue) {
446+
if !issue.IsPull {
447+
return
448+
}
446449
pull := issue.PullRequest
447450
ctx.Data["WillSign"] = false
448451
if ctx.Doer != nil {
@@ -478,32 +481,34 @@ func prepareIssueViewSidebarWatch(ctx *context.Context, issue *issues_model.Issu
478481
}
479482

480483
func prepareIssueViewSidebarTimeTracker(ctx *context.Context, issue *issues_model.Issue) {
481-
if ctx.Repo.Repository.IsTimetrackerEnabled(ctx) {
482-
if ctx.IsSigned {
483-
// Deal with the stopwatch
484-
ctx.Data["IsStopwatchRunning"] = issues_model.StopwatchExists(ctx, ctx.Doer.ID, issue.ID)
485-
if !ctx.Data["IsStopwatchRunning"].(bool) {
486-
exists, _, swIssue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
487-
if err != nil {
488-
ctx.ServerError("HasUserStopwatch", err)
489-
return
490-
}
491-
ctx.Data["HasUserStopwatch"] = exists
492-
if exists {
493-
// Add warning if the user has already a stopwatch
494-
// Add link to the issue of the already running stopwatch
495-
ctx.Data["OtherStopwatchURL"] = swIssue.Link()
496-
}
484+
if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) {
485+
return
486+
}
487+
488+
if ctx.IsSigned {
489+
// Deal with the stopwatch
490+
ctx.Data["IsStopwatchRunning"] = issues_model.StopwatchExists(ctx, ctx.Doer.ID, issue.ID)
491+
if !ctx.Data["IsStopwatchRunning"].(bool) {
492+
exists, _, swIssue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
493+
if err != nil {
494+
ctx.ServerError("HasUserStopwatch", err)
495+
return
496+
}
497+
ctx.Data["HasUserStopwatch"] = exists
498+
if exists {
499+
// Add warning if the user has already a stopwatch
500+
// Add link to the issue of the already running stopwatch
501+
ctx.Data["OtherStopwatchURL"] = swIssue.Link()
497502
}
498-
ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer)
499-
} else {
500-
ctx.Data["CanUseTimetracker"] = false
501-
}
502-
var err error
503-
if ctx.Data["WorkingUsers"], err = issues_model.TotalTimesForEachUser(ctx, &issues_model.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
504-
ctx.ServerError("TotalTimesForEachUser", err)
505-
return
506503
}
504+
ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(ctx, issue, ctx.Doer)
505+
} else {
506+
ctx.Data["CanUseTimetracker"] = false
507+
}
508+
var err error
509+
if ctx.Data["WorkingUsers"], err = issues_model.TotalTimesForEachUser(ctx, &issues_model.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
510+
ctx.ServerError("TotalTimesForEachUser", err)
511+
return
507512
}
508513
}
509514

@@ -763,165 +768,167 @@ func prepareIssueViewCommentsAndSidebarParticipants(ctx *context.Context, issue
763768

764769
func preparePullViewReviewAndMerge(ctx *context.Context, issue *issues_model.Issue) {
765770
getBranchData(ctx, issue)
766-
if issue.IsPull {
767-
pull := issue.PullRequest
768-
pull.Issue = issue
769-
canDelete := false
770-
allowMerge := false
771-
canWriteToHeadRepo := false
772-
773-
if ctx.IsSigned {
774-
if err := pull.LoadHeadRepo(ctx); err != nil {
775-
log.Error("LoadHeadRepo: %v", err)
776-
} else if pull.HeadRepo != nil {
777-
perm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
778-
if err != nil {
779-
ctx.ServerError("GetUserRepoPermission", err)
780-
return
781-
}
782-
if perm.CanWrite(unit.TypeCode) {
783-
// Check if branch is not protected
784-
if pull.HeadBranch != pull.HeadRepo.DefaultBranch {
785-
if protected, err := git_model.IsBranchProtected(ctx, pull.HeadRepo.ID, pull.HeadBranch); err != nil {
786-
log.Error("IsProtectedBranch: %v", err)
787-
} else if !protected {
788-
canDelete = true
789-
ctx.Data["DeleteBranchLink"] = issue.Link() + "/cleanup"
790-
}
791-
}
792-
canWriteToHeadRepo = true
793-
}
794-
}
771+
if !issue.IsPull {
772+
return
773+
}
795774

796-
if err := pull.LoadBaseRepo(ctx); err != nil {
797-
log.Error("LoadBaseRepo: %v", err)
798-
}
799-
perm, err := access_model.GetUserRepoPermission(ctx, pull.BaseRepo, ctx.Doer)
775+
pull := issue.PullRequest
776+
pull.Issue = issue
777+
canDelete := false
778+
allowMerge := false
779+
canWriteToHeadRepo := false
780+
781+
if ctx.IsSigned {
782+
if err := pull.LoadHeadRepo(ctx); err != nil {
783+
log.Error("LoadHeadRepo: %v", err)
784+
} else if pull.HeadRepo != nil {
785+
perm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
800786
if err != nil {
801787
ctx.ServerError("GetUserRepoPermission", err)
802788
return
803789
}
804-
if !canWriteToHeadRepo { // maintainers maybe allowed to push to head repo even if they can't write to it
805-
canWriteToHeadRepo = pull.AllowMaintainerEdit && perm.CanWrite(unit.TypeCode)
806-
}
807-
allowMerge, err = pull_service.IsUserAllowedToMerge(ctx, pull, perm, ctx.Doer)
808-
if err != nil {
809-
ctx.ServerError("IsUserAllowedToMerge", err)
810-
return
811-
}
812-
813-
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
814-
ctx.ServerError("CanMarkConversation", err)
815-
return
790+
if perm.CanWrite(unit.TypeCode) {
791+
// Check if branch is not protected
792+
if pull.HeadBranch != pull.HeadRepo.DefaultBranch {
793+
if protected, err := git_model.IsBranchProtected(ctx, pull.HeadRepo.ID, pull.HeadBranch); err != nil {
794+
log.Error("IsProtectedBranch: %v", err)
795+
} else if !protected {
796+
canDelete = true
797+
ctx.Data["DeleteBranchLink"] = issue.Link() + "/cleanup"
798+
}
799+
}
800+
canWriteToHeadRepo = true
816801
}
817802
}
818803

819-
ctx.Data["CanWriteToHeadRepo"] = canWriteToHeadRepo
820-
ctx.Data["ShowMergeInstructions"] = canWriteToHeadRepo
821-
ctx.Data["AllowMerge"] = allowMerge
822-
823-
prUnit, err := issue.Repo.GetUnit(ctx, unit.TypePullRequests)
804+
if err := pull.LoadBaseRepo(ctx); err != nil {
805+
log.Error("LoadBaseRepo: %v", err)
806+
}
807+
perm, err := access_model.GetUserRepoPermission(ctx, pull.BaseRepo, ctx.Doer)
824808
if err != nil {
825-
ctx.ServerError("GetUnit", err)
809+
ctx.ServerError("GetUserRepoPermission", err)
826810
return
827811
}
828-
prConfig := prUnit.PullRequestsConfig()
829-
830-
ctx.Data["AutodetectManualMerge"] = prConfig.AutodetectManualMerge
831-
832-
var mergeStyle repo_model.MergeStyle
833-
// Check correct values and select default
834-
if ms, ok := ctx.Data["MergeStyle"].(repo_model.MergeStyle); !ok ||
835-
!prConfig.IsMergeStyleAllowed(ms) {
836-
defaultMergeStyle := prConfig.GetDefaultMergeStyle()
837-
if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok {
838-
mergeStyle = defaultMergeStyle
839-
} else if prConfig.AllowMerge {
840-
mergeStyle = repo_model.MergeStyleMerge
841-
} else if prConfig.AllowRebase {
842-
mergeStyle = repo_model.MergeStyleRebase
843-
} else if prConfig.AllowRebaseMerge {
844-
mergeStyle = repo_model.MergeStyleRebaseMerge
845-
} else if prConfig.AllowSquash {
846-
mergeStyle = repo_model.MergeStyleSquash
847-
} else if prConfig.AllowFastForwardOnly {
848-
mergeStyle = repo_model.MergeStyleFastForwardOnly
849-
} else if prConfig.AllowManualMerge {
850-
mergeStyle = repo_model.MergeStyleManuallyMerged
851-
}
812+
if !canWriteToHeadRepo { // maintainers maybe allowed to push to head repo even if they can't write to it
813+
canWriteToHeadRepo = pull.AllowMaintainerEdit && perm.CanWrite(unit.TypeCode)
852814
}
853-
854-
ctx.Data["MergeStyle"] = mergeStyle
855-
856-
defaultMergeMessage, defaultMergeBody, err := pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pull, mergeStyle)
815+
allowMerge, err = pull_service.IsUserAllowedToMerge(ctx, pull, perm, ctx.Doer)
857816
if err != nil {
858-
ctx.ServerError("GetDefaultMergeMessage", err)
817+
ctx.ServerError("IsUserAllowedToMerge", err)
859818
return
860819
}
861-
ctx.Data["DefaultMergeMessage"] = defaultMergeMessage
862-
ctx.Data["DefaultMergeBody"] = defaultMergeBody
863820

864-
defaultSquashMergeMessage, defaultSquashMergeBody, err := pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pull, repo_model.MergeStyleSquash)
865-
if err != nil {
866-
ctx.ServerError("GetDefaultSquashMergeMessage", err)
821+
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
822+
ctx.ServerError("CanMarkConversation", err)
867823
return
868824
}
869-
ctx.Data["DefaultSquashMergeMessage"] = defaultSquashMergeMessage
870-
ctx.Data["DefaultSquashMergeBody"] = defaultSquashMergeBody
825+
}
871826

872-
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch)
873-
if err != nil {
874-
ctx.ServerError("LoadProtectedBranch", err)
875-
return
876-
}
827+
ctx.Data["CanWriteToHeadRepo"] = canWriteToHeadRepo
828+
ctx.Data["ShowMergeInstructions"] = canWriteToHeadRepo
829+
ctx.Data["AllowMerge"] = allowMerge
877830

878-
if pb != nil {
879-
pb.Repo = pull.BaseRepo
880-
ctx.Data["ProtectedBranch"] = pb
881-
ctx.Data["IsBlockedByApprovals"] = !issues_model.HasEnoughApprovals(ctx, pb, pull)
882-
ctx.Data["IsBlockedByRejection"] = issues_model.MergeBlockedByRejectedReview(ctx, pb, pull)
883-
ctx.Data["IsBlockedByOfficialReviewRequests"] = issues_model.MergeBlockedByOfficialReviewRequests(ctx, pb, pull)
884-
ctx.Data["IsBlockedByOutdatedBranch"] = issues_model.MergeBlockedByOutdatedBranch(pb, pull)
885-
ctx.Data["GrantedApprovals"] = issues_model.GetGrantedApprovalsCount(ctx, pb, pull)
886-
ctx.Data["RequireSigned"] = pb.RequireSignedCommits
887-
ctx.Data["ChangedProtectedFiles"] = pull.ChangedProtectedFiles
888-
ctx.Data["IsBlockedByChangedProtectedFiles"] = len(pull.ChangedProtectedFiles) != 0
889-
ctx.Data["ChangedProtectedFilesNum"] = len(pull.ChangedProtectedFiles)
890-
ctx.Data["RequireApprovalsWhitelist"] = pb.EnableApprovalsWhitelist
891-
}
831+
prUnit, err := issue.Repo.GetUnit(ctx, unit.TypePullRequests)
832+
if err != nil {
833+
ctx.ServerError("GetUnit", err)
834+
return
835+
}
836+
prConfig := prUnit.PullRequestsConfig()
892837

893-
preparePullViewSigning(ctx, issue)
894-
if ctx.Written() {
895-
return
896-
}
838+
ctx.Data["AutodetectManualMerge"] = prConfig.AutodetectManualMerge
897839

898-
preparePullViewDeleteBranch(ctx, issue, canDelete)
899-
if ctx.Written() {
900-
return
840+
var mergeStyle repo_model.MergeStyle
841+
// Check correct values and select default
842+
if ms, ok := ctx.Data["MergeStyle"].(repo_model.MergeStyle); !ok ||
843+
!prConfig.IsMergeStyleAllowed(ms) {
844+
defaultMergeStyle := prConfig.GetDefaultMergeStyle()
845+
if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok {
846+
mergeStyle = defaultMergeStyle
847+
} else if prConfig.AllowMerge {
848+
mergeStyle = repo_model.MergeStyleMerge
849+
} else if prConfig.AllowRebase {
850+
mergeStyle = repo_model.MergeStyleRebase
851+
} else if prConfig.AllowRebaseMerge {
852+
mergeStyle = repo_model.MergeStyleRebaseMerge
853+
} else if prConfig.AllowSquash {
854+
mergeStyle = repo_model.MergeStyleSquash
855+
} else if prConfig.AllowFastForwardOnly {
856+
mergeStyle = repo_model.MergeStyleFastForwardOnly
857+
} else if prConfig.AllowManualMerge {
858+
mergeStyle = repo_model.MergeStyleManuallyMerged
901859
}
860+
}
902861

903-
stillCanManualMerge := func() bool {
904-
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
905-
return false
906-
}
907-
if pull.CanAutoMerge() || pull.IsWorkInProgress(ctx) || pull.IsChecking() {
908-
return false
909-
}
910-
if allowMerge && prConfig.AllowManualMerge {
911-
return true
912-
}
862+
ctx.Data["MergeStyle"] = mergeStyle
863+
864+
defaultMergeMessage, defaultMergeBody, err := pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pull, mergeStyle)
865+
if err != nil {
866+
ctx.ServerError("GetDefaultMergeMessage", err)
867+
return
868+
}
869+
ctx.Data["DefaultMergeMessage"] = defaultMergeMessage
870+
ctx.Data["DefaultMergeBody"] = defaultMergeBody
871+
872+
defaultSquashMergeMessage, defaultSquashMergeBody, err := pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pull, repo_model.MergeStyleSquash)
873+
if err != nil {
874+
ctx.ServerError("GetDefaultSquashMergeMessage", err)
875+
return
876+
}
877+
ctx.Data["DefaultSquashMergeMessage"] = defaultSquashMergeMessage
878+
ctx.Data["DefaultSquashMergeBody"] = defaultSquashMergeBody
879+
880+
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch)
881+
if err != nil {
882+
ctx.ServerError("LoadProtectedBranch", err)
883+
return
884+
}
885+
886+
if pb != nil {
887+
pb.Repo = pull.BaseRepo
888+
ctx.Data["ProtectedBranch"] = pb
889+
ctx.Data["IsBlockedByApprovals"] = !issues_model.HasEnoughApprovals(ctx, pb, pull)
890+
ctx.Data["IsBlockedByRejection"] = issues_model.MergeBlockedByRejectedReview(ctx, pb, pull)
891+
ctx.Data["IsBlockedByOfficialReviewRequests"] = issues_model.MergeBlockedByOfficialReviewRequests(ctx, pb, pull)
892+
ctx.Data["IsBlockedByOutdatedBranch"] = issues_model.MergeBlockedByOutdatedBranch(pb, pull)
893+
ctx.Data["GrantedApprovals"] = issues_model.GetGrantedApprovalsCount(ctx, pb, pull)
894+
ctx.Data["RequireSigned"] = pb.RequireSignedCommits
895+
ctx.Data["ChangedProtectedFiles"] = pull.ChangedProtectedFiles
896+
ctx.Data["IsBlockedByChangedProtectedFiles"] = len(pull.ChangedProtectedFiles) != 0
897+
ctx.Data["ChangedProtectedFilesNum"] = len(pull.ChangedProtectedFiles)
898+
ctx.Data["RequireApprovalsWhitelist"] = pb.EnableApprovalsWhitelist
899+
}
900+
901+
preparePullViewSigning(ctx, issue)
902+
if ctx.Written() {
903+
return
904+
}
905+
906+
preparePullViewDeleteBranch(ctx, issue, canDelete)
907+
if ctx.Written() {
908+
return
909+
}
913910

911+
stillCanManualMerge := func() bool {
912+
if pull.HasMerged || issue.IsClosed || !ctx.IsSigned {
914913
return false
915914
}
915+
if pull.CanAutoMerge() || pull.IsWorkInProgress(ctx) || pull.IsChecking() {
916+
return false
917+
}
918+
if allowMerge && prConfig.AllowManualMerge {
919+
return true
920+
}
916921

917-
ctx.Data["StillCanManualMerge"] = stillCanManualMerge()
922+
return false
923+
}
918924

919-
// Check if there is a pending pr merge
920-
ctx.Data["HasPendingPullRequestMerge"], ctx.Data["PendingPullRequestMerge"], err = pull_model.GetScheduledMergeByPullID(ctx, pull.ID)
921-
if err != nil {
922-
ctx.ServerError("GetScheduledMergeByPullID", err)
923-
return
924-
}
925+
ctx.Data["StillCanManualMerge"] = stillCanManualMerge()
926+
927+
// Check if there is a pending pr merge
928+
ctx.Data["HasPendingPullRequestMerge"], ctx.Data["PendingPullRequestMerge"], err = pull_model.GetScheduledMergeByPullID(ctx, pull.ID)
929+
if err != nil {
930+
ctx.ServerError("GetScheduledMergeByPullID", err)
931+
return
925932
}
926933
}
927934

0 commit comments

Comments
 (0)