Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Nov 23, 2024
1 parent 004274f commit badabe7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 56 deletions.
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
}
46 changes: 1 addition & 45 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -70,47 +70,3 @@ const getCurrentLogin = async (octokit: github.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

0 comments on commit badabe7

Please sign in to comment.