Skip to content

Commit

Permalink
chore: 🚧 Store markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 2, 2024
1 parent 5ba1f05 commit dfc89c1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
64 changes: 63 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48821,8 +48821,52 @@ function downloadBinary() {
return filename;
});
}
function getChangedFiles(token) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const eventName = github_1.context.eventName;
if (eventName === void 0) {
return ["."];
}
let base;
let head;
if (eventName === "pull_request") {
base = (_b = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.base) === null || _b === void 0 ? void 0 : _b.sha;
head = (_d = (_c = github_1.context.payload.pull_request) === null || _c === void 0 ? void 0 : _c.head) === null || _d === void 0 ? void 0 : _d.sha;
} else {
base = github_1.context.payload.before;
head = github_1.context.payload.after;
}
console.log(`Base commit: ${base}`);
console.log(`Head commit: ${head}`);
const octokit = new action_1.Octokit({ auth: token });
const response = yield octokit.repos.compareCommits({
base,
head,
owner: github_1.context.repo.owner,
repo: github_1.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;
});
}
function isPullRequest() {
return github_1.context.eventName === "pull_request";
}
function getSourceBranch() {
if (github_1.context.eventName === "pull_request") {
if (isPullRequest()) {
return process.env.GITHUB_HEAD_REF;
} else {
return process.env.GITHUB_REF_NAME;
Expand Down Expand Up @@ -48870,6 +48914,10 @@ function main() {
const filename = yield downloadBinary();
console.log("Scanning codebase...");
yield (0, exec_1.exec)(filename, ["scan", "."]);
const totalsMarkdown = yield (0, exec_1.getExecOutput)(filename, ["report", "--totals", "--format", "markdown"]);
const unitsMarkdown = yield (0, exec_1.getExecOutput)(filename, ["report", "--full", "--format", "markdown"]);
console.log(totalsMarkdown.stdout);
console.log(unitsMarkdown.stdout);
const doUpload = (0, core_1.getInput)("upload") || false;
const token = (0, core_1.getInput)("token");
const octokit = new action_1.Octokit({ auth: token });
Expand All @@ -48886,6 +48934,8 @@ function main() {
if (reportContent) {
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/report.json`, reportContent);
}
yield (0, github_2.createOrUpdateFile)(octokit, owner, repo, "_codelimit_reports", `${branch}/codelimit.md`, `${totalsMarkdown}
${unitsMarkdown}`);
let exitCode = 0;
if (doUpload) {
console.log("Uploading results...");
Expand All @@ -48898,6 +48948,18 @@ function main() {
exitCode = yield (0, exec_1.exec)(filename, ["app", "upload", "--token", token, slug, branch]);
}
}
const doCheck = (0, core_1.getInput)("check") || true;
if (doCheck && exitCode === 0) {
const changedFiles = yield getChangedFiles(token);
console.log(`Number of files changed: ${changedFiles.length}`);
if (changedFiles.length === 0) {
console.log("No files changed, skipping Code Limit");
} else {
console.log("Running Code Limit...");
exitCode = yield (0, exec_1.exec)(filename, ["check"].concat(changedFiles), { ignoreReturnCode: true });
}
}
fs_1.default.unlinkSync(filename);
console.log("Done!");
process.exit(exitCode);
});
Expand Down
13 changes: 11 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {promisify} from "util";
import {context} from "@actions/github";
import {Octokit} from "@octokit/action";
import {branchExists, createBranch, createInitialCommit, createOrUpdateFile, getRepoName, getRepoOwner} from "./github";
import {exec} from "@actions/exec";
import {exec, getExecOutput} from "@actions/exec";
import {makeBadge} from "badge-maker";

const streamPipeline = promisify(require('stream').pipeline);
Expand Down Expand Up @@ -84,8 +84,12 @@ async function getChangedFiles(token: string) {
return result;
}

function isPullRequest() {
return context.eventName === 'pull_request';
}

function getSourceBranch() {
if (context.eventName === 'pull_request') {
if (isPullRequest()) {
return process.env.GITHUB_HEAD_REF;
} else {
return process.env.GITHUB_REF_NAME;
Expand Down Expand Up @@ -134,6 +138,10 @@ async function main() {
const filename = await downloadBinary();
console.log('Scanning codebase...');
await exec(filename, ['scan', '.']);
const totalsMarkdown = await getExecOutput(filename, ['report', '--totals', '--format', 'markdown']);
const unitsMarkdown = await getExecOutput(filename, ['report', '--full', '--format', 'markdown']);
console.log(totalsMarkdown.stdout);
console.log(unitsMarkdown.stdout);
const doUpload = getInput('upload') || false;
const token = getInput('token');
const octokit = new Octokit({auth: token});
Expand All @@ -150,6 +158,7 @@ async function main() {
if (reportContent) {
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/report.json`, reportContent);
}
await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, `${totalsMarkdown}\n${unitsMarkdown}`);
let exitCode = 0;
if (doUpload) {
console.log('Uploading results...');
Expand Down

0 comments on commit dfc89c1

Please sign in to comment.