Skip to content

Commit 38cd9ba

Browse files
kevans91zeripathguillep2k
authored
Allow unauthenticated users to compare (#11240)
* routers: make /compare route available to unauthenticated users Remove some bits of the compare interface if the user isn't signed in. Notably, they don't need to see the "New Pull Request" button box nor the hidden form that would fail to submit due to the POST request continuing to require proper privileges. Follow-up commits will improve the UI a bit around this, removing some "Pull Request" verbiage in favor of "Compare." * 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." * ui: branch list: use the new Compare language when available When viewing the branch listing as an unauthenticated user, you'll get "Pull Request" buttons. use the new "Compare" verbiage instead, which matches GitHub behavior when you can't issue a pull request from the branches. Co-authored-by: zeripath <[email protected]> Co-authored-by: guillep2k <[email protected]>
1 parent 680dfab commit 38cd9ba

File tree

7 files changed

+49
-41
lines changed

7 files changed

+49
-41
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

routers/repo/branch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Branches(ctx *context.Context) {
4646
ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
4747
ctx.Data["IsWriter"] = ctx.Repo.CanWrite(models.UnitTypeCode)
4848
ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
49+
ctx.Data["CanPull"] = ctx.Repo.CanWrite(models.UnitTypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID))
4950
ctx.Data["PageIsViewCode"] = true
5051
ctx.Data["PageIsBranches"] = true
5152

routers/routes/routes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ func RegisterRoutes(m *macaron.Macaron) {
709709
m.Group("/milestone", func() {
710710
m.Get("/:id", repo.MilestoneIssuesAndPulls)
711711
}, reqRepoIssuesOrPullsReader, context.RepoRef())
712+
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
713+
Get(repo.SetDiffViewStyle, repo.CompareDiff).
714+
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
712715
}, context.RepoAssignment(), context.UnitTypes())
713716

714717
// Grouping for those endpoints that do require authentication
@@ -769,9 +772,6 @@ func RegisterRoutes(m *macaron.Macaron) {
769772
m.Post("/:id/:action", repo.ChangeMilestonStatus)
770773
m.Post("/delete", repo.DeleteMilestone)
771774
}, context.RepoMustNotBeArchived(), reqRepoIssuesOrPullsWriter, context.RepoRef())
772-
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
773-
Get(repo.SetDiffViewStyle, repo.CompareDiff).
774-
Post(context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
775775
m.Group("/pull", func() {
776776
m.Post("/:index/target_branch", repo.UpdatePullRequestTarget)
777777
}, context.RepoMustNotBeArchived())

templates/repo/branch/list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@
8181
</a>
8282
{{else if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}}
8383
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
84-
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
84+
<button id="new-pull-request" class="ui compact basic button">{{if $.CanPull}}{{$.i18n.Tr "repo.pulls.compare_changes"}}{{else}}{{$.i18n.Tr "action.compare_branch"}}{{end}}</button>
8585
</a>
8686
{{end}}
8787
{{else if and .LatestPullRequest.HasMerged .MergeMovedOn}}
8888
{{if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}}
8989
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
90-
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
90+
<button id="new-pull-request" class="ui compact basic button">{{if $.CanPull}}{{$.i18n.Tr "repo.pulls.compare_changes"}}{{else}}{{$.i18n.Tr "action.compare_branch"}}{{end}}</button>
9191
</a>
9292
{{end}}
9393
{{else}}

templates/repo/diff/compare.tmpl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
{{if .PageIsComparePull}}
77
<h2 class="ui header">
8-
{{if not .Repository.IsArchived}}
8+
{{if and $.IsSigned (not .Repository.IsArchived)}}
99
{{.i18n.Tr "repo.pulls.compare_changes"}}
1010
<div class="sub header">{{.i18n.Tr "repo.pulls.compare_changes_desc"}}</div>
1111
{{ else }}
@@ -64,22 +64,24 @@
6464
<div class="ui segment">
6565
{{.i18n.Tr "repo.pulls.has_pull_request" $.RepoLink $.RepoRelPath .PullRequest.Index | Safe}}
6666
</div>
67-
{{else}}
68-
{{if not .Repository.IsArchived}}
69-
<div class="ui info message show-form-container">
70-
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
71-
</div>
72-
{{ else }}
73-
<div class="ui warning message">
74-
{{.i18n.Tr "repo.archive.title"}}
75-
</div>
76-
{{ end }}
77-
<div class="pullrequest-form" style="display: none">
78-
{{template "repo/issue/new_form" .}}
79-
</div>
80-
{{template "repo/commits_table" .}}
81-
{{template "repo/diff/box" .}}
82-
{{end}}
67+
{{else}}
68+
{{if and $.IsSigned (not .Repository.IsArchived)}}
69+
<div class="ui info message show-form-container">
70+
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
71+
</div>
72+
{{else if .Repository.IsArchived}}
73+
<div class="ui warning message">
74+
{{.i18n.Tr "repo.archive.title"}}
75+
</div>
76+
{{end}}
77+
{{if $.IsSigned}}
78+
<div class="pullrequest-form" style="display: none">
79+
{{template "repo/issue/new_form" .}}
80+
</div>
81+
{{end}}
82+
{{template "repo/commits_table" .}}
83+
{{template "repo/diff/box" .}}
84+
{{end}}
8385
{{else}}
8486
{{template "repo/commits_table" .}}
8587
{{template "repo/diff/box" .}}

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)