123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- // Copyright 2022 The Gogs Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
- package db
- import (
- "context"
- "os"
- "testing"
- "time"
- "github.com/gogs/git-module"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "gorm.io/gorm"
- "gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/dbtest"
- )
- func TestIssueReferencePattern(t *testing.T) {
- tests := []struct {
- name string
- message string
- want []string
- }{
- {
- name: "no match",
- message: "Hello world!",
- want: nil,
- },
- {
- name: "contains issue numbers",
- message: "#123 is fixed, and #456 is WIP",
- want: []string{"#123", " #456"},
- },
- {
- name: "contains full issue references",
- message: "#123 is fixed, and user/repo#456 is WIP",
- want: []string{"#123", " user/repo#456"},
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- got := issueReferencePattern.FindAllString(test.message, -1)
- assert.Equal(t, test.want, got)
- })
- }
- }
- func TestAction_BeforeCreate(t *testing.T) {
- now := time.Now()
- db := &gorm.DB{
- Config: &gorm.Config{
- SkipDefaultTransaction: true,
- NowFunc: func() time.Time {
- return now
- },
- },
- }
- t.Run("CreatedUnix has been set", func(t *testing.T) {
- action := &Action{
- CreatedUnix: 1,
- }
- _ = action.BeforeCreate(db)
- assert.Equal(t, int64(1), action.CreatedUnix)
- })
- t.Run("CreatedUnix has not been set", func(t *testing.T) {
- action := &Action{}
- _ = action.BeforeCreate(db)
- assert.Equal(t, db.NowFunc().Unix(), action.CreatedUnix)
- })
- }
- func TestAction_AfterFind(t *testing.T) {
- now := time.Now()
- db := &gorm.DB{
- Config: &gorm.Config{
- SkipDefaultTransaction: true,
- NowFunc: func() time.Time {
- return now
- },
- },
- }
- action := &Action{
- CreatedUnix: now.Unix(),
- }
- _ = action.AfterFind(db)
- assert.Equal(t, action.CreatedUnix, action.Created.Unix())
- }
- func TestActions(t *testing.T) {
- if testing.Short() {
- t.Skip()
- }
- ctx := context.Background()
- t.Parallel()
- tables := []any{new(Action), new(User), new(Repository), new(EmailAddress), new(Watch)}
- db := &actions{
- DB: dbtest.NewDB(t, "actions", tables...),
- }
- for _, tc := range []struct {
- name string
- test func(t *testing.T, ctx context.Context, db *actions)
- }{
- {"CommitRepo", actionsCommitRepo},
- {"ListByOrganization", actionsListByOrganization},
- {"ListByUser", actionsListByUser},
- {"MergePullRequest", actionsMergePullRequest},
- {"MirrorSyncCreate", actionsMirrorSyncCreate},
- {"MirrorSyncDelete", actionsMirrorSyncDelete},
- {"MirrorSyncPush", actionsMirrorSyncPush},
- {"NewRepo", actionsNewRepo},
- {"PushTag", actionsPushTag},
- {"RenameRepo", actionsRenameRepo},
- {"TransferRepo", actionsTransferRepo},
- } {
- t.Run(tc.name, func(t *testing.T) {
- t.Cleanup(func() {
- err := clearTables(t, db.DB, tables...)
- require.NoError(t, err)
- })
- tc.test(t, ctx, db)
- })
- if t.Failed() {
- break
- }
- }
- }
- func actionsCommitRepo(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- now := time.Unix(1588568886, 0).UTC()
- conf.SetMockSSH(t, conf.SSHOpts{})
- t.Run("new commit", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.CommitRepo(ctx,
- CommitRepoOptions{
- PusherName: alice.Name,
- Owner: alice,
- Repo: repo,
- RefFullName: "refs/heads/main",
- OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
- NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
- Commits: CommitsToPushCommits(
- []*git.Commit{
- {
- ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
- Author: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Committer: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Message: "A random commit",
- },
- },
- ),
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionCommitRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"alice@example.com","AuthorName":"alice","CommitterEmail":"alice@example.com","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":"alice/example/compare/ca82a6dff817ec66f44342007202690a93763949...085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"}`,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- t.Run("new ref", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.CommitRepo(ctx,
- CommitRepoOptions{
- PusherName: alice.Name,
- Owner: alice,
- Repo: repo,
- RefFullName: "refs/heads/main",
- OldCommitID: git.EmptyID,
- NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
- Commits: CommitsToPushCommits(
- []*git.Commit{
- {
- ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
- Author: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Committer: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Message: "A random commit",
- },
- },
- ),
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 2)
- got[0].ID = 0
- got[1].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionCommitRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"alice@example.com","AuthorName":"alice","CommitterEmail":"alice@example.com","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":""}`,
- CreatedUnix: db.NowFunc().Unix(),
- },
- {
- UserID: alice.ID,
- OpType: ActionCreateBranch,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"alice@example.com","AuthorName":"alice","CommitterEmail":"alice@example.com","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":""}`,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- want[1].Created = time.Unix(want[1].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- t.Run("delete ref", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.CommitRepo(ctx,
- CommitRepoOptions{
- PusherName: alice.Name,
- Owner: alice,
- Repo: repo,
- RefFullName: "refs/heads/main",
- OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
- NewCommitID: git.EmptyID,
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionDeleteBranch,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- }
- func actionsListByOrganization(t *testing.T, ctx context.Context, db *actions) {
- if os.Getenv("GOGS_DATABASE_TYPE") != "postgres" {
- t.Skip("Skipping testing with not using PostgreSQL")
- return
- }
- conf.SetMockUI(t,
- conf.UIOpts{
- User: conf.UIUserOpts{
- NewsFeedPagingNum: 20,
- },
- },
- )
- tests := []struct {
- name string
- orgID int64
- actorID int64
- afterID int64
- want string
- }{
- {
- name: "no afterID",
- orgID: 1,
- actorID: 1,
- afterID: 0,
- 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`,
- },
- {
- name: "has afterID",
- orgID: 1,
- actorID: 1,
- afterID: 5,
- 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`,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- got := db.DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
- return NewActionsStore(tx).(*actions).listByOrganization(ctx, test.orgID, test.actorID, test.afterID).Find(new(Action))
- })
- assert.Equal(t, test.want, got)
- })
- }
- }
- func actionsListByUser(t *testing.T, ctx context.Context, db *actions) {
- if os.Getenv("GOGS_DATABASE_TYPE") != "postgres" {
- t.Skip("Skipping testing with not using PostgreSQL")
- return
- }
- conf.SetMockUI(t,
- conf.UIOpts{
- User: conf.UIUserOpts{
- NewsFeedPagingNum: 20,
- },
- },
- )
- tests := []struct {
- name string
- userID int64
- actorID int64
- afterID int64
- isProfile bool
- want string
- }{
- {
- name: "same user no afterID not in profile",
- userID: 1,
- actorID: 1,
- afterID: 0,
- isProfile: false,
- 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`,
- },
- {
- name: "same user no afterID in profile",
- userID: 1,
- actorID: 1,
- afterID: 0,
- isProfile: true,
- 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`,
- },
- {
- name: "same user has afterID not in profile",
- userID: 1,
- actorID: 1,
- afterID: 5,
- isProfile: false,
- 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`,
- },
- {
- name: "different user no afterID in profile",
- userID: 1,
- actorID: 2,
- afterID: 0,
- isProfile: true,
- 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`,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- got := db.DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
- return NewActionsStore(tx).(*actions).listByUser(ctx, test.userID, test.actorID, test.afterID, test.isProfile).Find(new(Action))
- })
- assert.Equal(t, test.want, got)
- })
- }
- }
- func actionsMergePullRequest(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- err = db.MergePullRequest(ctx,
- alice,
- alice,
- repo,
- &Issue{
- Index: 1,
- Title: "Fix issue 1",
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionMergePullRequest,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- IsPrivate: false,
- Content: `1|Fix issue 1`,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
- func actionsMirrorSyncCreate(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- err = db.MirrorSyncCreate(ctx,
- alice,
- repo,
- "main",
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionMirrorSyncCreate,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
- func actionsMirrorSyncDelete(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- err = db.MirrorSyncDelete(ctx,
- alice,
- repo,
- "main",
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionMirrorSyncDelete,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
- func actionsMirrorSyncPush(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- now := time.Unix(1588568886, 0).UTC()
- err = db.MirrorSyncPush(ctx,
- MirrorSyncPushOptions{
- Owner: alice,
- Repo: repo,
- RefName: "main",
- OldCommitID: "ca82a6dff817ec66f44342007202690a93763949",
- NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
- Commits: CommitsToPushCommits(
- []*git.Commit{
- {
- ID: git.MustIDFromString("085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"),
- Author: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Committer: &git.Signature{
- Name: "alice",
- Email: "alice@example.com",
- When: now,
- },
- Message: "A random commit",
- },
- },
- ),
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionMirrorSyncPush,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "main",
- IsPrivate: false,
- Content: `{"Len":1,"Commits":[{"Sha1":"085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7","Message":"A random commit","AuthorEmail":"alice@example.com","AuthorName":"alice","CommitterEmail":"alice@example.com","CommitterName":"alice","Timestamp":"2020-05-04T05:08:06Z"}],"CompareURL":"alice/example/compare/ca82a6dff817ec66f44342007202690a93763949...085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7"}`,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
- func actionsNewRepo(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- t.Run("new repo", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.NewRepo(ctx, alice, alice, repo)
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionCreateRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- t.Run("fork repo", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- repo.IsFork = true
- err = db.NewRepo(ctx, alice, alice, repo)
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionForkRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- }
- func actionsPushTag(t *testing.T, ctx context.Context, db *actions) {
- // NOTE: We set a noop mock here to avoid data race with other tests that writes
- // to the mock server because this function holds a lock.
- conf.SetMockServer(t, conf.ServerOpts{})
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- t.Run("new tag", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.PushTag(ctx,
- PushTagOptions{
- Owner: alice,
- Repo: repo,
- PusherName: alice.Name,
- RefFullName: "refs/tags/v1.0.0",
- NewCommitID: "085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7",
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionPushTag,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "v1.0.0",
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- t.Run("delete tag", func(t *testing.T) {
- t.Cleanup(func() {
- err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).WithContext(ctx).Delete(new(Action)).Error
- require.NoError(t, err)
- })
- err = db.PushTag(ctx,
- PushTagOptions{
- Owner: alice,
- Repo: repo,
- PusherName: alice.Name,
- RefFullName: "refs/tags/v1.0.0",
- NewCommitID: git.EmptyID,
- },
- )
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionDeleteTag,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- RefName: "v1.0.0",
- IsPrivate: false,
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- })
- }
- func actionsRenameRepo(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- err = db.RenameRepo(ctx, alice, alice, "oldExample", repo)
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionRenameRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: alice.Name,
- RepoName: repo.Name,
- IsPrivate: false,
- Content: "oldExample",
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
- func actionsTransferRepo(t *testing.T, ctx context.Context, db *actions) {
- alice, err := NewUsersStore(db.DB).Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
- require.NoError(t, err)
- bob, err := NewUsersStore(db.DB).Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
- require.NoError(t, err)
- repo, err := NewRepositoriesStore(db.DB).Create(ctx,
- alice.ID,
- CreateRepoOptions{
- Name: "example",
- },
- )
- require.NoError(t, err)
- err = db.TransferRepo(ctx, alice, alice, bob, repo)
- require.NoError(t, err)
- got, err := db.ListByUser(ctx, alice.ID, alice.ID, 0, false)
- require.NoError(t, err)
- require.Len(t, got, 1)
- got[0].ID = 0
- want := []*Action{
- {
- UserID: alice.ID,
- OpType: ActionTransferRepo,
- ActUserID: alice.ID,
- ActUserName: alice.Name,
- RepoID: repo.ID,
- RepoUserName: bob.Name,
- RepoName: repo.Name,
- IsPrivate: false,
- Content: "alice/example",
- CreatedUnix: db.NowFunc().Unix(),
- },
- }
- want[0].Created = time.Unix(want[0].CreatedUnix, 0)
- assert.Equal(t, want, got)
- }
|