Skip to content

fix: Give write permission to github token on black formatting workflow #1530

fix: Give write permission to github token on black formatting workflow

fix: Give write permission to github token on black formatting workflow #1530

# SPDX-FileCopyrightText: 2017-2023 Contributors to the OpenSTEF project <korte.termijn.prognoses@alliander.com> # noqa E501
#
# SPDX-License-Identifier: MPL-2.0
# Copied from https://github.com/psf/black/actions/runs/17913292/workflow
# GitHub Action that uses Black to reformat the Python code in an incoming pull request.
# If all Python code in the pull request is compliant with Black then this Action
# does nothing. Otherwise, Black is run and its changes are committed to the
# incoming pull request. See https://github.com/cclauss/autoblack for a similar example.
name: Black Format Code
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11']
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
# Checkout
- name: Checkout
uses: actions/checkout@v2
# Setup
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
# Install black
- name: Install Black
run: pip install black==23.9.1 pydocstyle==6.1.1 docformatter==1.5.0 toml==0.10.2
# Run black --check
- name: Run black --check . and PydocStyle check
run: |
black --check .
pydocstyle .
# Run black (if needed)
- name: If needed, commit black changes to the pull request
if: failure()
run: |
black .
docformatter openstef --recursive --wrap-summaries 120 --in-place
git config --global user.name 'black'
git config --global user.email 'action@github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git fetch
git checkout ${{ github.head_ref }}
git commit --signoff -am "Format Python code with Black"
git push