Skip to content

Commit

Permalink
feat: ✨ Percentage badges
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 18, 2024
1 parent 4d039db commit 171fe46
Show file tree
Hide file tree
Showing 8 changed files with 2,228 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: 'Install dependencies'
run: yarn install

- name: "Run tests"
run: yarn test

- name: 'Build dist'
run: yarn dist

Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "tsc",
"start": "yarn build && node ./build/action.js",
"dist": "yarn build && esbuild build/action.js --bundle --platform=node --outfile=dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -21,9 +21,12 @@
},
"homepage": "https://github.com/getcodelimit/codelimit-action#readme",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.10.0",
"@types/node-fetch": "^2.6.12",
"esbuild": "^0.24.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"typescript": "^5.7.2"
},
"dependencies": {
Expand All @@ -33,5 +36,17 @@
"@octokit/action": "^6.0.5",
"badge-maker": "^4.1.0",
"node-fetch": "^2.6.13"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.json"
}
]
}
}
}
11 changes: 9 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isPullRequest, isPullRequestFromFork, updateComment
} from "./github";
import {exec, getExecOutput} from "@actions/exec";
import {downloadCodeLimitBinary, getBadgeContent, getReportContent} from "./codelimit";
import {downloadCodeLimitBinary, getReportContent, makeNotFoundBadgeSvg, makeStatusBadgeSvg} from "./codelimit";
import {getChangedFiles} from "./utils";
import {version} from "./version";

Expand All @@ -38,7 +38,14 @@ async function updateReportsBranch(octokit: Octokit, markdownReport: string) {
}
await createBranchIfNotExists(octokit, owner, repo, '_codelimit_reports');
const reportContent = getReportContent();
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/badge.svg`, getBadgeContent(reportContent));
let badgeContent;
if (reportContent) {
const reportJson = JSON.parse(reportContent);
badgeContent = makeStatusBadgeSvg(reportJson.codebase);
} else {
badgeContent = makeNotFoundBadgeSvg();
}
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/badge.svg`, badgeContent);
if (reportContent) {
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/report.json`, reportContent);
}
Expand Down
24 changes: 12 additions & 12 deletions src/codelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import fs from "fs";
import {promisify} from "util";
import {makeBadge} from "badge-maker";
import {Codebase} from "./entities/Codebase";

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

Expand Down Expand Up @@ -55,18 +56,17 @@ function makeBadgeSvg(message: string, color: string): string {
return makeBadge(badge);
}

export function getBadgeContent(reportContent: string | undefined): string {
if (!reportContent) {
return makeBadgeSvg('Not found', 'grey');
export function makeNotFoundBadgeSvg(): string {
return makeBadgeSvg('Not found', 'grey');
}

export function makeStatusBadgeSvg(codebase: Codebase): string {
const profile = codebase.tree['./'].profile
if (profile[3] > 0) {
return makeBadgeSvg('Needs refactoring', 'red');
} else {
const reportJson = JSON.parse(reportContent);
const profile = reportJson.codebase.tree['./'].profile
if (profile[3] > 0) {
return makeBadgeSvg('Needs refactoring', 'red');
} else if (profile[2] > 0) {
return makeBadgeSvg('Needs refactoring', 'orange');
} else {
return makeBadgeSvg('Passed', 'brightgreen');
}
const profile2Percentage = Math.floor((profile[2] / (profile[0] + profile[1] + profile[2])) * 100);
const color = profile2Percentage > 20 ? 'orange' : 'brightgreen';
return makeBadgeSvg(`${100 - profile2Percentage}%`, color);
}
}
5 changes: 5 additions & 0 deletions src/entities/Codebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {SourceFolder} from "./SourceFolder";

export interface Codebase {
tree: { [path: string]: SourceFolder };
}
4 changes: 4 additions & 0 deletions src/entities/SourceFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface SourceFolder {
entries: Array<string>;
profile: Array<number>;
}
65 changes: 65 additions & 0 deletions tests/codelimit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {makeStatusBadgeSvg} from "../src/codelimit";

test('make 100% badge', () => {
const codebase = {
tree: {
'./': {
entries: [],
profile: [1000, 0, 0, 0]
}
}
}

const result = makeStatusBadgeSvg(codebase)

expect(result).toContain('#4c1');
expect(result).toContain('100%</text>');
})

test('make 80% badge', () => {
const codebase = {
tree: {
'./': {
entries: [],
profile: [400, 400, 200, 0]
}
}
}

const result = makeStatusBadgeSvg(codebase)

expect(result).toContain('#4c1');
expect(result).toContain('80%</text>');
})

test('make 60% badge', () => {
const codebase = {
tree: {
'./': {
entries: [],
profile: [300, 300, 400, 0]
}
}
}

const result = makeStatusBadgeSvg(codebase)

expect(result).toContain('#fe7d37');
expect(result).toContain('60%</text>');
})

test('make 93% badge', () => {
const codebase = {
tree: {
'./': {
entries: [],
profile: [630, 300, 70, 0]
}
}
}

const result = makeStatusBadgeSvg(codebase)

expect(result).toContain('#4c1');
expect(result).toContain('93%</text>');
})
Loading

0 comments on commit 171fe46

Please sign in to comment.