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

Use @octokit/plugin-retry #1126

Merged
merged 2 commits into from
Nov 23, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

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

53 changes: 53 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -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<NonNullable<NonNullable<CommentsQuery['repository']>['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
}
8 changes: 8 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as github from '@actions/github'
import { retry } from '@octokit/plugin-retry'

export type Octokit = ReturnType<typeof github.getOctokit>

export const getOctokit = (token: string): Octokit => github.getOctokit(token, {}, retry)

export const context = github.context
6 changes: 2 additions & 4 deletions src/queries/comments.ts
Original file line number Diff line number Diff line change
@@ -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<typeof github.getOctokit>

const query = /* GraphQL */ `
query comments($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
Expand All @@ -23,6 +21,6 @@ const query = /* GraphQL */ `
}
`

export const queryComments = async (o: Octokit, v: CommentsQueryVariables): Promise<CommentsQuery> => {
export const queryComments = async (o: github.Octokit, v: CommentsQueryVariables): Promise<CommentsQuery> => {
return await o.graphql<CommentsQuery>(query, v)
}
6 changes: 2 additions & 4 deletions src/queries/minimize.ts
Original file line number Diff line number Diff line change
@@ -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<typeof github.getOctokit>

const query = /* GraphQL */ `
mutation minimizeComment($id: ID!) {
minimizeComment(input: { classifier: OUTDATED, subjectId: $id }) {
Expand All @@ -12,7 +10,7 @@ const query = /* GraphQL */ `
`

export const minimizeComment = async (
o: Octokit,
o: github.Octokit,
v: MinimizeCommentMutationVariables,
): Promise<MinimizeCommentMutation> => {
return await o.graphql<MinimizeCommentMutation>(query, v)
Expand Down
52 changes: 3 additions & 49 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { CommentsQuery } from './generated/graphql.js'
import * as github from './github.js'
import { filterComments } from './filter.js'
import { queryComments } from './queries/comments.js'
import { minimizeComment } from './queries/minimize.js'

type Octokit = ReturnType<typeof github.getOctokit>

type Inputs = {
authors: string[]
startsWith: string[]
Expand Down Expand Up @@ -63,7 +61,7 @@ export const run = async (inputs: Inputs): Promise<void> => {
}
}

const getCurrentLogin = async (octokit: Octokit) => {
const getCurrentLogin = async (octokit: github.Octokit) => {
try {
const { data: user } = await octokit.rest.users.getAuthenticated()
return user.login
Expand All @@ -72,47 +70,3 @@ const getCurrentLogin = async (octokit: Octokit) => {
return 'github-actions'
}
}

type Comment = NonNullable<
NonNullable<
NonNullable<NonNullable<NonNullable<CommentsQuery['repository']>['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
}
12 changes: 1 addition & 11 deletions tests/run.test.ts → tests/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toMinimize } from '../src/run.js'
import { toMinimize } from '../src/filter.js'

describe('filter to minimize', () => {
test('no filter', () => {
Expand All @@ -15,7 +15,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeFalsy()
Expand All @@ -36,7 +35,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeFalsy()
Expand All @@ -57,7 +55,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeTruthy()
Expand All @@ -77,7 +74,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeFalsy()
Expand All @@ -97,7 +93,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: ['<!-- head -->'],
contains: [],
token: `token`,
},
),
).toBeTruthy()
Expand All @@ -116,7 +111,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: ['<!-- head -->'],
contains: [],
token: `token`,
},
),
).toBeFalsy()
Expand All @@ -136,7 +130,6 @@ describe('filter to minimize', () => {
endsWith: ['<!-- tail -->'],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeTruthy()
Expand All @@ -155,7 +148,6 @@ describe('filter to minimize', () => {
endsWith: ['<!-- tail -->'],
startsWith: [],
contains: [],
token: `token`,
},
),
).toBeFalsy()
Expand All @@ -175,7 +167,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: ['<!-- mark -->'],
token: `token`,
},
),
).toBeTruthy()
Expand All @@ -194,7 +185,6 @@ describe('filter to minimize', () => {
endsWith: [],
startsWith: [],
contains: ['<!-- bar -->'],
token: `token`,
},
),
).toBeFalsy()
Expand Down
Loading