Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
130 changes: 108 additions & 22 deletions .github/package-lock.json

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

3 changes: 2 additions & 1 deletion .github/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"private": "true",
"type": "module",
Expand All @@ -17,8 +17,9 @@
"devDependencies": {
"@actions/github-script": "github:actions/github-script",
"@eslint/js": "^9.22.0",
"@octokit/webhooks-types": "^7.5.1",
"@octokit/rest": "^22.0.0",
"@octokit/types": "^14.1.0",
"@octokit/webhooks-types": "^7.5.1",
"@tsconfig/node20": "^20.1.4",
"@types/debug": "^4.1.12",
"@types/js-yaml": "^4.0.9",
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/src/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-check

import { inspect } from "util";
import { PER_PAGE_MAX } from "../../shared/src/github.js";
import { rateLimitHook } from "./github.js";
import { getIssueNumber } from "./issues.js";

/**
Expand Down Expand Up @@ -30,6 +31,8 @@
core.debug(`context: ${JSON.stringify(context)}`);
}

github.hook.after("request", rateLimitHook);

/** @type {{ owner: string, repo: string, head_sha: string, issue_number: number, run_id: number, details_url?: string }} */
let inputs;

Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/src/github.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// @ts-check

import { PER_PAGE_MAX } from "../../shared/src/github.js";
Expand Down Expand Up @@ -100,3 +100,19 @@
.filter((run) => run.name === workflowName)
.sort(invert(byDate((run) => run.updated_at)));
}

/**
* @param {import("@octokit/types").OctokitResponse<any>} response
* @param {import("@octokit/types").RequestParameters & {url: string, method: string}} options
*/
export function rateLimitHook(response, options) {
const limits = {
method: options.method.toUpperCase(),
url: options.url,
limit: response.headers["x-ratelimit-limit"],
remaining: response.headers["x-ratelimit-remaining"],
reset: new Date(parseInt(response.headers["x-ratelimit-reset"] || "") * 1000),
};

console.log(`rate-limits: ${JSON.stringify(limits)}`);
}
22 changes: 12 additions & 10 deletions .github/workflows/test/context.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { describe, expect, it } from "vitest";
import { PER_PAGE_MAX } from "../../shared/src/github.js";
import { extractInputs } from "../src/context.js";
Expand Down Expand Up @@ -34,7 +34,7 @@
},
};

await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
owner: "TestRepoOwnerLogin",
repo: "TestRepoName",
head_sha: "abc123",
Expand All @@ -44,6 +44,8 @@
});

it("pull_request_target", async () => {
const github = createMockGithub();

const context = {
eventName: "pull_request_target",
payload: {
Expand Down Expand Up @@ -71,23 +73,23 @@
run_id: NaN,
};

await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);

context.payload.action = "unlabeled";
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);

context.payload.action = "opened";
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);

context.payload.action = "synchronize";
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);

context.payload.action = "reopened";
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);

// Action not yet supported
context.payload.action = "assigned";
await expect(extractInputs(null, context, createMockCore())).rejects.toThrow();
await expect(extractInputs(github, context, createMockCore())).rejects.toThrow();
});

it("issue_comment:edited", async () => {
Expand Down Expand Up @@ -140,7 +142,7 @@
},
};

await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
owner: "TestRepoOwnerLogin",
repo: "TestRepoName",
head_sha: "",
Expand All @@ -157,7 +159,7 @@
},
};

await expect(extractInputs(null, context, createMockCore())).rejects.toThrow();
await expect(extractInputs(createMockGithub(), context, createMockCore())).rejects.toThrow();
});

it("workflow_run:completed:pull_request (same repo)", async () => {
Expand All @@ -180,7 +182,7 @@
},
};

await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
owner: "TestRepoOwnerLogin",
repo: "TestRepoName",
head_sha: "abc123",
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test/mocks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { RequestError } from "@octokit/request-error";
import { vi } from "vitest";

// Partial mock of `github` parameter passed into github-script actions
export function createMockGithub() {
return {
hook: {
after: vi.fn(),
},
paginate: async (func, params) => {
// Assume all test data fits in single page
const data = (await func(params)).data;
Expand Down
Loading