From 1a9b1fecb2c656026a41a1d4d17d713f1f01088b Mon Sep 17 00:00:00 2001 From: Rob van der Leek <5324924+robvanderleek@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:56:20 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20Refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 49 ++++++++++++++++++++++++++++------------------- action.yml | 4 ---- docs/logo.png | Bin 0 -> 9488 bytes src/action.ts | 52 +++++++++++++++++++++++--------------------------- src/utils.ts | 29 +++++++++++++++------------- 5 files changed, 69 insertions(+), 65 deletions(-) create mode 100644 docs/logo.png diff --git a/README.md b/README.md index e9b551a..4c268a7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,30 @@ -# codelimit-action +# Code Limit GitHub Action +
RDlVI5SZF?I+M?TpGH-w=A`Zp%|37k3K{FLIlWHKwb!p&vA1kO98WC41HnMy zSW(2teh)p+w&btzCA}&|m=J~k=2D}~@MLOgj0%t5j|WPTveH$4v463ui@e=d kcJO!gt>;sl>IH@C >p9xb;)r>u<;_yw)z(y8aAfGbe4!Qj+4W^^$t4 zF}~0`IqSc515hqPLzz}X@EH$`Fob^gzb^={V+(LFCqpwH (N1BpzGh4GT&lIKLe~rnvm=N$)=8Jnl9(yJ=GRh0+->A zS5pK3-EF|T?I*@!=WKM}PzptL8W|Sr(PAQ~IP2lN%adv~gnlAMk@19(5iI>6W;~IZ zw-zk38XBb!mP)PI{LgB079kmKQN#!L6h+xq@P#j{3r{o5w-TO6kc5t-GQ~Z#eFttH zHD@T2MG}>Xq -$)tG3#EI6Gn1Ky6UgrRX_y#b$o zCc-yrMa@5u$5aOZiAHvUOZSn )C2(TN%zBYP70#@I8~L$~X=tbIb_#NE$G-tV<^u ze?`dtkGYOtM%rLv+^$XCq$j{iK=)#B>jA?2!>OH5b(^J}H;~u~Qj96Xhv6ANF=oQX z8y?=4{<`K6J%5!Kg_>Xs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5R=i9*7SI}3Ohu$;dmRtEgaRWfZ6sGmuy_f=kk3iG4EXKd_I%sn0i}cJw0UV)g z(Mwl=4#r<+o69$RENlv@fE~EzjDfqVQ&p1>y^lbsOfp!$EaGA e4hCWt8zwZkrYk~i+i1h4#!RvAnT>rfV09yQ&4z;r=)D8WG zl>fab05Wg~Z`uh& 0C1oB5sdGZ9f6CH8$castc<%;63N{oBsecDPINbAtM&3Sw*pX*ka{&IO2ai#3 zx7uc!py(&y-a<9-OD9I^a`*5kWS?6B0=a_ak-j7tlRK}jBS?5qU$KNQ>@VRmJpyh; ztZ%Uu6mZT(e*au_l6T@!KODa)!nQeb70{=yP$5g5&*}Ev7ZQe|kAI}chYHsgc~wXl z{(KA0izR^S7rD*W;J`gxFvqXLd2(r52YO*j>RDaD?bwR+7$hEWyi?r8c=Y_=zMvVV z $Vk9Ib@;g xjhg~@NOloVENE81{618 { + const changedFiles = await getChangedFiles(octokit); + console.log(`Number of files changed: ${changedFiles.length}`); + if (changedFiles.length === 0) { + console.log('No files changed, skipping Code Limit'); + return 0; + } else { + console.log('Running Code Limit...'); + return await exec(clBinary, ['check'].concat(changedFiles), {ignoreReturnCode: true}); } +} + +async function main() { + let exitCode = 0; + const clBinary = await downloadCodeLimitBinary(); + console.log('Scanning codebase...'); + await exec(clBinary, ['scan', '.']); + const markdownReport = await generateMarkdownReport(clBinary); + const octokit = new Octokit({auth: getInput('token')}); + await updateReportsBranch(octokit, markdownReport); 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(clBinary, ['check'].concat(changedFiles), {ignoreReturnCode: true}); - } + if (doCheck) { + exitCode = await checkChangedFiles(octokit, clBinary); } fs.unlinkSync(clBinary); console.log('Done!'); diff --git a/src/utils.ts b/src/utils.ts index 0c794c0..ee1f357 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,21 +1,11 @@ import {context} from "@actions/github"; import {Octokit} from "@octokit/action"; -export async function getChangedFiles(token: string) { - const eventName = context.eventName - if (eventName === undefined) { +export async function getChangedFiles(octokit: Octokit) { + if (context.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 {base, head} = getShas(); const response = await octokit.repos.compareCommits({ base, head, owner: context.repo.owner, repo: context.repo.repo }); @@ -33,4 +23,17 @@ export async function getChangedFiles(token: string) { } } return result; +} + +function getShas(): { base: string, head: string } { + let base; + let head; + if (context.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 + } + return {base: base, head: head}; } \ No newline at end of file