Skip to content

Commit 1209cff

Browse files
committed
fix query param handling
1 parent 392461d commit 1209cff

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

routers/web/repo/editor.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
185185

186186
// redirectForCommitChoice redirects after committing the edit to a branch
187187
func redirectForCommitChoice[T any](ctx *context.Context, parsed *preparedEditorCommitForm[T], treePath string) {
188+
// when editing a file in a PR, it should return to the origin location
189+
if returnURI := ctx.FormString("return_uri"); returnURI != "" && httplib.IsCurrentGiteaSiteURL(ctx, returnURI) {
190+
ctx.JSONRedirect(returnURI)
191+
return
192+
}
193+
188194
if parsed.commonForm.CommitChoice == editorCommitChoiceNewBranch {
189195
// Redirect to a pull request when possible
190196
redirectToPullRequest := false
@@ -203,11 +209,9 @@ func redirectForCommitChoice[T any](ctx *context.Context, parsed *preparedEditor
203209
}
204210
}
205211

206-
returnURI := ctx.FormString("return_uri")
207-
if returnURI == "" || !httplib.IsCurrentGiteaSiteURL(ctx, returnURI) {
208-
returnURI = util.URLJoin(ctx.Repo.RepoLink, "src/branch", util.PathEscapeSegments(parsed.NewBranchName), util.PathEscapeSegments(treePath))
209-
}
210-
ctx.JSONRedirect(returnURI)
212+
// redirect to the newly updated file
213+
redirectTo := util.URLJoin(ctx.Repo.RepoLink, "src/branch", util.PathEscapeSegments(parsed.NewBranchName), util.PathEscapeSegments(treePath))
214+
ctx.JSONRedirect(redirectTo)
211215
}
212216

213217
func editFileOpenExisting(ctx *context.Context) (prefetch []byte, dataRc io.ReadCloser, fInfo *fileInfo) {

services/context/repo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
179179
} else {
180180
opts.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + editorPathParamRemaining
181181
}
182+
if ctx.Req.URL.RawQuery != "" {
183+
opts.TargetFormAction += util.Iif(strings.Contains(opts.TargetFormAction, "?"), "&", "?") + ctx.Req.URL.RawQuery
184+
}
182185
return opts, nil
183186
}
184187

tests/integration/editor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,13 @@ func testForkToEditFile(t *testing.T, session *TestSession, user, owner, repo, b
382382

383383
t.Run("CheckBaseRepoForm", func(t *testing.T) {
384384
// the base repo's edit form should have the correct action and upload links (pointing to the forked repo)
385-
req := NewRequest(t, "GET", path.Join(owner, repo, "_upload", branch, filePath))
385+
req := NewRequest(t, "GET", path.Join(owner, repo, "_upload", branch, filePath)+"?foo=bar")
386386
resp := session.MakeRequest(t, req, http.StatusOK)
387387
htmlDoc := NewHTMLParser(t, resp.Body)
388388

389389
uploadForm := htmlDoc.doc.Find(".form-fetch-action")
390390
formAction := uploadForm.AttrOr("action", "")
391-
assert.Equal(t, fmt.Sprintf("/%s/%s-1/_upload/%s/%s?from_base_branch=%s", user, repo, branch, filePath, branch), formAction)
391+
assert.Equal(t, fmt.Sprintf("/%s/%s-1/_upload/%s/%s?from_base_branch=%s&foo=bar", user, repo, branch, filePath, branch), formAction)
392392
uploadLink := uploadForm.Find(".dropzone").AttrOr("data-link-url", "")
393393
assert.Equal(t, fmt.Sprintf("/%s/%s-1/upload-file", user, repo), uploadLink)
394394
newBranchName := uploadForm.Find("input[name=new_branch_name]").AttrOr("value", "")

0 commit comments

Comments
 (0)