Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put an edit file button on pull request files to allow a quick operation #29697

Merged
merged 30 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bf1e779
Put an edit file button on pull request files to allow a quick operation
lunny Mar 9, 2024
470f893
Remove unnecessary test code
lunny Mar 9, 2024
4298a7a
Return back to the viewed file of pull request files tab after edit t…
lunny Mar 10, 2024
c49b896
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 10, 2024
f9db1ed
Some improvements
lunny Mar 10, 2024
ec6ab8d
Update pull request head sync to fix the redirecto problem and do som…
lunny Mar 11, 2024
2d75787
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 11, 2024
06fb88b
Use redirect to
lunny Mar 11, 2024
21a8260
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 11, 2024
a9ce74b
Fix test
lunny Mar 11, 2024
87f9389
Fix test
lunny Mar 11, 2024
7cf364e
Fix bug
lunny Mar 12, 2024
efcb8e2
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 12, 2024
22130ef
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 14, 2024
afe9aaf
Follow wxiaoguang's suggestion
lunny Mar 14, 2024
6a0064b
Update routers/web/repo/editor.go
lunny Mar 14, 2024
9c89597
Fix lint error and Use util.PathEscapeSegments instead of url.PathEscape
lunny Mar 14, 2024
c0dabd9
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 14, 2024
4bd286e
Add indents and add editor mode
lunny Mar 14, 2024
3f616b6
Remove unused code
lunny Mar 14, 2024
de2c535
Change cancel button link of editor page when edit from pull request …
lunny Mar 14, 2024
00e41d9
Apply suggestions from code review
lunny Mar 14, 2024
b0efe00
Add comment for the error handling when writting the pull head file w…
lunny Mar 16, 2024
5f15e70
Merge branch 'main' into lunny/edit_on_pr
silverwind Mar 21, 2024
9c1c4a5
Update routers/web/repo/editor.go
wxiaoguang Mar 22, 2024
00954b2
Merge branch 'main' into lunny/edit_on_pr
lunny Mar 26, 2024
e597548
fix merge conflicts
lunny Mar 26, 2024
5656851
Merge branch 'lunny/edit_on_pr' of github.com:lunny/gitea into lunny/…
lunny Mar 26, 2024
6907cdf
Update tests/integration/pull_compare_test.go
silverwind Mar 26, 2024
7cbbeeb
Merge branch 'main' into lunny/edit_on_pr
GiteaBot Mar 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"io"
"net/http"
"path"
"regexp"
"strings"
"time"

"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
Expand Down Expand Up @@ -80,6 +82,21 @@ func redirectForCommitChoice(ctx *context.Context, commitChoice, newBranchName,
}
}

prIndex := ctx.FormInt64("back_to_pr")
if prIndex > 0 {
// Redirect to the PR's files tab
link := ctx.Repo.RepoLink + fmt.Sprintf("/pulls/%d/files?refresh=%d", prIndex, time.Now().Unix())
diffHash := ctx.FormString("back_to_diff_hash")
lunny marked this conversation as resolved.
Show resolved Hide resolved
r := regexp.MustCompile(`^[0-9a-f]{40}$`)
if len(diffHash) == 40 && r.MatchString(diffHash) {
link += "#diff-" + diffHash
}
// FIXME: Because PushToBaseRepo is async, we need to wait a bit before redirecting
time.Sleep(2 * time.Second)
lunny marked this conversation as resolved.
Show resolved Hide resolved
ctx.Redirect(link, 302)
lunny marked this conversation as resolved.
Show resolved Hide resolved
return
}

// Redirect to viewing file or folder
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(newBranchName) + "/" + util.PathEscapeSegments(treePath))
}
Expand Down Expand Up @@ -190,6 +207,9 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, treePath)

ctx.Data["CanCreateNewBranch"] = !ctx.FormBool("hide_create_new_branch")
ctx.Data["CanCreatePullRequest"] = !ctx.FormBool("hide_create_pr")
lunny marked this conversation as resolved.
Show resolved Hide resolved

ctx.HTML(http.StatusOK, tplEditFile)
}

Expand Down
19 changes: 19 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,25 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
}
if !willShowSpecifiedCommit && !willShowSpecifiedCommitRange {
ctx.Data["SourcePath"] = pull.HeadRepo.Link() + "/src/branch/" + url.PathEscape(pull.HeadBranch)

if !pull.HasMerged && ctx.Doer != nil {
if err := pull.LoadHeadRepo(ctx); err != nil {
ctx.ServerError("LoadHeadRepo", err)
return
}
perm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
if err != nil {
ctx.ServerError("GetUserRepoPermission", err)
return
}
ctx.Data["CanEditFile"] = perm.CanWrite(unit.TypeCode) || issues_model.CanMaintainerWriteToBranch(ctx, perm, pull.HeadBranch, ctx.Doer)
lunny marked this conversation as resolved.
Show resolved Hide resolved
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
ctx.Data["HeadBranchName"] = pull.HeadBranch
}
}

ctx.HTML(http.StatusOK, tplPullFiles)
}
Expand Down
3 changes: 3 additions & 0 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
{{if $file.IsDeleted}}
<a class="ui basic tiny button" rel="nofollow" href="{{$.BeforeSourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
{{else}}
{{if and $.Repository.CanEnableEditor $.CanEditFile (not $file.IsLFSFile)}}
<a class="ui basic tiny button" rel="nofollow" href="{{$.HeadRepoLink}}/_edit/{{PathEscapeSegments $.HeadBranchName}}/{{PathEscapeSegments $file.Name}}?hide_create_pr=true&hide_create_new_branch=true&back_to_pr={{$.Issue.Index}}&back_to_diff_hash={{$file.NameHash}}">{{ctx.Locale.Tr "repo.editor.edit_this_file"}}</a>
{{end}}
lunny marked this conversation as resolved.
Show resolved Hide resolved
<a class="ui basic tiny button" rel="nofollow" href="{{$.SourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
{{end}}
{{end}}
Expand Down
4 changes: 4 additions & 0 deletions templates/repo/editor/commit_form.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</div>
</div>
{{if not .Repository.IsEmpty}}
{{if .CanCreatePullRequest}}
<div class="field">
<div class="ui radio checkbox">
{{if .CanCreatePullRequest}}
Expand All @@ -57,6 +58,8 @@
</label>
</div>
</div>
{{end}}
{{if .CanCreateNewBranch}}
<div class="quick-pull-branch-name {{if not (eq .commit_choice "commit-to-new-branch")}}gt-hidden{{end}}">
<div class="new-branch-name-input field {{if .Err_NewBranchName}}error{{end}}">
{{svg "octicon-git-branch"}}
Expand All @@ -65,6 +68,7 @@
</div>
</div>
{{end}}
{{end}}
</div>
</div>
<button id="commit-button" type="submit" class="ui primary button">
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
// Ensure the PR page works
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))

t.Run("EnsureCanEditPullFile", func(t *testing.T) {
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d/files", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
editButtonCount := doc.doc.Find("div.diff-file-header-actions a[href*='/_edit/']").Length()
assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
})

// Then get the diff string
var diffHash string
var diffLength int
Expand Down
Loading