cogent3 / c3dev

cogent3 developer tools
5 stars 9 forks source link

implementing precommit hooks as a github action #23

Open GavinHuttley opened 1 year ago

GavinHuttley commented 1 year ago

We want to avoid code style issues when doing reviews of PRs.

We need black and isort linters to start with. Setup with a GitHub action run on each PR.

Ideally, these would be applied by developers before the push to their own GitHub fork of cogent3.

Question: If these are setup on cogent3#develop, do forks also run them?

khiron commented 1 year ago

github action that runs black and isort on all code on push to the repository

.github/workflows/linters.yml

name: Format code using black and isort
# modified from https://towardsdatascience.com/black-with-git-hub-actions-4ffc5c61b5fe
# caching https://stackoverflow.com/questions/59127258/how-can-i-use-pip-cache-in-github-actions
# black integration https://black.readthedocs.io/en/stable/integrations/github_actions.html
# isort integration https://pycqa.github.io/isort/docs/configuration/github_action.html

on:
  push:
    branches: [ master ]

jobs:
  linters:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Format code with black 
        run: |
          pip install black==21.12b0
          black .
      - name: Format code with isort
        run: |
          pip install  isort==5.10.1
          isort .
      - name: Commit changes
        uses: EndBug/add-and-commit@v9
        with:
          author_name: ${{ github.actor }}
          author_email: ${{ github.actor }}@users.noreply.github.com
          message: "Format code with black and sort imports with isort"
          add: "."

1 TODO: find a way to only lint changed code to improve performance

khiron commented 1 year ago

Lint all code checked into local .git

#!/bin/sh

source ./env/Scripts/activate
black .
isort .

#echo "Black and isort ran successfully"
#exit 0

1: TODO: lint only changed code

fredjaya commented 9 months ago

@khiron is this already implemented? e.g. https://github.com/cogent3/cogent3/blob/develop/.github/workflows/linters.yml

GavinHuttley commented 9 months ago

@khiron see my comments on #48