Skip to content

Commit da4bb40

Browse files
committed
PR feedback + some touchups
1 parent 62fba4f commit da4bb40

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

tests/integration/api_pull_test.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestAPIMergePullWIP(t *testing.T) {
190190
}
191191

192192
func TestAPIMergePull(t *testing.T) {
193-
doCheckBranchExists := func(user *user_model.User, token, branchName string, repo *repo_model.Repository, status int) {
193+
doCheckBranchExists := func(t *testing.T, user *user_model.User, token, branchName string, repo *repo_model.Repository, status int) {
194194
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", user.Name, repo.Name, branchName)).AddTokenAuth(token)
195195
MakeRequest(t, req, status)
196196
}
@@ -203,36 +203,36 @@ func TestAPIMergePull(t *testing.T) {
203203

204204
t.Run("Normal", func(t *testing.T) {
205205
newBranch := "test-pull-1"
206-
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
206+
prDTO := createPullRequestWithCommit(t, owner, newBranch, repo)
207207

208-
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
208+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{
209209
Do: "merge",
210210
}).AddTokenAuth(token)
211211

212212
MakeRequest(t, req, http.StatusOK)
213-
doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK)
213+
doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusOK)
214214
// make sure we cannot perform a merge on the same PR
215215
MakeRequest(t, req, http.StatusUnprocessableEntity)
216-
doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK)
216+
doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusOK)
217217
})
218218

219219
t.Run("DeleteBranchAfterMergePassedByFormField", func(t *testing.T) {
220220
newBranch := "test-pull-2"
221-
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
221+
prDTO := createPullRequestWithCommit(t, owner, newBranch, repo)
222222
deleteBranch := true
223223

224-
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
224+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{
225225
Do: "merge",
226226
DeleteBranchAfterMerge: &deleteBranch,
227227
}).AddTokenAuth(token)
228228

229229
MakeRequest(t, req, http.StatusOK)
230-
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
230+
doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound)
231231
})
232232

233233
t.Run("DeleteBranchAfterMergePassedByRepoSettings", func(t *testing.T) {
234234
newBranch := "test-pull-3"
235-
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
235+
prDTO := createPullRequestWithCommit(t, owner, newBranch, repo)
236236

237237
// set the default branch after merge setting at the repo level
238238
prUnit, err := repo.GetUnit(t.Context(), unit_model.TypePullRequests)
@@ -250,17 +250,17 @@ func TestAPIMergePull(t *testing.T) {
250250
require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo, units, nil))
251251

252252
// perform merge
253-
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
253+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{
254254
Do: "merge",
255255
}).AddTokenAuth(token)
256256

257257
MakeRequest(t, req, http.StatusOK)
258-
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
258+
doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound)
259259
})
260260

261261
t.Run("DeleteBranchAfterMergeFormFieldIsSetButNotRepoSettings", func(t *testing.T) {
262262
newBranch := "test-pull-4"
263-
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
263+
prDTO := createPullRequestWithCommit(t, owner, newBranch, repo)
264264

265265
// make sure the default branch after merge setting is unset at the repo level
266266
prUnit, err := repo.GetUnit(t.Context(), unit_model.TypePullRequests)
@@ -279,13 +279,13 @@ func TestAPIMergePull(t *testing.T) {
279279

280280
// perform merge
281281
deleteBranch := true
282-
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
282+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prDTO.Index), &forms.MergePullRequestForm{
283283
Do: "merge",
284284
DeleteBranchAfterMerge: &deleteBranch,
285285
}).AddTokenAuth(token)
286286

287287
MakeRequest(t, req, http.StatusOK)
288-
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
288+
doCheckBranchExists(t, owner, token, newBranch, repo, http.StatusNotFound)
289289
})
290290
})
291291
}
@@ -625,7 +625,7 @@ func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) {
625625
})
626626
}
627627

628-
func creatPullRequestWithCommit(t *testing.T, user *user_model.User, token, newBranch string, repo *repo_model.Repository) *api.PullRequest {
628+
func createPullRequestWithCommit(t *testing.T, user *user_model.User, newBranch string, repo *repo_model.Repository) *api.PullRequest {
629629
// create a new branch with a commit
630630
filesResp, err := files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{
631631
Files: []*files_service.ChangeRepoFile{
@@ -656,14 +656,9 @@ func creatPullRequestWithCommit(t *testing.T, user *user_model.User, token, newB
656656
require.NotEmpty(t, filesResp)
657657

658658
// create a corresponding PR
659-
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", user.Name, repo.Name), &api.CreatePullRequestOption{
660-
Head: newBranch,
661-
Base: repo.DefaultBranch,
662-
Title: "Add a test file",
663-
}).AddTokenAuth(token)
664-
resp := MakeRequest(t, req, http.StatusCreated)
665-
pull := new(api.PullRequest)
666-
DecodeJSON(t, resp, pull)
659+
apiCtx := NewAPITestContext(t, repo.OwnerName, repo.Name, auth_model.AccessTokenScopeWriteRepository)
660+
prDTO, err := doAPICreatePullRequest(apiCtx, repo.OwnerName, repo.Name, repo.DefaultBranch, newBranch)(t)
661+
require.NoError(t, err)
667662

668-
return pull
663+
return &prDTO
669664
}

0 commit comments

Comments
 (0)