mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 17:31:43 +00:00
replace uses of ReadActionEmailQuery with ListEmailActions (#27508)
This replaces the remaining use of ReadActionEmailQuery with ListEmailActions, and removes ReadActionEmailQuery. This also removes one more instance of (CodeMonitorStore).Query(). Additionally, this allows us to make scanEmails private, which is nice since we really shouldn't be exposing database scans to external packages. This also gets rid of some unnecessary paging that was complicating the changed function. We should be able to handle listing all email actions configured for a monitor without needing to page through them.
This commit is contained in:
parent
1f65e5c8de
commit
eaa15225ff
@ -259,60 +259,19 @@ func sendTestEmail(ctx context.Context, recipient graphql.ID, description string
|
||||
}
|
||||
|
||||
func (r *Resolver) actionIDsForMonitorIDInt64(ctx context.Context, monitorID int64) (actionIDs []graphql.ID, err error) {
|
||||
limit := 50
|
||||
var (
|
||||
ids []graphql.ID
|
||||
after *string
|
||||
q *sqlf.Query
|
||||
)
|
||||
// Paging.
|
||||
for {
|
||||
q, err = r.store.ReadActionEmailQuery(ctx, monitorID, &graphqlbackend.ListActionArgs{
|
||||
First: int32(limit),
|
||||
After: after,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
es, cur, err := r.actionIDsForMonitorIDINT64SinglePage(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, es...)
|
||||
if cur == nil {
|
||||
break
|
||||
}
|
||||
after = cur
|
||||
emailActions, err := r.store.ListEmailActions(ctx, cm.ListActionsOpts{
|
||||
MonitorID: intPtr(int(monitorID)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]graphql.ID, len(emailActions))
|
||||
for i, emailAction := range emailActions {
|
||||
ids[i] = (&monitorEmail{MonitorEmail: emailAction}).ID()
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (r *Resolver) actionIDsForMonitorIDINT64SinglePage(ctx context.Context, q *sqlf.Query, limit int) (ids []graphql.ID, cursor *string, err error) {
|
||||
var rows *sql.Rows
|
||||
rows, err = r.store.Query(ctx, q)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var es []*cm.MonitorEmail
|
||||
es, err = cm.ScanEmails(rows)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
ids = make([]graphql.ID, 0, len(es))
|
||||
for _, e := range es {
|
||||
ids = append(ids, (&monitorEmail{MonitorEmail: e}).ID())
|
||||
}
|
||||
|
||||
// Set the cursor if the result size equals limit.
|
||||
if len(ids) == limit {
|
||||
stringID := string(ids[len(ids)-1])
|
||||
cursor = &stringID
|
||||
}
|
||||
return ids, cursor, nil
|
||||
}
|
||||
|
||||
// splitActionIDs splits actions into three buckets: create, delete and update.
|
||||
// Note: args is mutated. After splitActionIDs, args only contains actions to be updated.
|
||||
func splitActionIDs(ctx context.Context, args *graphqlbackend.UpdateCodeMonitorArgs, actionIDs []graphql.ID) (toCreate []*graphqlbackend.CreateActionArgs, toDelete []int64, err error) {
|
||||
|
||||
@ -94,7 +94,7 @@ func (s *codeMonitorStore) runEmailQuery(ctx context.Context, q *sqlf.Query) (*M
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
es, err := ScanEmails(rows)
|
||||
es, err := scanEmails(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -193,28 +193,7 @@ func (s *codeMonitorStore) ListEmailActions(ctx context.Context, opts ListAction
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return ScanEmails(rows)
|
||||
}
|
||||
|
||||
const readActionEmailFmtStr = `
|
||||
SELECT id, monitor, enabled, priority, header, created_by, created_at, changed_by, changed_at
|
||||
FROM cm_emails
|
||||
WHERE monitor = %s
|
||||
AND id > %s
|
||||
LIMIT %s;
|
||||
`
|
||||
|
||||
func (s *codeMonitorStore) ReadActionEmailQuery(ctx context.Context, monitorID int64, args *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
after, err := unmarshalAfter(args.After)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sqlf.Sprintf(
|
||||
readActionEmailFmtStr,
|
||||
monitorID,
|
||||
after,
|
||||
args.First,
|
||||
), nil
|
||||
return scanEmails(rows)
|
||||
}
|
||||
|
||||
const createActionEmailFmtStr = `
|
||||
@ -267,7 +246,7 @@ var EmailsColumns = []*sqlf.Query{
|
||||
sqlf.Sprintf("cm_emails.changed_at"),
|
||||
}
|
||||
|
||||
func ScanEmails(rows *sql.Rows) (ms []*MonitorEmail, err error) {
|
||||
func scanEmails(rows *sql.Rows) (ms []*MonitorEmail, err error) {
|
||||
for rows.Next() {
|
||||
m := &MonitorEmail{}
|
||||
if err = rows.Scan(
|
||||
|
||||
@ -114,9 +114,6 @@ type MockCodeMonitorStore struct {
|
||||
// QueryFunc is an instance of a mock function object controlling the
|
||||
// behavior of the method Query.
|
||||
QueryFunc *CodeMonitorStoreQueryFunc
|
||||
// ReadActionEmailQueryFunc is an instance of a mock function object
|
||||
// controlling the behavior of the method ReadActionEmailQuery.
|
||||
ReadActionEmailQueryFunc *CodeMonitorStoreReadActionEmailQueryFunc
|
||||
// RecipientsForEmailIDInt64Func is an instance of a mock function
|
||||
// object controlling the behavior of the method
|
||||
// RecipientsForEmailIDInt64.
|
||||
@ -322,11 +319,6 @@ func NewMockCodeMonitorStore() *MockCodeMonitorStore {
|
||||
return nil, nil
|
||||
},
|
||||
},
|
||||
ReadActionEmailQueryFunc: &CodeMonitorStoreReadActionEmailQueryFunc{
|
||||
defaultHook: func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
return nil, nil
|
||||
},
|
||||
},
|
||||
RecipientsForEmailIDInt64Func: &CodeMonitorStoreRecipientsForEmailIDInt64Func{
|
||||
defaultHook: func(context.Context, int64, *graphqlbackend.ListRecipientsArgs) ([]*Recipient, error) {
|
||||
return nil, nil
|
||||
@ -493,9 +485,6 @@ func NewMockCodeMonitorStoreFrom(i CodeMonitorStore) *MockCodeMonitorStore {
|
||||
QueryFunc: &CodeMonitorStoreQueryFunc{
|
||||
defaultHook: i.Query,
|
||||
},
|
||||
ReadActionEmailQueryFunc: &CodeMonitorStoreReadActionEmailQueryFunc{
|
||||
defaultHook: i.ReadActionEmailQuery,
|
||||
},
|
||||
RecipientsForEmailIDInt64Func: &CodeMonitorStoreRecipientsForEmailIDInt64Func{
|
||||
defaultHook: i.RecipientsForEmailIDInt64,
|
||||
},
|
||||
@ -3944,122 +3933,6 @@ func (c CodeMonitorStoreQueryFuncCall) Results() []interface{} {
|
||||
return []interface{}{c.Result0, c.Result1}
|
||||
}
|
||||
|
||||
// CodeMonitorStoreReadActionEmailQueryFunc describes the behavior when the
|
||||
// ReadActionEmailQuery method of the parent MockCodeMonitorStore instance
|
||||
// is invoked.
|
||||
type CodeMonitorStoreReadActionEmailQueryFunc struct {
|
||||
defaultHook func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error)
|
||||
hooks []func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error)
|
||||
history []CodeMonitorStoreReadActionEmailQueryFuncCall
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// ReadActionEmailQuery delegates to the next hook function in the queue and
|
||||
// stores the parameter and result values of this invocation.
|
||||
func (m *MockCodeMonitorStore) ReadActionEmailQuery(v0 context.Context, v1 int64, v2 *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
r0, r1 := m.ReadActionEmailQueryFunc.nextHook()(v0, v1, v2)
|
||||
m.ReadActionEmailQueryFunc.appendCall(CodeMonitorStoreReadActionEmailQueryFuncCall{v0, v1, v2, r0, r1})
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// SetDefaultHook sets function that is called when the ReadActionEmailQuery
|
||||
// method of the parent MockCodeMonitorStore instance is invoked and the
|
||||
// hook queue is empty.
|
||||
func (f *CodeMonitorStoreReadActionEmailQueryFunc) SetDefaultHook(hook func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error)) {
|
||||
f.defaultHook = hook
|
||||
}
|
||||
|
||||
// PushHook adds a function to the end of hook queue. Each invocation of the
|
||||
// ReadActionEmailQuery method of the parent MockCodeMonitorStore 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 *CodeMonitorStoreReadActionEmailQueryFunc) PushHook(hook func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error)) {
|
||||
f.mutex.Lock()
|
||||
f.hooks = append(f.hooks, hook)
|
||||
f.mutex.Unlock()
|
||||
}
|
||||
|
||||
// SetDefaultReturn calls SetDefaultDefaultHook with a function that returns
|
||||
// the given values.
|
||||
func (f *CodeMonitorStoreReadActionEmailQueryFunc) SetDefaultReturn(r0 *sqlf.Query, r1 error) {
|
||||
f.SetDefaultHook(func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
return r0, r1
|
||||
})
|
||||
}
|
||||
|
||||
// PushReturn calls PushDefaultHook with a function that returns the given
|
||||
// values.
|
||||
func (f *CodeMonitorStoreReadActionEmailQueryFunc) PushReturn(r0 *sqlf.Query, r1 error) {
|
||||
f.PushHook(func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
return r0, r1
|
||||
})
|
||||
}
|
||||
|
||||
func (f *CodeMonitorStoreReadActionEmailQueryFunc) nextHook() func(context.Context, int64, *graphqlbackend.ListActionArgs) (*sqlf.Query, error) {
|
||||
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 *CodeMonitorStoreReadActionEmailQueryFunc) appendCall(r0 CodeMonitorStoreReadActionEmailQueryFuncCall) {
|
||||
f.mutex.Lock()
|
||||
f.history = append(f.history, r0)
|
||||
f.mutex.Unlock()
|
||||
}
|
||||
|
||||
// History returns a sequence of
|
||||
// CodeMonitorStoreReadActionEmailQueryFuncCall objects describing the
|
||||
// invocations of this function.
|
||||
func (f *CodeMonitorStoreReadActionEmailQueryFunc) History() []CodeMonitorStoreReadActionEmailQueryFuncCall {
|
||||
f.mutex.Lock()
|
||||
history := make([]CodeMonitorStoreReadActionEmailQueryFuncCall, len(f.history))
|
||||
copy(history, f.history)
|
||||
f.mutex.Unlock()
|
||||
|
||||
return history
|
||||
}
|
||||
|
||||
// CodeMonitorStoreReadActionEmailQueryFuncCall is an object that describes
|
||||
// an invocation of method ReadActionEmailQuery on an instance of
|
||||
// MockCodeMonitorStore.
|
||||
type CodeMonitorStoreReadActionEmailQueryFuncCall 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 *graphqlbackend.ListActionArgs
|
||||
// Result0 is the value of the 1st result returned from this method
|
||||
// invocation.
|
||||
Result0 *sqlf.Query
|
||||
// Result1 is the value of the 2nd result returned from this method
|
||||
// invocation.
|
||||
Result1 error
|
||||
}
|
||||
|
||||
// Args returns an interface slice containing the arguments of this
|
||||
// invocation.
|
||||
func (c CodeMonitorStoreReadActionEmailQueryFuncCall) Args() []interface{} {
|
||||
return []interface{}{c.Arg0, c.Arg1, c.Arg2}
|
||||
}
|
||||
|
||||
// Results returns an interface slice containing the results of this
|
||||
// invocation.
|
||||
func (c CodeMonitorStoreReadActionEmailQueryFuncCall) Results() []interface{} {
|
||||
return []interface{}{c.Result0, c.Result1}
|
||||
}
|
||||
|
||||
// CodeMonitorStoreRecipientsForEmailIDInt64Func describes the behavior when
|
||||
// the RecipientsForEmailIDInt64 method of the parent MockCodeMonitorStore
|
||||
// instance is invoked.
|
||||
|
||||
@ -25,15 +25,14 @@ type CodeMonitorStore interface {
|
||||
Exec(ctx context.Context, query *sqlf.Query) error
|
||||
Query(ctx context.Context, query *sqlf.Query) (*sql.Rows, error)
|
||||
|
||||
ReadActionEmailQuery(ctx context.Context, monitorID int64, args *graphqlbackend.ListActionArgs) (*sqlf.Query, error)
|
||||
UpdateActionEmail(ctx context.Context, monitorID int64, action *graphqlbackend.EditActionArgs) (e *MonitorEmail, err error)
|
||||
CreateActionEmail(ctx context.Context, monitorID int64, action *graphqlbackend.CreateActionArgs) (e *MonitorEmail, err error)
|
||||
DeleteActionsInt64(ctx context.Context, actionIDs []int64, monitorID int64) (err error)
|
||||
TotalCountActionEmails(ctx context.Context, monitorID int64) (count int32, err error)
|
||||
ActionEmailByIDInt64(ctx context.Context, emailID int64) (m *MonitorEmail, err error)
|
||||
ListActionJobs(ctx context.Context, opts ListActionJobsOpts) ([]*ActionJob, error)
|
||||
CountActionJobs(ctx context.Context, opts ListActionJobsOpts) (int, error)
|
||||
ListEmailActions(ctx context.Context, opts ListActionsOpts) ([]*MonitorEmail, error)
|
||||
ListActionJobs(context.Context, ListActionJobsOpts) ([]*ActionJob, error)
|
||||
CountActionJobs(context.Context, ListActionJobsOpts) (int, error)
|
||||
ListEmailActions(context.Context, ListActionsOpts) ([]*MonitorEmail, error)
|
||||
EnqueueActionEmailsForQueryIDInt64(ctx context.Context, queryID int64, triggerEventID int) (err error)
|
||||
GetActionJobMetadata(ctx context.Context, recordID int) (*ActionJobMetadata, error)
|
||||
ActionJobForIDInt(ctx context.Context, recordID int) (*ActionJob, error)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user