refactor: remove context.Background() references from hooks#50
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #50 +/- ##
==========================================
- Coverage 62.91% 62.89% -0.02%
==========================================
Files 210 210
Lines 22152 22154 +2
==========================================
- Hits 13936 13934 -2
- Misses 7132 7133 +1
- Partials 1084 1087 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
mwbrooks
left a comment
There was a problem hiding this comment.
📝 Notes to help the very much appreciated reviewer!
|
|
||
| manifestMock := &app.ManifestMockObject{} | ||
| manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything).Return(types.SlackYaml{ | ||
| manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{ |
There was a problem hiding this comment.
note: A common change is updating mocks to support the added ctx context.Context argument.
| mockDoctorHook := `{"versions": [{"name": "deno", "current": "1.0.0", "message": "Secure runtimes make safer code", "error": {"message": "Something isn't right with this installation"}}]}` | ||
| mockDoctorScript := hooks.HookScript{Command: "echo checkup"} | ||
| cm.HookExecutor.On("Execute", hooks.HookExecOpts{Hook: mockDoctorScript}). | ||
| cm.HookExecutor.On("Execute", mock.Anything, hooks.HookExecOpts{Hook: mockDoctorScript}). |
There was a problem hiding this comment.
note: I know a few of these lines are getting longer and could be expanded to multiple lines, but I wanted to keep the changes low since this PR was already getting a little large.
| Hook: clients.SDKConfig.Hooks.Doctor, | ||
| } | ||
| getDoctorHookJSON, err := clients.HookExecutor.Execute(hookExecOpts) | ||
| getDoctorHookJSON, err := clients.HookExecutor.Execute(ctx, hookExecOpts) |
There was a problem hiding this comment.
note: This is the final result of the work - the new signature is .Execute(ctx, opts).
| return getManifestInfoProject(ctx, clients) | ||
| case source.Equals(config.MANIFEST_SOURCE_REMOTE): | ||
| return getManifestInfoRemote(ctx, clients) |
There was a problem hiding this comment.
note: It's nice to see that both getManifestInfoProject(ctx, clients) and getManifestInfoRemote(ctx, clients) have the same function signatures.
| } | ||
|
|
||
| func triggerRequestViaHook(clients *shared.ClientFactory, path string, isDev bool) (api.TriggerRequest, error) { | ||
| func triggerRequestViaHook(ctx context.Context, clients *shared.ClientFactory, path string, isDev bool) (api.TriggerRequest, error) { |
There was a problem hiding this comment.
note: Example of the cascading changes to pass ctx into areas where .Execute is called.
There was a problem hiding this comment.
@mwbrooks Legend ⭐
These changes seem tedious, but I am so hopeful that future changes can make use of the real ctx without hassle now.
| GetManifestLocal(ctx context.Context, sdkConfig hooks.SDKCLIConfig, hookExecutor hooks.HookExecutor) (types.SlackYaml, error) | ||
| GetManifestRemote(ctx context.Context, token string, appID string) (types.SlackYaml, error) |
There was a problem hiding this comment.
note: It's nice to see that both GetManifestLocal(ctx, ... and GetManifestRemote(ctx, ... now except context.
|
|
||
| // Execute processes the data received by the SDK. | ||
| func (e *HookExecutorDefaultProtocol) Execute(opts HookExecOpts) (string, error) { | ||
| func (e *HookExecutorDefaultProtocol) Execute(ctx context.Context, opts HookExecOpts) (string, error) { |
There was a problem hiding this comment.
⭐ ⭐ ⭐
note: This is 1 of 2 key changes in this PR. Here we update the default hook executor protocol to accept a context argument. This allows us to remove the context.Background() usage inside this function.
⭐ ⭐ ⭐
There was a problem hiding this comment.
@mwbrooks Fantastic catch here!
IIRC a "background" context was used as a quick workaround for debug printing. It's now more clear that a follow up for this is important and having it broken into a standalone PR makes reviewing swell 🫡
|
|
||
| // Execute processes the data received by the SDK. | ||
| func (e *HookExecutorMessageBoundaryProtocol) Execute(opts HookExecOpts) (string, error) { | ||
| func (e *HookExecutorMessageBoundaryProtocol) Execute(ctx context.Context, opts HookExecOpts) (string, error) { |
There was a problem hiding this comment.
⭐ ⭐ ⭐
note: This is 2 of 2 key changes in this PR. Here we update the V2 hook executor protocol to accept a context argument. This allows us to remove the context.Background() usage inside this function.
⭐ ⭐ ⭐
| AuthTokens: authTokens, | ||
| } | ||
| if err := clients.Runtime.PreparePackage(clients.SDKConfig, clients.HookExecutor, preparePackageOpts); err != nil { | ||
| if err := clients.Runtime.PreparePackage(ctx, clients.SDKConfig, clients.HookExecutor, preparePackageOpts); err != nil { |
There was a problem hiding this comment.
note: We also needed to update the package runtime interface for PreparePackage to accept a ctx argument. On the upside, this aligns with the other functions in the interface, which also accept a ctx.
| SetVersion(string) | ||
| HooksJSONTemplate() []byte | ||
| PreparePackage(hooks.SDKCLIConfig, hooks.HookExecutor, types.PreparePackageOpts) error | ||
| PreparePackage(context.Context, hooks.SDKCLIConfig, hooks.HookExecutor, types.PreparePackageOpts) error |
There was a problem hiding this comment.
note: Here we are updating the package runtime interface. Now both InstallProjectDependencies(ctx, ... and PreparePackage(ctx, ... accept context.
zimeg
left a comment
There was a problem hiding this comment.
@mwbrooks All super awesome changes within! 👾 ✨
The key change updates
.Execute(opts)to.Execute(ctx, opts).
This is big 🔑 I know a missing context here has confused me before!
Finding that we were using "background" contexts and replacing it with the shared and single context used elsewhere is so good for keeping things consistent.
With this in mind, all of the other (many) changes go well!
I might've commented this elsewhere, but thanks for keeping these changes scoped to single PRs 💌
| IO: clients.IO, | ||
| } | ||
| if _, err := shell.Execute(hookExecOpts); err != nil { | ||
| if _, err := shell.Execute(ctx, hookExecOpts); err != nil { |
There was a problem hiding this comment.
🔭 This change is super great to find!
| } | ||
|
|
||
| func triggerRequestViaHook(clients *shared.ClientFactory, path string, isDev bool) (api.TriggerRequest, error) { | ||
| func triggerRequestViaHook(ctx context.Context, clients *shared.ClientFactory, path string, isDev bool) (api.TriggerRequest, error) { |
There was a problem hiding this comment.
@mwbrooks Legend ⭐
These changes seem tedious, but I am so hopeful that future changes can make use of the real ctx without hassle now.
| @@ -256,7 +256,7 @@ func TestTriggersCreateCommand(t *testing.T) { | |||
| appSelectTeardown = setupMockCreateAppSelection(installedProdApp) | |||
| // TODO: testing chicken and egg: we need the default mocks in place before we can use any of the `clients` methods | |||
There was a problem hiding this comment.
📚 Unrelated to the changes of this PR, we might revisit these comments soon since setting up default mocks is seeming standard before customized mocks from what I can tell.
There was a problem hiding this comment.
🗒️ Good call! I'll add a note to loop back on this after I finish the context work. It almost looks like we can just remove this TODO because it's setting up the default mocks and then adding custom ones. 🤔
| } | ||
|
|
||
| slackManifestInfo, err := hookExecutor.Execute(manifestHookOpts) | ||
| slackManifestInfo, err := hookExecutor.Execute(ctx, manifestHookOpts) |
|
|
||
| // Execute processes the data received by the SDK. | ||
| func (e *HookExecutorDefaultProtocol) Execute(opts HookExecOpts) (string, error) { | ||
| func (e *HookExecutorDefaultProtocol) Execute(ctx context.Context, opts HookExecOpts) (string, error) { |
There was a problem hiding this comment.
@mwbrooks Fantastic catch here!
IIRC a "background" context was used as a quick workaround for debug printing. It's now more clear that a follow up for this is important and having it broken into a standalone PR makes reviewing swell 🫡
| mockHookExecutor.On("Execute", mock.Anything, mock.Anything). | ||
| Run(func(args mock.Arguments) { | ||
| opts := args.Get(0).(hooks.HookExecOpts) | ||
| opts := args.Get(1).(hooks.HookExecOpts) | ||
| _, err := opts.Stdout.Write([]byte(tt.hookExecuteStdout)) | ||
| require.NoError(t, err) | ||
| }). |
|
🙏🏻 @zimeg Thanks for the quick review and I've made a few notes about your suggestions for the future. Always appreciate that you're willing to review these PRs - they're not exciting, but hopefully moving the code into a better place. One PR at a time. |
Summary
Draft until after our upcoming release.Release v3.0.5 is readyThis pull request updates the
package hooksby removing all references tocontext.Background().Reviewers
This is a bit of a nasty change, so I've dedicated a PR to it.
The key change updates
.Execute(opts)to.Execute(ctx, opts). This causes a ripple effect of changes in order to passctxinto the hooks executor. The majority of the changes occur in the tests, where mocks must be updated to work with the new argument. 🪝Really appreciate the time of reviewer going through these changes!
Requirements