From 004274fd61bbd1ad18d41c1718fe2ccd2f07d4c5 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sat, 23 Nov 2024 15:29:02 +0900 Subject: [PATCH 1/2] Use @octokit/plugin-retry --- package.json | 1 + pnpm-lock.yaml | 41 +++++++++++++++++++++++++++++++++++++++++ src/github.ts | 8 ++++++++ src/queries/comments.ts | 6 ++---- src/queries/minimize.ts | 6 ++---- src/run.ts | 6 ++---- 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/github.ts diff --git a/package.json b/package.json index 9efb5061..79c4e83b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@actions/core": "1.11.1", "@actions/github": "6.0.0", + "@octokit/plugin-retry": "7.1.2", "graphql": "16.9.0" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 11316a57..8642693e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@actions/github': specifier: 6.0.0 version: 6.0.0 + '@octokit/plugin-retry': + specifier: 7.1.2 + version: 7.1.2(@octokit/core@5.1.0) graphql: specifier: 16.9.0 version: 16.9.0 @@ -911,6 +914,9 @@ packages: '@octokit/openapi-types@20.0.0': resolution: {integrity: sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA==} + '@octokit/openapi-types@22.2.0': + resolution: {integrity: sha512-QBhVjcUa9W7Wwhm6DBFu6ZZ+1/t/oYxqc2tp81Pi41YNuJinbFRx8B133qVOrAaBbF7D/m0Et6f9/pZt9Rc+tg==} + '@octokit/plugin-paginate-rest@9.2.1': resolution: {integrity: sha512-wfGhE/TAkXZRLjksFXuDZdmGnJQHvtU/joFQdweXUgzo1XwvBCD4o4+75NtFfjfLK5IwLf9vHTfSiU3sLRYpRw==} engines: {node: '>= 18'} @@ -923,10 +929,20 @@ packages: peerDependencies: '@octokit/core': '5' + '@octokit/plugin-retry@7.1.2': + resolution: {integrity: sha512-XOWnPpH2kJ5VTwozsxGurw+svB2e61aWlmk5EVIYZPwFK5F9h4cyPyj9CIKRyMXMHSwpIsI3mPOdpMmrRhe7UQ==} + engines: {node: '>= 18'} + peerDependencies: + '@octokit/core': '>=6' + '@octokit/request-error@5.0.1': resolution: {integrity: sha512-X7pnyTMV7MgtGmiXBwmO6M5kIPrntOXdyKZLigNfQWSEQzVxR4a4vo49vJjTWX70mPndj8KhfT4Dx+2Ng3vnBQ==} engines: {node: '>= 18'} + '@octokit/request-error@6.1.5': + resolution: {integrity: sha512-IlBTfGX8Yn/oFPMwSfvugfncK2EwRLjzbrpifNaMY8o/HTEAFqCA1FZxjD9cWvSKBHgrIhc4CSBIzMxiLsbzFQ==} + engines: {node: '>= 18'} + '@octokit/request@8.2.0': resolution: {integrity: sha512-exPif6x5uwLqv1N1irkLG1zZNJkOtj8bZxuVHd71U5Ftuxf2wGNvAJyNBcPbPC+EBzwYEbBDdSFb8EPcjpYxPQ==} engines: {node: '>= 18'} @@ -934,6 +950,9 @@ packages: '@octokit/types@12.6.0': resolution: {integrity: sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw==} + '@octokit/types@13.6.1': + resolution: {integrity: sha512-PHZE9Z+kWXb23Ndik8MKPirBPziOc0D2/3KH1P+6jK5nGWe96kadZuE4jev2/Jq7FvIfTlT2Ltg8Fv2x1v0a5g==} + '@repeaterjs/repeater@3.0.5': resolution: {integrity: sha512-l3YHBLAol6d/IKnB9LhpD0cEZWAoe3eFKUyTYWmFmCO2Q/WOckxLQAUyMZWwZV2M/m3+4vgRoaolFqaII82/TA==} @@ -1218,6 +1237,9 @@ packages: bl@4.1.0: resolution: {integrity: sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==} + bottleneck@2.19.5: + resolution: {integrity: sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==} + brace-expansion@1.1.11: resolution: {integrity: sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==} @@ -4164,6 +4186,8 @@ snapshots: '@octokit/openapi-types@20.0.0': {} + '@octokit/openapi-types@22.2.0': {} + '@octokit/plugin-paginate-rest@9.2.1(@octokit/core@5.1.0)': dependencies: '@octokit/core': 5.1.0 @@ -4174,12 +4198,23 @@ snapshots: '@octokit/core': 5.1.0 '@octokit/types': 12.6.0 + '@octokit/plugin-retry@7.1.2(@octokit/core@5.1.0)': + dependencies: + '@octokit/core': 5.1.0 + '@octokit/request-error': 6.1.5 + '@octokit/types': 13.6.1 + bottleneck: 2.19.5 + '@octokit/request-error@5.0.1': dependencies: '@octokit/types': 12.6.0 deprecation: 2.3.1 once: 1.4.0 + '@octokit/request-error@6.1.5': + dependencies: + '@octokit/types': 13.6.1 + '@octokit/request@8.2.0': dependencies: '@octokit/endpoint': 9.0.4 @@ -4191,6 +4226,10 @@ snapshots: dependencies: '@octokit/openapi-types': 20.0.0 + '@octokit/types@13.6.1': + dependencies: + '@octokit/openapi-types': 22.2.0 + '@repeaterjs/repeater@3.0.5': {} '@sinclair/typebox@0.27.8': {} @@ -4561,6 +4600,8 @@ snapshots: inherits: 2.0.4 readable-stream: 3.6.2 + bottleneck@2.19.5: {} + brace-expansion@1.1.11: dependencies: balanced-match: 1.0.2 diff --git a/src/github.ts b/src/github.ts new file mode 100644 index 00000000..102f2917 --- /dev/null +++ b/src/github.ts @@ -0,0 +1,8 @@ +import * as github from '@actions/github' +import { retry } from '@octokit/plugin-retry' + +export type Octokit = ReturnType + +export const getOctokit = (token: string): Octokit => github.getOctokit(token, {}, retry) + +export const context = github.context diff --git a/src/queries/comments.ts b/src/queries/comments.ts index 4e2b16a3..50b4e851 100644 --- a/src/queries/comments.ts +++ b/src/queries/comments.ts @@ -1,8 +1,6 @@ -import * as github from '@actions/github' +import * as github from '../github.js' import { CommentsQuery, CommentsQueryVariables } from '../generated/graphql.js' -type Octokit = ReturnType - const query = /* GraphQL */ ` query comments($owner: String!, $name: String!, $number: Int!) { repository(owner: $owner, name: $name) { @@ -23,6 +21,6 @@ const query = /* GraphQL */ ` } ` -export const queryComments = async (o: Octokit, v: CommentsQueryVariables): Promise => { +export const queryComments = async (o: github.Octokit, v: CommentsQueryVariables): Promise => { return await o.graphql(query, v) } diff --git a/src/queries/minimize.ts b/src/queries/minimize.ts index ad00fab6..f2054800 100644 --- a/src/queries/minimize.ts +++ b/src/queries/minimize.ts @@ -1,8 +1,6 @@ -import * as github from '@actions/github' +import * as github from '../github.js' import { MinimizeCommentMutation, MinimizeCommentMutationVariables } from '../generated/graphql.js' -type Octokit = ReturnType - const query = /* GraphQL */ ` mutation minimizeComment($id: ID!) { minimizeComment(input: { classifier: OUTDATED, subjectId: $id }) { @@ -12,7 +10,7 @@ const query = /* GraphQL */ ` ` export const minimizeComment = async ( - o: Octokit, + o: github.Octokit, v: MinimizeCommentMutationVariables, ): Promise => { return await o.graphql(query, v) diff --git a/src/run.ts b/src/run.ts index 8db708eb..eae2a82a 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,11 +1,9 @@ import * as core from '@actions/core' -import * as github from '@actions/github' +import * as github from './github.js' import { CommentsQuery } from './generated/graphql.js' import { queryComments } from './queries/comments.js' import { minimizeComment } from './queries/minimize.js' -type Octokit = ReturnType - type Inputs = { authors: string[] startsWith: string[] @@ -63,7 +61,7 @@ export const run = async (inputs: Inputs): Promise => { } } -const getCurrentLogin = async (octokit: Octokit) => { +const getCurrentLogin = async (octokit: github.Octokit) => { try { const { data: user } = await octokit.rest.users.getAuthenticated() return user.login From badabe7ee1cd8853e26fd623967e80c72fcd6026 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sat, 23 Nov 2024 15:34:42 +0900 Subject: [PATCH 2/2] Refactor --- src/filter.ts | 53 +++++++++++++++++++++++++++ src/run.ts | 46 +---------------------- tests/{run.test.ts => filter.test.ts} | 12 +----- 3 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 src/filter.ts rename tests/{run.test.ts => filter.test.ts} (92%) diff --git a/src/filter.ts b/src/filter.ts new file mode 100644 index 00000000..5012b1a8 --- /dev/null +++ b/src/filter.ts @@ -0,0 +1,53 @@ +import * as core from '@actions/core' +import { CommentsQuery } from './generated/graphql.js' + +type Inputs = { + authors: string[] + startsWith: string[] + endsWith: string[] + contains: string[] +} + +export type Comment = NonNullable< + NonNullable< + NonNullable['pullRequest']>['comments']>['nodes'] + >[number] +> + +export const filterComments = (q: CommentsQuery, inputs: Inputs): Comment[] => { + if (q.repository?.pullRequest?.comments.nodes == null) { + core.info(`unexpected response: repository === ${JSON.stringify(q.repository)}`) + return [] + } + const comments = [] + for (const node of q.repository.pullRequest.comments.nodes) { + if (node == null) { + continue + } + comments.push(node) + } + return comments.filter((c) => toMinimize(c, inputs)) +} + +export const toMinimize = (c: Comment, inputs: Inputs): boolean => { + if (c.isMinimized) { + return false + } + if (inputs.authors.some((a) => c.author?.login === a)) { + core.info(`authors filter matched: ${c.url}`) + return true + } + if (inputs.startsWith.some((s) => c.body.trimStart().startsWith(s))) { + core.info(`starts-with matched: ${c.url}`) + return true + } + if (inputs.endsWith.some((s) => c.body.trimEnd().endsWith(s))) { + core.info(`ends-with matched: ${c.url}`) + return true + } + if (inputs.contains.some((s) => c.body.includes(s))) { + core.info(`contains matched: ${c.url}`) + return true + } + return false +} diff --git a/src/run.ts b/src/run.ts index eae2a82a..09169e53 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core' import * as github from './github.js' -import { CommentsQuery } from './generated/graphql.js' +import { filterComments } from './filter.js' import { queryComments } from './queries/comments.js' import { minimizeComment } from './queries/minimize.js' @@ -70,47 +70,3 @@ const getCurrentLogin = async (octokit: github.Octokit) => { return 'github-actions' } } - -type Comment = NonNullable< - NonNullable< - NonNullable['pullRequest']>['comments']>['nodes'] - >[number] -> - -const filterComments = (q: CommentsQuery, inputs: Inputs): Comment[] => { - if (q.repository?.pullRequest?.comments.nodes == null) { - core.info(`unexpected response: repository === ${JSON.stringify(q.repository)}`) - return [] - } - const comments = [] - for (const node of q.repository.pullRequest.comments.nodes) { - if (node == null) { - continue - } - comments.push(node) - } - return comments.filter((c) => toMinimize(c, inputs)) -} - -export const toMinimize = (c: Comment, inputs: Inputs): boolean => { - if (c.isMinimized) { - return false - } - if (inputs.authors.some((a) => c.author?.login === a)) { - core.info(`authors filter matched: ${c.url}`) - return true - } - if (inputs.startsWith.some((s) => c.body.trimStart().startsWith(s))) { - core.info(`starts-with matched: ${c.url}`) - return true - } - if (inputs.endsWith.some((s) => c.body.trimEnd().endsWith(s))) { - core.info(`ends-with matched: ${c.url}`) - return true - } - if (inputs.contains.some((s) => c.body.includes(s))) { - core.info(`contains matched: ${c.url}`) - return true - } - return false -} diff --git a/tests/run.test.ts b/tests/filter.test.ts similarity index 92% rename from tests/run.test.ts rename to tests/filter.test.ts index c48ae78b..0dcaa7cd 100644 --- a/tests/run.test.ts +++ b/tests/filter.test.ts @@ -1,4 +1,4 @@ -import { toMinimize } from '../src/run.js' +import { toMinimize } from '../src/filter.js' describe('filter to minimize', () => { test('no filter', () => { @@ -15,7 +15,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [], - token: `token`, }, ), ).toBeFalsy() @@ -36,7 +35,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [], - token: `token`, }, ), ).toBeFalsy() @@ -57,7 +55,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [], - token: `token`, }, ), ).toBeTruthy() @@ -77,7 +74,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [], - token: `token`, }, ), ).toBeFalsy() @@ -97,7 +93,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [''], contains: [], - token: `token`, }, ), ).toBeTruthy() @@ -116,7 +111,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [''], contains: [], - token: `token`, }, ), ).toBeFalsy() @@ -136,7 +130,6 @@ describe('filter to minimize', () => { endsWith: [''], startsWith: [], contains: [], - token: `token`, }, ), ).toBeTruthy() @@ -155,7 +148,6 @@ describe('filter to minimize', () => { endsWith: [''], startsWith: [], contains: [], - token: `token`, }, ), ).toBeFalsy() @@ -175,7 +167,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [''], - token: `token`, }, ), ).toBeTruthy() @@ -194,7 +185,6 @@ describe('filter to minimize', () => { endsWith: [], startsWith: [], contains: [''], - token: `token`, }, ), ).toBeFalsy()