Skip to content

Commit

Permalink
feat: add search direction input
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Feb 2, 2021
1 parent 8168bad commit dcf34fe
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 45 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ jobs:
- if: steps.fc6.outputs.comment-id != 703352283
run: exit 1

- name: Find the last comment by body-includes and author
uses: ./
id: fc7
with:
issue-number: 1
comment-author: peter-evans
body-includes: search string 1
direction: last
- if: steps.fc7.outputs.comment-id != 771260630
run: exit 1

package:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: [test]
Expand Down
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

A GitHub action to find an issue or pull request comment.

The action will output the comment ID of the first comment matching the search criteria.
The action will output the comment ID of the comment matching the search criteria.

## Usage

### Find a comment containing the specified string
### Find the first comment containing the specified string

```yml
- name: Find Comment
Expand All @@ -19,7 +19,7 @@ The action will output the comment ID of the first comment matching the search c
body-includes: search string 1
```
### Find a comment by the specified author
### Find the first comment by the specified author
```yml
- name: Find Comment
Expand All @@ -30,7 +30,7 @@ The action will output the comment ID of the first comment matching the search c
comment-author: peter-evans
```
### Find a comment containing the specified string AND by the specified author
### Find the first comment containing the specified string AND by the specified author
```yml
- name: Find Comment
Expand All @@ -42,6 +42,18 @@ The action will output the comment ID of the first comment matching the search c
body-includes: search string 1
```
### Find the last comment containing the specified string
```yml
- name: Find Comment
uses: peter-evans/find-comment@v1
id: fc
with:
issue-number: 1
body-includes: search string 1
direction: last
```
### Action inputs
| Name | Description | Default |
Expand All @@ -51,10 +63,11 @@ The action will output the comment ID of the first comment matching the search c
| `issue-number` | The number of the issue or pull request in which to search. | |
| `comment-author` | The GitHub user name of the comment author. | |
| `body-includes` | A string to search for in the body of comments. | |
| `direction` | Search direction, specified as `first` or `last` | `first` |

#### Outputs

The `comment-id` and `comment-body` of the first matching comment found will be output for use in later steps.
The `comment-id` and `comment-body` of the matching comment found will be output for use in later steps.
They will be empty strings if no matching comment was found.
Note that in order to read the step outputs the action step must have an id.

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ inputs:
description: 'The GitHub user name of the comment author.'
body-includes:
description: 'A string to search for in the body of comments.'
direction:
description: 'Search direction, specified as `first` or `last`'
default: first
outputs:
comment-id:
description: 'The id of the first matching comment found.'
description: 'The id of the matching comment found.'
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
66 changes: 43 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__webpack_require__(186));
const github = __importStar(__webpack_require__(438));
const util_1 = __webpack_require__(669);
function findCommentPredicate(inputs, comment) {
return ((inputs.commentAuthor
? comment.user.login === inputs.commentAuthor
: true) &&
(inputs.bodyIncludes ? comment.body.includes(inputs.bodyIncludes) : true));
}
function findComment(inputs) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -56,32 +62,45 @@ function findComment(inputs) {
repo: repo,
issue_number: inputs.issueNumber
};
try {
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.issues.listComments, parameters)), _c; _c = yield _b.next(), !_c.done;) {
const { data: comments } = _c.value;
// Search each page for the comment
const comment = comments.find(comment => {
return ((inputs.commentAuthor
? comment.user.login === inputs.commentAuthor
: true) &&
(inputs.bodyIncludes
? comment.body.includes(inputs.bodyIncludes)
: true));
});
if (comment) {
return {
id: comment.id,
body: comment.body
};
if (inputs.direction == 'first') {
try {
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.issues.listComments, parameters)), _c; _c = yield _b.next(), !_c.done;) {
const { data: comments } = _c.value;
// Search each page for the comment
const comment = comments.find(comment => findCommentPredicate(inputs, comment));
if (comment) {
return {
id: comment.id,
body: comment.body,
user: {
login: comment.user.login
}
};
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
else {
// direction == 'last'
const comments = yield octokit.paginate(octokit.issues.listComments, parameters);
comments.reverse();
const comment = comments.find(comment => findCommentPredicate(inputs, comment));
if (comment) {
return {
id: comment.id,
body: comment.body,
user: {
login: comment.user.login
}
};
}
finally { if (e_1) throw e_1.error; }
}
return undefined;
});
Expand All @@ -94,7 +113,8 @@ function run() {
repository: core.getInput('repository'),
issueNumber: Number(core.getInput('issue-number')),
commentAuthor: core.getInput('comment-author'),
bodyIncludes: core.getInput('body-includes')
bodyIncludes: core.getInput('body-includes'),
direction: core.getInput('direction')
};
core.debug(`Inputs: ${util_1.inspect(inputs)}`);
const comment = yield findComment(inputs);
Expand Down
64 changes: 48 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ interface Inputs {
issueNumber: number
commentAuthor: string
bodyIncludes: string
direction: string
}

interface Comment {
id: number
body: string
user: {
login: string
}
}

function findCommentPredicate(inputs: Inputs, comment: Comment): boolean {
return (
(inputs.commentAuthor
? comment.user.login === inputs.commentAuthor
: true) &&
(inputs.bodyIncludes ? comment.body.includes(inputs.bodyIncludes) : true)
)
}

async function findComment(inputs: Inputs): Promise<Comment | undefined> {
Expand All @@ -24,25 +37,43 @@ async function findComment(inputs: Inputs): Promise<Comment | undefined> {
repo: repo,
issue_number: inputs.issueNumber
}
for await (const {data: comments} of octokit.paginate.iterator(
octokit.issues.listComments,
parameters
)) {
// Search each page for the comment
const comment = comments.find(comment => {
return (
(inputs.commentAuthor
? comment.user.login === inputs.commentAuthor
: true) &&
(inputs.bodyIncludes
? comment.body.includes(inputs.bodyIncludes)
: true)

if (inputs.direction == 'first') {
for await (const {data: comments} of octokit.paginate.iterator(
octokit.issues.listComments,
parameters
)) {
// Search each page for the comment
const comment = comments.find(comment =>
findCommentPredicate(inputs, comment)
)
})
if (comment) {
return {
id: comment.id,
body: comment.body,
user: {
login: comment.user.login
}
}
}
}
} else {
// direction == 'last'
const comments = await octokit.paginate(
octokit.issues.listComments,
parameters
)
comments.reverse()
const comment = comments.find(comment =>
findCommentPredicate(inputs, comment)
)
if (comment) {
return {
id: comment.id,
body: comment.body
body: comment.body,
user: {
login: comment.user.login
}
}
}
}
Expand All @@ -56,7 +87,8 @@ async function run(): Promise<void> {
repository: core.getInput('repository'),
issueNumber: Number(core.getInput('issue-number')),
commentAuthor: core.getInput('comment-author'),
bodyIncludes: core.getInput('body-includes')
bodyIncludes: core.getInput('body-includes'),
direction: core.getInput('direction')
}
core.debug(`Inputs: ${inspect(inputs)}`)

Expand Down

0 comments on commit dcf34fe

Please sign in to comment.