Skip to content

Commit

Permalink
Remove cache system
Browse files Browse the repository at this point in the history
I wanted to cache the request to Workflow Run usages to avoid rate limit
on GitHub. This API endpoint requires a Workflow Run ID to retrieve each
usage of Workflow Run, so we usually reach the rate limit when there are
a lot of Workflow Runs.

Thus, I followed this guideline and used conditional requests to avoid
consumption of rate limit on GitHub.

https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#use-conditional-requests-if-appropriate

However, this didn't seem to work for token assigned to GitHub Workflow
while it worked as expected with a Personal Access Token.
Maybe, this is a constraint by GitHub, so I gave up using conditional
requests for tokens available on GitHub Workflows.
  • Loading branch information
yykamei committed Mar 31, 2024
1 parent ee51347 commit ae0f15a
Show file tree
Hide file tree
Showing 8 changed files with 24,207 additions and 80,449 deletions.
104,455 changes: 24,197 additions & 80,258 deletions dist/index.js

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions src/CacheStore.ts

This file was deleted.

46 changes: 1 addition & 45 deletions src/GitHubAPIClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { debug } from "@actions/core";
import { getOctokit } from "@actions/github";
import type { Octokit } from "@octokit/core";
import { RequestError } from "@octokit/request-error";
import type { APIClient, GetWorkflowRunsOptions } from "./APIClient";
import type { CacheStore, OctokitCachedData } from "./CacheStore";
import { DateTime } from "./DateTime";
import { Duration } from "./Duration";
import { GitHubIssue } from "./GitHubIssue";
Expand All @@ -17,50 +15,9 @@ const GITHUB_LINK_REL_REXT = 'rel="next"';
export class GitHubAPIClient implements APIClient {
public readonly client: Octokit;

constructor(
token: string,
private readonly cacheStore: CacheStore,
) {
constructor(token: string) {
this.client = getOctokit(token);

this.client.hook.wrap("request", async (request, options) => {
let cache: OctokitCachedData | null = null;
// @ts-ignore
const cacheKey: string | null | undefined = options.cacheKey;
// @ts-ignore
options.cacheKey = undefined;
if (cacheKey) {
debug(`Cache for key ${cacheKey} is being extracted...`);
cache = await this.cacheStore.read(cacheKey);
if (cache) {
debug(`Cache for key ${cacheKey} has been found: etag=${cache.etag}`);
options.headers["If-None-Match"] = cache.etag;
} else {
debug(`Cache for key ${cacheKey} is missing`);
}
}
try {
const response = await request(options);
if (cacheKey) {
await this.cacheStore.write(cacheKey, {
etag: response.headers.etag,
data: response.data,
});
}
return response;
} catch (e) {
if (
cache &&
e instanceof RequestError &&
e.status === 304 &&
e.response
) {
return { ...e.response, data: cache.data };
}
throw e;
}
});

this.client.hook.after("request", async (response) => {
const rateLimit = response.headers["x-ratelimit-limit"];
const rateLimitRemaining = response.headers["x-ratelimit-remaining"];
Expand Down Expand Up @@ -182,7 +139,6 @@ export class GitHubAPIClient implements APIClient {
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
cacheKey: `/repos/${owner}/${repo}/actions/runs/${runId}/timing`,
},
);
const durationMs = response.data.run_duration_ms;
Expand Down
56 changes: 0 additions & 56 deletions src/GitHubActionsCacheStore.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/MemoryCacheStore.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GitHubAPIClient } from "./GitHubAPIClient";
import { GitHubActionsCacheStore } from "./GitHubActionsCacheStore";
import { GitHubIssueContent } from "./GitHubIssueContent";
import { GitHubRepository } from "./GitHubRepository";
import { Input } from "./Input";
Expand All @@ -8,8 +7,7 @@ import { MermaidXYChart } from "./MermaidXYChart";
const main = async () => {
const now = new Date();
const input = new Input();
const cacheStore = new GitHubActionsCacheStore();
const apiClient = new GitHubAPIClient(input.token, cacheStore);
const apiClient = new GitHubAPIClient(input.token);
const repository = new GitHubRepository(input.owner, input.repo, apiClient);
const workflows = await repository.getWorkflows(input.only);
const charts = await Promise.all(
Expand Down
17 changes: 8 additions & 9 deletions tests/GitHubAPIClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { GitHubAPIClient } from "../src/GitHubAPIClient";
import { GitHubIssue } from "../src/GitHubIssue";
import { GitHubIssueContent } from "../src/GitHubIssueContent";
import { GitHubWorkflow } from "../src/GitHubWorkflow";
import { MemoryCacheStore } from "../src/MemoryCacheStore";

describe("GitHubAPIClient.getWorkflow()", () => {
afterEach(() => {
vi.restoreAllMocks();
});

it("should get workflow from GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: { id: 8123, name: "CI" },
Expand Down Expand Up @@ -39,7 +38,7 @@ describe("GitHubAPIClient.getWorkflows()", () => {
});

it("should get workflows from GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
Expand All @@ -64,7 +63,7 @@ describe("GitHubAPIClient.getWorkflows()", () => {
});

it("should call GitHub API twice", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
Expand Down Expand Up @@ -99,7 +98,7 @@ describe("GitHubAPIClient.getWorkflowRuns()", () => {
});

it("should get workflow runs from GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
Expand Down Expand Up @@ -142,7 +141,7 @@ describe("GitHubAPIClient.getWorkflowRuns()", () => {
});

it("should call GitHub API five times", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
for (const id of [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) {
spy.mockResolvedValueOnce({
Expand Down Expand Up @@ -183,7 +182,7 @@ describe("GitHubAPIClient.getIssues()", () => {
});

it("should get issues from GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: [
Expand Down Expand Up @@ -213,7 +212,7 @@ describe("GitHubAPIClient.createIssue()", () => {
});

it("should get workflow runs from GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
Expand Down Expand Up @@ -244,7 +243,7 @@ describe("GitHubAPIClient.closeIssue()", () => {
});

it("should close an issue on GitHub", async () => {
const client = new GitHubAPIClient("token", new MemoryCacheStore());
const client = new GitHubAPIClient("token");
const spy = vi.spyOn(client.client, "request");
spy.mockResolvedValueOnce({
data: {
Expand Down
56 changes: 0 additions & 56 deletions tests/GitHubActionsCacheStore.test.ts

This file was deleted.

0 comments on commit ae0f15a

Please sign in to comment.