Skip to content

Commit

Permalink
feat: delete comment (#8)
Browse files Browse the repository at this point in the history
* add delete capability

* generates dist for updated code

* add docs

* revert formatting from prettier

* fix tense

* address review
  • Loading branch information
robertjdominguez committed Jul 21, 2023
1 parent 1d904b4 commit 1055836
Show file tree
Hide file tree
Showing 6 changed files with 1,143 additions and 549 deletions.
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If the workflow succeeds, the bot comments the details of the preview environmen

## Usage
```yml
- uses: hasura/comment-progress@v2.1.0
- uses: hasura/comment-progress@v2.2.0
with:
# The GitHub token to be used when creating/updating comments
# ${{ secrets.GITHUB_TOKEN }} is provided by default by GitHub actions
Expand Down Expand Up @@ -57,6 +57,9 @@ If the workflow succeeds, the bot comments the details of the preview environmen
# Deletes all the existing comments matching the given id and
# creates a new comment with the given message
recreate: true

# Deletes all the existing comments matching the given id
delete: true
```


Expand All @@ -69,6 +72,7 @@ If the workflow succeeds, the bot comments the details of the preview environmen
- [Make a simple comment on a commit](#make-a-simple-comment-on-a-commit)
- [Make a comment and append updates to the same comment](#make-a-comment-and-append-updates-to-the-same-comment)
- [Delete older/stale comment and add a new comment](#delete-olderstale-comment-and-add-a-new-comment)
- [Delete a comment which is no longer relevant](#delete-a-comment-which-is-no-longer-relevant)

### Make a simple comment on an issue or pull request

Expand All @@ -85,7 +89,7 @@ jobs:
name: Say thanks for the PR
steps:
- name: comment on the pull request
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand All @@ -111,7 +115,7 @@ jobs:
name: Comment on commit with some info
steps:
- name: Comment on commit
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand All @@ -136,7 +140,7 @@ jobs:
name: Deploy preview
steps:
- name: Notify about starting this deployment
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand All @@ -150,7 +154,7 @@ jobs:
# long running step
- name: Notify about the result of this deployment
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand Down Expand Up @@ -178,7 +182,7 @@ jobs:
name: Deploy preview
steps:
- name: Notify about starting this deployment
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand All @@ -192,7 +196,7 @@ jobs:
# long running step
- name: Notify about the result of this deployment
uses: hasura/comment-progress@v2.1.0
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
Expand All @@ -202,6 +206,29 @@ jobs:
recreate: true
```

## Delete a comment which is no longer relevant

Take a case where you need to delete a comment which is no longer relevant. E.g., let's say we previously added a comment in a PR with `id: preview-url` to post a link where the changes of the pull request could be previewed. It might be useful to delete such a comment when the PR is closed to avoid users from accessing stale preview links. We can use the `delete` flag to achieve this.

```yml
on:
pull_request:
types: [closed]

jobs:
cleanup-automated-comments:
runs-on: ubuntu-20.04
name: Delete automated PR comments
steps:
- name: delete comment that contains a preview link
uses: hasura/comment-progress@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: 'my-org/my-repo'
number: ${{ github.event.number }}
id: preview-url
```

## Contributing

Contributions are welcome :pray: Please [open an issue](https://github.com/hasura/comment-progress/issues/new) before working on something big or breaking.
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ inputs:
fail:
description: 'If true, the step will be marked as failure'
append:
description: 'If true, message will be appened to existing comment'
description: 'If true, message will be appended to existing comment'
required: false
default: false
recreate:
description: 'If true, message will be commented after deleting existing comment'
required: false
default: false
delete:
description: 'If true, the comment will be deleted'
required: false
default: false
runs:
using: 'node12'
main: 'dist/index.js'
54 changes: 38 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ async function appendMode(commenter, identifier, message) {
console.log(`Created comment: ${resp.data.html_url}`);
}

// delete mode deletes an existing comment that matches the identifier
async function deleteMode(commenter, identifier) {
console.log(`Finding matching comment for ${identifier}.`);
const matchingComment = await findMatchingComment(commenter, identifier);

if (matchingComment) {
console.log(`Deleting github comment ${matchingComment.id}`);
await commenter.deleteComment(matchingComment.id);
}
}

// CONCATENATED MODULE: ./index.js


Expand All @@ -230,27 +241,31 @@ async function appendMode(commenter, identifier, message) {

(async () => {
try {
const repository = core.getInput('repository');
const [owner, repo] = repository.split('/');
const repository = core.getInput("repository");
const [owner, repo] = repository.split("/");
if (!owner || !repo) {
throw new Error(`Invalid repository: ${repository}`);
}

const number = core.getInput('number');
const commitSHA = core.getInput('commit-sha');
const identifier = core.getInput('id');
const append = core.getInput('append');
const recreate = core.getInput('recreate');
const fail = core.getInput('fail');
const githubToken = core.getInput('github-token');
const message = core.getInput('message');
const number = core.getInput("number");
const commitSHA = core.getInput("commit-sha");
const identifier = core.getInput("id");
const appendComment = core.getInput("append");
const recreateComment = core.getInput("recreate");
const deleteComment = core.getInput("delete");
const fail = core.getInput("fail");
const githubToken = core.getInput("github-token");
const message = core.getInput("message");

const octokit = github.getOctokit(githubToken);

let commenter;
try {
commenter = getCommenter(octokit, {
owner, repo, number, commitSHA,
owner,
repo,
number,
commitSHA,
});
} catch (err) {
core.setFailed(err);
Expand All @@ -259,20 +274,27 @@ async function appendMode(commenter, identifier, message) {

let mode = normalMode;

if (append === 'true' && recreate === 'true') {
core.setFailed('Not allowed to set both `append` and `recreate` to true.');
if (appendComment === "true" && recreateComment === "true") {
core.setFailed("Not allowed to set both `append` and `recreate` to true.");
return;
}

if (deleteComment === "true" && (appendComment === "true" || recreateComment === "true")) {
core.setFailed("Not allowed to set `delete` to true with `append` or `recreate`.");
return;
}

if (recreate === 'true') {
if (recreateComment === "true") {
mode = recreateMode;
} else if (append === 'true') {
} else if (appendComment === "true") {
mode = appendMode;
} else if (deleteComment === "true") {
mode = deleteMode;
}

await mode(commenter, identifier, message);

if (fail === 'true') {
if (fail === "true") {
core.setFailed(message);
}
} catch (error) {
Expand Down
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import { normalMode, recreateMode, appendMode } from './modes';
import { normalMode, recreateMode, appendMode, deleteMode } from './modes';
import { getCommenter } from './comment/commenter';

(async () => {
Expand All @@ -14,8 +14,9 @@ import { getCommenter } from './comment/commenter';
const number = core.getInput('number');
const commitSHA = core.getInput('commit-sha');
const identifier = core.getInput('id');
const append = core.getInput('append');
const recreate = core.getInput('recreate');
const appendComment = core.getInput('append');
const recreateComment = core.getInput('recreate');
const deleteComment = core.getInput('delete');
const fail = core.getInput('fail');
const githubToken = core.getInput('github-token');
const message = core.getInput('message');
Expand All @@ -25,7 +26,10 @@ import { getCommenter } from './comment/commenter';
let commenter;
try {
commenter = getCommenter(octokit, {
owner, repo, number, commitSHA,
owner,
repo,
number,
commitSHA,
});
} catch (err) {
core.setFailed(err);
Expand All @@ -34,15 +38,22 @@ import { getCommenter } from './comment/commenter';

let mode = normalMode;

if (append === 'true' && recreate === 'true') {
if (appendComment === 'true' && recreateComment === 'true') {
core.setFailed('Not allowed to set both `append` and `recreate` to true.');
return;
}

if (recreate === 'true') {
if (deleteComment === 'true' && (appendComment === 'true' || recreateComment === 'true')) {
core.setFailed('Not allowed to set `delete` to true with `append` or `recreate`.');
return;
}

if (recreateComment === 'true') {
mode = recreateMode;
} else if (append === 'true') {
} else if (appendComment === 'true') {
mode = appendMode;
} else if (deleteComment === 'true') {
mode = deleteMode;
}

await mode(commenter, identifier, message);
Expand Down
11 changes: 11 additions & 0 deletions modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ export async function appendMode(commenter, identifier, message) {
const resp = await commenter.createComment(comment);
console.log(`Created comment: ${resp.data.html_url}`);
}

// delete mode deletes an existing comment that matches the identifier
export async function deleteMode(commenter, identifier) {
console.log(`Finding matching comment for ${identifier}.`);
const matchingComment = await findMatchingComment(commenter, identifier);

if (matchingComment) {
console.log(`Deleting github comment ${matchingComment.id}`);
await commenter.deleteComment(matchingComment.id);
}
}

0 comments on commit 1055836

Please sign in to comment.