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 +
+ +![Logo](docs/logo.png) + +
+ +
+ + *Your Refactoring Alarm 🔔 As a GitHub Action* + +
+ +
+ +[![main](https://github.com/getcodelimit/codelimit-action/actions/workflows/main.yml/badge.svg)](https://github.com/getcodelimit/codelimit-action/actions/workflows/main.yml) [![Checked with Code Limit](https://github.com/getcodelimit/codelimit-action/blob/_codelimit_reports/main/badge.svg)](https://github.com/getcodelimit/codelimit-action/blob/_codelimit_reports/main/codelimit.md) -To run Code Limit on every push and before every merge to main, append it to your GH Action workflow: +
+ +To run Code Limit on every push and before every merge to main, append it to +your GH Action workflow: ```yaml name: 'main' + on: push: branches: @@ -13,24 +32,14 @@ on: pull_request: branches: - main - jobs: - ci: - runs-on: ubuntu-latest - steps: - - name: 'Checkout sources' - uses: actions/checkout@v4 - - name: 'Run Code Limit' - uses: getcodelimit/codelimit-action@main -``` - -## Upload report -To upload the report to Code Limit, add the following step to your workflow: +jobs: + ci: + runs-on: ubuntu-latest + steps: + - name: 'Checkout sources' + uses: actions/checkout@v4 -```yaml - - name: 'Run Code Limit' - uses: getcodelimit/codelimit-action@main - with: - upload: true - token: ${{ secrets.GITHUB_TOKEN }} + - name: 'Run Code Limit' + uses: getcodelimit/codelimit-action@main ``` diff --git a/action.yml b/action.yml index c92abad..c2e6823 100644 --- a/action.yml +++ b/action.yml @@ -14,7 +14,3 @@ inputs: description: 'Checked changed files' default: 'true' required: false - upload: - description: 'Upload report to Code Limit' - default: 'false' - required: false diff --git a/docs/logo.png b/docs/logo.png new file mode 100644 index 0000000..c6b4b52 Binary files /dev/null and b/docs/logo.png differ diff --git a/src/action.ts b/src/action.ts index df80b0a..dbe5a4b 100644 --- a/src/action.ts +++ b/src/action.ts @@ -27,14 +27,7 @@ async function generateMarkdownReport(clBinary: string) { return result; } -async function main() { - const clBinary = await downloadCodeLimitBinary(); - console.log('Scanning codebase...'); - await exec(clBinary, ['scan', '.']); - const markdownReport = await generateMarkdownReport(clBinary); - const doUpload = getInput('upload') || false; - const token = getInput('token'); - const octokit = new Octokit({auth: token}); +async function updateReportsBranch(octokit: Octokit, markdownReport: string) { const owner = getRepoOwner(context); const repo = getRepoName(context); if (!owner || !repo) { @@ -55,28 +48,31 @@ async function main() { await createPRComment(octokit, owner, repo, prNumber, markdownReport); } } - let exitCode = 0; - if (doUpload) { - console.log('Uploading results...'); - if (!token) { - console.error('Token for upload not provided.'); - exitCode = 1; - } - const slug = context.payload.repository?.full_name; - if (slug && branch) { - exitCode = await exec(clBinary, ['app', 'upload', '--token', token, slug, branch]); - } +} + +async function checkChangedFiles(octokit: Octokit, clBinary: string): Promise { + 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