Skip to content

Commit

Permalink
Add ends-with option (int128#11)
Browse files Browse the repository at this point in the history
* Add ends-with option

* Fix optional inputs

* Fix e2e test

* Add test and fix bug
  • Loading branch information
int128 authored Aug 7, 2021
1 parent ca74f97 commit 1dc33ce
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 43 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/ts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `<!-- hide-comment-action --> 👋 e2e-test`
body: `👋 e2e-test\n<!-- tail -->`
})
- if: github.event_name == 'pull_request'
uses: actions/github-script@v4
with:
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `<!-- head -->\n👋 e2e-test`
})
- name: e2e-test
uses: ./
with:
ends-with: |
<!-- tail -->
starts-with: |
<!-- hide-comment-action -->
<!-- head -->
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ This is an action to hide comment(s) in a pull request.

## Getting Started

This action will hide comment(s) which matches to the following filters:

- The body of comment starts with one of `starts-with`
- The author of comment is one of `authors`
- The comment is not hidden

To run this action:

```yaml
Expand All @@ -20,15 +14,25 @@ jobs:
steps:
- uses: int128/hide-comment-action@v1
with:
starts-with: |
ends-with: |
<!-- your-workflow-job -->
```
This action hides comment(s) which matches to the following filters:
- The body of comment starts with one of `starts-with`
- The body of comment ends with one of `ends-with`
- The author of comment is one of `authors`
- The comment is not hidden

It hides all comments created by `github-actions` by default.


## Inputs

| Name | Required | Description
|------|----------|-------------
| `starts-with` | yes | multi-line string of starts-with filter
| `authors` | yes | multi-line string of author filter (default to `github-actions`)
| `starts-with` | no | multi-line string of starts-with filter
| `ends-with` | no | multi-line string of ends-with filter
| `authors` | no | multi-line string of author filter (default to `github-actions`)
| `token` | no | GitHub token to post a comment
7 changes: 5 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ description: hide comment(s) in a pull request
inputs:
authors:
description: multi-line string of author filter
required: true
required: false
default: github-actions
starts-with:
description: multi-line string of starts-with filter
required: true
required: false
ends-with:
description: multi-line string of ends-with filter
required: false
token:
description: GitHub token to post a comment
required: true
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { run } from './run'
const main = async (): Promise<void> => {
try {
await run({
authors: core.getMultilineInput('authors', { required: true }),
startsWith: core.getMultilineInput('starts-with', { required: true }),
authors: core.getMultilineInput('authors'),
startsWith: core.getMultilineInput('starts-with'),
endsWith: core.getMultilineInput('ends-with'),
token: core.getInput('token', { required: true }),
})
} catch (error) {
Expand Down
48 changes: 22 additions & 26 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { minimizeComment } from './queries/minimize'
type Inputs = {
authors: string[]
startsWith: string[]
endsWith: string[]
token: string
}

Expand All @@ -32,38 +33,33 @@ export const run = async (inputs: Inputs): Promise<void> => {
}
}

type FilteredComment = Pick<IssueComment, 'id' | 'url'>
type Comment = Pick<IssueComment, 'id' | 'url' | 'isMinimized' | 'author' | 'body'>

const filterComments = (q: CommentsQuery, inputs: Inputs): FilteredComment[] => {
const filterComments = (q: CommentsQuery, inputs: Inputs): Comment[] => {
if (q.repository?.pullRequest?.comments.nodes == null) {
core.info(`unexpected response: repository === ${q.repository}`)
core.info(`unexpected response: repository.pullRequest === ${q.repository?.pullRequest}`)
return []
}
const comments = q.repository.pullRequest.comments.nodes.filter((c) => c != null) as Comment[]
return comments.filter((c) => toMinimize(c, inputs))
}

const f: FilteredComment[] = []
for (const c of q.repository.pullRequest.comments.nodes) {
if (c === null) {
continue
}
if (c.isMinimized) {
continue
}

if (inputs.authors.length > 0) {
if (!inputs.authors.some((a) => c.author?.login === a)) {
core.info(`authors did not match: ${c.url}`)
continue
}
}
if (inputs.startsWith.length > 0) {
if (!inputs.startsWith.some((s) => c.body.trimStart().startsWith(s))) {
core.info(`starts-with did not match: ${c.url}`)
continue
}
}

f.push(c)
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
}
return f
return false
}
156 changes: 154 additions & 2 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,155 @@
test('run successfully', async () => {
//TODO
import { toMinimize } from '../src/run'

describe('filter to minimize', () => {
test('no filter', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: false,
url: `https://www.example.com`,
},
{
authors: [],
endsWith: [],
startsWith: [],
token: `token`,
}
)
).toBeFalsy()
})

test('already minimized', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: true,
url: `https://www.example.com`,
author: { login: 'github-actions', url: '', avatarUrl: '', resourcePath: '' },
},
{
authors: ['github-actions'],
endsWith: [],
startsWith: [],
token: `token`,
}
)
).toBeFalsy()
})

test('authors filter', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: false,
url: `https://www.example.com`,
author: { login: 'github-actions', url: '', avatarUrl: '', resourcePath: '' },
},
{
authors: ['github-actions'],
endsWith: [],
startsWith: [],
token: `token`,
}
)
).toBeTruthy()
})
test('authors filter did not match', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: false,
url: `https://www.example.com`,
author: { login: 'github-actions', url: '', avatarUrl: '', resourcePath: '' },
},
{
authors: ['bot'],
endsWith: [],
startsWith: [],
token: `token`,
}
)
).toBeFalsy()
})

test('starts-with filter', () => {
expect(
toMinimize(
{
id: `id`,
body: `<!-- head -->body`,
isMinimized: false,
url: `https://www.example.com`,
},
{
authors: [],
endsWith: [],
startsWith: ['<!-- head -->'],
token: `token`,
}
)
).toBeTruthy()
})
test('starts-with filter did not match', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: false,
url: `https://www.example.com`,
},
{
authors: [],
endsWith: [],
startsWith: ['<!-- head -->'],
token: `token`,
}
)
).toBeFalsy()
})

test('ends-with filter', () => {
expect(
toMinimize(
{
id: `id`,
body: `body<!-- tail -->`,
isMinimized: false,
url: `https://www.example.com`,
},
{
authors: [],
endsWith: ['<!-- tail -->'],
startsWith: [],
token: `token`,
}
)
).toBeTruthy()
})
test('ends-with filter did not match', () => {
expect(
toMinimize(
{
id: `id`,
body: `body`,
isMinimized: false,
url: `https://www.example.com`,
},
{
authors: [],
endsWith: ['<!-- tail -->'],
startsWith: [],
token: `token`,
}
)
).toBeFalsy()
})
})

0 comments on commit 1dc33ce

Please sign in to comment.