Skip to content

Commit 309ce79

Browse files
authored
Merge pull request #34 from peter-evans/search-direction
feat: add search direction input
2 parents 8168bad + dcf34fe commit 309ce79

File tree

5 files changed

+124
-45
lines changed

5 files changed

+124
-45
lines changed

.github/workflows/ci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ jobs:
109109
- if: steps.fc6.outputs.comment-id != 703352283
110110
run: exit 1
111111

112+
- name: Find the last comment by body-includes and author
113+
uses: ./
114+
id: fc7
115+
with:
116+
issue-number: 1
117+
comment-author: peter-evans
118+
body-includes: search string 1
119+
direction: last
120+
- if: steps.fc7.outputs.comment-id != 771260630
121+
run: exit 1
122+
112123
package:
113124
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
114125
needs: [test]

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

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

7-
The action will output the comment ID of the first comment matching the search criteria.
7+
The action will output the comment ID of the comment matching the search criteria.
88

99
## Usage
1010

11-
### Find a comment containing the specified string
11+
### Find the first comment containing the specified string
1212

1313
```yml
1414
- name: Find Comment
@@ -19,7 +19,7 @@ The action will output the comment ID of the first comment matching the search c
1919
body-includes: search string 1
2020
```
2121
22-
### Find a comment by the specified author
22+
### Find the first comment by the specified author
2323
2424
```yml
2525
- name: Find Comment
@@ -30,7 +30,7 @@ The action will output the comment ID of the first comment matching the search c
3030
comment-author: peter-evans
3131
```
3232
33-
### Find a comment containing the specified string AND by the specified author
33+
### Find the first comment containing the specified string AND by the specified author
3434
3535
```yml
3636
- name: Find Comment
@@ -42,6 +42,18 @@ The action will output the comment ID of the first comment matching the search c
4242
body-includes: search string 1
4343
```
4444
45+
### Find the last comment containing the specified string
46+
47+
```yml
48+
- name: Find Comment
49+
uses: peter-evans/find-comment@v1
50+
id: fc
51+
with:
52+
issue-number: 1
53+
body-includes: search string 1
54+
direction: last
55+
```
56+
4557
### Action inputs
4658
4759
| Name | Description | Default |
@@ -51,10 +63,11 @@ The action will output the comment ID of the first comment matching the search c
5163
| `issue-number` | The number of the issue or pull request in which to search. | |
5264
| `comment-author` | The GitHub user name of the comment author. | |
5365
| `body-includes` | A string to search for in the body of comments. | |
66+
| `direction` | Search direction, specified as `first` or `last` | `first` |
5467

5568
#### Outputs
5669

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

action.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ inputs:
1313
description: 'The GitHub user name of the comment author.'
1414
body-includes:
1515
description: 'A string to search for in the body of comments.'
16+
direction:
17+
description: 'Search direction, specified as `first` or `last`'
18+
default: first
1619
outputs:
1720
comment-id:
18-
description: 'The id of the first matching comment found.'
21+
description: 'The id of the matching comment found.'
1922
runs:
2023
using: 'node12'
2124
main: 'dist/index.js'

dist/index.js

+43-23
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
4646
const core = __importStar(__webpack_require__(186));
4747
const github = __importStar(__webpack_require__(438));
4848
const util_1 = __webpack_require__(669);
49+
function findCommentPredicate(inputs, comment) {
50+
return ((inputs.commentAuthor
51+
? comment.user.login === inputs.commentAuthor
52+
: true) &&
53+
(inputs.bodyIncludes ? comment.body.includes(inputs.bodyIncludes) : true));
54+
}
4955
function findComment(inputs) {
5056
var e_1, _a;
5157
return __awaiter(this, void 0, void 0, function* () {
@@ -56,32 +62,45 @@ function findComment(inputs) {
5662
repo: repo,
5763
issue_number: inputs.issueNumber
5864
};
59-
try {
60-
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.issues.listComments, parameters)), _c; _c = yield _b.next(), !_c.done;) {
61-
const { data: comments } = _c.value;
62-
// Search each page for the comment
63-
const comment = comments.find(comment => {
64-
return ((inputs.commentAuthor
65-
? comment.user.login === inputs.commentAuthor
66-
: true) &&
67-
(inputs.bodyIncludes
68-
? comment.body.includes(inputs.bodyIncludes)
69-
: true));
70-
});
71-
if (comment) {
72-
return {
73-
id: comment.id,
74-
body: comment.body
75-
};
65+
if (inputs.direction == 'first') {
66+
try {
67+
for (var _b = __asyncValues(octokit.paginate.iterator(octokit.issues.listComments, parameters)), _c; _c = yield _b.next(), !_c.done;) {
68+
const { data: comments } = _c.value;
69+
// Search each page for the comment
70+
const comment = comments.find(comment => findCommentPredicate(inputs, comment));
71+
if (comment) {
72+
return {
73+
id: comment.id,
74+
body: comment.body,
75+
user: {
76+
login: comment.user.login
77+
}
78+
};
79+
}
80+
}
81+
}
82+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
83+
finally {
84+
try {
85+
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
7686
}
87+
finally { if (e_1) throw e_1.error; }
7788
}
7889
}
79-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
80-
finally {
81-
try {
82-
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
90+
else {
91+
// direction == 'last'
92+
const comments = yield octokit.paginate(octokit.issues.listComments, parameters);
93+
comments.reverse();
94+
const comment = comments.find(comment => findCommentPredicate(inputs, comment));
95+
if (comment) {
96+
return {
97+
id: comment.id,
98+
body: comment.body,
99+
user: {
100+
login: comment.user.login
101+
}
102+
};
83103
}
84-
finally { if (e_1) throw e_1.error; }
85104
}
86105
return undefined;
87106
});
@@ -94,7 +113,8 @@ function run() {
94113
repository: core.getInput('repository'),
95114
issueNumber: Number(core.getInput('issue-number')),
96115
commentAuthor: core.getInput('comment-author'),
97-
bodyIncludes: core.getInput('body-includes')
116+
bodyIncludes: core.getInput('body-includes'),
117+
direction: core.getInput('direction')
98118
};
99119
core.debug(`Inputs: ${util_1.inspect(inputs)}`);
100120
const comment = yield findComment(inputs);

src/main.ts

+48-16
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@ interface Inputs {
88
issueNumber: number
99
commentAuthor: string
1010
bodyIncludes: string
11+
direction: string
1112
}
1213

1314
interface Comment {
1415
id: number
1516
body: string
17+
user: {
18+
login: string
19+
}
20+
}
21+
22+
function findCommentPredicate(inputs: Inputs, comment: Comment): boolean {
23+
return (
24+
(inputs.commentAuthor
25+
? comment.user.login === inputs.commentAuthor
26+
: true) &&
27+
(inputs.bodyIncludes ? comment.body.includes(inputs.bodyIncludes) : true)
28+
)
1629
}
1730

1831
async function findComment(inputs: Inputs): Promise<Comment | undefined> {
@@ -24,25 +37,43 @@ async function findComment(inputs: Inputs): Promise<Comment | undefined> {
2437
repo: repo,
2538
issue_number: inputs.issueNumber
2639
}
27-
for await (const {data: comments} of octokit.paginate.iterator(
28-
octokit.issues.listComments,
29-
parameters
30-
)) {
31-
// Search each page for the comment
32-
const comment = comments.find(comment => {
33-
return (
34-
(inputs.commentAuthor
35-
? comment.user.login === inputs.commentAuthor
36-
: true) &&
37-
(inputs.bodyIncludes
38-
? comment.body.includes(inputs.bodyIncludes)
39-
: true)
40+
41+
if (inputs.direction == 'first') {
42+
for await (const {data: comments} of octokit.paginate.iterator(
43+
octokit.issues.listComments,
44+
parameters
45+
)) {
46+
// Search each page for the comment
47+
const comment = comments.find(comment =>
48+
findCommentPredicate(inputs, comment)
4049
)
41-
})
50+
if (comment) {
51+
return {
52+
id: comment.id,
53+
body: comment.body,
54+
user: {
55+
login: comment.user.login
56+
}
57+
}
58+
}
59+
}
60+
} else {
61+
// direction == 'last'
62+
const comments = await octokit.paginate(
63+
octokit.issues.listComments,
64+
parameters
65+
)
66+
comments.reverse()
67+
const comment = comments.find(comment =>
68+
findCommentPredicate(inputs, comment)
69+
)
4270
if (comment) {
4371
return {
4472
id: comment.id,
45-
body: comment.body
73+
body: comment.body,
74+
user: {
75+
login: comment.user.login
76+
}
4677
}
4778
}
4879
}
@@ -56,7 +87,8 @@ async function run(): Promise<void> {
5687
repository: core.getInput('repository'),
5788
issueNumber: Number(core.getInput('issue-number')),
5889
commentAuthor: core.getInput('comment-author'),
59-
bodyIncludes: core.getInput('body-includes')
90+
bodyIncludes: core.getInput('body-includes'),
91+
direction: core.getInput('direction')
6092
}
6193
core.debug(`Inputs: ${inspect(inputs)}`)
6294

0 commit comments

Comments
 (0)