You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_pull_test.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "testing"
  9. auth_model "code.gitea.io/gitea/models/auth"
  10. "code.gitea.io/gitea/models/db"
  11. issues_model "code.gitea.io/gitea/models/issues"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. user_model "code.gitea.io/gitea/models/user"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/services/forms"
  18. issue_service "code.gitea.io/gitea/services/issue"
  19. "code.gitea.io/gitea/tests"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func TestAPIViewPulls(t *testing.T) {
  23. defer tests.PrepareTestEnv(t)()
  24. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  25. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  26. ctx := NewAPITestContext(t, "user2", repo.Name, auth_model.AccessTokenScopeReadRepository)
  27. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all", owner.Name, repo.Name).
  28. AddTokenAuth(ctx.Token)
  29. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  30. var pulls []*api.PullRequest
  31. DecodeJSON(t, resp, &pulls)
  32. expectedLen := unittest.GetCount(t, &issues_model.Issue{RepoID: repo.ID}, unittest.Cond("is_pull = ?", true))
  33. assert.Len(t, pulls, expectedLen)
  34. pull := pulls[0]
  35. if assert.EqualValues(t, 5, pull.ID) {
  36. resp = ctx.Session.MakeRequest(t, NewRequest(t, "GET", pull.DiffURL), http.StatusOK)
  37. _, err := io.ReadAll(resp.Body)
  38. assert.NoError(t, err)
  39. // TODO: use diff to generate stats to test against
  40. t.Run(fmt.Sprintf("APIGetPullFiles_%d", pull.ID),
  41. doAPIGetPullFiles(ctx, pull, func(t *testing.T, files []*api.ChangedFile) {
  42. if assert.Len(t, files, 1) {
  43. assert.Equal(t, "File-WoW", files[0].Filename)
  44. assert.Empty(t, files[0].PreviousFilename)
  45. assert.EqualValues(t, 1, files[0].Additions)
  46. assert.EqualValues(t, 1, files[0].Changes)
  47. assert.EqualValues(t, 0, files[0].Deletions)
  48. assert.Equal(t, "added", files[0].Status)
  49. }
  50. }))
  51. }
  52. }
  53. func TestAPIViewPullsByBaseHead(t *testing.T) {
  54. defer tests.PrepareTestEnv(t)()
  55. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  56. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  57. ctx := NewAPITestContext(t, "user2", repo.Name, auth_model.AccessTokenScopeReadRepository)
  58. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls/master/branch2", owner.Name, repo.Name).
  59. AddTokenAuth(ctx.Token)
  60. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  61. pull := &api.PullRequest{}
  62. DecodeJSON(t, resp, pull)
  63. assert.EqualValues(t, 3, pull.Index)
  64. assert.EqualValues(t, 2, pull.ID)
  65. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls/master/branch-not-exist", owner.Name, repo.Name).
  66. AddTokenAuth(ctx.Token)
  67. ctx.Session.MakeRequest(t, req, http.StatusNotFound)
  68. }
  69. // TestAPIMergePullWIP ensures that we can't merge a WIP pull request
  70. func TestAPIMergePullWIP(t *testing.T) {
  71. defer tests.PrepareTestEnv(t)()
  72. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  73. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  74. pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{Status: issues_model.PullRequestStatusMergeable}, unittest.Cond("has_merged = ?", false))
  75. pr.LoadIssue(db.DefaultContext)
  76. issue_service.ChangeTitle(db.DefaultContext, pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title)
  77. // force reload
  78. pr.LoadAttributes(db.DefaultContext)
  79. assert.Contains(t, pr.Issue.Title, setting.Repository.PullRequest.WorkInProgressPrefixes[0])
  80. session := loginUser(t, owner.Name)
  81. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  82. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner.Name, repo.Name, pr.Index), &forms.MergePullRequestForm{
  83. MergeMessageField: pr.Issue.Title,
  84. Do: string(repo_model.MergeStyleMerge),
  85. }).AddTokenAuth(token)
  86. MakeRequest(t, req, http.StatusMethodNotAllowed)
  87. }
  88. func TestAPICreatePullSuccess(t *testing.T) {
  89. defer tests.PrepareTestEnv(t)()
  90. repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
  91. // repo10 have code, pulls units.
  92. repo11 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
  93. // repo11 only have code unit but should still create pulls
  94. owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID})
  95. owner11 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo11.OwnerID})
  96. session := loginUser(t, owner11.Name)
  97. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  98. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &api.CreatePullRequestOption{
  99. Head: fmt.Sprintf("%s:master", owner11.Name),
  100. Base: "master",
  101. Title: "create a failure pr",
  102. }).AddTokenAuth(token)
  103. MakeRequest(t, req, http.StatusCreated)
  104. MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
  105. }
  106. func TestAPICreatePullSameRepoSuccess(t *testing.T) {
  107. defer tests.PrepareTestEnv(t)()
  108. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  109. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  110. session := loginUser(t, owner.Name)
  111. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  112. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner.Name, repo.Name), &api.CreatePullRequestOption{
  113. Head: fmt.Sprintf("%s:pr-to-update", owner.Name),
  114. Base: "master",
  115. Title: "successfully create a PR between branches of the same repository",
  116. }).AddTokenAuth(token)
  117. MakeRequest(t, req, http.StatusCreated)
  118. MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
  119. }
  120. func TestAPICreatePullWithFieldsSuccess(t *testing.T) {
  121. defer tests.PrepareTestEnv(t)()
  122. // repo10 have code, pulls units.
  123. repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
  124. owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID})
  125. // repo11 only have code unit but should still create pulls
  126. repo11 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
  127. owner11 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo11.OwnerID})
  128. session := loginUser(t, owner11.Name)
  129. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  130. opts := &api.CreatePullRequestOption{
  131. Head: fmt.Sprintf("%s:master", owner11.Name),
  132. Base: "master",
  133. Title: "create a failure pr",
  134. Body: "foobaaar",
  135. Milestone: 5,
  136. Assignees: []string{owner10.Name},
  137. Labels: []int64{5},
  138. }
  139. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), opts).
  140. AddTokenAuth(token)
  141. res := MakeRequest(t, req, http.StatusCreated)
  142. pull := new(api.PullRequest)
  143. DecodeJSON(t, res, pull)
  144. assert.NotNil(t, pull.Milestone)
  145. assert.EqualValues(t, opts.Milestone, pull.Milestone.ID)
  146. if assert.Len(t, pull.Assignees, 1) {
  147. assert.EqualValues(t, opts.Assignees[0], owner10.Name)
  148. }
  149. assert.NotNil(t, pull.Labels)
  150. assert.EqualValues(t, opts.Labels[0], pull.Labels[0].ID)
  151. }
  152. func TestAPICreatePullWithFieldsFailure(t *testing.T) {
  153. defer tests.PrepareTestEnv(t)()
  154. // repo10 have code, pulls units.
  155. repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
  156. owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID})
  157. // repo11 only have code unit but should still create pulls
  158. repo11 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
  159. owner11 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo11.OwnerID})
  160. session := loginUser(t, owner11.Name)
  161. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  162. opts := &api.CreatePullRequestOption{
  163. Head: fmt.Sprintf("%s:master", owner11.Name),
  164. Base: "master",
  165. }
  166. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), opts).
  167. AddTokenAuth(token)
  168. MakeRequest(t, req, http.StatusUnprocessableEntity)
  169. opts.Title = "is required"
  170. opts.Milestone = 666
  171. MakeRequest(t, req, http.StatusUnprocessableEntity)
  172. opts.Milestone = 5
  173. opts.Assignees = []string{"qweruqweroiuyqweoiruywqer"}
  174. MakeRequest(t, req, http.StatusUnprocessableEntity)
  175. opts.Assignees = []string{owner10.LoginName}
  176. opts.Labels = []int64{55555}
  177. MakeRequest(t, req, http.StatusUnprocessableEntity)
  178. opts.Labels = []int64{5}
  179. }
  180. func TestAPIEditPull(t *testing.T) {
  181. defer tests.PrepareTestEnv(t)()
  182. repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
  183. owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID})
  184. session := loginUser(t, owner10.Name)
  185. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  186. title := "create a success pr"
  187. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &api.CreatePullRequestOption{
  188. Head: "develop",
  189. Base: "master",
  190. Title: title,
  191. }).AddTokenAuth(token)
  192. apiPull := new(api.PullRequest)
  193. resp := MakeRequest(t, req, http.StatusCreated)
  194. DecodeJSON(t, resp, apiPull)
  195. assert.EqualValues(t, "master", apiPull.Base.Name)
  196. newTitle := "edit a this pr"
  197. newBody := "edited body"
  198. req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner10.Name, repo10.Name, apiPull.Index), &api.EditPullRequestOption{
  199. Base: "feature/1",
  200. Title: newTitle,
  201. Body: &newBody,
  202. }).AddTokenAuth(token)
  203. resp = MakeRequest(t, req, http.StatusCreated)
  204. DecodeJSON(t, resp, apiPull)
  205. assert.EqualValues(t, "feature/1", apiPull.Base.Name)
  206. // check comment history
  207. pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: apiPull.ID})
  208. err := pull.LoadIssue(db.DefaultContext)
  209. assert.NoError(t, err)
  210. unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: pull.Issue.ID, OldTitle: title, NewTitle: newTitle})
  211. unittest.AssertExistsAndLoadBean(t, &issues_model.ContentHistory{IssueID: pull.Issue.ID, ContentText: newBody, IsFirstCreated: false})
  212. req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner10.Name, repo10.Name, pull.Index), &api.EditPullRequestOption{
  213. Base: "not-exist",
  214. }).AddTokenAuth(token)
  215. MakeRequest(t, req, http.StatusNotFound)
  216. }
  217. func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, callback func(*testing.T, []*api.ChangedFile)) func(*testing.T) {
  218. return func(t *testing.T) {
  219. req := NewRequest(t, http.MethodGet, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files", ctx.Username, ctx.Reponame, pr.Index)).
  220. AddTokenAuth(ctx.Token)
  221. if ctx.ExpectedCode == 0 {
  222. ctx.ExpectedCode = http.StatusOK
  223. }
  224. resp := ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  225. files := make([]*api.ChangedFile, 0, 1)
  226. DecodeJSON(t, resp, &files)
  227. if callback != nil {
  228. callback(t, files)
  229. }
  230. }
  231. }