diff --git a/.github/workflows/ts.yaml b/.github/workflows/ts.yaml index f8eac1d4..6cf31895 100644 --- a/.github/workflows/ts.yaml +++ b/.github/workflows/ts.yaml @@ -50,6 +50,13 @@ jobs: label: changed - run: test ${{ steps.no-diff.outputs.different }} = false + - name: e2e-test (comment-body-no-diff) + uses: ./ + with: + base: tests/fixtures/head + head: tests/fixtures/head + comment-body-no-diff: '' + generate: runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/README.md b/README.md index 4a06907a..ed5294e3 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ To post a comment of the diff between `old-directory` and `new-directory`, head: new-directory ``` -If no difference, it post a comment of "No diff". +If no difference, it post a comment of "No diff" by default. ### Show diff of generated manifests @@ -67,6 +67,24 @@ To add label(s) if there is difference or remove it if not: label: manifest-changed ``` +### No diff comment + +To change the comment of when no difference, + +```yaml +- uses: int128/diff-action@v1 + with: + comment-body-no-diff: No diff of kustomize build +``` + +To suppress any comment when no difference, + +```yaml +- uses: int128/diff-action@v1 + with: + comment-body-no-diff: '' +``` + ### Comment strategy This action supports the following strategies: @@ -87,11 +105,12 @@ This action posts a comment on `pull_request` or `pull_request_target` event onl ### Inputs -| Name | Required | Description | +| Name | Default | Description | | ---------------------- | ------------------------------------------ | ---------------------------------------------------------- | | `base` | (required) | Path(s) of base (multiline) | | `head` | (required) | Path(s) of head (multiline) | | `label` | - | Label(s) to add or remove to indicate the diff (multiline) | +| `comment-body-no-diff` | No diff | Comment body when no difference | | `comment-header` | - | Header of a comment to post | | `comment-footer` | - | Footer of a comment to post | | `update-if-exists` | (optional) | Either `create`, `replace`, `append` or `recreate` | diff --git a/action.yaml b/action.yaml index b8c980b7..a4b08ca2 100644 --- a/action.yaml +++ b/action.yaml @@ -10,6 +10,10 @@ inputs: label: description: Label(s) to add or remove to indicate the diff (multiline) required: false + comment-body-no-diff: + description: Comment body when no difference + required: false + default: No diff comment-header: description: Header of a comment to post required: false diff --git a/src/comment.ts b/src/comment.ts index d389b973..f69efab9 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -16,6 +16,10 @@ export const addComment = async (github: GitHubContext, comment: Comment): Promi } if (comment.updateIfExists === 'create') { + if (comment.body === '') { + core.info('Nothing to create') + return + } core.info(`Creating a comment to #${github.issueNumber}`) const { data: created } = await github.octokit.rest.issues.createComment({ owner: github.owner, @@ -32,6 +36,10 @@ export const addComment = async (github: GitHubContext, comment: Comment): Promi const existingComment = await findComment(github, commentKey) if (!existingComment) { core.info(`Key not found in #${github.issueNumber}`) + if (comment.body === '') { + core.info('Nothing to create') + return + } const { data: created } = await github.octokit.rest.issues.createComment({ owner: github.owner, repo: github.repo, @@ -50,7 +58,10 @@ export const addComment = async (github: GitHubContext, comment: Comment): Promi comment_id: existingComment.id, }) core.info(`Deleted the comment ${existingComment.html_url}`) - + if (comment.body === '') { + core.info('Nothing to create') + return + } const { data: created } = await github.octokit.rest.issues.createComment({ owner: github.owner, repo: github.repo, diff --git a/src/format.ts b/src/format.ts index 66a96dc7..7f9956ba 100644 --- a/src/format.ts +++ b/src/format.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core' import { Diff } from './diff.js' type CommentOptions = { + bodyNoDiff: string header: string footer: string workflowRunURL: string @@ -9,6 +10,9 @@ type CommentOptions = { export const formatComment = (diffs: Diff[], o: CommentOptions): string => { if (diffs.length === 0) { + if (o.bodyNoDiff === '') { + return '' + } return generateNoDiffComment(o) } @@ -35,7 +39,7 @@ export const formatComment = (diffs: Diff[], o: CommentOptions): string => { const generateNoDiffComment = (o: CommentOptions): string => `\ ${o.header} -No diff +${o.bodyNoDiff} ${o.footer}` diff --git a/src/main.ts b/src/main.ts index b073afaf..eeaef969 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,7 @@ const main = async (): Promise => { base: core.getInput('base', { required: true }), head: core.getInput('head', { required: true }), label: core.getMultilineInput('label', { required: false }), + commentBodyNoDiff: core.getInput('comment-body-no-diff'), commentHeader: core.getInput('comment-header'), commentFooter: core.getInput('comment-footer'), updateIfExists: updateIfExistsValue(core.getInput('update-if-exists')), diff --git a/src/run.ts b/src/run.ts index e23cbb55..53468d68 100644 --- a/src/run.ts +++ b/src/run.ts @@ -9,6 +9,7 @@ type Inputs = { base: string head: string label: string[] + commentBodyNoDiff: string commentHeader: string commentFooter: string updateIfExists: UpdateIfExistsType @@ -26,6 +27,7 @@ export const run = async (github: GitHubContext, inputs: Inputs): Promise { }, ], { + bodyNoDiff: 'No diff', header: '## diff', footer: '', workflowRunURL: 'https://github.com/int128/diff-action/actions/runs/6282216330',