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

feat: Support posting of dive results to PR for succesful runs #69

Merged
merged 27 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,32 @@ jobs:
echo "Expected step to fail"
exit 1
fi

- name: '[Run] Negative test: always-comment w/o github-token'
if: always() && steps.pre-setup.outcome == 'success'
id: always-comment-missing-github-token
uses: ./
with:
image: ${{ env.TEST_IMAGE }}
always-comment: true

- name: '[Verification] Negative test: always-comment w/o github-token'
# It will not be able to create comment outside PR. result in main branch:
# Not Found - https://docs.github.com/rest
if: always() && steps.pre-setup.outcome == 'success' && github.ref !=
'refs/heads/main'
shell: bash
run: |
error_message="${{ steps.always-comment-missing-github-token.outputs.error }}"
expected_message="Resource not accessible by integration - \
https://docs.github.com/rest/issues/comments#create-an-issue-comment"

if [[ ! "$error_message" =~ "$expected_message" ]]; then
echo "Expected error message to contain: $expected_message"
echo "Got: $error_message"
exit 1
fi
if [[ "${{ steps.always-comment-missing-github-token.outcome }}" != "failure" ]]; then
echo "Expected step to fail"
exit 1
fi
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ reduce your container image size as early as possible.

### Inputs

| Name                      | Type | Required | Default | Description                              |
| ------------------- | ------ | -------- | -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| image | String | true | | Image to analyze |
| config-file | String | false | `${{ github.workspace }}/.dive.yaml` | Path to [dive config file](https://github.com/joschi/dive#ci-integration) |
| github-token | String | false | | GitHub token to post PR comment on dive failure |
| dive-image-registry | String | false | `ghcr.io/joschi/dive` | Docker registry to pull the Dive image from |
| dive-image-version | String | false | `0.13.1@sha256:f016a4bd2837` `130545e391acee7876aa5f7258` `ccdb12640ab4afaffa1c597d17` | Version of the Dive docker image to use. <br> While `latest` is supported, using a specific version with SHA is recommended for security and reproducibility |
| Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Type | Required | Default | Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |
| ---------------------------------------------------------------------------------------------------------------------------------- | ------- | -------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| image | String | true | | Image to analyze |
| always-comment | Boolean | false | `false` | Post dive analysis results as PR comment regardless of whether any inefficiencies were found. By default, comments are only posted when issues are detected. |
| config-file | String | false | `${{ github.workspace }}/.dive.yaml` | Path to [dive config file](https://github.com/joschi/dive#ci-integration) |
| github-token | String | false | | GitHub token to post PR comment on dive failure |
| dive-image-registry | String | false | `ghcr.io/joschi/dive` | Docker registry to pull the Dive image from |
| dive-image-version | String | false | `0.13.1@sha256:f016a4bd2837` `130545e391acee7876aa5f7258` `ccdb12640ab4afaffa1c597d17` | Version of the Dive docker image to use. <br> While `latest` is supported, using a specific version with SHA is recommended for security and reproducibility |


### Workflow
Expand All @@ -47,7 +48,7 @@ jobs:
- name: Build image
run: docker build -t sample:latest .
- name: Dive
uses: MaxymVlasov/dive-action@v1.2.1
uses: MaxymVlasov/dive-action@v1.3.0
with:
image: sample:latest
config-file: ${{ github.workspace }}/.dive-ci.yml
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ inputs:
image:
description: Image to analyze
required: true
always-comment:
description: Post dive analysis results as PR comment regardless of whether
any inefficiencies were found. By default, comments are only posted when
issues are detected.
required: false
default: 'false'
config-file:
description: Path to dive config file
required: false
Expand Down
8 changes: 7 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ function run() {
try {
const image = core.getInput('image');
const configFile = core.getInput('config-file');
// Convert always-comment input to boolean value.
// All values other than 'true' are considered false.
const alwaysComment = core.getInput('always-comment').toLowerCase() === 'true';
const diveRepo = core.getInput('dive-image-registry');
// Validate Docker image name format
if (!/^[\w.\-_/]+$/.test(diveRepo)) {
Expand Down Expand Up @@ -166,7 +169,7 @@ function run() {
}
};
const exitCode = yield exec.exec('docker', parameters, execOptions);
if (exitCode === 0) {
if (exitCode === 0 && !alwaysComment) {
// success
return;
}
Expand All @@ -179,6 +182,9 @@ function run() {
const octokit = github.getOctokit(token);
const comment = Object.assign(Object.assign({}, github.context.issue), { issue_number: github.context.issue.number, body: format(output) });
yield octokit.rest.issues.createComment(comment);
if (exitCode === 0 && alwaysComment) {
return;
}
error(`Scan failed (exit code: ${exitCode})`);
}
catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dive-action",
"version": "1.2.1",
"version": "1.3.0",
"description": "[![Release][release-badge]][release] [![GitHub Marketplace][marketplace-badge]][marketplace] [![License][license-badge]][license]",
"main": "lib/main.js",
"scripts": {
Expand Down
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ async function run(): Promise<void> {
try {
const image = core.getInput('image')
const configFile = core.getInput('config-file')
// Convert always-comment input to boolean value.
// All values other than 'true' are considered false.
const alwaysComment =
core.getInput('always-comment').toLowerCase() === 'true'

const diveRepo = core.getInput('dive-image-registry')
// Validate Docker image name format
Expand Down Expand Up @@ -122,7 +126,7 @@ async function run(): Promise<void> {
}
}
const exitCode = await exec.exec('docker', parameters, execOptions)
if (exitCode === 0) {
if (exitCode === 0 && !alwaysComment) {
// success
return
}
Expand All @@ -142,6 +146,11 @@ async function run(): Promise<void> {
body: format(output)
}
await octokit.rest.issues.createComment(comment)

if (exitCode === 0 && alwaysComment) {
return
}

error(`Scan failed (exit code: ${exitCode})`)
} catch (e) {
error(e instanceof Error ? e.message : String(e))
Expand Down
Loading