Skip to content

Commit c2affe0

Browse files
authored
[.github] Log rate limits after each API call (#36386)
1 parent 0ef6289 commit c2affe0

File tree

6 files changed

+144
-33
lines changed

6 files changed

+144
-33
lines changed

.github/package-lock.json

Lines changed: 108 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
"devDependencies": {
1818
"@actions/github-script": "github:actions/github-script",
1919
"@eslint/js": "^9.22.0",
20-
"@octokit/webhooks-types": "^7.5.1",
2120
"@octokit/rest": "^22.0.0",
21+
"@octokit/types": "^14.1.0",
22+
"@octokit/webhooks-types": "^7.5.1",
2223
"@tsconfig/node20": "^20.1.4",
2324
"@types/debug": "^4.1.12",
2425
"@types/js-yaml": "^4.0.9",

.github/workflows/src/context.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { inspect } from "util";
44
import { PER_PAGE_MAX } from "../../shared/src/github.js";
5+
import { rateLimitHook } from "./github.js";
56
import { getIssueNumber } from "./issues.js";
67

78
/**
@@ -30,6 +31,8 @@ export async function extractInputs(github, context, core) {
3031
core.debug(`context: ${JSON.stringify(context)}`);
3132
}
3233

34+
github.hook.after("request", rateLimitHook);
35+
3336
/** @type {{ owner: string, repo: string, head_sha: string, issue_number: number, run_id: number, details_url?: string }} */
3437
let inputs;
3538

.github/workflows/src/github.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,19 @@ export async function getWorkflowRuns(github, context, workflowName, ref) {
100100
.filter((run) => run.name === workflowName)
101101
.sort(invert(byDate((run) => run.updated_at)));
102102
}
103+
104+
/**
105+
* @param {import("@octokit/types").OctokitResponse<any>} response
106+
* @param {import("@octokit/types").RequestParameters & {url: string, method: string}} options
107+
*/
108+
export function rateLimitHook(response, options) {
109+
const limits = {
110+
method: options.method.toUpperCase(),
111+
url: options.url,
112+
limit: response.headers["x-ratelimit-limit"],
113+
remaining: response.headers["x-ratelimit-remaining"],
114+
reset: new Date(parseInt(response.headers["x-ratelimit-reset"] || "") * 1000),
115+
};
116+
117+
console.log(`rate-limits: ${JSON.stringify(limits)}`);
118+
}

.github/workflows/test/context.test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("extractInputs", () => {
3434
},
3535
};
3636

37-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
37+
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
3838
owner: "TestRepoOwnerLogin",
3939
repo: "TestRepoName",
4040
head_sha: "abc123",
@@ -44,6 +44,8 @@ describe("extractInputs", () => {
4444
});
4545

4646
it("pull_request_target", async () => {
47+
const github = createMockGithub();
48+
4749
const context = {
4850
eventName: "pull_request_target",
4951
payload: {
@@ -71,23 +73,23 @@ describe("extractInputs", () => {
7173
run_id: NaN,
7274
};
7375

74-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
76+
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);
7577

7678
context.payload.action = "unlabeled";
77-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
79+
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);
7880

7981
context.payload.action = "opened";
80-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
82+
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);
8183

8284
context.payload.action = "synchronize";
83-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
85+
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);
8486

8587
context.payload.action = "reopened";
86-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual(expected);
88+
await expect(extractInputs(github, context, createMockCore())).resolves.toEqual(expected);
8789

8890
// Action not yet supported
8991
context.payload.action = "assigned";
90-
await expect(extractInputs(null, context, createMockCore())).rejects.toThrow();
92+
await expect(extractInputs(github, context, createMockCore())).rejects.toThrow();
9193
});
9294

9395
it("issue_comment:edited", async () => {
@@ -140,7 +142,7 @@ describe("extractInputs", () => {
140142
},
141143
};
142144

143-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
145+
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
144146
owner: "TestRepoOwnerLogin",
145147
repo: "TestRepoName",
146148
head_sha: "",
@@ -157,7 +159,7 @@ describe("extractInputs", () => {
157159
},
158160
};
159161

160-
await expect(extractInputs(null, context, createMockCore())).rejects.toThrow();
162+
await expect(extractInputs(createMockGithub(), context, createMockCore())).rejects.toThrow();
161163
});
162164

163165
it("workflow_run:completed:pull_request (same repo)", async () => {
@@ -180,7 +182,7 @@ describe("extractInputs", () => {
180182
},
181183
};
182184

183-
await expect(extractInputs(null, context, createMockCore())).resolves.toEqual({
185+
await expect(extractInputs(createMockGithub(), context, createMockCore())).resolves.toEqual({
184186
owner: "TestRepoOwnerLogin",
185187
repo: "TestRepoName",
186188
head_sha: "abc123",

.github/workflows/test/mocks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { vi } from "vitest";
44
// Partial mock of `github` parameter passed into github-script actions
55
export function createMockGithub() {
66
return {
7+
hook: {
8+
after: vi.fn(),
9+
},
710
paginate: async (func, params) => {
811
// Assume all test data fits in single page
912
const data = (await func(params)).data;

0 commit comments

Comments
 (0)