-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement codeHealth and badge
- Loading branch information
1 parent
d9f7ba8
commit 1d5de1f
Showing
6 changed files
with
142 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as core from "@actions/core"; | ||
|
||
const baseUrl: string = "https://img.shields.io/badge/"; | ||
|
||
export type BadgeStyle = | ||
| "flat" | ||
| "plastic" | ||
| "flat-square" | ||
| "for-the-badge" | ||
| "social"; | ||
|
||
interface BadgeOptions { | ||
text: string; | ||
style: BadgeStyle; | ||
label: string; | ||
labelColor: string; | ||
color: string; | ||
} | ||
|
||
export function buildBadgeUrl({ | ||
text, | ||
style, | ||
label, | ||
labelColor, | ||
color | ||
}: BadgeOptions): string { | ||
return `${baseUrl}${text}-${text}?style=${style}&label=${encodeURIComponent(label)}&labelColor=${labelColor}&color=${color}`; | ||
} | ||
|
||
export function getCoverageBadge( | ||
percentage: number | undefined | string | ||
): string { | ||
let color: string = "blue"; | ||
const label: string = "Code Health"; | ||
|
||
if (percentage === undefined) { | ||
percentage = "N/A"; | ||
color = "lightgrey"; | ||
core.warning( | ||
"No code health data found in the analysis output! you may need to update vue-mess-detector to >= 0.54.1" | ||
); | ||
} | ||
|
||
if (typeof percentage === "number") { | ||
if (percentage < 40) { | ||
color = "red"; | ||
} else if (percentage < 70) { | ||
color = "yellow"; | ||
} else { | ||
color = "green"; | ||
} | ||
} | ||
|
||
const badgeUrl: string = buildBadgeUrl({ | ||
text: `${percentage.toString()}%`, | ||
style: "flat", | ||
label, | ||
labelColor: "8A2BE2", | ||
color | ||
}); | ||
|
||
return `![${label}](${badgeUrl})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,70 @@ | ||
import { CodeHealthOutputElement } from "../types.js"; | ||
import { CodeHealth, VMDAnalysis } from "../types.js"; | ||
import * as github from "@actions/github"; | ||
import { getCoverageBadge } from "./badge.js"; | ||
|
||
const comment: string = ` | ||
## 🎉 Vue Mess Detector Analysis Results 🎉 | ||
📊 **Coverage Information:** | ||
## 📊 Vue Mess Detector Analysis Results | ||
#### Code Health: {{coverageBadge}} | ||
{{coverageInfo}} | ||
{{artifactText}} | ||
_For any issues, please report them [here](https://github.com/brenoepics/vmd-action/issues) 🐞._ | ||
###### For any issues, please [report them here](https://github.com/brenoepics/vmd-action/issues/). | ||
`; | ||
|
||
const coverageInfo: string = ` | ||
Errors: {{errors}} | ||
Warnings: {{warnings}} | ||
Total Lines: {{linesCount}} | ||
Total Files: {{filesCount}} | ||
Points: {{points}} | ||
`; | ||
|
||
const artifactText: string = ` | ||
🚀 The detailed coverage report has been successfully uploaded! You can access it [here](../actions/runs/{{runId}}/artifacts/{{artifactId}}) 🔗. | ||
[See analysis details here](../actions/runs/{{runId}}/artifacts/{{artifactId}}) | ||
`; | ||
|
||
/** | ||
* TODO: this is a tweak to remove the progress,in the future would be great to have a fully parsed errors, warns... message. | ||
* | ||
*/ | ||
export function getCommentTemplate( | ||
codeHealthOutput: CodeHealthOutputElement[], | ||
artifactId: number | undefined | ||
): string { | ||
const coverageInfo: string = codeHealthOutput | ||
.filter((_, index) => index !== 1) // Skip the progress element (index 1) | ||
.map(element => element.info) | ||
.join("\n"); | ||
function getCoverageInfo(result: VMDAnalysis) { | ||
return result.codeHealthOutput.map(element => element.info).join("\n"); | ||
} | ||
|
||
return comment | ||
function replaceCodeHealth(message: string, health: CodeHealth) { | ||
return message | ||
.replace(/{{coverageInfo}}/g, coverageInfo) | ||
.replace(/{{errors}}/g, health.errors.toString()) | ||
.replace(/{{warnings}}/g, health.warnings.toString()) | ||
.replace(/{{linesCount}}/g, health.linesCount.toString()) | ||
.replace(/{{filesCount}}/g, health.filesCount.toString()) | ||
.replace(/{{points}}/g, health.points.toString()); | ||
} | ||
|
||
function replaceRepoData(message: string, artifactId: number | undefined) { | ||
return message | ||
.replace(/{{artifactText}}/g, artifactId ? artifactText : "") | ||
.replace(/{{artifactId}}/g, String(artifactId ?? 0)) | ||
.replace(/{{runId}}/g, github.context.runId.toString()) | ||
.replace(/{{repository/g, github.context.repo.repo) | ||
.replace(/{{repositoryOwner/g, github.context.repo.owner); | ||
} | ||
|
||
function replaceBadges(message: string, result: VMDAnalysis) { | ||
return message.replace( | ||
/{{coverageBadge}}/g, | ||
getCoverageBadge(result.codeHealth?.points) | ||
); | ||
} | ||
|
||
export function getCommentTemplate( | ||
result: VMDAnalysis, | ||
artifactId: number | undefined | ||
): string { | ||
let message: string = replaceRepoData(comment, artifactId); | ||
if (result.codeHealth) { | ||
message = replaceCodeHealth(message, result.codeHealth); | ||
} else { | ||
message = message.replace(/{{coverageInfo}}/g, getCoverageInfo(result)); | ||
} | ||
|
||
message = replaceBadges(message, result); | ||
return message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters