Skip to content

Commit

Permalink
fix: Make output more verbose to track down issues
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Apr 29, 2024
1 parent ea0ee28 commit 6888058
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ jobs:
id: checkout
uses: actions/checkout@v4

- name: Set up node
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: package.json

- name: Test Local Action
id: test-action
uses: ./
with:
fix: false

- name: Print Output
id: output
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ The idea is to run this action together with the [create-pull-request](https://g
uses: susnux/npm-audit-action
with:
# Optionally set an output path
- output-path: pr-content.md
output-path: pr-content.md

- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
- body: { { steps.npm-audit.outputs.markdown } }
body: ${{ steps.npm-audit.outputs.markdown }}
# Alternativly use the output file
- body-path: pr-content.md
body-path: pr-content.md
```
### Action inputs
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
description: "If also 'npm audit fix' should be run. Otherwise just the Markdown output is generated."
required: false
default: true
output-path:
description: Optional output file for formatte markown
required: false

# Define your outputs here.
outputs:
Expand Down
17 changes: 13 additions & 4 deletions dist/index.js

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

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ export function runNpmAudit(fix = false): Promise<string> {

return new Promise((resolve, reject) =>
exec(`npm audit --json ${fix ? 'fix' : ''}`, (error, stdout, stderr) => {
if (error) {
core.debug(`[npm audit] Error: ${error.message}`)
}
if (stderr) {
core.debug(`[npm audit]: ${stderr}`)
}
if (error) {
reject(error)
if (stdout) {
core.debug(`[npm audit]: ${stdout}`)
resolve(stdout.slice(stdout.indexOf('{')))
return
}
resolve(stdout.slice(stdout.indexOf('{')))
reject(error)
}),
)
}
Expand All @@ -41,7 +45,7 @@ export function runNpmAudit(fix = false): Promise<string> {
* @param json The output JSON string
* @return Formatted output as markdown
*/
export async function formatNpmAuditOutput(data: NPMAudit) {
export async function formatNpmAuditOutput(data: NPMAudit): Promise<string> {
const fixable = Object.values(data.vulnerabilities).filter(isFixable)
core.info(`Found ${fixable.length} fixable issues`)

Expand Down Expand Up @@ -81,6 +85,7 @@ This audit fix resolves ${fixable.length} of the total ${Object.values(data.vuln
output += ` * \`${node}\`\n`
}
}
return output
}

// Typescript helper
Expand All @@ -103,7 +108,7 @@ export async function run() {
core.debug(`Setting working directory to "${resolvedWD}".`)
process.chdir(resolvedWD)

const output = await runNpmAudit(fix)
const output = await runNpmAudit()
let data: NPMAudit | NPMAuditFix = JSON.parse(output)
if (isNPMAuditFix(data)) {
data = data as NPMAuditFix
Expand Down Expand Up @@ -134,6 +139,11 @@ export async function run() {
}
await writeFile(resolvedPath, formattedOutput)
}

if (fix) {
core.info('Running `npm audit` with `fix` flag')
await runNpmAudit(true)
}
} catch (error) {
// Fail the workflow run if an error occurs
core.setFailed(error.message)
Expand Down

0 comments on commit 6888058

Please sign in to comment.