Skip to content

Commit

Permalink
feat: Split components into seperate packages (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 authored Apr 2, 2021
1 parent 08a1a17 commit 92cca6a
Show file tree
Hide file tree
Showing 22 changed files with 525 additions and 31 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,34 @@ jobs:
node-version: 12.x
- run: npm ci
- run: npm run build:schema
- run: cp payload-examples/index.{d.ts,json} ./
- run: cp payload-schemas/schema.json ./schema.json
- run: cp payload-types/schema.d.ts ./schema.d.ts
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.OCTOKITBOT_PAT }}
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
- run: npm publish payload-examples
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/master' }}
- run: npm publish payload-examples --tag beta
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/beta' }}
- run: npm publish payload-schemas
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/master' }}
- run: npm publish payload-schemas --tag beta
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/beta' }}
- run: npm publish payload-types
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/master' }}
- run: npm publish payload-types --tag beta
env:
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
if: ${{ github.ref == 'refs/head/beta' }}
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ for these scripts in `bin/docs`, which can also be accessed by passing `--help`
## Adding examples

The
[`index.json` file](https://github.com/octokit/webhooks/blob/master/index.json)
[`index.json` file](https://github.com/octokit/webhooks/blob/master/payload-examples/index.json)
is generated, please do not edit it. Instead, make changes in the
[`payload-examples/api.github.com/` folder](https://github.com/octokit/webhooks/tree/master/payload-examples/api.github.com),
then update `index.json` by running the following command
Expand All @@ -42,7 +42,7 @@ tests will fail.
## Updating types

The
[`schema.d.ts` file](https://github.com/octokit/webhooks/blob/master/schema.d.ts)
[`schema.d.ts` file](https://github.com/octokit/webhooks/blob/master/payload-types/schema.d.ts)
is generated, please do not edit it. Instead, make changes to the schemas in the
[`payload-schemas/schemas/` folder](https://github.com/octokit/webhooks/tree/master/payload-schemas/schemas),
then generate the schema and update the types with the following commands:
Expand Down
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,31 +229,31 @@ Example webhook definition
## Download webhook definitions and webhook payloads schema

You can download the latest `index.json` and `schema.json` files from
[unpkg](https://unpkg.com/browse/@octokit/[email protected]/)
[unpkg](https://unpkg.com/)

- [`index.json`](https://unpkg.com/@octokit/webhooks-definitions/index.json)
- [`schema.json`](https://unpkg.com/@octokit/webhooks-definitions/schema.json)
- [`index.json`](https://unpkg.com/@octokit/webhooks-examples/index.json)
- [`schema.json`](https://unpkg.com/@octokit/webhooks-schemas/schema.json)

## Usage as Node module

To get an array of all webhook definition objects, require the `index.json` file
To get an array of all webhook definition objects, require the `@octokit/webhooks-examples` package.

```js
// Use Node.js require:
const WEBHOOKS = require("@octokit/webhooks-definitions/index.json");
const WEBHOOKS = require("@octokit/webhooks-examples/index.json");

// Or ESM/TypeScript import:
import WEBHOOKS from "@octokit/webhooks-definitions/index.json";
import WEBHOOKS from "@octokit/webhooks-examples/index.json";
```

To get the JSON schema for webhook payloads, require the `schema.json` file
To get the JSON schema for webhook payloads, require the `@octokit/webhooks-schemas` package.

```js
// Use Node.js require:
const SCHEMA = require("@octokit/webhooks-definitions/schema.json");
const SCHEMA = require("@octokit/webhooks-schemas");

// Or ESM/TypeScript import:
import SCHEMA from "@octokit/webhooks-definitions/schema.json";
import SCHEMA from "@octokit/webhooks-schemas";
```

### Usage with `ajv` in `strict` mode
Expand All @@ -267,8 +267,8 @@ the custom keyword `tsAdditionalProperties`.
Here is an example of how you can set this up:

```ts
import type { WebhookEvent } from "@octokit/webhooks-definitions/schema";
import * as githubWebhookSchema from "@octokit/webhooks-definitions/schema.json";
import type { WebhookEvent } from "@octokit/webhooks-types";
import * as githubWebhookSchema from "@octokit/webhooks-schemas";
import Ajv from "ajv";
import addFormats from "ajv-formats";

Expand All @@ -286,10 +286,7 @@ This package ships with types for the webhook events generated from the
respective json schemas, which you can use like so:

```typescript
import {
WebhookEvent,
IssuesOpenedEvent,
} from "@octokit/webhooks-definitions/schema";
import { WebhookEvent, IssuesOpenedEvent } from "@octokit/webhooks-types";

const handleWebhookEvent = (event: WebhookEvent) => {
if ("action" in event && event.action === "completed") {
Expand Down
2 changes: 1 addition & 1 deletion bin/docs/octokit-schema.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# bin/octokit-schema.ts

Generates the singular `schema.json` made up of all the schemas found within
`payload-schemas/schemas`, and which is included in the package published from
`payload-schemas/schemas`, and which is included in the [@octokit/webhooks-schemas package](https://www.npmjs.com/package/@octokit/webhooks-schemas) published from
this repo.

## Usage
Expand Down
2 changes: 1 addition & 1 deletion bin/docs/octokit-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Generates the singular `schema.d.ts` that provide strict typings for TypeScript
users, based off the `schema.json` generated by `bin/octokit-schema `, and which
is included in the package published from this repo.
is included in the [@octokit/webhooks-types package](https://www.npmjs.com/package/@octokit/webhooks-types) published from this repo.

Note that this uses `schema.json`, _not_ the individual schemas stored in
`payload-schemas/schemas` - this means you have to regenerate `schema.json`
Expand Down
2 changes: 1 addition & 1 deletion bin/octokit-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function run() {
};

fs.writeFileSync(
"schema.json",
"./payload-schemas/schema.json",
format(
JSON.stringify(schema, (key, value: unknown) => {
if (key === "$id") {
Expand Down
11 changes: 8 additions & 3 deletions bin/octokit-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const buildEventPayloadMap = (schema: Schema): string => {
};

const getSchema = async () =>
JSON.parse(await fs.readFile("./schema.json", "utf-8")) as Schema;
JSON.parse(
await fs.readFile("./payload-schemas/schema.json", "utf-8")
) as Schema;

declare module "json-schema" {
interface JSONSchema7 {
Expand All @@ -54,7 +56,7 @@ declare module "json-schema" {
const compileSchema = async (): Promise<string> => {
// has to be 4 due to https://github.com/bcherny/json-schema-to-typescript/issues/359
const schema: JSONSchema4 = JSON.parse(
await fs.readFile("./schema.json", "utf-8"),
await fs.readFile("./payload-schemas/schema.json", "utf-8"),
(key, value: unknown) => {
if (isJsonSchemaObject(value)) {
// $refs with a description result in a duplicate interface being made
Expand Down Expand Up @@ -88,7 +90,10 @@ const run = async () => {
"export type WebhookEventName = keyof EventPayloadMap;",
].join("\n");

await fs.writeFile("./schema.d.ts", format(ts, { parser: "typescript" }));
await fs.writeFile(
"./payload-types/schema.d.ts",
format(ts, { parser: "typescript" })
);
};

run();
2 changes: 1 addition & 1 deletion bin/validate-payload-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { DefinedError, ErrorObject } from "ajv";
import path from "path";
import { inspect } from "util";
import { ajv, validate } from "../payload-schemas";
import { ajv, validate } from "../payload-schemas/index";
import { forEachJsonFile, parseArgv, pathToPayloads } from "./utils";

const [, { continueOnError = false }] = parseArgv(
Expand Down
2 changes: 1 addition & 1 deletion bin/validate-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ addFormats(ajv);
ajv.addKeyword("tsAdditionalProperties");

const schema = JSON.parse(
readFileSync(resolve(__dirname, "../schema.json"), "utf-8")
readFileSync(resolve(__dirname, "../payload-schemas/schema.json"), "utf-8")
);

// if this is invalid, an error will be thrown
Expand Down
4 changes: 2 additions & 2 deletions lib/check-or-update-webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getSections,
toWebhook,
} from ".";
import currentWebhooks from "../index.json";
import currentWebhooks from "../payload-examples/index.json";

const isNotNull = <T>(value: T | null): value is T => value !== null;

Expand Down Expand Up @@ -63,7 +63,7 @@ export const checkOrUpdateWebhooks = async ({
}

writeFileSync(
"index.json",
"./payload-examples/index.json",
prettier.format(JSON.stringify(webhooks, null, 2), {
parser: "json",
})
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"build:schema": "ts-node -T bin/octokit-schema.ts",
"build:types": "ts-node -T bin/octokit-types.ts",
"build:webhooks": "ts-node -T bin/octokit-webhooks.ts update",
"lint": "prettier --check '{bin,lib}/*.ts' '*.{ts,md}' '{payload-examples,payload-schemas}/**/*.json' 'payload-schemas/*.ts' *.md package.json index.json",
"lint:fix": "prettier --write '{bin,lib}/*.ts' '*.{ts,md}' '{payload-examples,payload-schemas}/**/*.json' 'payload-schemas/*.ts' *.md package.json index.json",
"lint": "prettier --check '{bin,lib}/*.ts' '*.{ts,md}' '{payload-examples,payload-schemas,payload-types}/**/*.md' '{payload-examples,payload-schemas}/**/*.json' 'payload-schemas/*.ts' 'payload-types/*.d.ts' *.md package.json",
"lint:fix": "prettier --write '{bin,lib}/*.ts' '*.{ts,md}' '{payload-examples,payload-schemas,payload-types}/**/*.md' '{payload-examples,payload-schemas,payload-types}/**/*.json' 'payload-schemas/*.ts' 'payload-types/*.d.ts' *.md package.json",
"octokit-schema": "ts-node -T bin/octokit-schema.ts",
"octokit-webhooks": "ts-node -T bin/octokit-webhooks.ts",
"pretest": "npm run -s lint",
Expand Down Expand Up @@ -51,6 +51,7 @@
"prettier": "^2.0.5",
"prettier-plugin-packagejson": "^2.2.9",
"semantic-release": "^17.0.0",
"semantic-release-plugin-update-version-in-files": "^1.1.0",
"ts-node": "^9.1.1",
"turndown": "^7.0.0",
"typescript": "^4.1.3",
Expand All @@ -59,6 +60,23 @@
"publishConfig": {
"access": "public"
},
"releases": {
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/github",
"@semantic-release/npm",
[
"semantic-release-plugin-update-version-in-files",
{
"files": [
"payload-*/package.json"
],
"placeholder": "0.0.0-development"
}
]
]
},
"renovate": {
"extends": [
"github>octokit/.github"
Expand Down
Loading

0 comments on commit 92cca6a

Please sign in to comment.