|
1 |
| -name: Copy Issue Description to Comment with Header |
| 1 | +name: Update Comment with Original Request on Edit |
2 | 2 |
|
3 | 3 | on:
|
4 | 4 | issues:
|
5 |
| - types: [opened] |
| 5 | + types: [opened, edited] |
6 | 6 |
|
7 | 7 | jobs:
|
8 |
| - copy_description_to_comment: |
| 8 | + update_comment: |
9 | 9 | runs-on: ubuntu-latest
|
10 | 10 | steps:
|
11 |
| - - name: Copy issue description to comment with header |
| 11 | + - name: Update comment with header and horizontal rule on issue edit |
12 | 12 | uses: actions/github-script@v6
|
13 | 13 | with:
|
14 | 14 | github-token: ${{secrets.GITHUB_TOKEN}}
|
15 | 15 | script: |
|
16 | 16 | const issue = context.issue;
|
17 | 17 | const issueBody = context.payload.issue.body;
|
| 18 | + const octokit = github.getOctokit(process.env.GITHUB_TOKEN); |
18 | 19 |
|
19 |
| - // Create a comment with a header and the issue description |
20 |
| - const commentBody = `### Original Request\n\n${issueBody}`; |
| 20 | + // Create or update a comment with a header, horizontal rule, and the issue description |
| 21 | + const commentBody = `### Original Request\n\n-----\n\n${issueBody}`; |
21 | 22 |
|
22 |
| - await github.rest.issues.createComment({ |
| 23 | + // Fetch all comments for the issue |
| 24 | + const { data: comments } = await octokit.rest.issues.listComments({ |
23 | 25 | owner: issue.owner,
|
24 | 26 | repo: issue.repo,
|
25 | 27 | issue_number: issue.number,
|
26 |
| - body: commentBody |
27 | 28 | });
|
| 29 | +
|
| 30 | + // Find a specific comment by its unique header (if it exists) |
| 31 | + const originalRequestComment = comments.find(comment => comment.body.startsWith('### Original Request')); |
| 32 | +
|
| 33 | + if (originalRequestComment) { |
| 34 | + // If the comment exists, update it |
| 35 | + await octokit.rest.issues.updateComment({ |
| 36 | + owner: issue.owner, |
| 37 | + repo: issue.repo, |
| 38 | + comment_id: originalRequestComment.id, |
| 39 | + body: commentBody, |
| 40 | + }); |
| 41 | + } else { |
| 42 | + // If the comment does not exist, create it |
| 43 | + await octokit.rest.issues.createComment({ |
| 44 | + owner: issue.owner, |
| 45 | + repo: issue.repo, |
| 46 | + issue_number: issue.number, |
| 47 | + body: commentBody, |
| 48 | + }); |
| 49 | + } |
0 commit comments