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

feat: update @octokit/webhooks to v9 #1559

Merged
merged 35 commits into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1e6eb74
feat: upgrade to `@octokit/webhooks` v9
wolfy1339 Apr 27, 2021
072e675
fix: remove path option when creating the `Webhooks` class
wolfy1339 Apr 27, 2021
677bbad
fix: use the new middleware from `@octokit/webhooks`
wolfy1339 Apr 27, 2021
c59bbdc
refactor(tests): simplify declaration of `event`
wolfy1339 Apr 27, 2021
8b576d8
refactor(tests): move declaration of `event` variable to it's own `be…
wolfy1339 Apr 27, 2021
b6b2326
tests: remove the `*` event
wolfy1339 Apr 27, 2021
663f619
fix: remove wrapper around `Webooks#on`
wolfy1339 Apr 27, 2021
dbec054
fix: clean up imports in `src/context.ts`
wolfy1339 Apr 27, 2021
7502f94
fix: update `getErrorHandler()` imports and typecasting
wolfy1339 Apr 27, 2021
2df7788
lint: run prettier
wolfy1339 Apr 27, 2021
b0fe0c2
tests: fixups
wolfy1339 Apr 28, 2021
8d67e8a
build: bump `@octokit/webhooks-examples`
wolfy1339 Apr 28, 2021
40b7645
fix: comment out `implement` in `Context` for now
wolfy1339 Apr 28, 2021
f020b6c
fix: pass path down to the webhooks middleware
wolfy1339 Apr 28, 2021
baa050d
fix: add missing parameter name to JSDoc comment
wolfy1339 Apr 28, 2021
c8ea489
style: prettier
gr2m Apr 29, 2021
b9c545e
test: fix context tests
gr2m Apr 29, 2021
19cd186
fix: `x-hub-signature-256` header is now required
wolfy1339 Apr 29, 2021
ac66195
refactor: remove un-needed `path` option
wolfy1339 Apr 29, 2021
e4aea01
tests: change test name to reflect x-hub-signature-256 requirement
wolfy1339 Apr 29, 2021
de6c747
feat: introduce new `webhooksPath` option for the middleware
wolfy1339 Apr 29, 2021
b70c486
test: remove unhandled promise rejection error in `test/create-node-m…
gr2m Apr 29, 2021
cf57401
test: adapt server test for latest `@octokit/webhooks`. It is techinc…
gr2m Apr 29, 2021
38e59e2
build(package): lock file
gr2m Apr 29, 2021
ff4a986
build(deps): Unpin `@octokit/webhooks`, bump `@octokit/webhooks-examp…
wolfy1339 Apr 29, 2021
13b41cb
build(deps): bump `@octokit/webhooks*` packages to latest
gr2m Jun 18, 2021
28144bc
build(package): lock file
gr2m Jun 18, 2021
1d49aa1
WIP: workaround "Expression produces a union type that is too complex…
gr2m Jun 19, 2021
ce64dd3
build(package): bump `@octokit/webhooks` to `^9.8.4`
gr2m Jun 22, 2021
49dfe2c
build(package): lock file
gr2m Jun 22, 2021
e46459c
style: prettier
gr2m Jun 22, 2021
293e903
test: pass string payload body instead of object
gr2m Jun 22, 2021
26574c9
docs: add comment about `new Webhooks<Context>({...})`
gr2m Jun 22, 2021
5774187
test: skip run test that was skipped before
gr2m Jun 22, 2021
4070737
fix: mount `@octokit/webhooks` middleware to the webhooks path option
gr2m Jun 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 11 additions & 33 deletions src/context.ts
@@ -1,7 +1,6 @@
import path from "path";

import { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks";
import { Repository } from "@octokit/webhooks-types";
import merge from "deepmerge";

import type { Logger } from "pino";
Expand All @@ -13,32 +12,6 @@ import { EmitterWebhookEventName as WebhookEvents } from "@octokit/webhooks/dist

export type MergeOptions = merge.Options;

export interface WebhookPayloadWithRepository {
[key: string]: any;
repository?: Repository;
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;
};
}

/**
* The context of the event that was triggered, including the payload and
* helpers for extracting information can be passed to GitHub API calls.
Expand All @@ -56,10 +29,9 @@ export interface WebhookPayloadWithRepository {
* @property {log} log - A pino instance
*/
export class Context<E extends WebhookEvents = WebhookEvents> {
/*implements WebhookEvent<E>*/
public name: WebhookEvents;
public id: string;
public payload: WebhookPayloadWithRepository;
public payload: WebhookEvent<E>["payload"];

public octokit: InstanceType<typeof ProbotOctokit>;
public log: DeprecatedLogger;
Expand Down Expand Up @@ -90,6 +62,7 @@ export class Context<E extends WebhookEvents = WebhookEvents> {
*
*/
public repo<T>(object?: T) {
// @ts-ignore `repository` is not always present in this.payload
const repo = this.payload.repository;

if (!repo) {
Expand All @@ -100,7 +73,7 @@ export class Context<E extends WebhookEvents = WebhookEvents> {

return Object.assign(
{
owner: repo.owner.login || repo.owner.name!,
owner: repo.owner.login || repo.owner.name,
repo: repo.name,
},
object
Expand All @@ -120,10 +93,12 @@ export class Context<E extends WebhookEvents = WebhookEvents> {
* @param object - Params to be merged with the issue params.
*/
public issue<T>(object?: T) {
const payload = this.payload;
return Object.assign(
{
issue_number: (payload.issue || payload.pull_request || payload).number,
issue_number:
// @ts-ignore - this.payload may not have `issue` or `pull_request` keys
(this.payload.issue || this.payload.pull_request || this.payload)
.number,
},
this.repo(object)
);
Expand All @@ -145,6 +120,7 @@ export class Context<E extends WebhookEvents = WebhookEvents> {
const payload = this.payload;
return Object.assign(
{
// @ts-ignore - this.payload may not have `issue` or `pull_request` keys
pull_number: (payload.issue || payload.pull_request || payload).number,
},
this.repo(object)
Expand All @@ -156,7 +132,9 @@ export class Context<E extends WebhookEvents = WebhookEvents> {
* @type {boolean}
*/
get isBot() {
return this.payload.sender!.type === "Bot";
// @ts-expect-error - `sender` key is currently not present in all events
// see https://github.com/octokit/webhooks/issues/510
return this.payload.sender.type === "Bot";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
@@ -1,6 +1,6 @@
import { Logger } from "pino";

import { Context, WebhookPayloadWithRepository } from "./context";
import { Context } from "./context";
import { Options, ApplicationFunctionOptions } from "./types";
import { Probot } from "./probot";
import { Server } from "./server/server";
Expand All @@ -21,4 +21,4 @@ export {
};

/** NOTE: exported types might change at any point in time */
export { Options, WebhookPayloadWithRepository, ApplicationFunctionOptions };
export { Options, ApplicationFunctionOptions };
4 changes: 2 additions & 2 deletions src/octokit/get-webhooks.ts
Expand Up @@ -4,10 +4,10 @@ import { State } from "../types";
import { getErrorHandler } from "../helpers/get-error-handler";
import { webhookTransform } from "./octokit-webhooks-transform";

import { Context } from "../context";
// import { Context } from "../context";

export function getWebhooks(state: State) {
const webhooks = new Webhooks<Context>({
const webhooks = new Webhooks({
wolfy1339 marked this conversation as resolved.
Show resolved Hide resolved
secret: state.webhooks.secret!,
transform: webhookTransform.bind(null, state),
});
Expand Down
25 changes: 16 additions & 9 deletions test/context.test.ts
Expand Up @@ -9,15 +9,21 @@ import { Context } from "../src";
import { ProbotOctokit } from "../src/octokit/probot-octokit";
import { PushEvent } from "@octokit/webhooks-types";

const pushEventPayload = (WebhookExamples.filter(
(event) => event.name === "push"
)[0] as WebhookDefinition<"push">).examples[0];
const issuesEventPayload = (WebhookExamples.filter(
(event) => event.name === "issues"
)[0] as WebhookDefinition<"issues">).examples[0];
const pullRequestEventPayload = (WebhookExamples.filter(
(event) => event.name === "pull_request"
)[0] as WebhookDefinition<"pull_request">).examples[0];
const pushEventPayload = (
WebhookExamples.filter(
(event) => event.name === "push"
)[0] as WebhookDefinition<"push">
).examples[0];
const issuesEventPayload = (
WebhookExamples.filter(
(event) => event.name === "issues"
)[0] as WebhookDefinition<"issues">
).examples[0];
const pullRequestEventPayload = (
WebhookExamples.filter(
(event) => event.name === "pull_request"
)[0] as WebhookDefinition<"pull_request">
).examples[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are unrelated style changes


describe("Context", () => {
let event: WebhookEvent<"push"> = {
Expand Down Expand Up @@ -191,6 +197,7 @@ describe("Context", () => {
retry: { enabled: false },
throttle: { enabled: false },
});
// @ts-ignore - Expression produces a union type that is too complex to represent
context = new Context(event, octokit, {} as any);
});

Expand Down