diff options
author | Joe Chen <jc@unknwon.io> | 2022-10-09 22:55:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-09 22:55:32 +0800 |
commit | 8982a42d38c537de87fabcc59f062841f1c437c2 (patch) | |
tree | bae626255a1582feb14be4b73f5aa0a581c141ab /internal/route | |
parent | fb00e3e56fd777df96e71df1b497e50ff3fbb069 (diff) |
refactor(db): move `User.HasForkedRepository` to `users.HasForkedRepository` (#7176)
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/lfs/mocks_test.go | 126 | ||||
-rw-r--r-- | internal/route/repo/issue.go | 2 |
2 files changed, 127 insertions, 1 deletions
diff --git a/internal/route/lfs/mocks_test.go b/internal/route/lfs/mocks_test.go index 0b7d010b..57bc9fbf 100644 --- a/internal/route/lfs/mocks_test.go +++ b/internal/route/lfs/mocks_test.go @@ -2308,6 +2308,9 @@ type MockUsersStore struct { // GetByUsernameFunc is an instance of a mock function object // controlling the behavior of the method GetByUsername. GetByUsernameFunc *UsersStoreGetByUsernameFunc + // HasForkedRepositoryFunc is an instance of a mock function object + // controlling the behavior of the method HasForkedRepository. + HasForkedRepositoryFunc *UsersStoreHasForkedRepositoryFunc } // NewMockUsersStore creates a new mock of the UsersStore interface. All @@ -2339,6 +2342,11 @@ func NewMockUsersStore() *MockUsersStore { return }, }, + HasForkedRepositoryFunc: &UsersStoreHasForkedRepositoryFunc{ + defaultHook: func(context.Context, int64, int64) (r0 bool) { + return + }, + }, } } @@ -2371,6 +2379,11 @@ func NewStrictMockUsersStore() *MockUsersStore { panic("unexpected invocation of MockUsersStore.GetByUsername") }, }, + HasForkedRepositoryFunc: &UsersStoreHasForkedRepositoryFunc{ + defaultHook: func(context.Context, int64, int64) bool { + panic("unexpected invocation of MockUsersStore.HasForkedRepository") + }, + }, } } @@ -2393,6 +2406,9 @@ func NewMockUsersStoreFrom(i db.UsersStore) *MockUsersStore { GetByUsernameFunc: &UsersStoreGetByUsernameFunc{ defaultHook: i.GetByUsername, }, + HasForkedRepositoryFunc: &UsersStoreHasForkedRepositoryFunc{ + defaultHook: i.HasForkedRepository, + }, } } @@ -2946,3 +2962,113 @@ func (c UsersStoreGetByUsernameFuncCall) Args() []interface{} { func (c UsersStoreGetByUsernameFuncCall) Results() []interface{} { return []interface{}{c.Result0, c.Result1} } + +// UsersStoreHasForkedRepositoryFunc describes the behavior when the +// HasForkedRepository method of the parent MockUsersStore instance is +// invoked. +type UsersStoreHasForkedRepositoryFunc struct { + defaultHook func(context.Context, int64, int64) bool + hooks []func(context.Context, int64, int64) bool + history []UsersStoreHasForkedRepositoryFuncCall + mutex sync.Mutex +} + +// HasForkedRepository delegates to the next hook function in the queue and +// stores the parameter and result values of this invocation. +func (m *MockUsersStore) HasForkedRepository(v0 context.Context, v1 int64, v2 int64) bool { + r0 := m.HasForkedRepositoryFunc.nextHook()(v0, v1, v2) + m.HasForkedRepositoryFunc.appendCall(UsersStoreHasForkedRepositoryFuncCall{v0, v1, v2, r0}) + return r0 +} + +// SetDefaultHook sets function that is called when the HasForkedRepository +// method of the parent MockUsersStore instance is invoked and the hook +// queue is empty. +func (f *UsersStoreHasForkedRepositoryFunc) SetDefaultHook(hook func(context.Context, int64, int64) bool) { + f.defaultHook = hook +} + +// PushHook adds a function to the end of hook queue. Each invocation of the +// HasForkedRepository method of the parent MockUsersStore instance invokes +// the hook at the front of the queue and discards it. After the queue is +// empty, the default hook function is invoked for any future action. +func (f *UsersStoreHasForkedRepositoryFunc) PushHook(hook func(context.Context, int64, int64) bool) { + f.mutex.Lock() + f.hooks = append(f.hooks, hook) + f.mutex.Unlock() +} + +// SetDefaultReturn calls SetDefaultHook with a function that returns the +// given values. +func (f *UsersStoreHasForkedRepositoryFunc) SetDefaultReturn(r0 bool) { + f.SetDefaultHook(func(context.Context, int64, int64) bool { + return r0 + }) +} + +// PushReturn calls PushHook with a function that returns the given values. +func (f *UsersStoreHasForkedRepositoryFunc) PushReturn(r0 bool) { + f.PushHook(func(context.Context, int64, int64) bool { + return r0 + }) +} + +func (f *UsersStoreHasForkedRepositoryFunc) nextHook() func(context.Context, int64, int64) bool { + f.mutex.Lock() + defer f.mutex.Unlock() + + if len(f.hooks) == 0 { + return f.defaultHook + } + + hook := f.hooks[0] + f.hooks = f.hooks[1:] + return hook +} + +func (f *UsersStoreHasForkedRepositoryFunc) appendCall(r0 UsersStoreHasForkedRepositoryFuncCall) { + f.mutex.Lock() + f.history = append(f.history, r0) + f.mutex.Unlock() +} + +// History returns a sequence of UsersStoreHasForkedRepositoryFuncCall +// objects describing the invocations of this function. +func (f *UsersStoreHasForkedRepositoryFunc) History() []UsersStoreHasForkedRepositoryFuncCall { + f.mutex.Lock() + history := make([]UsersStoreHasForkedRepositoryFuncCall, len(f.history)) + copy(history, f.history) + f.mutex.Unlock() + + return history +} + +// UsersStoreHasForkedRepositoryFuncCall is an object that describes an +// invocation of method HasForkedRepository on an instance of +// MockUsersStore. +type UsersStoreHasForkedRepositoryFuncCall struct { + // Arg0 is the value of the 1st argument passed to this method + // invocation. + Arg0 context.Context + // Arg1 is the value of the 2nd argument passed to this method + // invocation. + Arg1 int64 + // Arg2 is the value of the 3rd argument passed to this method + // invocation. + Arg2 int64 + // Result0 is the value of the 1st result returned from this method + // invocation. + Result0 bool +} + +// Args returns an interface slice containing the arguments of this +// invocation. +func (c UsersStoreHasForkedRepositoryFuncCall) Args() []interface{} { + return []interface{}{c.Arg0, c.Arg1, c.Arg2} +} + +// Results returns an interface slice containing the results of this +// invocation. +func (c UsersStoreHasForkedRepositoryFuncCall) Results() []interface{} { + return []interface{}{c.Result0} +} diff --git a/internal/route/repo/issue.go b/internal/route/repo/issue.go index 5caa16cb..cf59856e 100644 --- a/internal/route/repo/issue.go +++ b/internal/route/repo/issue.go @@ -68,7 +68,7 @@ func MustAllowPulls(c *context.Context) { } // User can send pull request if owns a forked repository. - if c.IsLogged && c.User.HasForkedRepo(c.Repo.Repository.ID) { + if c.IsLogged && db.Users.HasForkedRepository(c.Req.Context(), c.User.ID, c.Repo.Repository.ID) { c.Repo.PullRequest.Allowed = true c.Repo.PullRequest.HeadInfo = c.User.Name + ":" + c.Repo.BranchName } |