New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Toolkit.run #63
Add Toolkit.run #63
Conversation
Definitely agree with this 👌 I don't like the pattern of a module.exports = async tools => {
if (!tools) tools = new Toolkit()
} This lets me write an async function and do this in tests: it('does something', async () => {
const tools = new Toolkit()
// Mock something on the Toolkit instance
tools.getPackageJSON = jest.fn(() => ({ })
await myAction(tools)
}) So with that in mind, what about an API like this: class Toolkit {
static public async run (fn: (tools: Toolkit) => Promise<void>, options?: ToolkitOptions) {
const tools = new Toolkit(options)
return fn(tools)
}
} And used like: const { Toolkit } = require('actions-toolkit')
Toolkit.run(async tools => {
return tools.github.repos.delete(tools.context.repo)
}, { event: 'issues' }) This seems like an easier way to export a function to test, but still run the action code. What do you think @jclem? |
I think for the pattern of using a single action for many scenarios/actions that makes sense. Personally, I haven't actually done that yet, but I can see why one would. I think this proposal is great. I can still use it the way I planned to if the |
Oh good point, yeah it should definitely still be optional (updated my above snippet). Also not sure if |
I think I wonder if a generic makes more sense, but that seems overcomplicated, since we don't plan on using the value. |
Makes sense 👍 🚀
Yeah agreed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this @jclem 🎉 🙏 I left a couple of small notes, I'll open up a separate PR for the changes to the . Let me know if I can make anything more clear!exit
methods but I don't think its a blocker
Co-Authored-By: jclem <jclem@github.com>
Co-Authored-By: jclem <jclem@github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonEtco No problem at all, I appreciate the help. Got partway through the suggestions, and some things came up. Thanks for wrapping this up! |
Why?
I've been finding myself using this pattern over and over:
In order to reduce boilerplate when using async/await, I'm proposing
tools.run
:How?
This adds a simple static
run
function to the Toolkit class. It accepts an async function as an argument, and optionally some toolkit options. If that function throws an error,run
will log the error and exit with a failure status.