Skip to content

Commit

Permalink
Merge branch 'main' into feat/always_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxymVlasov committed Feb 13, 2025
2 parents a9b422c + 62cf5cb commit 0ff5630
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 70 deletions.
80 changes: 45 additions & 35 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,58 +58,68 @@ const exec = __importStar(__nccwpck_require__(5236));
const github = __importStar(__nccwpck_require__(3228));
const strip_ansi_1 = __importDefault(__nccwpck_require__(348));
const fs_1 = __importDefault(__nccwpck_require__(9896));
function formatTableRow(line) {
// https://github.com/joschi/dive/blob/v0.12.0/runtime/ci/evaluator.go#L138
const count = line.slice(0, 5);
const wastedSpace = line.slice(7, 19);
const filePath = line.slice(21);
return `| ${count} | ${wastedSpace} | ${filePath} |`;
}
function composeComment(diveOutput, customLeadingComment) {
const ret = customLeadingComment;
let summarySection = false;
let inefficientFilesSection = false;
let resultSection = false;
for (const line of diveOutput.split('\n')) {
if (line.includes('Analyzing image')) {
summarySection = true;
inefficientFilesSection = false;
resultSection = false;
ret.push('### Dive Summary');
}
else if (line.includes('Inefficient Files:')) {
summarySection = false;
inefficientFilesSection = true;
resultSection = false;
ret.push('### Inefficient Files');
}
else if (line.includes('Results:')) {
summarySection = false;
inefficientFilesSection = false;
resultSection = true;
ret.push('### Results');
}
else if (summarySection || resultSection) {
ret.push((0, strip_ansi_1.default)(line));
}
else if (inefficientFilesSection) {
if (line.startsWith('Count')) {
ret.push('| Count | Wasted Space | File Path |');
ret.push('|---|---|---|');
}
else {
// https://github.com/joschi/dive/blob/v0.12.0/runtime/ci/evaluator.go#L138
ret.push(`| ${line.slice(0, 5)} | ${line.slice(7, 19)} | ${line.slice(21)} |`);
}
switch (true) {
case line.includes('Analyzing image'):
summarySection = true;
inefficientFilesSection = false;
resultSection = false;
ret.push('### Dive Summary');
break;
case line.includes('Inefficient Files:'):
summarySection = false;
inefficientFilesSection = true;
resultSection = false;
ret.push('### Inefficient Files');
break;
case line.includes('Results:'):
summarySection = false;
inefficientFilesSection = false;
resultSection = true;
ret.push('### Results');
break;
case summarySection || resultSection:
ret.push((0, strip_ansi_1.default)(line));
break;
case inefficientFilesSection:
if (line.startsWith('Count')) {
ret.push('| Count | Wasted Space | File Path |');
ret.push('|---|---|---|');
}
else {
ret.push(formatTableRow(line));
}
break;
default:
break;
}
}
return ret.join('\n');
}
function error(message) {
core.setOutput('error', message);
core.setFailed(message);
process.exit(1);
}
function postComment(ghToken_1, diveOutput_1) {
return __awaiter(this, arguments, void 0, function* (ghToken, diveOutput, customLeadingComment = []) {
const octokit = github.getOctokit(ghToken);
const comment = Object.assign(Object.assign({}, github.context.issue), { issue_number: github.context.issue.number, body: composeComment(diveOutput, customLeadingComment) });
yield octokit.rest.issues.createComment(comment);
});
}
function error(message) {
core.setOutput('error', message);
core.setFailed(message);
process.exit(1);
}
/**
* Executes a Docker image analysis using the dive tool and handles the results.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

86 changes: 52 additions & 34 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import * as github from '@actions/github'
import stripAnsi from 'strip-ansi'
import fs from 'fs'

function formatTableRow(line: string): string {
// https://github.com/joschi/dive/blob/v0.12.0/runtime/ci/evaluator.go#L138
const count = line.slice(0, 5)
const wastedSpace = line.slice(7, 19)
const filePath = line.slice(21)
return `| ${count} | ${wastedSpace} | ${filePath} |`
}

function composeComment(
diveOutput: string,
customLeadingComment: string[]
Expand All @@ -15,42 +23,47 @@ function composeComment(
let resultSection = false

for (const line of diveOutput.split('\n')) {
if (line.includes('Analyzing image')) {
summarySection = true
inefficientFilesSection = false
resultSection = false
ret.push('### Dive Summary')
} else if (line.includes('Inefficient Files:')) {
summarySection = false
inefficientFilesSection = true
resultSection = false
ret.push('### Inefficient Files')
} else if (line.includes('Results:')) {
summarySection = false
inefficientFilesSection = false
resultSection = true
ret.push('### Results')
} else if (summarySection || resultSection) {
ret.push(stripAnsi(line))
} else if (inefficientFilesSection) {
if (line.startsWith('Count')) {
ret.push('| Count | Wasted Space | File Path |')
ret.push('|---|---|---|')
} else {
// https://github.com/joschi/dive/blob/v0.12.0/runtime/ci/evaluator.go#L138
ret.push(
`| ${line.slice(0, 5)} | ${line.slice(7, 19)} | ${line.slice(21)} |`
)
}
switch (true) {
case line.includes('Analyzing image'):
summarySection = true
inefficientFilesSection = false
resultSection = false
ret.push('### Dive Summary')
break

case line.includes('Inefficient Files:'):
summarySection = false
inefficientFilesSection = true
resultSection = false
ret.push('### Inefficient Files')
break

case line.includes('Results:'):
summarySection = false
inefficientFilesSection = false
resultSection = true
ret.push('### Results')
break

case summarySection || resultSection:
ret.push(stripAnsi(line))
break

case inefficientFilesSection:
if (line.startsWith('Count')) {
ret.push('| Count | Wasted Space | File Path |')
ret.push('|---|---|---|')
} else {
ret.push(formatTableRow(line))
}
break

default:
break
}
}
return ret.join('\n')
}

function error(message: string): void {
core.setOutput('error', message)
core.setFailed(message)
process.exit(1)
return ret.join('\n')
}

async function postComment(
Expand All @@ -67,6 +80,12 @@ async function postComment(
await octokit.rest.issues.createComment(comment)
}

function error(message: string): void {
core.setOutput('error', message)
core.setFailed(message)
process.exit(1)
}

/**
* Executes a Docker image analysis using the dive tool and handles the results.
*
Expand Down Expand Up @@ -170,7 +189,6 @@ async function run(): Promise<void> {
'a PR comment, please provide the github-token in the action inputs.'
)
}

await postComment(ghToken, diveOutput, [
'> [!WARNING]',
'> The container image has inefficient files.'
Expand Down

0 comments on commit 0ff5630

Please sign in to comment.