gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues_test
  4. import (
  5. "fmt"
  6. "slices"
  7. "sort"
  8. "sync"
  9. "testing"
  10. "time"
  11. "code.gitea.io/gitea/models/db"
  12. issues_model "code.gitea.io/gitea/models/issues"
  13. repo_model "code.gitea.io/gitea/models/repo"
  14. "code.gitea.io/gitea/models/unittest"
  15. user_model "code.gitea.io/gitea/models/user"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/stretchr/testify/assert"
  18. "xorm.io/builder"
  19. )
  20. func TestIssue_ReplaceLabels(t *testing.T) {
  21. assert.NoError(t, unittest.PrepareTestDatabase())
  22. testSuccess := func(issueID int64, labelIDs, expectedLabelIDs []int64) {
  23. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueID})
  24. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
  25. doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  26. labels := make([]*issues_model.Label, len(labelIDs))
  27. for i, labelID := range labelIDs {
  28. labels[i] = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID, RepoID: repo.ID})
  29. }
  30. assert.NoError(t, issues_model.ReplaceIssueLabels(t.Context(), issue, labels, doer))
  31. unittest.AssertCount(t, &issues_model.IssueLabel{IssueID: issueID}, len(expectedLabelIDs))
  32. for _, labelID := range expectedLabelIDs {
  33. unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID})
  34. }
  35. }
  36. testSuccess(1, []int64{2}, []int64{2})
  37. testSuccess(1, []int64{1, 2}, []int64{1, 2})
  38. testSuccess(1, []int64{}, []int64{})
  39. // mutually exclusive scoped labels 7 and 8
  40. testSuccess(18, []int64{6, 7}, []int64{6, 7})
  41. testSuccess(18, []int64{7, 8}, []int64{8})
  42. testSuccess(18, []int64{6, 8, 7}, []int64{6, 7})
  43. }
  44. func Test_GetIssueIDsByRepoID(t *testing.T) {
  45. assert.NoError(t, unittest.PrepareTestDatabase())
  46. ids, err := issues_model.GetIssueIDsByRepoID(t.Context(), 1)
  47. assert.NoError(t, err)
  48. assert.Len(t, ids, 5)
  49. }
  50. func TestIssueAPIURL(t *testing.T) {
  51. assert.NoError(t, unittest.PrepareTestDatabase())
  52. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
  53. err := issue.LoadAttributes(t.Context())
  54. assert.NoError(t, err)
  55. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL(t.Context()))
  56. }
  57. func TestGetIssuesByIDs(t *testing.T) {
  58. assert.NoError(t, unittest.PrepareTestDatabase())
  59. testSuccess := func(expectedIssueIDs, nonExistentIssueIDs []int64) {
  60. issues, err := issues_model.GetIssuesByIDs(t.Context(), append(expectedIssueIDs, nonExistentIssueIDs...), true)
  61. assert.NoError(t, err)
  62. actualIssueIDs := make([]int64, len(issues))
  63. for i, issue := range issues {
  64. actualIssueIDs[i] = issue.ID
  65. }
  66. assert.Equal(t, expectedIssueIDs, actualIssueIDs)
  67. }
  68. testSuccess([]int64{1, 2, 3}, []int64{})
  69. testSuccess([]int64{1, 2, 3}, []int64{unittest.NonexistentID})
  70. testSuccess([]int64{3, 2, 1}, []int64{})
  71. }
  72. func TestGetParticipantIDsByIssue(t *testing.T) {
  73. assert.NoError(t, unittest.PrepareTestDatabase())
  74. checkParticipants := func(issueID int64, userIDs []int) {
  75. issue, err := issues_model.GetIssueByID(t.Context(), issueID)
  76. assert.NoError(t, err)
  77. participants, err := issue.GetParticipantIDsByIssue(t.Context())
  78. if assert.NoError(t, err) {
  79. participantsIDs := make([]int, len(participants))
  80. for i, uid := range participants {
  81. participantsIDs[i] = int(uid)
  82. }
  83. sort.Ints(participantsIDs)
  84. sort.Ints(userIDs)
  85. assert.Equal(t, userIDs, participantsIDs)
  86. }
  87. }
  88. // User 1 is issue1 poster (see fixtures/issue.yml)
  89. // User 2 only labeled issue1 (see fixtures/comment.yml)
  90. // Users 3 and 5 made actual comments (see fixtures/comment.yml)
  91. // User 3 is inactive, thus not active participant
  92. checkParticipants(1, []int{1, 5})
  93. }
  94. func TestIssue_ClearLabels(t *testing.T) {
  95. tests := []struct {
  96. issueID int64
  97. doerID int64
  98. }{
  99. {1, 2}, // non-pull-request, has labels
  100. {2, 2}, // pull-request, has labels
  101. {3, 2}, // pull-request, has no labels
  102. }
  103. for _, test := range tests {
  104. assert.NoError(t, unittest.PrepareTestDatabase())
  105. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID})
  106. doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID})
  107. assert.NoError(t, issues_model.ClearIssueLabels(t.Context(), issue, doer))
  108. unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: test.issueID})
  109. }
  110. }
  111. func TestUpdateIssueCols(t *testing.T) {
  112. assert.NoError(t, unittest.PrepareTestDatabase())
  113. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
  114. const newTitle = "New Title for unit test"
  115. issue.Title = newTitle
  116. prevContent := issue.Content
  117. issue.Content = "This should have no effect"
  118. now := time.Now().Unix()
  119. assert.NoError(t, issues_model.UpdateIssueCols(t.Context(), issue, "name"))
  120. then := time.Now().Unix()
  121. updatedIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue.ID})
  122. assert.Equal(t, newTitle, updatedIssue.Title)
  123. assert.Equal(t, prevContent, updatedIssue.Content)
  124. unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
  125. }
  126. func TestIssues(t *testing.T) {
  127. assert.NoError(t, unittest.PrepareTestDatabase())
  128. for _, test := range []struct {
  129. Opts issues_model.IssuesOptions
  130. ExpectedIssueIDs []int64
  131. }{
  132. {
  133. issues_model.IssuesOptions{
  134. AssigneeID: "1",
  135. SortType: "oldest",
  136. },
  137. []int64{1, 6},
  138. },
  139. {
  140. issues_model.IssuesOptions{
  141. RepoCond: builder.In("repo_id", 1, 3),
  142. SortType: "oldest",
  143. Paginator: &db.ListOptions{
  144. Page: 1,
  145. PageSize: 4,
  146. },
  147. },
  148. []int64{1, 2, 3, 5},
  149. },
  150. {
  151. issues_model.IssuesOptions{
  152. LabelIDs: []int64{1},
  153. Paginator: &db.ListOptions{
  154. Page: 1,
  155. PageSize: 4,
  156. },
  157. },
  158. []int64{2, 1},
  159. },
  160. {
  161. issues_model.IssuesOptions{
  162. LabelIDs: []int64{1, 2},
  163. Paginator: &db.ListOptions{
  164. Page: 1,
  165. PageSize: 4,
  166. },
  167. },
  168. []int64{}, // issues with **both** label 1 and 2, none of these issues matches, TODO: add more tests
  169. },
  170. {
  171. issues_model.IssuesOptions{
  172. MilestoneIDs: []int64{1},
  173. },
  174. []int64{2},
  175. },
  176. } {
  177. issues, err := issues_model.Issues(t.Context(), &test.Opts)
  178. assert.NoError(t, err)
  179. if assert.Len(t, issues, len(test.ExpectedIssueIDs)) {
  180. for i, issue := range issues {
  181. assert.Equal(t, test.ExpectedIssueIDs[i], issue.ID)
  182. }
  183. }
  184. }
  185. }
  186. func TestIssue_loadTotalTimes(t *testing.T) {
  187. assert.NoError(t, unittest.PrepareTestDatabase())
  188. ms, err := issues_model.GetIssueByID(t.Context(), 2)
  189. assert.NoError(t, err)
  190. assert.NoError(t, ms.LoadTotalTimes(t.Context()))
  191. assert.Equal(t, int64(3682), ms.TotalTrackedTime)
  192. }
  193. func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *issues_model.Issue {
  194. var newIssue issues_model.Issue
  195. t.Run(title, func(t *testing.T) {
  196. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  197. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  198. issue := issues_model.Issue{
  199. RepoID: repo.ID,
  200. PosterID: user.ID,
  201. Poster: user,
  202. Title: title,
  203. Content: content,
  204. }
  205. err := issues_model.NewIssue(t.Context(), repo, &issue, nil, nil)
  206. assert.NoError(t, err)
  207. has, err := db.GetEngine(t.Context()).ID(issue.ID).Get(&newIssue)
  208. assert.NoError(t, err)
  209. assert.True(t, has)
  210. assert.Equal(t, issue.Title, newIssue.Title)
  211. assert.Equal(t, issue.Content, newIssue.Content)
  212. if expectIndex > 0 {
  213. assert.Equal(t, expectIndex, newIssue.Index)
  214. }
  215. })
  216. return &newIssue
  217. }
  218. func TestIssue_InsertIssue(t *testing.T) {
  219. assert.NoError(t, unittest.PrepareTestDatabase())
  220. // there are 5 issues and max index is 5 on repository 1, so this one should 6
  221. issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6)
  222. _, err := db.DeleteByID[issues_model.Issue](t.Context(), issue.ID)
  223. assert.NoError(t, err)
  224. issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7)
  225. _, err = db.DeleteByID[issues_model.Issue](t.Context(), issue.ID)
  226. assert.NoError(t, err)
  227. }
  228. func TestIssue_ResolveMentions(t *testing.T) {
  229. assert.NoError(t, unittest.PrepareTestDatabase())
  230. testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) {
  231. o := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: owner})
  232. r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: o.ID, LowerName: repo})
  233. issue := &issues_model.Issue{RepoID: r.ID}
  234. d := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: doer})
  235. resolved, err := issues_model.ResolveIssueMentionsByVisibility(t.Context(), issue, d, mentions)
  236. assert.NoError(t, err)
  237. ids := make([]int64, len(resolved))
  238. for i, user := range resolved {
  239. ids[i] = user.ID
  240. }
  241. slices.Sort(ids)
  242. assert.Equal(t, expected, ids)
  243. }
  244. // Public repo, existing user
  245. testSuccess("user2", "repo1", "user1", []string{"user5"}, []int64{5})
  246. // Public repo, non-existing user
  247. testSuccess("user2", "repo1", "user1", []string{"nonexisting"}, []int64{})
  248. // Public repo, doer
  249. testSuccess("user2", "repo1", "user1", []string{"user1"}, []int64{})
  250. // Private repo, team member
  251. testSuccess("org17", "big_test_private_4", "user20", []string{"user2"}, []int64{2})
  252. // Private repo, not a team member
  253. testSuccess("org17", "big_test_private_4", "user20", []string{"user5"}, []int64{})
  254. // Private repo, whole team
  255. testSuccess("org17", "big_test_private_4", "user15", []string{"org17/owners"}, []int64{18})
  256. }
  257. func TestResourceIndex(t *testing.T) {
  258. assert.NoError(t, unittest.PrepareTestDatabase())
  259. var wg sync.WaitGroup
  260. for i := range 100 {
  261. wg.Add(1)
  262. go func(i int) {
  263. testInsertIssue(t, fmt.Sprintf("issue %d", i+1), "my issue", 0)
  264. wg.Done()
  265. }(i)
  266. }
  267. wg.Wait()
  268. }
  269. func TestCorrectIssueStats(t *testing.T) {
  270. assert.NoError(t, unittest.PrepareTestDatabase())
  271. // Because the condition is to have chunked database look-ups,
  272. // We have to more issues than `maxQueryParameters`, we will insert.
  273. // maxQueryParameters + 10 issues into the testDatabase.
  274. // Each new issues will have a constant description "Bugs are nasty"
  275. // Which will be used later on.
  276. issueAmount := issues_model.MaxQueryParameters + 10
  277. var wg sync.WaitGroup
  278. for i := range issueAmount {
  279. wg.Add(1)
  280. go func(i int) {
  281. testInsertIssue(t, fmt.Sprintf("Issue %d", i+1), "Bugs are nasty", 0)
  282. wg.Done()
  283. }(i)
  284. }
  285. wg.Wait()
  286. // Now we will get all issueID's that match the "Bugs are nasty" query.
  287. issues, err := issues_model.Issues(t.Context(), &issues_model.IssuesOptions{
  288. Paginator: &db.ListOptions{
  289. PageSize: issueAmount,
  290. },
  291. RepoIDs: []int64{1},
  292. })
  293. total := int64(len(issues))
  294. var ids []int64
  295. for _, issue := range issues {
  296. if issue.Content == "Bugs are nasty" {
  297. ids = append(ids, issue.ID)
  298. }
  299. }
  300. // Just to be sure.
  301. assert.NoError(t, err)
  302. assert.EqualValues(t, issueAmount, total)
  303. // Now we will call the GetIssueStats with these IDs and if working,
  304. // get the correct stats back.
  305. issueStats, err := issues_model.GetIssueStats(t.Context(), &issues_model.IssuesOptions{
  306. RepoIDs: []int64{1},
  307. IssueIDs: ids,
  308. })
  309. // Now check the values.
  310. assert.NoError(t, err)
  311. assert.EqualValues(t, issueStats.OpenCount, issueAmount)
  312. }
  313. func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
  314. assert.NoError(t, unittest.PrepareTestDatabase())
  315. miles := issues_model.MilestoneList{
  316. unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}),
  317. }
  318. assert.NoError(t, miles.LoadTotalTrackedTimes(t.Context()))
  319. assert.Equal(t, int64(3682), miles[0].TotalTrackedTime)
  320. }
  321. func TestLoadTotalTrackedTime(t *testing.T) {
  322. assert.NoError(t, unittest.PrepareTestDatabase())
  323. milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
  324. assert.NoError(t, milestone.LoadTotalTrackedTime(t.Context()))
  325. assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
  326. }
  327. func TestCountIssues(t *testing.T) {
  328. assert.NoError(t, unittest.PrepareTestDatabase())
  329. count, err := issues_model.CountIssues(t.Context(), &issues_model.IssuesOptions{})
  330. assert.NoError(t, err)
  331. assert.EqualValues(t, 22, count)
  332. }
  333. func TestIssueLoadAttributes(t *testing.T) {
  334. assert.NoError(t, unittest.PrepareTestDatabase())
  335. setting.Service.EnableTimetracking = true
  336. issueList := issues_model.IssueList{
  337. unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}),
  338. unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4}),
  339. }
  340. for _, issue := range issueList {
  341. assert.NoError(t, issue.LoadAttributes(t.Context()))
  342. assert.Equal(t, issue.RepoID, issue.Repo.ID)
  343. for _, label := range issue.Labels {
  344. assert.Equal(t, issue.RepoID, label.RepoID)
  345. unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  346. }
  347. if issue.PosterID > 0 {
  348. assert.Equal(t, issue.PosterID, issue.Poster.ID)
  349. }
  350. if issue.AssigneeID > 0 {
  351. assert.Equal(t, issue.AssigneeID, issue.Assignee.ID)
  352. }
  353. if issue.MilestoneID > 0 {
  354. assert.Equal(t, issue.MilestoneID, issue.Milestone.ID)
  355. }
  356. if issue.IsPull {
  357. assert.Equal(t, issue.ID, issue.PullRequest.IssueID)
  358. }
  359. for _, attachment := range issue.Attachments {
  360. assert.Equal(t, issue.ID, attachment.IssueID)
  361. }
  362. for _, comment := range issue.Comments {
  363. assert.Equal(t, issue.ID, comment.IssueID)
  364. }
  365. if issue.ID == int64(1) {
  366. assert.Equal(t, int64(400), issue.TotalTrackedTime)
  367. assert.NotNil(t, issue.Project)
  368. assert.Equal(t, int64(1), issue.Project.ID)
  369. } else {
  370. assert.Nil(t, issue.Project)
  371. }
  372. }
  373. }
  374. func assertCreateIssues(t *testing.T, isPull bool) {
  375. assert.NoError(t, unittest.PrepareTestDatabase())
  376. reponame := "repo1"
  377. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
  378. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  379. label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
  380. milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
  381. assert.EqualValues(t, 1, milestone.ID)
  382. reaction := &issues_model.Reaction{
  383. Type: "heart",
  384. UserID: owner.ID,
  385. }
  386. title := "issuetitle1"
  387. is := &issues_model.Issue{
  388. RepoID: repo.ID,
  389. MilestoneID: milestone.ID,
  390. Repo: repo,
  391. Title: title,
  392. Content: "issuecontent1",
  393. IsPull: isPull,
  394. PosterID: owner.ID,
  395. Poster: owner,
  396. IsClosed: true,
  397. Labels: []*issues_model.Label{label},
  398. Reactions: []*issues_model.Reaction{reaction},
  399. }
  400. err := issues_model.InsertIssues(t.Context(), is)
  401. assert.NoError(t, err)
  402. i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: title})
  403. unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: owner.ID, IssueID: i.ID})
  404. }
  405. func TestMigrate_CreateIssuesIsPullFalse(t *testing.T) {
  406. assertCreateIssues(t, false)
  407. }
  408. func TestMigrate_CreateIssuesIsPullTrue(t *testing.T) {
  409. assertCreateIssues(t, true)
  410. }