Skip to content

Commit 01dce97

Browse files
committed
Add tests
1 parent 61c4e69 commit 01dce97

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

tests/integration/api_pull_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
issues_model "code.gitea.io/gitea/models/issues"
1818
"code.gitea.io/gitea/models/perm"
1919
repo_model "code.gitea.io/gitea/models/repo"
20+
"code.gitea.io/gitea/models/unit"
21+
unit_model "code.gitea.io/gitea/models/unit"
2022
"code.gitea.io/gitea/models/unittest"
2123
user_model "code.gitea.io/gitea/models/user"
2224
"code.gitea.io/gitea/modules/setting"
@@ -26,10 +28,12 @@ import (
2628
"code.gitea.io/gitea/services/gitdiff"
2729
issue_service "code.gitea.io/gitea/services/issue"
2830
pull_service "code.gitea.io/gitea/services/pull"
31+
repo_service "code.gitea.io/gitea/services/repository"
2932
files_service "code.gitea.io/gitea/services/repository/files"
3033
"code.gitea.io/gitea/tests"
3134

3235
"github.com/stretchr/testify/assert"
36+
"github.com/stretchr/testify/require"
3337
)
3438

3539
func TestAPIViewPulls(t *testing.T) {
@@ -186,6 +190,76 @@ func TestAPIMergePullWIP(t *testing.T) {
186190
MakeRequest(t, req, http.StatusMethodNotAllowed)
187191
}
188192

193+
func TestAPIMergePull(t *testing.T) {
194+
doCheckBranchExists := func(user *user_model.User, token, branchName string, repo *repo_model.Repository, status int) {
195+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", user.Name, repo.Name, branchName)).AddTokenAuth(token)
196+
MakeRequest(t, req, status)
197+
}
198+
199+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
200+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
201+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
202+
session := loginUser(t, owner.Name)
203+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
204+
205+
t.Run("Normal", func(t *testing.T) {
206+
newBranch := "test-pull-1"
207+
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
208+
209+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
210+
Do: "merge",
211+
}).AddTokenAuth(token)
212+
213+
MakeRequest(t, req, http.StatusOK)
214+
doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK)
215+
// make sure we cannot perform a merge on the same PR
216+
MakeRequest(t, req, http.StatusUnprocessableEntity)
217+
doCheckBranchExists(owner, token, newBranch, repo, http.StatusOK)
218+
})
219+
220+
t.Run("DeleteBranchAfterMergePassedByFormField", func(t *testing.T) {
221+
newBranch := "test-pull-2"
222+
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
223+
224+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, prResp.Index), &forms.MergePullRequestForm{
225+
Do: "merge",
226+
DeleteBranchAfterMerge: true,
227+
}).AddTokenAuth(token)
228+
229+
MakeRequest(t, req, http.StatusOK)
230+
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
231+
})
232+
233+
t.Run("DeleteBranchAfterMergePassedByRepoSettings", func(t *testing.T) {
234+
newBranch := "test-pull-3"
235+
prResp := creatPullRequestWithCommit(t, owner, token, newBranch, repo)
236+
237+
// set the default branch after merge setting at the repo level
238+
prUnit, err := repo.GetUnit(t.Context(), unit.TypePullRequests)
239+
require.NoError(t, err)
240+
241+
prConfig := prUnit.PullRequestsConfig()
242+
prConfig.DefaultDeleteBranchAfterMerge = true
243+
244+
var units []repo_model.RepoUnit
245+
units = append(units, repo_model.RepoUnit{
246+
RepoID: repo.ID,
247+
Type: unit_model.TypePullRequests,
248+
Config: prConfig,
249+
})
250+
require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo, units, nil))
251+
252+
// 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{
254+
Do: "merge",
255+
}).AddTokenAuth(token)
256+
257+
MakeRequest(t, req, http.StatusOK)
258+
doCheckBranchExists(owner, token, newBranch, repo, http.StatusNotFound)
259+
})
260+
})
261+
}
262+
189263
func TestAPICreatePullSuccess(t *testing.T) {
190264
defer tests.PrepareTestEnv(t)()
191265
repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
@@ -520,3 +594,46 @@ func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) {
520594
})(t)
521595
})
522596
}
597+
598+
func creatPullRequestWithCommit(t *testing.T, user *user_model.User, token, newBranch string, repo *repo_model.Repository) *api.PullRequest {
599+
// create a new branch with a commit
600+
filesResp, err := files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{
601+
Files: []*files_service.ChangeRepoFile{
602+
{
603+
Operation: "create",
604+
TreePath: strings.ReplaceAll(newBranch, " ", "_"),
605+
ContentReader: strings.NewReader("This is a test."),
606+
},
607+
},
608+
Message: "Add test file using branch name as its name",
609+
OldBranch: repo.DefaultBranch,
610+
NewBranch: newBranch,
611+
Author: &files_service.IdentityOptions{
612+
GitUserName: user.Name,
613+
GitUserEmail: user.Email,
614+
},
615+
Committer: &files_service.IdentityOptions{
616+
GitUserName: user.Name,
617+
GitUserEmail: user.Email,
618+
},
619+
Dates: &files_service.CommitDateOptions{
620+
Author: time.Now(),
621+
Committer: time.Now(),
622+
},
623+
})
624+
625+
require.NoError(t, err)
626+
require.NotEmpty(t, filesResp)
627+
628+
// create a corresponding PR
629+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", user.Name, repo.Name), &api.CreatePullRequestOption{
630+
Head: newBranch,
631+
Base: repo.DefaultBranch,
632+
Title: "Add a test file",
633+
}).AddTokenAuth(token)
634+
resp := MakeRequest(t, req, http.StatusCreated)
635+
pull := new(api.PullRequest)
636+
DecodeJSON(t, resp, pull)
637+
638+
return pull
639+
}

0 commit comments

Comments
 (0)