From dd4a1107ed8977594be48d06aaec2039c983c943 Mon Sep 17 00:00:00 2001 From: Litchi Pi Date: Mon, 16 Dec 2024 13:44:25 +0100 Subject: [PATCH] template: repo: compare: display a warning if the user is not logged in Signed-off-by: Litchi Pi --- options/locale/locale_en-US.ini | 1 + routers/web/repo/compare.go | 1 + templates/repo/diff/compare.tmpl | 4 ++++ tests/integration/compare_test.go | 40 +++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3b413b8f92..f00d738026 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -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 = Sign in 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 diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index e5eab2bffa..03d49fa692 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -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 { diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 612d08ec4f..024577afcc 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -215,6 +215,10 @@ {{ctx.Locale.Tr "repo.archive.title_date" (DateUtils.AbsoluteLong .Repository.ArchivedUnix)}} {{end}} + {{else}} +
+ {{ctx.Locale.Tr "repo.pulls.sign_in_require" .SignInLink}} +
{{end}} {{if $.IsSigned}}
diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index a6f994835b..2d27a430ac 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -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) + }) + }) +}