template: repo: compare: display a warning if the user is not logged in

Signed-off-by: Litchi Pi <litchi.pi@proton.me>
(cherry picked from commit dd4a1107ed)
This commit is contained in:
Litchi Pi 2024-12-16 13:44:25 +01:00 committed by forgejo-backport-action
parent 424f85304e
commit 5a0c79e6b4
4 changed files with 46 additions and 0 deletions

View file

@ -1845,6 +1845,7 @@ pulls.new = New pull request
pulls.view = View pull request
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
pulls.compare_changes = New pull request
pulls.sign_in_require = <a href="%s">Sign in</a> to create a new pull request.
pulls.allow_edits_from_maintainers = Allow edits from maintainers
pulls.allow_edits_from_maintainers_desc = Users with write access to the base branch can also push to this branch
pulls.allow_edits_from_maintainers_err = Updating failed

View file

@ -51,6 +51,7 @@ const (
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
ctx.Data["BeforeCommit"] = before
ctx.Data["HeadCommit"] = head
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + url.QueryEscape(ctx.Data["Link"].(string))
ctx.Data["GetBlobByPathForCommit"] = func(commit *git.Commit, path string) *git.Blob {
if commit == nil {

View file

@ -215,6 +215,10 @@
{{ctx.Locale.Tr "repo.archive.title_date" (DateUtils.AbsoluteLong .Repository.ArchivedUnix)}}
{{end}}
</div>
{{else}}
<div class="ui warning message tw-mb-4">
{{ctx.Locale.Tr "repo.pulls.sign_in_require" .SignInLink}}
</div>
{{end}}
{{if $.IsSigned}}
<div class="pullrequest-form {{if not .Flash}}tw-hidden{{end}}">

View file

@ -291,3 +291,43 @@ func TestCompareCodeExpand(t *testing.T) {
})
})
}
func TestCompareSignedIn(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// Setup the test with a connected user
session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
testCreateBranch(t, session, "user1", "repo1", "branch/master", "recent-push", http.StatusSeeOther)
testEditFile(t, session, "user1", "repo1", "recent-push", "README.md", "Hello recently!\n")
newPrSelector := "button.ui.button.primary.show-form"
t.Run("PR creation button displayed if logged in", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user1/repo1/compare/master...recent-push")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
// Check that the "Sign in" button doesn't show up
htmlDoc.AssertElement(t, "a[href='/user/login?redirect_to=%2Fuser1%2Frepo1%2Fcompare%2Fmaster...recent-push']", false)
// Check that the "New pull request" button shows up
htmlDoc.AssertElement(t, newPrSelector, true)
})
t.Run("no PR creation button but display warning", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user1/repo1/compare/master...recent-push")
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
// Check that the "Sign in" button shows up
htmlDoc.AssertElement(t, "a[href='/user/login?redirect_to=%2Fuser1%2Frepo1%2Fcompare%2Fmaster...recent-push']", true)
// Check that the "New pull request" button doesn't show up
htmlDoc.AssertElement(t, newPrSelector, false)
})
})
}