Skip to content
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

Update Payload Interface to use Definitions from @octokit/webhooks #292

Closed
wants to merge 8 commits into from

Conversation

thboop
Copy link
Collaborator

@thboop thboop commented Jan 8, 2020

Resolves #289
Resolves #147

This is a breaking compat change and will require a major version bump.

```ts
const github = require('@actions/github');

const payload = github.context.payload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: const payload = github.context.payload as Webhooks.WebhookPayloadPush would eliminate the redundant casts further below

Copy link
Collaborator Author

@thboop thboop Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should recommend users do that though. Type assertions rely on the developer to perform relevant checks before casting, and this example shows that. Going to take a look at the event_name field and then cast based on that

expect(context.repo).toEqual({owner: 'user', repo: 'test'})
})

it("return error for context.repo when repository doesn't exist", () => {
delete process.env.GITHUB_REPOSITORY

context.payload.repository = undefined
delete process.env.GITHUB_EVENT_PATH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should an after all restore the original values? otherwise may affect test suites that execute after

Copy link
Collaborator Author

@thboop thboop Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should an after-all cleanup? so the changes don't spill-over and affect other test suites?

Copy link
Collaborator Author

@thboop thboop Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I misunderstood your previous comment. Yeah, I'll go ahead and update this.

EDIT: I'm not sure we should do this. Currently, the only place this env is set is in this suite (or if you happen to have it set on the machine). If we set the value after the suite runs, suites that run before this suite will have different values then suites that run after, changing their behavior if they took a dependency on this value. If another suite needs this value, they should set it. If we build out multiple suites that need it, we should

@@ -38,7 +38,8 @@
},
"dependencies": {
"@octokit/graphql": "^4.3.1",
"@octokit/rest": "^16.15.0"
"@octokit/rest": "^16.15.0",
"@octokit/webhooks": "^7.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the size impact to consumers of the github module?

Copy link
Collaborator Author

@thboop thboop Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the code ends up in compiled ncc folders so there is no impact for consumers using that.

However, I think we can install this as a dev dependency, so users doing npm install --prod won't even download it. Going to test that out and make that corresponding change, so the only real users effected would be those upload their node_modules and installed the dev dependencies in there, which is not recommended and should be solved with better messaging and documentation to action authors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bit of a mixed reaction in the ts/js community, but it looks like we shouldn't install as dev-dependencies we export these types open @types issue

In its current state (saved as a dependency)
Users who upload their production npm packages rather then using ncc are going to have the size of their action increased by the size of the npm package and its dependencies, which is 291 KB or 400kb on disk for my machine.


export interface PayloadRepository {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we re-export PayloadRepository then can we avoid a new major version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that there's a great way to keep back-compat and set us on a good foot going forward.

We took some of the events and combined them into one big type where we marked each field as optional in WebhookPayload This doesn't really set us on a great path forward because it makes it hard to tell which fields should exist on the event.

For example, a push event would not have an issue or a pull_request, but they are marked as optional when consuming. Ideally, this would be distinct from fields that are actually optional fields, but there is no way to tell now.

It conveys the wrong message to a user that those fields are optional, when in fact they depend on the type of event. Our users who want type safety should be casting based on the type of event, we shouldn't combine every event into one big interface with each field is marked as optional, it makes it harder to consume.

It also sends the wrong message because the individual fields depend on the type of event. For example the "pull_request" field on a PullRequestReview is a different object with different fields from the pull_request field on a "PullRequest" event. payload.pull_request.commits is only a valid field for PullRequests but not PullRequestReviews.

By setting these as union types, we enforce that users need to do a type assertion using the as keyword, or ignore types by using these as any objects

@thboop
Copy link
Collaborator Author

thboop commented Jan 15, 2020

Closing in favor of #310

@thboop thboop closed this Jan 15, 2020
@thboop thboop deleted the thboop/UpdateWebhookInterfaces branch March 17, 2020 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants