-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
7347cdd
99fffe8
e6c3ae7
730170e
1864dee
c28f7df
f3b1b7b
09ed431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import * as path from 'path' | ||
import {Context} from '../src/context' | ||
import {PayloadRepository} from '@octokit/webhooks' | ||
import {WebhookPayload} from '../src/interfaces' | ||
|
||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
|
||
// TODO: https://github.com/actions/toolkit/issues/291, ESLint chokes on the a?.b syntax introduced in Typescript 3.7 | ||
/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */ | ||
|
||
describe('@actions/context', () => { | ||
let context: Context | ||
|
||
|
@@ -16,11 +21,18 @@ describe('@actions/context', () => { | |
expect(context.payload).toEqual(require('./payload.json')) | ||
}) | ||
|
||
it('returns an empty payload if the GITHUB_EVENT_PATH environment variable is falsey', () => { | ||
it('returns an undefined payload if the GITHUB_EVENT_PATH environment variable is falsey', () => { | ||
delete process.env.GITHUB_EVENT_PATH | ||
|
||
context = new Context() | ||
expect(context.payload).toEqual({}) | ||
expect(context.payload).toEqual(undefined) | ||
}) | ||
|
||
it('returns an undefined payload if the GITHUB_EVENT_PATH environment variable does not point to a file', () => { | ||
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'invalidfile.json') | ||
|
||
context = new Context() | ||
expect(context.payload).toEqual(undefined) | ||
}) | ||
|
||
it('returns attributes from the GITHUB_REPOSITORY', () => { | ||
|
@@ -29,18 +41,13 @@ describe('@actions/context', () => { | |
|
||
it('returns attributes from the repository payload', () => { | ||
delete process.env.GITHUB_REPOSITORY | ||
|
||
context.payload.repository = { | ||
thboop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: 'test', | ||
owner: {login: 'user'} | ||
} | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The before each function sets up these values There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
context = new Context() | ||
expect(() => context.repo).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
|
@@ -55,9 +62,10 @@ describe('@actions/context', () => { | |
it('works with pullRequest payloads', () => { | ||
delete process.env.GITHUB_REPOSITORY | ||
context.payload = { | ||
pullRequest: {number: 2}, | ||
repository: {owner: {login: 'user'}, name: 'test'} | ||
} | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
thboop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pull_request: {number: 2}, | ||
repository: {owner: {login: 'user'}, name: 'test'} as PayloadRepository | ||
} as WebhookPayload | ||
expect(context.issue).toEqual({ | ||
number: 2, | ||
owner: 'user', | ||
|
@@ -69,8 +77,8 @@ describe('@actions/context', () => { | |
delete process.env.GITHUB_REPOSITORY | ||
context.payload = { | ||
number: 2, | ||
repository: {owner: {login: 'user'}, name: 'test'} | ||
} | ||
repository: {owner: {login: 'user'}, name: 'test'} as PayloadRepository | ||
} as WebhookPayload | ||
expect(context.issue).toEqual({ | ||
number: 2, | ||
owner: 'user', | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the size impact to consumers of the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
}, | ||
"devDependencies": { | ||
"jest": "^24.7.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,25 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
export interface PayloadRepository { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 For example, a push event would not have an 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 By setting these as union types, we enforce that users need to do a type assertion using the |
||
[key: string]: any | ||
full_name?: string | ||
name: string | ||
owner: { | ||
[key: string]: any | ||
login: string | ||
name?: string | ||
} | ||
html_url?: string | ||
} | ||
|
||
export interface WebhookPayload { | ||
[key: string]: any | ||
repository?: PayloadRepository | ||
issue?: { | ||
[key: string]: any | ||
number: number | ||
html_url?: string | ||
body?: string | ||
} | ||
pull_request?: { | ||
[key: string]: any | ||
number: number | ||
html_url?: string | ||
body?: string | ||
} | ||
sender?: { | ||
[key: string]: any | ||
type: string | ||
} | ||
action?: string | ||
installation?: { | ||
id: number | ||
[key: string]: any | ||
} | ||
} | ||
import Webhooks from '@octokit/webhooks' | ||
export type WebhookPayload = | ||
| Webhooks.WebhookPayloadPush | ||
thboop marked this conversation as resolved.
Show resolved
Hide resolved
thboop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Webhooks.WebhookPayloadPullRequest | ||
| Webhooks.WebhookPayloadPullRequestReview | ||
| Webhooks.WebhookPayloadPullRequestReviewComment | ||
| Webhooks.WebhookPayloadStatus | ||
| Webhooks.WebhookPayloadIssues | ||
| Webhooks.WebhookPayloadIssueComment | ||
| Webhooks.WebhookPayloadRelease | ||
| Webhooks.WebhookPayloadRepositoryDispatch | ||
| Webhooks.WebhookPayloadCheckRun | ||
| Webhooks.WebhookPayloadDeployment | ||
| Webhooks.WebhookPayloadCheckSuite | ||
| Webhooks.WebhookPayloadWatch | ||
| Webhooks.WebhookPayloadDeploymentStatus | ||
| Webhooks.WebhookPayloadCreate | ||
| Webhooks.WebhookPayloadDelete | ||
| Webhooks.WebhookPayloadProjectCard | ||
| Webhooks.WebhookPayloadPageBuild | ||
| Webhooks.WebhookPayloadFork | ||
| Webhooks.WebhookPayloadGollum | ||
| Webhooks.WebhookPayloadMilestone | ||
| Webhooks.WebhookPayloadProject | ||
| Webhooks.WebhookPayloadLabel |
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.
nit:
const payload = github.context.payload as Webhooks.WebhookPayloadPush
would eliminate the redundant casts further belowThere 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.
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