Skip to content

Commit

Permalink
chore: πŸ”Š Add logging library
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 23, 2024
1 parent 61e09ec commit b375376
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 15 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/jest": "^29.5.14",
"@types/node": "^22.10.0",
"@types/node-fetch": "^2.6.12",
"@types/signale": "^1.4.7",
"esbuild": "^0.24.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
Expand All @@ -35,7 +36,8 @@
"@actions/github": "^5.1.1",
"@octokit/action": "^6.0.5",
"badge-maker": "^4.1.0",
"node-fetch": "^2.6.13"
"node-fetch": "^2.6.13",
"signale": "^1.4.0"
},
"jest": {
"preset": "ts-jest",
Expand Down
31 changes: 20 additions & 11 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {exec, getExecOutput} from "@actions/exec";
import {downloadCodeLimitBinary, getReportContent, makeNotFoundBadgeSvg, makeStatusBadgeSvg} from "./codelimit";
import {getChangedFiles} from "./utils";
import {version} from "./version";
import signale, {error, info, pending, success} from "signale";

signale.config({
displayFilename: true,
displayTimestamp: true
});

async function generateMarkdownReport(clBinary: string) {
const totalsMarkdown = await getExecOutput(clBinary, ['report', '--format', 'markdown']);
Expand All @@ -41,10 +47,13 @@ async function updateReportsBranch(octokit: Octokit, owner: string, repo: string
badgeContent = makeNotFoundBadgeSvg();
}
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/badge.svg`, badgeContent);
success(`Updated badge in branch _codelimit_reports/${branch}`);
if (reportContent) {
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/report.json`, reportContent);
success(`Updated JSON report in branch _codelimit_reports/${branch}`);
}
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, markdownReport);
success(`Updated markdown report in branch _codelimit_reports/${branch}`);
}

async function updatePullRequestComment(octokit: Octokit, owner: string, repo: string, branch: string, markdownReport: string) {
Expand All @@ -55,10 +64,10 @@ async function updatePullRequestComment(octokit: Octokit, owner: string, repo: s
const fileContent = Buffer.from(actionStateFile.content, 'base64').toString('utf-8');
const actionState = JSON.parse(fileContent) as ActionState;
const commentId = actionState.commentId;
console.log(`Updating existing comment with ID: ${commentId}`);
pending(`Updating existing comment with ID: ${commentId}`);
await updateComment(octokit, owner, repo, prNumber, markdownReport, commentId);
} else {
console.log('State file not found, creating new comment');
info('State file not found, creating new comment');
const commentId = await createPRComment(octokit, owner, repo, prNumber, markdownReport);
const actionState: ActionState = {commentId: commentId};
const actionStateJson = JSON.stringify(actionState);
Expand All @@ -69,21 +78,21 @@ async function updatePullRequestComment(octokit: Octokit, owner: string, repo: s

async function checkChangedFiles(octokit: Octokit, clBinary: string): Promise<number> {
const changedFiles = await getChangedFiles(octokit);
console.log(`Number of files changed: ${changedFiles.length}`);
info(`Number of files changed: ${changedFiles.length}`);
if (changedFiles.length === 0) {
console.log('No files changed, skipping CodeLimit');
info('No files changed, skipping CodeLimit');
return 0;
} else {
console.log('Running CodeLimit...');
pending('Running CodeLimit...');
return await exec(clBinary, ['check'].concat(changedFiles), {ignoreReturnCode: true});
}
}

async function main() {
console.log(`CodeLimit action, version: ${version.revision}`);
info(`CodeLimit action, version: ${version.revision}`);
let exitCode = 0;
const clBinary = await downloadCodeLimitBinary();
console.log('Scanning codebase...');
pending('Scanning codebase...');
await exec(clBinary, ['scan', '.']);
const markdownReport = await generateMarkdownReport(clBinary);
const octokit = new Octokit({auth: getInput('token')});
Expand All @@ -95,22 +104,22 @@ async function main() {
const repo = getRepoName(context);
const branch = getSourceBranch();
if (!owner || !repo || !branch) {
console.error('Could not determine repository owner, name, or branch');
error('Could not determine repository owner, name, or branch');
process.exit(1);
}
try {
await updateReportsBranch(octokit, owner, repo, branch, markdownReport);
} catch (e: unknown) {
console.error('Failed to update reports branch');
error('Failed to update reports branch');
if (e instanceof Error) {
console.error(`Reason: ${e.message}`);
error(`Reason: ${e.message}`);
}
}
if (isPullRequest()) {
await updatePullRequestComment(octokit, owner, repo, branch, markdownReport);
}
fs.unlinkSync(clBinary);
console.log('Done!');
success('Done!');
process.exit(exitCode);
}

Expand Down
3 changes: 1 addition & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export async function createBranchIfNotExists(octokit: Octokit, owner: string, r
if (!await branchExists(octokit, owner, repo, branchName)) {
const initialCommitSha = await createInitialCommit(octokit, owner, repo);
await createBranch(octokit, owner, repo, branchName, initialCommitSha);
} else {
console.log(`Branch ${branchName} already exists`);
console.log(`Branch ${branchName} created`);
}
}
Loading

0 comments on commit b375376

Please sign in to comment.