actions_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // Copyright 2022 The Gogs 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 database
  5. import (
  6. "context"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/gogs/git-module"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "gorm.io/gorm"
  14. "gogs.io/gogs/internal/conf"
  15. )
  16. func TestIssueReferencePattern(t *testing.T) {
  17. tests := []struct {
  18. name string
  19. message string
  20. want []string
  21. }{
  22. {
  23. name: "no match",
  24. message: "Hello world!",
  25. want: nil,
  26. },
  27. {
  28. name: "contains issue numbers",
  29. message: "#123 is fixed, and #456 is WIP",
  30. want: []string{"#123", " #456"},
  31. },
  32. {
  33. name: "contains full issue references",
  34. message: "#123 is fixed, and user/repo#456 is WIP",
  35. want: []string{"#123", " user/repo#456"},
  36. },
  37. }
  38. for _, test := range tests {
  39. t.Run(test.name, func(t *testing.T) {
  40. got := issueReferencePattern.FindAllString(test.message, -1)
  41. assert.Equal(t, test.want, got)
  42. })
  43. }
  44. }
  45. func TestAction_BeforeCreate(t *testing.T) {
  46. now := time.Now()
  47. db := &gorm.DB{
  48. Config: &gorm.Config{
  49. SkipDefaultTransaction: true,
  50. NowFunc: func() time.Time {
  51. return now
  52. },
  53. },
  54. }
  55. t.Run("CreatedUnix has been set", func(t *testing.T) {
  56. action := &Action{
  57. CreatedUnix: 1,
  58. }
  59. _ = action.BeforeCreate(db)
  60. assert.Equal(t, int64(1), action.CreatedUnix)
  61. })
  62. t.Run("CreatedUnix has not been set", func(t *testing.T) {
  63. action := &Action{}
  64. _ = action.BeforeCreate(db)
  65. assert.Equal(t, db.NowFunc().Unix(), action.CreatedUnix)
  66. })
  67. }
  68. func TestAction_AfterFind(t *testing.T) {
  69. now := time.Now()
  70. db := &gorm.DB{
  71. Config: &gorm.Config{
  72. SkipDefaultTransaction: true,
  73. NowFunc: func() time.Time {
  74. return now
  75. },
  76. },
  77. }
  78. action := &Action{
  79. CreatedUnix: now.Unix(),
  80. }
  81. _ = action.AfterFind(db)
  82. assert.Equal(t, action.CreatedUnix, action.Created.Unix())
  83. }
  84. func TestActions(t *testing.T) {
  85. if testing.Short() {
  86. t.Skip()
  87. }
  88. ctx := context.Background()
  89. t.Parallel()
  90. s := &ActionsStore{
  91. db: newTestDB(t, "ActionsStore"),
  92. }
  93. for _, tc := range []struct {
  94. name string
  95. test func(t *testing.T, ctx context.Context, s *ActionsStore)
  96. }{
  97. {"CommitRepo", actionsCommitRepo},
  98. {"ListByOrganization", actionsListByOrganization},
  99. {"ListByUser", actionsListByUser},
  100. {"MergePullRequest", actionsMergePullRequest},
  101. {"MirrorSyncCreate", actionsMirrorSyncCreate},
  102. {"MirrorSyncDelete", actionsMirrorSyncDelete},
  103. {"MirrorSyncPush", actionsMirrorSyncPush},
  104. {"NewRepo", actionsNewRepo},
  105. {"PushTag", actionsPushTag},
  106. {"RenameRepo", actionsRenameRepo},
  107. {"TransferRepo", actionsTransferRepo},
  108. } {
  109. t.Run(tc.name, func(t *testing.T) {
  110. t.Cleanup(func() {
  111. err := clearTables(t, s.db)
  112. require.NoError(t, err)
  113. })
  114. tc.test(t, ctx, s)
  115. })
  116. if t.Failed() {
  117. break
  118. }
  119. }
  120. }
  121. func actionsCommitRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
  122. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  123. require.NoError(t, err)
  124. repo, err := newReposStore(s.db).Create(ctx,
  125. alice.ID,
  126. CreateRepoOptions{
  127. Name: "example",
  128. },
  129. )
  130. require.NoError(t, err)
  131. now := time.Unix(1588568886, 0).UTC()
  132. conf.SetMockSSH(t, conf.SSHOpts{})
  133. t.Run("new commit", func(t *testing.T) {
  134. t.Cleanup(func() {
  135. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  136. require.NoError(t, err)
  137. })
  138. err = s.CommitRepo(ctx,
  139. CommitRepoOptions{
  140. PusherName: alice.Name,
  141. Owner: alice,
  142. Repo: repo,
  143. RefFullName: "refs/heads/main",
  144. OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
  145. NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
  146. Commits: CommitsToPushCommits(
  147. []*git.Commit{
  148. {
  149. ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
  150. Author: &git.Signature{
  151. Name: "alice",
  152. Email: "[email protected]",
  153. When: now,
  154. },
  155. Committer: &git.Signature{
  156. Name: "alice",
  157. Email: "[email protected]",
  158. When: now,
  159. },
  160. Message: "A random commit",
  161. },
  162. },
  163. ),
  164. },
  165. )
  166. require.NoError(t, err)
  167. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  168. require.NoError(t, err)
  169. require.Len(t, got, 1)
  170. got[0].ID = 0
  171. want := []*Action{
  172. {
  173. UserID: alice.ID,
  174. OpType: ActionCommitRepo,
  175. ActUserID: alice.ID,
  176. ActUserName: alice.Name,
  177. RepoID: repo.ID,
  178. RepoUserName: alice.Name,
  179. RepoName: repo.Name,
  180. RefName: "main",
  181. IsPrivate: false,
  182. Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"[email protected]","AuthorName":"alice","CommitterEmail":"[email protected]","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":"alice/example/compare/ca82a6dff817ec66f44342007202690a93763949...085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"}`,
  183. CreatedUnix: s.db.NowFunc().Unix(),
  184. },
  185. }
  186. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  187. assert.Equal(t, want, got)
  188. })
  189. t.Run("new ref", func(t *testing.T) {
  190. t.Cleanup(func() {
  191. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  192. require.NoError(t, err)
  193. })
  194. err = s.CommitRepo(ctx,
  195. CommitRepoOptions{
  196. PusherName: alice.Name,
  197. Owner: alice,
  198. Repo: repo,
  199. RefFullName: "refs/heads/main",
  200. OldCommitID: git.EmptyID,
  201. NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
  202. Commits: CommitsToPushCommits(
  203. []*git.Commit{
  204. {
  205. ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
  206. Author: &git.Signature{
  207. Name: "alice",
  208. Email: "[email protected]",
  209. When: now,
  210. },
  211. Committer: &git.Signature{
  212. Name: "alice",
  213. Email: "[email protected]",
  214. When: now,
  215. },
  216. Message: "A random commit",
  217. },
  218. },
  219. ),
  220. },
  221. )
  222. require.NoError(t, err)
  223. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  224. require.NoError(t, err)
  225. require.Len(t, got, 2)
  226. got[0].ID = 0
  227. got[1].ID = 0
  228. want := []*Action{
  229. {
  230. UserID: alice.ID,
  231. OpType: ActionCommitRepo,
  232. ActUserID: alice.ID,
  233. ActUserName: alice.Name,
  234. RepoID: repo.ID,
  235. RepoUserName: alice.Name,
  236. RepoName: repo.Name,
  237. RefName: "main",
  238. IsPrivate: false,
  239. Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"[email protected]","AuthorName":"alice","CommitterEmail":"[email protected]","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":""}`,
  240. CreatedUnix: s.db.NowFunc().Unix(),
  241. },
  242. {
  243. UserID: alice.ID,
  244. OpType: ActionCreateBranch,
  245. ActUserID: alice.ID,
  246. ActUserName: alice.Name,
  247. RepoID: repo.ID,
  248. RepoUserName: alice.Name,
  249. RepoName: repo.Name,
  250. RefName: "main",
  251. IsPrivate: false,
  252. Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"[email protected]","AuthorName":"alice","CommitterEmail":"[email protected]","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":""}`,
  253. CreatedUnix: s.db.NowFunc().Unix(),
  254. },
  255. }
  256. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  257. want[1].Created = time.Unix(want[1].CreatedUnix, 0)
  258. assert.Equal(t, want, got)
  259. })
  260. t.Run("delete ref", func(t *testing.T) {
  261. t.Cleanup(func() {
  262. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  263. require.NoError(t, err)
  264. })
  265. err = s.CommitRepo(ctx,
  266. CommitRepoOptions{
  267. PusherName: alice.Name,
  268. Owner: alice,
  269. Repo: repo,
  270. RefFullName: "refs/heads/main",
  271. OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
  272. NewCommitID: git.EmptyID,
  273. },
  274. )
  275. require.NoError(t, err)
  276. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  277. require.NoError(t, err)
  278. require.Len(t, got, 1)
  279. got[0].ID = 0
  280. want := []*Action{
  281. {
  282. UserID: alice.ID,
  283. OpType: ActionDeleteBranch,
  284. ActUserID: alice.ID,
  285. ActUserName: alice.Name,
  286. RepoID: repo.ID,
  287. RepoUserName: alice.Name,
  288. RepoName: repo.Name,
  289. RefName: "main",
  290. IsPrivate: false,
  291. CreatedUnix: s.db.NowFunc().Unix(),
  292. },
  293. }
  294. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  295. assert.Equal(t, want, got)
  296. })
  297. }
  298. func actionsListByOrganization(t *testing.T, ctx context.Context, s *ActionsStore) {
  299. if os.Getenv("GOGS_DATABASE_TYPE") != "postgres" {
  300. t.Skip("Skipping testing with not using PostgreSQL")
  301. return
  302. }
  303. conf.SetMockUI(t,
  304. conf.UIOpts{
  305. User: conf.UIUserOpts{
  306. NewsFeedPagingNum: 20,
  307. },
  308. },
  309. )
  310. tests := []struct {
  311. name string
  312. orgID int64
  313. actorID int64
  314. afterID int64
  315. want string
  316. }{
  317. {
  318. name: "no afterID",
  319. orgID: 1,
  320. actorID: 1,
  321. afterID: 0,
  322. want: `SELECT * FROM "action" WHERE user_id = 1 AND (true OR id < 0) AND repo_id IN (SELECT repository.id FROM "repository" JOIN team_repo ON repository.id = team_repo.repo_id WHERE team_repo.team_id IN (SELECT team_id FROM "team_user" WHERE team_user.org_id = 1 AND uid = 1) OR (repository.is_private = false AND repository.is_unlisted = false)) ORDER BY id DESC LIMIT 20`,
  323. },
  324. {
  325. name: "has afterID",
  326. orgID: 1,
  327. actorID: 1,
  328. afterID: 5,
  329. want: `SELECT * FROM "action" WHERE user_id = 1 AND (false OR id < 5) AND repo_id IN (SELECT repository.id FROM "repository" JOIN team_repo ON repository.id = team_repo.repo_id WHERE team_repo.team_id IN (SELECT team_id FROM "team_user" WHERE team_user.org_id = 1 AND uid = 1) OR (repository.is_private = false AND repository.is_unlisted = false)) ORDER BY id DESC LIMIT 20`,
  330. },
  331. }
  332. for _, test := range tests {
  333. t.Run(test.name, func(t *testing.T) {
  334. got := s.db.ToSQL(func(tx *gorm.DB) *gorm.DB {
  335. return newActionsStore(tx).listByOrganization(ctx, test.orgID, test.actorID, test.afterID).Find(new(Action))
  336. })
  337. assert.Equal(t, test.want, got)
  338. })
  339. }
  340. }
  341. func actionsListByUser(t *testing.T, ctx context.Context, s *ActionsStore) {
  342. if os.Getenv("GOGS_DATABASE_TYPE") != "postgres" {
  343. t.Skip("Skipping testing with not using PostgreSQL")
  344. return
  345. }
  346. conf.SetMockUI(t,
  347. conf.UIOpts{
  348. User: conf.UIUserOpts{
  349. NewsFeedPagingNum: 20,
  350. },
  351. },
  352. )
  353. tests := []struct {
  354. name string
  355. userID int64
  356. actorID int64
  357. afterID int64
  358. isProfile bool
  359. want string
  360. }{
  361. {
  362. name: "same user no afterID not in profile",
  363. userID: 1,
  364. actorID: 1,
  365. afterID: 0,
  366. isProfile: false,
  367. want: `SELECT * FROM "action" WHERE user_id = 1 AND (true OR id < 0) AND (true OR (is_private = false AND act_user_id = 1)) ORDER BY id DESC LIMIT 20`,
  368. },
  369. {
  370. name: "same user no afterID in profile",
  371. userID: 1,
  372. actorID: 1,
  373. afterID: 0,
  374. isProfile: true,
  375. want: `SELECT * FROM "action" WHERE user_id = 1 AND (true OR id < 0) AND (true OR (is_private = false AND act_user_id = 1)) ORDER BY id DESC LIMIT 20`,
  376. },
  377. {
  378. name: "same user has afterID not in profile",
  379. userID: 1,
  380. actorID: 1,
  381. afterID: 5,
  382. isProfile: false,
  383. want: `SELECT * FROM "action" WHERE user_id = 1 AND (false OR id < 5) AND (true OR (is_private = false AND act_user_id = 1)) ORDER BY id DESC LIMIT 20`,
  384. },
  385. {
  386. name: "different user no afterID in profile",
  387. userID: 1,
  388. actorID: 2,
  389. afterID: 0,
  390. isProfile: true,
  391. want: `SELECT * FROM "action" WHERE user_id = 1 AND (true OR id < 0) AND (false OR (is_private = false AND act_user_id = 1)) ORDER BY id DESC LIMIT 20`,
  392. },
  393. }
  394. for _, test := range tests {
  395. t.Run(test.name, func(t *testing.T) {
  396. got := s.db.ToSQL(func(tx *gorm.DB) *gorm.DB {
  397. return newActionsStore(tx).listByUser(ctx, test.userID, test.actorID, test.afterID, test.isProfile).Find(new(Action))
  398. })
  399. assert.Equal(t, test.want, got)
  400. })
  401. }
  402. }
  403. func actionsMergePullRequest(t *testing.T, ctx context.Context, s *ActionsStore) {
  404. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  405. require.NoError(t, err)
  406. repo, err := newReposStore(s.db).Create(ctx,
  407. alice.ID,
  408. CreateRepoOptions{
  409. Name: "example",
  410. },
  411. )
  412. require.NoError(t, err)
  413. err = s.MergePullRequest(ctx,
  414. alice,
  415. alice,
  416. repo,
  417. &Issue{
  418. Index: 1,
  419. Title: "Fix issue 1",
  420. },
  421. )
  422. require.NoError(t, err)
  423. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  424. require.NoError(t, err)
  425. require.Len(t, got, 1)
  426. got[0].ID = 0
  427. want := []*Action{
  428. {
  429. UserID: alice.ID,
  430. OpType: ActionMergePullRequest,
  431. ActUserID: alice.ID,
  432. ActUserName: alice.Name,
  433. RepoID: repo.ID,
  434. RepoUserName: alice.Name,
  435. RepoName: repo.Name,
  436. IsPrivate: false,
  437. Content: `1|Fix issue 1`,
  438. CreatedUnix: s.db.NowFunc().Unix(),
  439. },
  440. }
  441. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  442. assert.Equal(t, want, got)
  443. }
  444. func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, s *ActionsStore) {
  445. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  446. require.NoError(t, err)
  447. repo, err := newReposStore(s.db).Create(ctx,
  448. alice.ID,
  449. CreateRepoOptions{
  450. Name: "example",
  451. },
  452. )
  453. require.NoError(t, err)
  454. err = s.MirrorSyncCreate(ctx,
  455. alice,
  456. repo,
  457. "main",
  458. )
  459. require.NoError(t, err)
  460. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  461. require.NoError(t, err)
  462. require.Len(t, got, 1)
  463. got[0].ID = 0
  464. want := []*Action{
  465. {
  466. UserID: alice.ID,
  467. OpType: ActionMirrorSyncCreate,
  468. ActUserID: alice.ID,
  469. ActUserName: alice.Name,
  470. RepoID: repo.ID,
  471. RepoUserName: alice.Name,
  472. RepoName: repo.Name,
  473. RefName: "main",
  474. IsPrivate: false,
  475. CreatedUnix: s.db.NowFunc().Unix(),
  476. },
  477. }
  478. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  479. assert.Equal(t, want, got)
  480. }
  481. func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, s *ActionsStore) {
  482. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  483. require.NoError(t, err)
  484. repo, err := newReposStore(s.db).Create(ctx,
  485. alice.ID,
  486. CreateRepoOptions{
  487. Name: "example",
  488. },
  489. )
  490. require.NoError(t, err)
  491. err = s.MirrorSyncDelete(ctx,
  492. alice,
  493. repo,
  494. "main",
  495. )
  496. require.NoError(t, err)
  497. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  498. require.NoError(t, err)
  499. require.Len(t, got, 1)
  500. got[0].ID = 0
  501. want := []*Action{
  502. {
  503. UserID: alice.ID,
  504. OpType: ActionMirrorSyncDelete,
  505. ActUserID: alice.ID,
  506. ActUserName: alice.Name,
  507. RepoID: repo.ID,
  508. RepoUserName: alice.Name,
  509. RepoName: repo.Name,
  510. RefName: "main",
  511. IsPrivate: false,
  512. CreatedUnix: s.db.NowFunc().Unix(),
  513. },
  514. }
  515. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  516. assert.Equal(t, want, got)
  517. }
  518. func actionsMirrorSyncPush(t *testing.T, ctx context.Context, s *ActionsStore) {
  519. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  520. require.NoError(t, err)
  521. repo, err := newReposStore(s.db).Create(ctx,
  522. alice.ID,
  523. CreateRepoOptions{
  524. Name: "example",
  525. },
  526. )
  527. require.NoError(t, err)
  528. now := time.Unix(1588568886, 0).UTC()
  529. err = s.MirrorSyncPush(ctx,
  530. MirrorSyncPushOptions{
  531. Owner: alice,
  532. Repo: repo,
  533. RefName: "main",
  534. OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
  535. NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
  536. Commits: CommitsToPushCommits(
  537. []*git.Commit{
  538. {
  539. ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
  540. Author: &git.Signature{
  541. Name: "alice",
  542. Email: "[email protected]",
  543. When: now,
  544. },
  545. Committer: &git.Signature{
  546. Name: "alice",
  547. Email: "[email protected]",
  548. When: now,
  549. },
  550. Message: "A random commit",
  551. },
  552. },
  553. ),
  554. },
  555. )
  556. require.NoError(t, err)
  557. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  558. require.NoError(t, err)
  559. require.Len(t, got, 1)
  560. got[0].ID = 0
  561. want := []*Action{
  562. {
  563. UserID: alice.ID,
  564. OpType: ActionMirrorSyncPush,
  565. ActUserID: alice.ID,
  566. ActUserName: alice.Name,
  567. RepoID: repo.ID,
  568. RepoUserName: alice.Name,
  569. RepoName: repo.Name,
  570. RefName: "main",
  571. IsPrivate: false,
  572. Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"[email protected]","AuthorName":"alice","CommitterEmail":"[email protected]","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":"alice/example/compare/ca82a6dff817ec66f44342007202690a93763949...085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"}`,
  573. CreatedUnix: s.db.NowFunc().Unix(),
  574. },
  575. }
  576. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  577. assert.Equal(t, want, got)
  578. }
  579. func actionsNewRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
  580. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  581. require.NoError(t, err)
  582. repo, err := newReposStore(s.db).Create(ctx,
  583. alice.ID,
  584. CreateRepoOptions{
  585. Name: "example",
  586. },
  587. )
  588. require.NoError(t, err)
  589. t.Run("new repo", func(t *testing.T) {
  590. t.Cleanup(func() {
  591. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  592. require.NoError(t, err)
  593. })
  594. err = s.NewRepo(ctx, alice, alice, repo)
  595. require.NoError(t, err)
  596. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  597. require.NoError(t, err)
  598. require.Len(t, got, 1)
  599. got[0].ID = 0
  600. want := []*Action{
  601. {
  602. UserID: alice.ID,
  603. OpType: ActionCreateRepo,
  604. ActUserID: alice.ID,
  605. ActUserName: alice.Name,
  606. RepoID: repo.ID,
  607. RepoUserName: alice.Name,
  608. RepoName: repo.Name,
  609. IsPrivate: false,
  610. CreatedUnix: s.db.NowFunc().Unix(),
  611. },
  612. }
  613. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  614. assert.Equal(t, want, got)
  615. })
  616. t.Run("fork repo", func(t *testing.T) {
  617. t.Cleanup(func() {
  618. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  619. require.NoError(t, err)
  620. })
  621. repo.IsFork = true
  622. err = s.NewRepo(ctx, alice, alice, repo)
  623. require.NoError(t, err)
  624. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  625. require.NoError(t, err)
  626. require.Len(t, got, 1)
  627. got[0].ID = 0
  628. want := []*Action{
  629. {
  630. UserID: alice.ID,
  631. OpType: ActionForkRepo,
  632. ActUserID: alice.ID,
  633. ActUserName: alice.Name,
  634. RepoID: repo.ID,
  635. RepoUserName: alice.Name,
  636. RepoName: repo.Name,
  637. IsPrivate: false,
  638. CreatedUnix: s.db.NowFunc().Unix(),
  639. },
  640. }
  641. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  642. assert.Equal(t, want, got)
  643. })
  644. }
  645. func actionsPushTag(t *testing.T, ctx context.Context, s *ActionsStore) {
  646. // NOTE: We set a noop mock here to avoid data race with other tests that writes
  647. // to the mock server because this function holds a lock.
  648. conf.SetMockServer(t, conf.ServerOpts{})
  649. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  650. require.NoError(t, err)
  651. repo, err := newReposStore(s.db).Create(ctx,
  652. alice.ID,
  653. CreateRepoOptions{
  654. Name: "example",
  655. },
  656. )
  657. require.NoError(t, err)
  658. t.Run("new tag", func(t *testing.T) {
  659. t.Cleanup(func() {
  660. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  661. require.NoError(t, err)
  662. })
  663. err = s.PushTag(ctx,
  664. PushTagOptions{
  665. Owner: alice,
  666. Repo: repo,
  667. PusherName: alice.Name,
  668. RefFullName: "refs/tags/v1.0.0",
  669. NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
  670. },
  671. )
  672. require.NoError(t, err)
  673. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  674. require.NoError(t, err)
  675. require.Len(t, got, 1)
  676. got[0].ID = 0
  677. want := []*Action{
  678. {
  679. UserID: alice.ID,
  680. OpType: ActionPushTag,
  681. ActUserID: alice.ID,
  682. ActUserName: alice.Name,
  683. RepoID: repo.ID,
  684. RepoUserName: alice.Name,
  685. RepoName: repo.Name,
  686. RefName: "v1.0.0",
  687. IsPrivate: false,
  688. CreatedUnix: s.db.NowFunc().Unix(),
  689. },
  690. }
  691. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  692. assert.Equal(t, want, got)
  693. })
  694. t.Run("delete tag", func(t *testing.T) {
  695. t.Cleanup(func() {
  696. err := s.db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
  697. require.NoError(t, err)
  698. })
  699. err = s.PushTag(ctx,
  700. PushTagOptions{
  701. Owner: alice,
  702. Repo: repo,
  703. PusherName: alice.Name,
  704. RefFullName: "refs/tags/v1.0.0",
  705. NewCommitID: git.EmptyID,
  706. },
  707. )
  708. require.NoError(t, err)
  709. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  710. require.NoError(t, err)
  711. require.Len(t, got, 1)
  712. got[0].ID = 0
  713. want := []*Action{
  714. {
  715. UserID: alice.ID,
  716. OpType: ActionDeleteTag,
  717. ActUserID: alice.ID,
  718. ActUserName: alice.Name,
  719. RepoID: repo.ID,
  720. RepoUserName: alice.Name,
  721. RepoName: repo.Name,
  722. RefName: "v1.0.0",
  723. IsPrivate: false,
  724. CreatedUnix: s.db.NowFunc().Unix(),
  725. },
  726. }
  727. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  728. assert.Equal(t, want, got)
  729. })
  730. }
  731. func actionsRenameRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
  732. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  733. require.NoError(t, err)
  734. repo, err := newReposStore(s.db).Create(ctx,
  735. alice.ID,
  736. CreateRepoOptions{
  737. Name: "example",
  738. },
  739. )
  740. require.NoError(t, err)
  741. err = s.RenameRepo(ctx, alice, alice, "oldExample", repo)
  742. require.NoError(t, err)
  743. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  744. require.NoError(t, err)
  745. require.Len(t, got, 1)
  746. got[0].ID = 0
  747. want := []*Action{
  748. {
  749. UserID: alice.ID,
  750. OpType: ActionRenameRepo,
  751. ActUserID: alice.ID,
  752. ActUserName: alice.Name,
  753. RepoID: repo.ID,
  754. RepoUserName: alice.Name,
  755. RepoName: repo.Name,
  756. IsPrivate: false,
  757. Content: "oldExample",
  758. CreatedUnix: s.db.NowFunc().Unix(),
  759. },
  760. }
  761. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  762. assert.Equal(t, want, got)
  763. }
  764. func actionsTransferRepo(t *testing.T, ctx context.Context, s *ActionsStore) {
  765. alice, err := newUsersStore(s.db).Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  766. require.NoError(t, err)
  767. bob, err := newUsersStore(s.db).Create(ctx, "bob", "[email protected]", CreateUserOptions{})
  768. require.NoError(t, err)
  769. repo, err := newReposStore(s.db).Create(ctx,
  770. alice.ID,
  771. CreateRepoOptions{
  772. Name: "example",
  773. },
  774. )
  775. require.NoError(t, err)
  776. err = s.TransferRepo(ctx, alice, alice, bob, repo)
  777. require.NoError(t, err)
  778. got, err := s.ListByUser(ctx, alice.ID, alice.ID, 0, false)
  779. require.NoError(t, err)
  780. require.Len(t, got, 1)
  781. got[0].ID = 0
  782. want := []*Action{
  783. {
  784. UserID: alice.ID,
  785. OpType: ActionTransferRepo,
  786. ActUserID: alice.ID,
  787. ActUserName: alice.Name,
  788. RepoID: repo.ID,
  789. RepoUserName: bob.Name,
  790. RepoName: repo.Name,
  791. IsPrivate: false,
  792. Content: "alice/example",
  793. CreatedUnix: s.db.NowFunc().Unix(),
  794. },
  795. }
  796. want[0].Created = time.Unix(want[0].CreatedUnix, 0)
  797. assert.Equal(t, want, got)
  798. }