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.

action_test.go 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/repository"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestUpdateIssuesCommit(t *testing.T) {
  13. assert.NoError(t, models.PrepareTestDatabase())
  14. pushCommits := []*repository.PushCommit{
  15. {
  16. Sha1: "abcdef1",
  17. CommitterEmail: "user2@example.com",
  18. CommitterName: "User Two",
  19. AuthorEmail: "user4@example.com",
  20. AuthorName: "User Four",
  21. Message: "start working on #FST-1, #1",
  22. },
  23. {
  24. Sha1: "abcdef2",
  25. CommitterEmail: "user2@example.com",
  26. CommitterName: "User Two",
  27. AuthorEmail: "user2@example.com",
  28. AuthorName: "User Two",
  29. Message: "a plain message",
  30. },
  31. {
  32. Sha1: "abcdef2",
  33. CommitterEmail: "user2@example.com",
  34. CommitterName: "User Two",
  35. AuthorEmail: "user2@example.com",
  36. AuthorName: "User Two",
  37. Message: "close #2",
  38. },
  39. }
  40. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  41. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  42. repo.Owner = user
  43. commentBean := &models.Comment{
  44. Type: models.CommentTypeCommitRef,
  45. CommitSHA: "abcdef1",
  46. PosterID: user.ID,
  47. IssueID: 1,
  48. }
  49. issueBean := &models.Issue{RepoID: repo.ID, Index: 4}
  50. models.AssertNotExistsBean(t, commentBean)
  51. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  52. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  53. models.AssertExistsAndLoadBean(t, commentBean)
  54. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  55. models.CheckConsistencyFor(t, &models.Action{})
  56. // Test that push to a non-default branch closes no issue.
  57. pushCommits = []*repository.PushCommit{
  58. {
  59. Sha1: "abcdef1",
  60. CommitterEmail: "user2@example.com",
  61. CommitterName: "User Two",
  62. AuthorEmail: "user4@example.com",
  63. AuthorName: "User Four",
  64. Message: "close #1",
  65. },
  66. }
  67. repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
  68. commentBean = &models.Comment{
  69. Type: models.CommentTypeCommitRef,
  70. CommitSHA: "abcdef1",
  71. PosterID: user.ID,
  72. IssueID: 6,
  73. }
  74. issueBean = &models.Issue{RepoID: repo.ID, Index: 1}
  75. models.AssertNotExistsBean(t, commentBean)
  76. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  77. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  78. models.AssertExistsAndLoadBean(t, commentBean)
  79. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  80. models.CheckConsistencyFor(t, &models.Action{})
  81. pushCommits = []*repository.PushCommit{
  82. {
  83. Sha1: "abcdef3",
  84. CommitterEmail: "user2@example.com",
  85. CommitterName: "User Two",
  86. AuthorEmail: "user2@example.com",
  87. AuthorName: "User Two",
  88. Message: "close " + setting.AppURL + repo.FullName() + "/pulls/1",
  89. },
  90. }
  91. repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
  92. commentBean = &models.Comment{
  93. Type: models.CommentTypeCommitRef,
  94. CommitSHA: "abcdef3",
  95. PosterID: user.ID,
  96. IssueID: 6,
  97. }
  98. issueBean = &models.Issue{RepoID: repo.ID, Index: 1}
  99. models.AssertNotExistsBean(t, commentBean)
  100. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  101. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  102. models.AssertExistsAndLoadBean(t, commentBean)
  103. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  104. models.CheckConsistencyFor(t, &models.Action{})
  105. }
  106. func TestUpdateIssuesCommit_Colon(t *testing.T) {
  107. assert.NoError(t, models.PrepareTestDatabase())
  108. pushCommits := []*repository.PushCommit{
  109. {
  110. Sha1: "abcdef2",
  111. CommitterEmail: "user2@example.com",
  112. CommitterName: "User Two",
  113. AuthorEmail: "user2@example.com",
  114. AuthorName: "User Two",
  115. Message: "close: #2",
  116. },
  117. }
  118. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  119. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  120. repo.Owner = user
  121. issueBean := &models.Issue{RepoID: repo.ID, Index: 4}
  122. models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  123. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  124. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  125. models.CheckConsistencyFor(t, &models.Action{})
  126. }
  127. func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  128. assert.NoError(t, models.PrepareTestDatabase())
  129. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  130. // Test that push to a non-default branch closes an issue.
  131. pushCommits := []*repository.PushCommit{
  132. {
  133. Sha1: "abcdef1",
  134. CommitterEmail: "user2@example.com",
  135. CommitterName: "User Two",
  136. AuthorEmail: "user4@example.com",
  137. AuthorName: "User Four",
  138. Message: "close #2",
  139. },
  140. }
  141. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  142. commentBean := &models.Comment{
  143. Type: models.CommentTypeCommitRef,
  144. CommitSHA: "abcdef1",
  145. PosterID: user.ID,
  146. IssueID: 7,
  147. }
  148. issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7}
  149. models.AssertNotExistsBean(t, commentBean)
  150. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  151. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  152. models.AssertExistsAndLoadBean(t, commentBean)
  153. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  154. models.CheckConsistencyFor(t, &models.Action{})
  155. }
  156. func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
  157. assert.NoError(t, models.PrepareTestDatabase())
  158. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  159. // Test that a push to default branch closes issue in another repo
  160. // If the user also has push permissions to that repo
  161. pushCommits := []*repository.PushCommit{
  162. {
  163. Sha1: "abcdef1",
  164. CommitterEmail: "user2@example.com",
  165. CommitterName: "User Two",
  166. AuthorEmail: "user2@example.com",
  167. AuthorName: "User Two",
  168. Message: "close user2/repo1#1",
  169. },
  170. }
  171. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  172. commentBean := &models.Comment{
  173. Type: models.CommentTypeCommitRef,
  174. CommitSHA: "abcdef1",
  175. PosterID: user.ID,
  176. IssueID: 1,
  177. }
  178. issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1}
  179. models.AssertNotExistsBean(t, commentBean)
  180. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  181. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  182. models.AssertExistsAndLoadBean(t, commentBean)
  183. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  184. models.CheckConsistencyFor(t, &models.Action{})
  185. }
  186. func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) {
  187. assert.NoError(t, models.PrepareTestDatabase())
  188. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  189. // Test that a push to default branch closes issue in another repo
  190. // If the user also has push permissions to that repo
  191. pushCommits := []*repository.PushCommit{
  192. {
  193. Sha1: "abcdef1",
  194. CommitterEmail: "user2@example.com",
  195. CommitterName: "User Two",
  196. AuthorEmail: "user2@example.com",
  197. AuthorName: "User Two",
  198. Message: "close " + setting.AppURL + "user2/repo1/issues/1",
  199. },
  200. }
  201. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  202. commentBean := &models.Comment{
  203. Type: models.CommentTypeCommitRef,
  204. CommitSHA: "abcdef1",
  205. PosterID: user.ID,
  206. IssueID: 1,
  207. }
  208. issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1}
  209. models.AssertNotExistsBean(t, commentBean)
  210. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  211. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  212. models.AssertExistsAndLoadBean(t, commentBean)
  213. models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  214. models.CheckConsistencyFor(t, &models.Action{})
  215. }
  216. func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
  217. assert.NoError(t, models.PrepareTestDatabase())
  218. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User)
  219. // Test that a push with close reference *can not* close issue
  220. // If the commiter doesn't have push rights in that repo
  221. pushCommits := []*repository.PushCommit{
  222. {
  223. Sha1: "abcdef3",
  224. CommitterEmail: "user10@example.com",
  225. CommitterName: "User Ten",
  226. AuthorEmail: "user10@example.com",
  227. AuthorName: "User Ten",
  228. Message: "close user3/repo3#1",
  229. },
  230. {
  231. Sha1: "abcdef4",
  232. CommitterEmail: "user10@example.com",
  233. CommitterName: "User Ten",
  234. AuthorEmail: "user10@example.com",
  235. AuthorName: "User Ten",
  236. Message: "close " + setting.AppURL + "user3/repo3/issues/1",
  237. },
  238. }
  239. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository)
  240. commentBean := &models.Comment{
  241. Type: models.CommentTypeCommitRef,
  242. CommitSHA: "abcdef3",
  243. PosterID: user.ID,
  244. IssueID: 6,
  245. }
  246. commentBean2 := &models.Comment{
  247. Type: models.CommentTypeCommitRef,
  248. CommitSHA: "abcdef4",
  249. PosterID: user.ID,
  250. IssueID: 6,
  251. }
  252. issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6}
  253. models.AssertNotExistsBean(t, commentBean)
  254. models.AssertNotExistsBean(t, commentBean2)
  255. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  256. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  257. models.AssertNotExistsBean(t, commentBean)
  258. models.AssertNotExistsBean(t, commentBean2)
  259. models.AssertNotExistsBean(t, issueBean, "is_closed=1")
  260. models.CheckConsistencyFor(t, &models.Action{})
  261. }