Skip to content

Commit fc915fe

Browse files
committed
fix and add tests
1 parent 21dd561 commit fc915fe

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

routers/web/repo/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
144144

145145
fromBaseBranch := ctx.FormString("from_base_branch")
146146
if fromBaseBranch != "" {
147-
err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, ctx.Repo.Repository, ctx.Repo.RefFullName.BranchName())
147+
err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, ctx.Repo.Repository, targetBranchName)
148148
if err != nil {
149149
log.Error("Unable to editorPushBranchToForkedRepository: %v", err)
150150
ctx.JSONError(ctx.Tr("repo.editor.fork_failed_to_push_branch", targetBranchName))

services/context/repo.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
121121
}
122122
// now, we get our own forked repo; it must be writable by us.
123123
}
124-
submitToOriginRepo := targetRepo.ID == originRepo.ID
124+
submitToForkedRepo := targetRepo.ID != originRepo.ID
125125
err := targetRepo.GetBaseRepo(ctx)
126126
if err != nil {
127127
return nil, err
@@ -147,7 +147,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
147147
return nil, err
148148
}
149149

150-
canCommitToBranch := submitToOriginRepo && targetRepo.CanEnableEditor() && canPushWithProtection
150+
canCommitToBranch := !submitToForkedRepo /* same repo */ && targetRepo.CanEnableEditor() && canPushWithProtection
151151
if protectionRequireSigned {
152152
canCommitToBranch = canCommitToBranch && willSign
153153
}
@@ -157,7 +157,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
157157

158158
cfb := &CommitFormOptions{
159159
TargetRepo: targetRepo,
160-
WillSubmitToFork: !submitToOriginRepo,
160+
WillSubmitToFork: submitToForkedRepo,
161161
CanCommitToBranch: canCommitToBranch,
162162
UserCanPush: canPushWithProtection,
163163
RequireSigned: protectionRequireSigned,
@@ -168,15 +168,17 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
168168
CanCreatePullRequest: canCreatePullRequest,
169169
CanCreateBasePullRequest: canCreateBasePullRequest,
170170
}
171-
editorAction, editorPathParamRemaining := ctx.PathParam("editor_action"), ctx.PathParam("*")
171+
editorAction := ctx.PathParam("editor_action")
172+
editorPathParamRemaining := util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
173+
if submitToForkedRepo {
174+
// there is only "default branch" in forked repo, we will use "from_base_branch" to get a new branch from base repo
175+
editorPathParamRemaining = util.PathEscapeSegments(targetRepo.DefaultBranch) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) + "?from_base_branch=" + url.QueryEscape(branchName)
176+
}
172177
if editorAction == "_cherrypick" {
173178
cfb.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + ctx.PathParam("sha") + "/" + editorPathParamRemaining
174179
} else {
175180
cfb.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + editorPathParamRemaining
176181
}
177-
if originRepo.ID != targetRepo.ID {
178-
cfb.TargetFormAction += "?from_base_branch=" + url.QueryEscape(branchName)
179-
}
180182
return cfb, nil
181183
}
182184

tests/integration/editor_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,15 @@ index 0000000000..bbbbbbbbbb
342342
}
343343

344344
func forkToEdit(t *testing.T, session *TestSession, owner, repo, operation, branch, filePath string) {
345-
// Attempt to edit file
345+
// attempt to edit a file, see the guideline page
346346
req := NewRequest(t, "GET", path.Join(owner, repo, operation, branch, filePath))
347347
resp := session.MakeRequest(t, req, http.StatusOK)
348+
assert.Contains(t, resp.Body.String(), "Fork Repository to Propose Changes")
348349

349-
// Fork
350+
// fork the repository
350351
req = NewRequestWithValues(t, "POST", path.Join(owner, repo, "_fork", branch), map[string]string{"_csrf": GetUserCSRFToken(t, session)})
351352
resp = session.MakeRequest(t, req, http.StatusOK)
352-
assert.Equal(t, `{"redirect":""}`, strings.TrimSpace(resp.Body.String()))
353+
assert.JSONEq(t, `{"redirect":""}`, resp.Body.String())
353354
}
354355

355356
func testForkToEditFile(t *testing.T, session *TestSession, user, owner, repo, branch, filePath string) {
@@ -387,6 +388,37 @@ func testForkToEditFile(t *testing.T, session *TestSession, user, owner, repo, b
387388
// Check the existence of the forked repo with unique name
388389
req = NewRequestf(t, "GET", "/%s/%s-1", user, repo)
389390
session.MakeRequest(t, req, http.StatusOK)
391+
392+
// the base repo's edit form should have the correct action and upload links (pointing to the forked repo)
393+
req = NewRequest(t, "GET", path.Join(owner, repo, "_upload", branch, filePath))
394+
resp = session.MakeRequest(t, req, http.StatusOK)
395+
htmlDoc := NewHTMLParser(t, resp.Body)
396+
397+
form := htmlDoc.doc.Find(".form-fetch-action")
398+
formAction := form.AttrOr("action", "")
399+
assert.Equal(t, fmt.Sprintf("/%s/%s-1/_upload/%s/%s?from_base_branch=%s", user, repo, branch, filePath, branch), formAction)
400+
uploadLink := form.Find(".dropzone").AttrOr("data-link-url", "")
401+
assert.Equal(t, fmt.Sprintf("/%s/%s-1/upload-file", user, repo), uploadLink)
402+
newBranchName := form.Find("input[name=new_branch_name]").AttrOr("value", "")
403+
assert.Equal(t, user+"-patch-1", newBranchName)
404+
commitChoice := form.Find("input[name=commit_choice][checked]").AttrOr("value", "")
405+
assert.Equal(t, "commit-to-new-branch", commitChoice)
406+
lastCommit := form.Find("input[name=last_commit]").AttrOr("value", "")
407+
assert.NotEmpty(t, lastCommit)
408+
409+
// change a file in the forked repo
410+
req = NewRequestWithValues(t, "POST", formAction,
411+
map[string]string{
412+
"_csrf": GetUserCSRFToken(t, session),
413+
"last_commit": lastCommit,
414+
"tree_path": filePath,
415+
"content": "new content in fork",
416+
"commit_choice": commitChoice,
417+
"new_branch_name": newBranchName,
418+
},
419+
)
420+
resp = session.MakeRequest(t, req, http.StatusOK)
421+
assert.Equal(t, fmt.Sprintf("/%s/%s/compare/%s...%s/%s-1:%s-patch-1", owner, repo, branch, user, repo, user), test.RedirectURL(resp))
390422
}
391423

392424
func TestForkToEditFile(t *testing.T) {

0 commit comments

Comments
 (0)