From c17e1a20ea004ffedb47d3d56a04fecae2701ea0 Mon Sep 17 00:00:00 2001 From: Matthias Volk Date: Fri, 19 Jul 2024 11:56:21 +0200 Subject: [PATCH] Automatic formatting via black + CI support --- .git-blame-ignore-revs | 0 .github/workflows/formatapply.yml | 38 +++++++++++++++++++++++++++++++ .github/workflows/formatcheck.yml | 15 ++++++++++++ pyproject.toml | 10 ++++++++ setup.py | 1 + 5 files changed, 64 insertions(+) create mode 100644 .git-blame-ignore-revs create mode 100644 .github/workflows/formatapply.yml create mode 100644 .github/workflows/formatcheck.yml create mode 100644 pyproject.toml diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.github/workflows/formatapply.yml b/.github/workflows/formatapply.yml new file mode 100644 index 0000000000..e2d74cf666 --- /dev/null +++ b/.github/workflows/formatapply.yml @@ -0,0 +1,38 @@ +name: apply-code-format + +on: [ workflow_dispatch ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: psf/black@stable + with: + options: "" + src: "." + jupyter: true + - name: Commit Formatting + run: | + git config user.name 'Auto Format' + git config user.email 'dev@stormchecker.org' + if [ -z "$(git status --porcelain)" ] + then + echo "Code did not change" + else + git commit -am "Applied code formatting" + git rev-parse HEAD >> .git-blame-ignore-revs + git commit -am "Add code formatting commit to .git-blame-ignore-revs" + fi + - name: Create Pull Request + id: cpr + uses: peter-evans/create-pull-request@v6 + with: + branch: ci/apply-code-format + delete-branch: true + title: 'Code formatting' + body: | + Auto-generated pull request triggered by the `apply-code-format` workflow. + - Manually close and reopen this PR to trigger the CI. + - Make sure to **merge** (and not rebase) this PR so that the added commit hash in `.git-blame-ignore-revs` remains valid. diff --git a/.github/workflows/formatcheck.yml b/.github/workflows/formatcheck.yml new file mode 100644 index 0000000000..93b4ca3452 --- /dev/null +++ b/.github/workflows/formatcheck.yml @@ -0,0 +1,15 @@ +name: check-code-format + +on: [push, pull_request, workflow_dispatch] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: psf/black@stable + with: + options: "--check --diff --color" + src: "." + jupyter: true diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000..207aa10ace --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,10 @@ +[tool.black] +line-length = 160 +target-version = [ + "py37", + "py38", + "py39", + "py310", + "py311", +] +include = "\\.pyi?$" diff --git a/setup.py b/setup.py index f341a9921f..02108bf4eb 100755 --- a/setup.py +++ b/setup.py @@ -236,6 +236,7 @@ def finalize_options(self): "numpy": ["numpy"], "plot": ["matplotlib","numpy","scipy"], "doc": ["Sphinx", "sphinx-bootstrap-theme", "nbsphinx", "ipython", "ipykernel"], # also requires pandoc to be installed + "dev": ["black"], }, python_requires='>=3.7', # required by packaging )