Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
/ lint-action Public archive
forked from wearerequired/lint-action

✨ GitHub Action for detecting and fixing linting errors

License

Notifications You must be signed in to change notification settings

taskade/lint-action

 
 

Repository files navigation

✨ Lint Action

Screenshots

  • Checks on pull requests:

    Screenshot of check runs
  • Commit annotations:

    Screenshot of ESLint annotations

Supported tools

Usage

Create a new GitHub Actions workflow in your project, e.g. at .github/workflows/lint.yml. The content of the file should be in the following format:

name: Lint

on: push

jobs:
  run-linters:
    name: Run linters
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      # Install your linters here

      - name: Run linters
        uses: samuelmeuli/lint-action@v1
        with:
          github_token: ${{ secrets.github_token }}
          # Enable your linters here

Examples

All linters are disabled by default. To enable a linter, simply set the option with its name to true, e.g. eslint: true.

The action doesn't install the linters for you; you are responsible for installing them in your CI environment.

JavaScript example (ESLint and Prettier)

name: Lint

on: push

jobs:
  run-linters:
    name: Run linters
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 12

      # ESLint and Prettier must be in `package.json`
      - name: Install Node.js dependencies
        run: npm install

      - name: Run linters
        uses: samuelmeuli/lint-action@v1
        with:
          github_token: ${{ secrets.github_token }}
          # Enable linters
          eslint: true
          prettier: true

Python example (Flake8 and Black)

name: Lint

on: push

jobs:
  run-linters:
    name: Run linters
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: 3.8

      - name: Install Python dependencies
        run: pip install black flake8

      - name: Run linters
        uses: samuelmeuli/lint-action@v1
        with:
          github_token: ${{ secrets.github_token }}
          # Enable linters
          black: true
          flake8: true

Configuration

Linter-specific options

[linter] can be one of black, eslint, flake8, gofmt, golint, prettier, rubocop, stylelint, swiftformat and swiftlint:

  • [linter]: Enables the linter in your repository. Default: false
  • [linter]_args: Additional arguments to pass to the linter. Example: eslint_args: "--max-warnings 0" if ESLint checks should fail even if there are no errors and only warnings. Default: ""
  • [linter]_dir: Directory where the linting command should be run. Example: eslint_dir: server/ if ESLint is installed in the server subdirectory. Default: Repository root
  • [linter]_extensions: Extensions of files to check with the linter. Example: eslint_extensions: js,ts to lint JavaScript and TypeScript files with ESLint. Default: Varies by linter, see action.yml

General options

  • auto_fix: Whether linters should try to fix code style issues automatically. If some issues can be fixed, the action will commit and push the changes to the corresponding branch. Default: false

    Screenshot of auto-fix commit

    If you only want to create auto-fix commits on PRs (e.g. not on the master branch), you can configure your workflow as follows:

    name: Lint
    
    on:
      push:
        branches:
          - master
      pull_request:
    
    jobs:
      run-linters:
        name: Run linters
        runs-on: ubuntu-latest
    
        steps:
          - name: Check out Git repository
            uses: actions/checkout@v2
    
          # ...
    
          - name: Run linters
            uses: samuelmeuli/lint-action@v1
            with:
              github_token: ${{ secrets.github_token }}
              auto_fix: ${{ github.event_name == 'pull_request' }}
              # ...
  • commit_message: Template for auto-fix commit messages. The ${linter} variable can be used to insert the name of the linter. Default: "Fix code style issues with ${linter}"

Development

Contributing

Suggestions and contributions are always welcome! Please discuss larger changes via issue before submitting a pull request.

Adding a new linter

If you want to add support for an additional linter, please open an issue to discuss its inclusion in the project. Afterwards, you can follow these steps to add support for your linter:

  • Clone the repository and install its dependencies with yarn install.
  • Create a new class for the linter, e.g. src/linters/my-linter.js. Have a look at the other files in that directory to see what functions the class needs to implement.
  • Import your class in the src/linters/index.js file.
  • Provide a sample project for the linter under test/linters/projects/my-linter/. It should be simple and contain a few linting errors which your tests will detect.
  • Provide the expected linting output for your sample project in a test/linters/params/my-linter.js file. Import this file in test/linters/linters.test.js. You can run the tests with yarn test.
  • Update the action.yml file with the options provided by the new linter.
  • Mention your linter in the README.md file.
  • Update the test workflow file.

Related

About

✨ GitHub Action for detecting and fixing linting errors

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 96.2%
  • Python 1.0%
  • Ruby 1.0%
  • Go 1.0%
  • Swift 0.4%
  • CSS 0.2%
  • TypeScript 0.2%