Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
krizzu committed Nov 18, 2019
0 parents commit 2915f78
Show file tree
Hide file tree
Showing 14 changed files with 4,441 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "krizzu/ts"
}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.idea/
pids
*.pid
*.seed
*.pid.lock
lib-cov
coverage
node_modules/
.npm
.eslintcache
.node_repl_history
*.tgz
.yarn-integrity
.env
.env.test
dist/
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:10-slim

COPY . /eslint_check_action

WORKDIR /eslint_check_action

ENTRYPOINT ["/eslint_check_action/start.sh"]

LABEL maintainer="Krzysztof Borowy <dev@krizzu.dev>"
LABEL com.github.actions.name="ESLint check action"
LABEL com.github.actions.description="Run Eslint checks in project"
LABEL com.github.actions.icon="octagon"
LABEL com.github.actions.color="green"


21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Krzysztof Borowy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Eslint check action

GitHub action running eslint check in your project, annotating errors/warnings in PR.

## Usage

Add workflow to your project (ex. `.github/workflows/eslint.yml`):

```yaml
name: Eslint check
on: [push]
jobs:
eslint_check:
name: prepare
runs-on: ubuntu-latest
steps:
# Checkout action must run prior to eslint check
- name: Checkout
uses: actions/checkout@v1
- name: Install dependencies
run: yarn install
- name: Lint
uses: Krizzu/eslint-check-action@v1.0.0
with:
ghToken: ${{ secrets.GITHUB_TOKEN }}
eslintFiles: "lib, scripts"
eslintConfig: "myConfigs/eslint.config.js",
eslintExt: "ts, tsx"
```

### params

**eslintFiles**

Relative path to files/directories to run lint on.
Default: `.`


**eslintConfig**

Relative path to eslint config. Can either be `.js` config, `.eslintrc` or `package.json`.
Default: `.eslintrc`


**eslintExt**

File extension to run linting on.
Default: `js, ts, jsx, tsx`

## License

MIT.

27 changes: 27 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'Eslint check action'
description: "GitHub action running eslint check in your project, annotating errors/warnings in PR."
author: "krizzu"
inputs:
ghToken:
description: 'GitHub token'
required: true
eslintFiles:
description: 'Relative path to files/directories to run lint on.'
default: '.'
eslintConfig:
description: 'Relative path to eslint config. Can either be ".js" config, ".eslintrc" or "package.json".'
default: '.eslintrc'
eslintExt:
description: 'File extension to run linting on.'
default: 'js, ts, jsx, tsx'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.ghToken }}
- ${{ inputs.eslintFiles }}
- ${{ inputs.eslintConfig }}
- ${{ inputs.eslintExt }}
branding:
icon: 'octagon'
color: 'green'
17 changes: 17 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
targets: {
node: 10,
},
},
],
],
plugins: [
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread',
],
};
30 changes: 30 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable camelcase */

type ActionOptionsType = {
repoName: string;
repoOwner: string;
repoPath: string;
prSha: string;
eslintFiles: Array<string>;
eslintConfig: string;
eslintExtensions: Array<string>;
};

type GitHubAnnotationLevel = 'notice' | 'warning' | 'failure';

type GitHubAnnotation = {
annotation_level: GitHubAnnotationLevel;
end_column?: number;
end_line: number;
message: string;
path: string;
raw_details?: string;
start_column?: number;
start_line: number;
title?: string;
};

type ReportCounts = {
error: number;
warning: number;
};
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@krizzu/eslint-check-action",
"version": "0.0.1",
"description": "GitHub action running eslint check in your project, annotating errors/warnings in PR.",
"main": "dist/index.js",
"author": "Krzysztof Borowy <dev@krizzu.dev>",
"license": "MIT",
"scripts": {
"test": "tsc",
"dev": "source .env && yarn build && node dist/index.js",
"build": "rm -rf dist/ && babel src --out-dir dist/ --extensions .ts",
"prepublishOnly": "yarn build"
},
"dependencies": {
"@actions/core": "1.2.0",
"@actions/github": "1.1.0",
"eslint": "6.6.0"
},
"devDependencies": {
"@babel/cli": "7.7.0",
"@babel/core": "7.7.2",
"@babel/plugin-proposal-class-properties": "7.7.0",
"@babel/plugin-proposal-object-rest-spread": "7.6.2",
"@babel/preset-env": "7.7.1",
"@babel/preset-typescript": "7.7.2",
"@types/eslint": "6.1.3",
"eslint-config-krizzu": "1.0.3",
"typescript": "3.7.2"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"repository": {
"type": "git",
"url": "git://github.com/Krizzu/eslint-check-action.git"
},
"keywords": [
"eslint",
"github",
"action",
"check",
"runner",
"ci",
"lint"
]
}
135 changes: 135 additions & 0 deletions src/eslintRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { GitHub } from '@actions/github';
import { error as logError } from '@actions/core';
import eslint from 'eslint';
import path from 'path';

class EslintRunner {
private name = 'Eslint check Action';

private kit: GitHub;

private opts: ActionOptionsType;

checkRunID: number = -1;

constructor(ghToken: string, options: ActionOptionsType) {
this.kit = new GitHub(ghToken);
this.opts = options;
}

run = async () => {
this.checkRunID = await this.startGitHubCheck();
const report = this.runEslintCheck()!;
const { success, annotations, counts } = this.prepareAnnotation(report);
this.finishGitHubCheck(success, annotations, counts);
};

private startGitHubCheck = async () => {
let runId = -1;
try {
const response = await this.kit.checks.create({
name: this.name,
head_sha: this.opts.prSha,
repo: this.opts.repoName,
owner: this.opts.repoOwner,
started_at: new Date().toISOString(),
status: 'in_progress',
});

runId = response.data.id;
} catch (e) {
exitWithError(e.message);
}

return runId;
};

private finishGitHubCheck = async (
success: boolean,
annotations: Array<GitHubAnnotation>,
counts: ReportCounts
) => {
try {
await this.kit.checks.update({
owner: this.opts.repoOwner,
repo: this.opts.repoName,
check_run_id: this.checkRunID,
status: 'completed',
completed_at: new Date().toISOString(),
conclusion: success ? 'success' : 'failure',
output: {
title: this.name,
summary: `Found ${counts.error} error(s), ${counts.warning} warning(s).`,
annotations,
},
});
} catch (e) {
exitWithError(e.message);
}
};

private pathRelative = (location: string) => {
return path.resolve(this.opts.repoPath, location);
};

private runEslintCheck = () => {
const cliOptions = {
useEslintrc: false,
configFile: this.pathRelative(this.opts.eslintConfig),
extensions: this.opts.eslintExtensions,
cwd: this.opts.repoPath,
};

try {
const cli = new eslint.CLIEngine(cliOptions);
const lintFiles = this.opts.eslintFiles.map(this.pathRelative);

return cli.executeOnFiles(lintFiles);
} catch (e) {
exitWithError(e.message);

return null;
}
};

private prepareAnnotation = (report: eslint.CLIEngine.LintReport) => {
// 0 - no error, 1 - warning, 2 - error
const reportLevel = ['', 'warning', 'failure'];

const githubAnnotations: Array<GitHubAnnotation> = [];
report.results.forEach(result => {
const { filePath, messages } = result;
const path = filePath.replace(`${this.opts.repoPath}/`, '');

for (let msg of messages) {
const { ruleId, message, severity, endLine, line } = msg;

const annotation: GitHubAnnotation = {
path,
start_line: line,
end_line: endLine || line,
annotation_level: reportLevel[severity] as GitHubAnnotationLevel,
message: `${ruleId}: ${message}`,
};

githubAnnotations.push(annotation);
}
});

return {
success: report.errorCount === 0,
annotations: githubAnnotations,
counts: {
error: report.errorCount,
warning: report.warningCount,
},
};
};
}

function exitWithError(errorMessage: string) {
logError(errorMessage);
process.exit(1);
}

export default EslintRunner;
Loading

0 comments on commit 2915f78

Please sign in to comment.