Skip to content

Commit

Permalink
chore: 🚧 Create report branch if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Nov 28, 2024
1 parent 55854f6 commit 4550117
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 29 deletions.
82 changes: 53 additions & 29 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import nodeFetch from "node-fetch";
import path from "path";
import fs from "fs";
import {exec} from "@actions/exec";
import {getInput} from "@actions/core";
import {promisify} from "util";
import {context} from "@actions/github";
import {Octokit} from "@octokit/action";
import {branchExists, createBranch, getBranchHeadSha, getDefaultBranch, getRepoName, getRepoOwner} from "./github";

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

Expand Down Expand Up @@ -91,38 +91,62 @@ function getSourceBranch() {
}

async function main() {
const filename = await downloadBinary();
const doUpload = getInput('upload') || false;

// const filename = await downloadBinary();
// const doUpload = getInput('upload') || false;
const token = getInput('token');
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 octokit = new Octokit({auth: token});
const owner = getRepoOwner(context);
const repo = getRepoName(context);
if (!owner || !repo) {
console.error('Could not determine repository owner or name');
process.exit(1);
}
const doCheck = getInput('check') || true;
if (doCheck && exitCode === 0) {
const changedFiles = await 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 = await exec(filename, ['check'].concat(changedFiles), {ignoreReturnCode: true});
if (!await branchExists(octokit, owner, repo, '_codelimit_reports')) {
const defaultBranch = getDefaultBranch(context);
if (!defaultBranch) {
console.error('Could not determine default branch');
process.exit(1);
}
const sha = await getBranchHeadSha(octokit, owner, repo, defaultBranch);
if (!sha) {
console.error('Could not determine default branch sha');
process.exit(1);
}
await createBranch(octokit, owner, repo, '_codelimit_reports', sha);
} else {
console.log('Branch _codelimit_reports already exists');
}
fs.unlinkSync(filename);
console.log('Done!');
process.exit(exitCode);

// 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);
// 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 = await exec(filename, ['check'].concat(changedFiles), {ignoreReturnCode: true});
// }
// }
// fs.unlinkSync(filename);
// console.log('Done!');
// process.exit(exitCode);
}

main();
49 changes: 49 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {Octokit} from "@octokit/action";
import {Context} from "@actions/github/lib/context";

export async function branchExists(octokit: Octokit, owner: string, repo: string, branchName: string) {
try {
await octokit.git.getRef({
owner: owner, repo: repo, ref: `heads/${branchName}`
})
return true
} catch (err) {
return false
}
}

export async function createBranch(octokit: Octokit, owner: string, repo: string, branchName: string, sha: string) {
try {
const res = await octokit.git.createRef({
owner: owner, repo: repo, ref: `refs/heads/${branchName}`, sha: sha
})
console.log(`Branch created: ${branchName}`);
return res
} catch (e: any) {
console.error(`Could not create branch \`${branchName}\` due to: ${e.message}`);
}
}

export async function getBranchHeadSha(octokit: Octokit, owner: string, repo: string, branch: string) {
try {
const res = await octokit.git.getRef({
owner: owner, repo: repo, ref: `heads/${branch}`
})
const ref = res.data.object
return ref.sha
} catch (e) {
return undefined
}
}

export function getDefaultBranch(ctx: Context): string | undefined {
return ctx.payload.repository?.default_branch
}

export function getRepoOwner(ctx: Context): string | undefined {
return ctx.payload.repository?.owner.login
}

export function getRepoName(ctx: Context): string | undefined {
return ctx.payload.repository?.name
}

0 comments on commit 4550117

Please sign in to comment.