Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment-body-no-diff option #838

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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` |
Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import * as core from '@actions/core'
import { Diff } from './diff.js'

type CommentOptions = {
bodyNoDiff: string
header: string
footer: string
workflowRunURL: string
}

export const formatComment = (diffs: Diff[], o: CommentOptions): string => {
if (diffs.length === 0) {
if (o.bodyNoDiff === '') {
return ''
}
return generateNoDiffComment(o)
}

Expand All @@ -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}`

Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const main = async (): Promise<void> => {
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')),
Expand Down
2 changes: 2 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Inputs = {
base: string
head: string
label: string[]
commentBodyNoDiff: string
commentHeader: string
commentFooter: string
updateIfExists: UpdateIfExistsType
Expand All @@ -26,6 +27,7 @@ export const run = async (github: GitHubContext, inputs: Inputs): Promise<Output

const diffs = await computeDiff(inputs.base, inputs.head)
const body = formatComment(diffs, {
bodyNoDiff: inputs.commentBodyNoDiff,
header: inputs.commentHeader,
footer: inputs.commentFooter,
workflowRunURL: github.workflowRunURL,
Expand Down
1 change: 1 addition & 0 deletions tests/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('formatComment', () => {
},
],
{
bodyNoDiff: 'No diff',
header: '## diff',
footer: '<!-- diff-action -->',
workflowRunURL: 'https://github.com/int128/diff-action/actions/runs/6282216330',
Expand Down