-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from getcodelimit/improve-action
refactor: ♻️ Refactor
- Loading branch information
Showing
4 changed files
with
152 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import nodeFetch from "node-fetch"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import {promisify} from "util"; | ||
import {makeBadge} from "badge-maker"; | ||
|
||
const streamPipeline = promisify(require('stream').pipeline); | ||
|
||
function getBinaryName() { | ||
const binaries: { [platform: string]: string } = { | ||
'darwin': 'codelimit-macos', | ||
'win32': 'codelimit.exe', | ||
'linux': 'codelimit-linux' | ||
}; | ||
if (process.env.RUNNER_OS) { | ||
const platform = process.env.RUNNER_OS.toLowerCase(); | ||
if (platform in binaries) { | ||
return binaries[platform]; | ||
} | ||
} | ||
if (process.platform in binaries) { | ||
return binaries[process.platform]; | ||
} | ||
return binaries['linux']; | ||
} | ||
|
||
async function getLatestBinaryUrl() { | ||
const latestUrl = 'https://github.com/getcodelimit/codelimit/releases/latest'; | ||
const res = await nodeFetch(latestUrl); | ||
const downloadUrl = res.url.replace('/tag/', '/download/'); | ||
return `${downloadUrl}/${getBinaryName()}`; | ||
} | ||
|
||
export async function downloadCodeLimitBinary() { | ||
const binaryUrl = await getLatestBinaryUrl(); | ||
console.log(`Downloading Code Limit binary from URL: ${binaryUrl}`); | ||
const response = await nodeFetch(binaryUrl); | ||
const filename = path.join(__dirname, getBinaryName()); | ||
await streamPipeline(response.body, fs.createWriteStream(filename)); | ||
fs.chmodSync(filename, '777'); | ||
console.log(`Code Limit binary downloaded: ${filename}`); | ||
return filename; | ||
} | ||
|
||
export function getReportContent(): string | undefined { | ||
return fs.readFileSync('.codelimit_cache/codelimit.json', 'utf8'); | ||
} | ||
|
||
function makeBadgeSvg(message: string, color: 'red' | 'orange' | 'green' | 'grey'): string { | ||
const badge = { | ||
label: 'Code Limit', | ||
message: message, | ||
color: color | ||
}; | ||
return makeBadge(badge); | ||
} | ||
|
||
export function getBadgeContent(reportContent: string | undefined): string { | ||
if (!reportContent) { | ||
return makeBadgeSvg('Not found', 'grey'); | ||
} 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', 'green'); | ||
} | ||
} | ||
} |
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,36 @@ | ||
import {context} from "@actions/github"; | ||
import {Octokit} from "@octokit/action"; | ||
|
||
export async function getChangedFiles(token: string) { | ||
const eventName = context.eventName | ||
if (eventName === undefined) { | ||
return ['.']; | ||
} | ||
let base; | ||
let head; | ||
if (eventName === 'pull_request') { | ||
base = context.payload.pull_request?.base?.sha | ||
head = context.payload.pull_request?.head?.sha | ||
} else { | ||
base = context.payload.before | ||
head = context.payload.after | ||
} | ||
const octokit = new Octokit({auth: token}); | ||
const response = await octokit.repos.compareCommits({ | ||
base, head, owner: context.repo.owner, repo: context.repo.repo | ||
}); | ||
if (response.status !== 200) { | ||
return ['.']; | ||
} | ||
const files = response.data.files | ||
const result = []; | ||
if (files) { | ||
for (const file of files) { | ||
const filename = file.filename | ||
if (file.status === 'modified' || file.status === 'added') { | ||
result.push(filename); | ||
} | ||
} | ||
} | ||
return result; | ||
} |