Skip to content

Commit

Permalink
chore: 🚧 Generate badge
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 2, 2024
1 parent 920cc25 commit 371b8bf
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@octokit/action": "^6.0.5",
"badge-maker": "^4.1.0",
"node-fetch": "^2.6.13"
}
}
84 changes: 57 additions & 27 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {promisify} from "util";
import {context} from "@actions/github";
import {Octokit} from "@octokit/action";
import {branchExists, createBranch, createFile, createInitialCommit, getRepoName, getRepoOwner} from "./github";
import {exec} from "@actions/exec";
import {makeBadge} from "badge-maker";

const streamPipeline = promisify(require('stream').pipeline);

Expand Down Expand Up @@ -90,10 +92,46 @@ function getSourceBranch() {
}
}

async function main() {
async function createReportsBranchIfNotExists(octokit: Octokit, owner: string, repo: string) {
if (!await branchExists(octokit, owner, repo, '_codelimit_reports')) {
const initialCommitSha = await createInitialCommit(octokit, owner, repo);
await createBranch(octokit, owner, repo, '_codelimit_reports', initialCommitSha);
} else {
console.log('Branch _codelimit_reports already exists');
}
}

function makeResponse(message: string, color: 'red' | 'orange' | 'green' | 'grey'): string {
const badge = {
label: 'Code Limit',
message: message,
color: color
};
return makeBadge(badge);
}

function getBadgeContent(): string {
const report = fs.readFileSync('.codelimit_cache/codelimit.json', 'utf8');
if (!report) {
return makeResponse('Not found', 'grey');
} else {
const reportJson = JSON.parse(report);
const profile = reportJson.codebase.tree['./'].profile
if (profile[3] > 0) {
return makeResponse('Needs refactoring', 'red');
} else if (profile[2] > 0) {
return makeResponse('Needs refactoring', 'orange');
} else {
return makeResponse('Passed', 'green');
}
}
}

// const filename = await downloadBinary();
// const doUpload = getInput('upload') || false;
async function main() {
const filename = await downloadBinary();
console.log('Scanning codebase...');
await exec(filename, ['scan', '.']);
const doUpload = getInput('upload') || false;
const token = getInput('token');
const octokit = new Octokit({auth: token});
const owner = getRepoOwner(context);
Expand All @@ -102,29 +140,21 @@ async function main() {
console.error('Could not determine repository owner or name');
process.exit(1);
}
if (!await branchExists(octokit, owner, repo, '_codelimit_reports')) {
const initialCommitSha = await createInitialCommit(octokit, owner, repo);
await createBranch(octokit, owner, repo, '_codelimit_reports', initialCommitSha);
} else {
console.log('Branch _codelimit_reports already exists');
await createReportsBranchIfNotExists(octokit, owner, repo);
await createFile(octokit, owner, repo, '_codelimit_reports', 'main/badge.svg', getBadgeContent());
let exitCode = 0;
if (doUpload) {
console.log('Uploading results...');
if (!token) {
console.error('Token for upload not provided.');
exitCode = 1;
}
const slug = context.payload.repository?.full_name;
const branch = getSourceBranch();
if (slug && branch) {
exitCode = await exec(filename, ['app', 'upload', '--token', token, slug, branch]);
}
}
await createFile(octokit, owner, repo, '_codelimit_reports', 'main/badge.svg', 'Hello from Code Limit');

// let exitCode = 0;
// if (doUpload) {
// console.log('Scanning codebase...');
// await exec(filename, ['scan', '.']);
// console.log('Uploading results...');
// if (!token) {
// console.error('Token for upload not provided.');
// exitCode = 1;
// }
// const slug = context.payload.repository?.full_name;
// const branch = getSourceBranch();
// if (slug && branch) {
// exitCode = await exec(filename, ['app', 'upload', '--token', token, slug, branch]);
// }
// }
// const doCheck = getInput('check') || true;
// if (doCheck && exitCode === 0) {
// const changedFiles = await getChangedFiles(token);
Expand All @@ -137,8 +167,8 @@ async function main() {
// }
// }
// fs.unlinkSync(filename);
// console.log('Done!');
// process.exit(exitCode);
console.log('Done!');
process.exit(exitCode);
}

main();
51 changes: 51 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -378,23 +378,74 @@
dependencies:
undici-types "~6.20.0"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anafanafo/-/anafanafo-2.0.0.tgz#43f56274680bc553dd67a9625a920f88d0057b5c"
integrity sha512-Nlfq7NC4AOkTJerWRIZcOAiMNtIDVIGWGvQ98O7Jl6Kr2Dk0dX5u4MqN778kSRTy5KRqchpLdF2RtLFEz9FVkQ==
dependencies:
char-width-table-consumer "^1.0.0"

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

badge-maker@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/badge-maker/-/badge-maker-4.1.0.tgz#4035b31e7dd3036d915eddb81699b42f9070b019"
integrity sha512-qYImXoz0WZRMaauqSMo6QNurKp26K3RcOhefuGfno50xmAzHEJsgHbP4gnHs6Ps53KgQgFi4MJKB6Rq8H7siww==
dependencies:
anafanafo "2.0.0"
css-color-converter "^2.0.0"

before-after-hook@^2.2.0:
version "2.2.3"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==

binary-search@^1.3.5:
version "1.3.6"
resolved "https://registry.yarnpkg.com/binary-search/-/binary-search-1.3.6.tgz#e32426016a0c5092f0f3598836a1c7da3560565c"
integrity sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA==

char-width-table-consumer@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/char-width-table-consumer/-/char-width-table-consumer-1.0.0.tgz#bb44ccd1ba3ed4fcdb062e22876721858a7697a8"
integrity sha512-Fz4UD0LBpxPgL9i29CJ5O4KANwaMnX/OhhbxzvNa332h+9+nRKyeuLw4wA51lt/ex67+/AdsoBQJF3kgX2feYQ==
dependencies:
binary-search "^1.3.5"

color-convert@^0.5.2:
version "0.5.3"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-0.5.3.tgz#bdb6c69ce660fadffe0b0007cc447e1b9f7282bd"
integrity sha512-RwBeO/B/vZR3dfKL1ye/vx8MHZ40ugzpyfeVG5GsiuGnrlMWe2o8wxBbLCpw9CsxV+wHuzYlCiWnybrIA0ling==

color-name@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

css-color-converter@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/css-color-converter/-/css-color-converter-2.0.0.tgz#70c00fa451a19675e2808f28de9be360c84db5fb"
integrity sha512-oLIG2soZz3wcC3aAl/7Us5RS8Hvvc6I8G8LniF/qfMmrm7fIKQ8RIDDRZeKyGL2SrWfNqYspuLShbnjBMVWm8g==
dependencies:
color-convert "^0.5.2"
color-name "^1.1.4"
css-unit-converter "^1.1.2"

css-unit-converter@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/css-unit-converter/-/css-unit-converter-1.1.2.tgz#4c77f5a1954e6dbff60695ecb214e3270436ab21"
integrity sha512-IiJwMC8rdZE0+xiEZHeru6YoONC4rfPMqGm2W85jMIbkFvv5nFTwJVFHam2eFrN6txmoUYFAFXiv8ICVeTO0MA==

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
Expand Down

0 comments on commit 371b8bf

Please sign in to comment.