loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[GITEA] pulls: "Edit File" button in "Files Changed" tab

Closes #1894.
Gitea issue: https://github.com/go-gitea/gitea/issues/23848

(cherry picked from commit 79c75164ca70937261b1d9a68420ebfdbdcfa4d4)
(cherry picked from commit 58c76aad8f624d7701e3fa6c12264328962cdf58)
(cherry picked from commit 5bdb3c6c53527da23ba76a8289ca6a81c6fcecdf)
(cherry picked from commit 94e954ce2248f14082f0c3071cc076c118c4a791)
(cherry picked from commit 1388e7c7bef7a34018b993c24b34e053849eb93a)
(cherry picked from commit 6a234abff532bfc8806e0cccf8c2d1d8c3e90c24)

authored by

Antonin Delpeuch and committed by
Earl Warren
f3b29813 5c0894a5

+43 -8
+12
routers/web/repo/pull.go
··· 967 967 return 968 968 } 969 969 970 + // determine if the user viewing the pull request can edit the head branch 971 + if ctx.Doer != nil && pull.HeadRepo != nil && !pull.HasMerged { 972 + headRepoPerm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer) 973 + if err != nil { 974 + ctx.ServerError("GetUserRepoPermission", err) 975 + return 976 + } 977 + ctx.Data["HeadBranchIsEditable"] = pull.HeadRepo.CanEnableEditor() && issues_model.CanMaintainerWriteToBranch(ctx, headRepoPerm, pull.HeadBranch, ctx.Doer) 978 + ctx.Data["SourceRepoLink"] = pull.HeadRepo.Link() 979 + ctx.Data["HeadBranch"] = pull.HeadBranch 980 + } 981 + 970 982 if ctx.IsSigned && ctx.Doer != nil { 971 983 if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil { 972 984 ctx.ServerError("CanMarkConversation", err)
+3
templates/repo/diff/box.tmpl
··· 166 166 <a class="ui basic tiny button" rel="nofollow" href="{{$.BeforeSourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a> 167 167 {{else}} 168 168 <a class="ui basic tiny button" rel="nofollow" href="{{$.SourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a> 169 + {{if and $.HeadBranchIsEditable (not $file.IsLFSFile)}} 170 + <a class="ui basic tiny button" rel="nofollow" href="{{$.SourceRepoLink}}/_edit/{{PathEscapeSegments $.HeadBranch}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.editor.edit_this_file"}}</a> 171 + {{end}} 169 172 {{end}} 170 173 {{end}} 171 174 {{if $isReviewFile}}
+1 -1
tests/integration/forgejo_git_test.go
··· 134 134 doerCtx.ExpectedCode = http.StatusCreated 135 135 t.Run("AutoMergePR", doAPIAutoMergePullRequest(doerCtx, ctx.Username, ctx.Reponame, pr.Index)) 136 136 // Ensure the PR page works 137 - t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr)) 137 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr, true)) 138 138 } 139 139 }
+27 -7
tests/integration/git_test.go
··· 455 455 assert.NoError(t, err) 456 456 }) 457 457 458 - // Ensure the PR page works 459 - t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 458 + // Ensure the PR page works. 459 + // For the base repository owner, the PR is not editable (maintainer edits are not enabled): 460 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr, false)) 461 + // For the head repository owner, the PR is editable: 462 + headSession := loginUser(t, "user2") 463 + headToken := getTokenForLoggedInUser(t, headSession, auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopeReadUser) 464 + headCtx := APITestContext{ 465 + Session: headSession, 466 + Token: headToken, 467 + Username: baseCtx.Username, 468 + Reponame: baseCtx.Reponame, 469 + } 470 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(headCtx, pr, true)) 460 471 461 472 // Then get the diff string 462 473 var diffHash string ··· 470 481 471 482 // Now: Merge the PR & make sure that doesn't break the PR page or change its diff 472 483 t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index)) 473 - t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 484 + // for both users the PR is still visible but not editable anymore after it was merged 485 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr, false)) 486 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(headCtx, pr, false)) 474 487 t.Run("CheckPR", func(t *testing.T) { 475 488 oldMergeBase := pr.MergeBase 476 489 pr2, err := doAPIGetPullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index)(t) ··· 481 494 482 495 // Then: Delete the head branch & make sure that doesn't break the PR page or change its diff 483 496 t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch)) 484 - t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 497 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr, false)) 485 498 t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength)) 486 499 487 500 // Delete the head repository & make sure that doesn't break the PR page or change its diff 488 501 t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx)) 489 - t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 502 + t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr, false)) 490 503 t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength)) 491 504 } 492 505 } ··· 520 533 } 521 534 } 522 535 523 - func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.T) { 536 + func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest, editable bool) func(t *testing.T) { 524 537 return func(t *testing.T) { 525 538 req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index)) 526 539 ctx.Session.MakeRequest(t, req, http.StatusOK) 527 540 req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/files", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index)) 528 - ctx.Session.MakeRequest(t, req, http.StatusOK) 541 + resp := ctx.Session.MakeRequest(t, req, http.StatusOK) 542 + doc := NewHTMLParser(t, resp.Body) 543 + editButtonCount := doc.doc.Find("div.diff-file-header-actions a[href*='/_edit/']").Length() 544 + if editable { 545 + assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none") 546 + } else { 547 + assert.Equal(t, 0, editButtonCount, "Expected not to find any buttons to edit files in PR diff view but there were some") 548 + } 529 549 req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/commits", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index)) 530 550 ctx.Session.MakeRequest(t, req, http.StatusOK) 531 551 }