Skip to content

Commit 565ef1f

Browse files
committed
ui: home: show "compare" button for unauthenticated users
This change requires pulling in the BaseRepo unconditionally and recording if the pull request is in-fact not allowed (.PullRequestCtx.Allowed). If the user isn't allowed to create a pull request, either because this isn't a fork or same-fork branch PRs aren't allowed, then we'll name the button "Compare" instead of "Pull Request."
1 parent 69291c7 commit 565ef1f

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

modules/context/repo.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -547,23 +547,27 @@ func RepoAssignment() macaron.Handler {
547547
ctx.Data["CommitID"] = ctx.Repo.CommitID
548548

549549
// People who have push access or have forked repository can propose a new pull request.
550-
if ctx.Repo.CanWrite(models.UnitTypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)) {
551-
// Pull request is allowed if this is a fork repository
552-
// and base repository accepts pull requests.
553-
if repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls() {
554-
ctx.Data["BaseRepo"] = repo.BaseRepo
555-
ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
556-
ctx.Repo.PullRequest.Allowed = true
557-
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
558-
} else if repo.AllowsPulls() {
559-
// Or, this is repository accepts pull requests between branches.
560-
ctx.Data["BaseRepo"] = repo
561-
ctx.Repo.PullRequest.BaseRepo = repo
562-
ctx.Repo.PullRequest.Allowed = true
563-
ctx.Repo.PullRequest.SameRepo = true
564-
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
565-
}
566-
}
550+
canPush := ctx.Repo.CanWrite(models.UnitTypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID))
551+
canCompare := false
552+
553+
// Pull request is allowed if this is a fork repository
554+
// and base repository accepts pull requests.
555+
if repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls() {
556+
canCompare = true
557+
ctx.Data["BaseRepo"] = repo.BaseRepo
558+
ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
559+
ctx.Repo.PullRequest.Allowed = canPush
560+
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
561+
} else if repo.AllowsPulls() {
562+
// Or, this is repository accepts pull requests between branches.
563+
canCompare = true
564+
ctx.Data["BaseRepo"] = repo
565+
ctx.Repo.PullRequest.BaseRepo = repo
566+
ctx.Repo.PullRequest.Allowed = canPush
567+
ctx.Repo.PullRequest.SameRepo = true
568+
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
569+
}
570+
ctx.Data["CanCompareOrPull"] = canCompare
567571
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
568572

569573
if ctx.Query("go-get") == "1" {

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,7 @@ transfer_repo = transferred repository <code>%s</code> to <a href="%s">%s</a>
22702270
push_tag = pushed tag <a href="%s/src/tag/%s">%[2]s</a> to <a href="%[1]s">%[3]s</a>
22712271
delete_tag = deleted tag %[2]s from <a href="%[1]s">%[3]s</a>
22722272
delete_branch = deleted branch %[2]s from <a href="%[1]s">%[3]s</a>
2273+
compare_branch = Compare
22732274
compare_commits = Compare %d commits
22742275
compare_commits_general = Compare commits
22752276
mirror_sync_push = synced commits to <a href="%[1]s/src/%[2]s">%[3]s</a> at <a href="%[1]s">%[4]s</a> from mirror

templates/repo/home.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@
6262
{{ $l := Subtract $n 1}}
6363
<!-- If home page, show new PR. If not, show breadcrumb -->
6464
{{if eq $n 0}}
65-
{{if and .PullRequestCtx.Allowed .IsViewBranch (not .Repository.IsArchived)}}
65+
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
6666
<div class="fitted item">
6767
<a href="{{.BaseRepo.Link}}/compare/{{.BaseRepo.DefaultBranch | EscapePound}}...{{if ne .Repository.Owner.Name .BaseRepo.Owner.Name}}{{.Repository.Owner.Name}}:{{end}}{{.BranchName | EscapePound}}">
68-
<button id="new-pull-request" class="ui compact basic button">{{.i18n.Tr "repo.pulls.compare_changes"}}</button>
68+
<button id="new-pull-request" class="ui compact basic button">{{if .PullRequestCtx.Allowed}}{{.i18n.Tr "repo.pulls.compare_changes"}}{{else}}{{.i18n.Tr "action.compare_branch"}}{{end}}</button>
6969
</a>
7070
</div>
7171
{{end}}

0 commit comments

Comments
 (0)