Skip to content

Commit d6a4565

Browse files
Merge pull request #6 from sandeshjangam/dev
Direction for fine
2 parents dc19bb3 + 035e6dd commit d6a4565

File tree

5 files changed

+105
-43
lines changed

5 files changed

+105
-43
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
type: find
7474
author: github-actions[bot]
7575
search_term: To check the delete action
76+
direction: newer
7677
number: ${{ steps.get_issue_number.outputs.issue_number }}
7778

7879
- name: Delete comment

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ A GitHub action to create, update, append, prepend, delete or find a comment on
1212
| ------------- | ---------------------------------------------------------------------------- | -------- | -------------------------- |
1313
| `token` | A GitHub token. | `false` | `${{ github.token }}` |
1414
| `repository` | Owner and repository name. e.g. `sandeshjangam/comment-actions` | `false` | `${{ github.repository }}` |
15-
| `type` | `create` | `true` | `N/A` |
16-
| `body` | Comment body. Required with `create` type. | `true` | `N/A` |
17-
| `number` | Number of the pull request or issue. Required with `create` and `find` type. | `true` | `N/A` |
18-
| `reactions` | Add comma separated list of reactions to react on the comment. `+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes` | `false` | `N/A` |
15+
| `type` | `create` | `true` | N/A |
16+
| `body` | Comment body. Required with `create` type. | `true` | N/A |
17+
| `number` | Number of the pull request or issue. Required with `create` and `find` type. | `true` | N/A |
18+
| `reactions` | Add comma separated list of reactions to react on the comment. `+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes` | `false` | N/A |
1919

2020
### Update Comment
2121

2222
| NAME | DESCRIPTION | REQUIRED | DEFAULT |
2323
| ------------- | ---------------------------------------------------------------------------- | -------- | -------------------------- |
2424
| `token` | A GitHub token. | `false` | `${{ github.token }}` |
2525
| `repository` | Owner and repository name. e.g. `sandeshjangam/comment-actions` | `false` | `${{ github.repository }}` |
26-
| `type` | `update`, `append` or `prepend` | `true` | `N/A` |
27-
| `body` | Comment body. Required with `update`, `append` or `prepend` | `true` | `N/A` |
28-
| `comment_id` | Comment id. Required with `update`, `append` or `prepend` | `true` | `N/A` |
29-
| `reactions` | Add comma separated list of reactions to react on the comment. `+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes` | `false` | `N/A` |
26+
| `type` | `update`, `append` or `prepend` | `true` | N/A |
27+
| `body` | Comment body. Required with `update`, `append` or `prepend` | `true` | N/A |
28+
| `comment_id` | Comment id. Required with `update`, `append` or `prepend` | `true` | N/A |
29+
| `reactions` | Add comma separated list of reactions to react on the comment. `+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes` | `false` | N/A |
3030

3131
### Delete Comment
3232

3333
| NAME | DESCRIPTION | REQUIRED | DEFAULT |
3434
| ------------- | ---------------------------------------------------------------------------- | -------- | -------------------------- |
3535
| `token` | A GitHub token. | `false` | `${{ github.token }}` |
3636
| `repository` | Owner and repository name. e.g. `sandeshjangam/comment-actions` | `false` | `${{ github.repository }}` |
37-
| `type` | `delete` | `true` | `N/A` |
38-
| `comment_id` | Comment id. Required `delete` type. | `true` | `N/A` |
37+
| `type` | `delete` | `true` | N/A |
38+
| `comment_id` | Comment id. Required `delete` type. | `true` | N/A |
3939

4040

4141
### Find Comment
@@ -44,12 +44,13 @@ A GitHub action to create, update, append, prepend, delete or find a comment on
4444
| ------------- | ---------------------------------------------------------------------------- | -------- | -------------------------- |
4545
| `token` | A GitHub token. | `false` | `${{ github.token }}` |
4646
| `repository` | Owner and repository name. e.g. `sandeshjangam/comment-actions` | `false` | `${{ github.repository }}` |
47-
| `type` | `find` | `true` | `N/A` |
48-
| `number` | Number of the pull request or issue. Required with `find` type. | `true` | `N/A` |
49-
| `search_term` | Search in body. Can use with conjunction of `author` arg. | `true` | `N/A` |
50-
| `author` | GitHub user name of the comment author. | `false` | `N/A` |
47+
| `type` | `find` | `true` | N/A |
48+
| `number` | Number of the pull request or issue. Required with `find` type. | `true` | N/A |
49+
| `search_term` | Search in body. Can use with conjunction of `author` arg. | `true` | N/A |
50+
| `author` | GitHub user name of the comment author. | `false` | N/A |
51+
| `direction` | `older` - To find an older comment. `newer` - To find a newer comment.' | `false` | `older` |
5152

52-
Note - To find a comment you can use either search_term or author and search_term both.
53+
Note - To find a comment you can use either `search_term` or `author` and `search_term` both.
5354

5455
## Outputs
5556

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ inputs:
3030
search_term:
3131
description: 'Search term, which is included in the comment body.'
3232
required: false
33+
direction:
34+
description: 'older - To find an older comment. newer - To find a newer comment.'
35+
default: older
36+
required: false
3337
# Reactions
3438
reactions:
3539
description: 'Add comma separated list of reactions to react on the comment.'

dist/index.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6288,7 +6288,7 @@ async function run() {
62886288
let actionType; let body;
62896289
let issueNumber; let commentId;
62906290
let searchTerm; let author;
6291-
let reactions;
6291+
let direction; let reactions;
62926292

62936293
const allowedReactions = [
62946294
'+1',
@@ -6442,31 +6442,58 @@ async function run() {
64426442
return outVars;
64436443
}
64446444

6445+
const args = {
6446+
owner,
6447+
repo,
6448+
issue_number: issueNumber,
6449+
};
6450+
64456451
let foundComment = false;
6446-
// eslint-disable-next-line no-restricted-syntax
6447-
for await ( const { data: listComments } of
6448-
octokit.paginate.iterator(
6452+
6453+
if ( direction === 'older' ) {
6454+
// eslint-disable-next-line no-restricted-syntax
6455+
for await ( const { data: listComments } of
6456+
octokit.paginate.iterator(
6457+
octokit.rest.issues.listComments,
6458+
args,
6459+
)
6460+
) {
6461+
// Search a comment which included user comment.
6462+
const comment = listComments.find(
6463+
// eslint-disable-next-line no-loop-func
6464+
( listComment ) => (
6465+
( searchTerm && listComment.body ? listComment.body.includes( searchTerm ) : true )
6466+
&& ( author && listComment.user ? listComment.user.login === author : true )
6467+
),
6468+
);
6469+
6470+
// If a comment found, assign.
6471+
if ( comment ) {
6472+
foundComment = comment;
6473+
break;
6474+
}
6475+
}
6476+
} else {
6477+
// Find a newer comment.
6478+
const listComments = await octokit.paginate(
64496479
octokit.rest.issues.listComments,
6450-
{
6451-
owner,
6452-
repo,
6453-
issue_number: issueNumber,
6454-
},
6455-
)
6456-
) {
6480+
args,
6481+
);
6482+
6483+
// Reverse the comments.
6484+
listComments.reverse();
6485+
64576486
// Search a comment which included user comment.
64586487
const comment = listComments.find(
6459-
// eslint-disable-next-line no-loop-func
64606488
( listComment ) => (
64616489
( searchTerm && listComment.body ? listComment.body.includes( searchTerm ) : true )
64626490
&& ( author && listComment.user ? listComment.user.login === author : true )
64636491
),
64646492
);
64656493

6466-
// If a comment found, return.
6494+
// If a comment found, assign.
64676495
if ( comment ) {
64686496
foundComment = comment;
6469-
break;
64706497
}
64716498
}
64726499

@@ -6524,6 +6551,7 @@ async function run() {
65246551
issueNumber = core.getInput( 'number' );
65256552
commentId = core.getInput( 'comment_id' );
65266553
searchTerm = core.getInput( 'search_term' );
6554+
direction = core.getInput( 'direction' );
65276555
author = core.getInput( 'author' );
65286556
reactions = core.getInput( 'reactions' );
65296557

src/action.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function run() {
77
let actionType; let body;
88
let issueNumber; let commentId;
99
let searchTerm; let author;
10-
let reactions;
10+
let direction; let reactions;
1111

1212
const allowedReactions = [
1313
'+1',
@@ -161,31 +161,58 @@ async function run() {
161161
return outVars;
162162
}
163163

164+
const args = {
165+
owner,
166+
repo,
167+
issue_number: issueNumber,
168+
};
169+
164170
let foundComment = false;
165-
// eslint-disable-next-line no-restricted-syntax
166-
for await ( const { data: listComments } of
167-
octokit.paginate.iterator(
171+
172+
if ( direction === 'older' ) {
173+
// eslint-disable-next-line no-restricted-syntax
174+
for await ( const { data: listComments } of
175+
octokit.paginate.iterator(
176+
octokit.rest.issues.listComments,
177+
args,
178+
)
179+
) {
180+
// Search a comment which included user comment.
181+
const comment = listComments.find(
182+
// eslint-disable-next-line no-loop-func
183+
( listComment ) => (
184+
( searchTerm && listComment.body ? listComment.body.includes( searchTerm ) : true )
185+
&& ( author && listComment.user ? listComment.user.login === author : true )
186+
),
187+
);
188+
189+
// If a comment found, assign.
190+
if ( comment ) {
191+
foundComment = comment;
192+
break;
193+
}
194+
}
195+
} else {
196+
// Find a newer comment.
197+
const listComments = await octokit.paginate(
168198
octokit.rest.issues.listComments,
169-
{
170-
owner,
171-
repo,
172-
issue_number: issueNumber,
173-
},
174-
)
175-
) {
199+
args,
200+
);
201+
202+
// Reverse the comments.
203+
listComments.reverse();
204+
176205
// Search a comment which included user comment.
177206
const comment = listComments.find(
178-
// eslint-disable-next-line no-loop-func
179207
( listComment ) => (
180208
( searchTerm && listComment.body ? listComment.body.includes( searchTerm ) : true )
181209
&& ( author && listComment.user ? listComment.user.login === author : true )
182210
),
183211
);
184212

185-
// If a comment found, return.
213+
// If a comment found, assign.
186214
if ( comment ) {
187215
foundComment = comment;
188-
break;
189216
}
190217
}
191218

@@ -243,6 +270,7 @@ async function run() {
243270
issueNumber = core.getInput( 'number' );
244271
commentId = core.getInput( 'comment_id' );
245272
searchTerm = core.getInput( 'search_term' );
273+
direction = core.getInput( 'direction' );
246274
author = core.getInput( 'author' );
247275
reactions = core.getInput( 'reactions' );
248276

0 commit comments

Comments
 (0)