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: v7 #605

Merged
merged 16 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
# https://github.com/octokit/action.js/pull/494
- name: "Fix pkg.files file pattern"
run: node scripts/fix-package-json.js
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
node_modules/
pkg/
.vscode/
245 changes: 81 additions & 164 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"access": "public",
"provenance": true
},
"type": "module",
"description": "GitHub API client for GitHub Actions",
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*.{js,ts,css,less,scss,vue,json,gql,md}' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*.{js,ts,css,less,scss,vue,json,gql,md}' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --allowImportingTsExtensions test/typescript-validate.ts"
},
"repository": "github:octokit/action.js",
Expand All @@ -25,15 +26,15 @@
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/auth-action": "^4.0.0",
"@octokit/core": "^5.0.0",
"@octokit/plugin-paginate-rest": "^9.0.0",
"@octokit/plugin-rest-endpoint-methods": "^10.0.0",
"@octokit/types": "^12.0.0",
"@octokit/auth-action": "^5.1.1",
"@octokit/core": "^6.1.2",
"@octokit/plugin-paginate-rest": "^11.1.1",
"@octokit/plugin-rest-endpoint-methods": "^13.0.1",
"@octokit/types": "^13.0.0",
"undici": "^6.0.0"
},
"devDependencies": {
"@octokit/tsconfig": "^2.0.0",
"@octokit/tsconfig": "^3.0.0",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"esbuild": "^0.20.0",
Expand All @@ -46,11 +47,15 @@
"typescript": "^5.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
Expand All @@ -62,6 +67,11 @@
"lines": 100
}
},
"testPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/test/deno/"
],
"testEnvironment": "node",
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
Expand Down
44 changes: 18 additions & 26 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const sharedOptions = {
minify: false,
allowOverwrite: true,
packages: "external",
format: "esm",
platform: "neutral",
target: "es2022"
};

async function main() {
Expand All @@ -18,8 +21,6 @@ async function main() {
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
outdir: "pkg/dist-src",
bundle: false,
platform: "neutral",
format: "esm",
...sharedOptions,
sourcemap: false,
});
Expand All @@ -35,27 +36,13 @@ async function main() {

const entryPoints = ["./pkg/dist-src/index.js"];

await Promise.all([
// Build the a CJS Node.js bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-node",
bundle: true,
platform: "node",
target: "node18",
format: "cjs",
...sharedOptions,
}),
// Build an ESM browser bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-web",
bundle: true,
platform: "browser",
format: "esm",
...sharedOptions,
}),
]);
// Build the source code for Node.js
await esbuild.build({
entryPoints,
outdir: "pkg/dist-bundle",
bundle: true,
...sharedOptions,
});

// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
Expand All @@ -74,10 +61,15 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
default: "./dist-bundle/index.js",
}
},
sideEffects: false,
},
null,
Expand Down
17 changes: 0 additions & 17 deletions scripts/fix-package-json.js

This file was deleted.

43 changes: 26 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Octokit as Core } from "@octokit/core";
import type { Constructor, OctokitOptions } from "@octokit/core/types";
import { createActionAuth } from "@octokit/auth-action";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import {
paginateRest,
type PaginateInterface,
} from "@octokit/plugin-paginate-rest";
import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
export type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import { fetch as undiciFetch, ProxyAgent } from "undici";

import { VERSION } from "./version.js";
import type { OctokitOptions } from "@octokit/core/dist-types/types.js";
import { fetch as undiciFetch, ProxyAgent } from "undici";

export type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";


const DEFAULTS = {
authStrategy: createActionAuth,
Expand Down Expand Up @@ -35,19 +40,23 @@ export const customFetch = async function (url: string, opts: any) {
});
};

export const Octokit = Core.plugin(
paginateRest,
legacyRestEndpointMethods,
).defaults(function buildDefaults(options: OctokitOptions): OctokitOptions {
return {
...DEFAULTS,
...options,
request: {
fetch: customFetch,
...options.request,
},
};
});
export const Octokit: typeof Core &
Constructor<
{
paginate: PaginateInterface;
} & ReturnType<typeof legacyRestEndpointMethods>
> = Core.plugin(paginateRest, legacyRestEndpointMethods).defaults(
function buildDefaults(options: OctokitOptions): OctokitOptions {
return {
...DEFAULTS,
...options,
request: {
fetch: customFetch,
...options.request,
},
};
},
);

export type Octokit = InstanceType<typeof Octokit>;

Expand Down
48 changes: 34 additions & 14 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import fetchMock from "fetch-mock";
import { createServer, type Server } from "node:https";
import { Octokit, getProxyAgent, customFetch } from "../src/index.ts";
import { ProxyAgent } from "undici";
import { Octokit, getProxyAgent } from "../src/index.ts";
import {
type Dispatcher,
type RequestInfo,
type RequestInit,
Response,
ProxyAgent,
} from "undici";
import { jest } from "@jest/globals";

// mock undici such that we can substitute our own fetch implementation
// but use the actual ProxyAgent implementation for most tests. the
// exception is "should call undiciFetch with the correct dispatcher"
// where we want to validate that a mocked ProxyAgent is passed through
// to undici.fetch.
jest.mock("undici", () => {
jest.unstable_mockModule("undici", () => {
return {
__esModule: true,
fetch: jest.fn(),
ProxyAgent: jest.requireActual("undici").ProxyAgent,
ProxyAgent,
};
});
const undici = jest.requireMock("undici");
const undici = await import("undici");

describe("Smoke test", () => {
let server: Server;
Expand Down Expand Up @@ -279,30 +287,42 @@ describe("Smoke test", () => {
it("should call undiciFetch with the correct dispatcher", async () => {
process.env.HTTPS_PROXY = "https://127.0.0.1";
const expectedAgent = new ProxyAgent("https://127.0.0.1");

jest.mock("../src", () => {
const actualModule = jest.requireActual("../src");
const actualModule = await import("../src/index.js");
jest.unstable_mockModule("../src/index.js", () => {
return {
__esModule: true,
...actualModule,
getProxyAgent: jest.fn(() => expectedAgent),
customFetch: async function (url: string, opts: any) {
return await undici.fetch(url, {
dispatcher: getProxyAgent(),
...opts,
});
},
wolfy1339 marked this conversation as resolved.
Show resolved Hide resolved
};
});
expect(JSON.stringify(getProxyAgent())).toBe(

const {
customFetch: customFetchMocked,
getProxyAgent: getProxyAgentMocked,
} = await import("../src/index.js");

expect(JSON.stringify(getProxyAgentMocked())).toBe(
JSON.stringify(expectedAgent),
);

// mock undici.fetch to extract the `dispatcher` option passed in.
// this allows us to verify that `customFetch` correctly sets
// the dispatcher to `expectedAgent` when HTTPS_PROXY is set.
let dispatcher: any;
(undici.fetch as jest.Mock).mockImplementation(
(_url: string, options: any) => {
dispatcher = options.dispatcher;
let dispatcher: Dispatcher | undefined;
(undici.fetch as jest.Mock<typeof undici.fetch>).mockImplementation(
(_url: RequestInfo, options?: RequestInit) => {
dispatcher = options?.dispatcher;

return Promise.resolve(new Response());
},
);
await customFetch("http://api.github.com", {});
await customFetchMocked("http://api.github.com", {});
expect(JSON.stringify(dispatcher)).toEqual(JSON.stringify(expectedAgent));
});
});
Expand Down
1 change: 0 additions & 1 deletion test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false,
"allowImportingTsExtensions": true
},
"include": ["src/**/*"]
Expand Down
Loading