Skip to content

Commit

Permalink
refactor: ♻️ Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 4, 2024
1 parent 283741d commit 1a9b1fe
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 65 deletions.
49 changes: 29 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
# codelimit-action
# Code Limit GitHub Action

<div align="center">

![Logo](docs/logo.png)

</div>

<div align="center">

*Your Refactoring Alarm 🔔 As a GitHub Action*

</div>

<div align="center">

[![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:
</div>

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:
- main
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
```
4 changes: 0 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ inputs:
description: 'Checked changed files'
default: 'true'
required: false
upload:
description: 'Upload report to Code Limit'
default: 'false'
required: false
Binary file added docs/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 24 additions & 28 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<number> {
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!');
Expand Down
29 changes: 16 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
});
Expand All @@ -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};
}

0 comments on commit 1a9b1fe

Please sign in to comment.