Skip to content

Commit 5a0c79e

Browse files
litchipiforgejo-backport-action
authored andcommitted
template: repo: compare: display a warning if the user is not logged in
Signed-off-by: Litchi Pi <[email protected]> (cherry picked from commit dd4a110)
1 parent 424f853 commit 5a0c79e

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,7 @@ pulls.new = New pull request
18451845
pulls.view = View pull request
18461846
pulls.edit.already_changed = Unable to save changes to the pull request. It appears the content has already been changed by another user. Please refresh the page and try editing again to avoid overwriting their changes
18471847
pulls.compare_changes = New pull request
1848+
pulls.sign_in_require = <a href="%s">Sign in</a> to create a new pull request.
18481849
pulls.allow_edits_from_maintainers = Allow edits from maintainers
18491850
pulls.allow_edits_from_maintainers_desc = Users with write access to the base branch can also push to this branch
18501851
pulls.allow_edits_from_maintainers_err = Updating failed

routers/web/repo/compare.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
5252
ctx.Data["BeforeCommit"] = before
5353
ctx.Data["HeadCommit"] = head
54+
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + url.QueryEscape(ctx.Data["Link"].(string))
5455

5556
ctx.Data["GetBlobByPathForCommit"] = func(commit *git.Commit, path string) *git.Blob {
5657
if commit == nil {

templates/repo/diff/compare.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@
215215
{{ctx.Locale.Tr "repo.archive.title_date" (DateUtils.AbsoluteLong .Repository.ArchivedUnix)}}
216216
{{end}}
217217
</div>
218+
{{else}}
219+
<div class="ui warning message tw-mb-4">
220+
{{ctx.Locale.Tr "repo.pulls.sign_in_require" .SignInLink}}
221+
</div>
218222
{{end}}
219223
{{if $.IsSigned}}
220224
<div class="pullrequest-form {{if not .Flash}}tw-hidden{{end}}">

tests/integration/compare_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,43 @@ func TestCompareCodeExpand(t *testing.T) {
291291
})
292292
})
293293
}
294+
295+
func TestCompareSignedIn(t *testing.T) {
296+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
297+
// Setup the test with a connected user
298+
session := loginUser(t, "user1")
299+
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
300+
testCreateBranch(t, session, "user1", "repo1", "branch/master", "recent-push", http.StatusSeeOther)
301+
testEditFile(t, session, "user1", "repo1", "recent-push", "README.md", "Hello recently!\n")
302+
303+
newPrSelector := "button.ui.button.primary.show-form"
304+
305+
t.Run("PR creation button displayed if logged in", func(t *testing.T) {
306+
defer tests.PrintCurrentTest(t)()
307+
308+
req := NewRequest(t, "GET", "/user1/repo1/compare/master...recent-push")
309+
resp := session.MakeRequest(t, req, http.StatusOK)
310+
htmlDoc := NewHTMLParser(t, resp.Body)
311+
312+
// Check that the "Sign in" button doesn't show up
313+
htmlDoc.AssertElement(t, "a[href='/user/login?redirect_to=%2Fuser1%2Frepo1%2Fcompare%2Fmaster...recent-push']", false)
314+
315+
// Check that the "New pull request" button shows up
316+
htmlDoc.AssertElement(t, newPrSelector, true)
317+
})
318+
319+
t.Run("no PR creation button but display warning", func(t *testing.T) {
320+
defer tests.PrintCurrentTest(t)()
321+
322+
req := NewRequest(t, "GET", "/user1/repo1/compare/master...recent-push")
323+
resp := MakeRequest(t, req, http.StatusOK)
324+
htmlDoc := NewHTMLParser(t, resp.Body)
325+
326+
// Check that the "Sign in" button shows up
327+
htmlDoc.AssertElement(t, "a[href='/user/login?redirect_to=%2Fuser1%2Frepo1%2Fcompare%2Fmaster...recent-push']", true)
328+
329+
// Check that the "New pull request" button doesn't show up
330+
htmlDoc.AssertElement(t, newPrSelector, false)
331+
})
332+
})
333+
}

0 commit comments

Comments
 (0)