From 7c05e525c3db00c3db032da73a351c3a9e87e1a0 Mon Sep 17 00:00:00 2001 From: Carmen Tawalika Date: Sat, 2 Sep 2023 15:40:15 +0200 Subject: [PATCH] Add mdformat (#13) * add mdformat * fix typo * fix typo * add hints how to lint --- .github/workflows/linting.yml | 18 +++++++++++++++ Dockerfile | 1 + pre_commit_hooks/linting.sh | 41 ++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 2111b38..3b9f09c 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -15,6 +15,10 @@ on: required: false type: string default: '2.17.4' + mdformat-version: + required: false + type: string + default: '0.7.17' jobs: flake8: @@ -84,3 +88,17 @@ jobs: - name: Check code style with Black run: | black --check --diff --line-length 79 . + mdformat: + name: mdformat + if: ${{ inputs.mdformat-version != '' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install pip dependencies + run: | + python -m pip install --upgrade pip + pip3 install mdformat==${{ inputs.mdformat-version }} + mdformat --version + - name: Check code style with mdformat + run: | + mdformat --check . diff --git a/Dockerfile b/Dockerfile index 1927974..969b3bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ COPY .github/workflows/linting.yml $WORKFLOW_LINTING_WORKFLOW RUN echo black==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."black-version"'.default) >> /requirements.txt RUN echo flake8==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."flake8-version"'.default) >> /requirements.txt RUN echo pylint==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."pylint-version"'.default) >> /requirements.txt +RUN echo mdformat==$(cat $WORKFLOW_LINTING_WORKFLOW | yq .on.workflow_call.inputs | jq -r '."mdformat-version"'.default) >> /requirements.txt RUN pip install -r requirements.txt # Copy script to be executed on docker startup diff --git a/pre_commit_hooks/linting.sh b/pre_commit_hooks/linting.sh index 0fe4f6e..657536e 100755 --- a/pre_commit_hooks/linting.sh +++ b/pre_commit_hooks/linting.sh @@ -24,6 +24,7 @@ fi RUN_BLACK=TRUE RUN_FLAKE8=TRUE RUN_PYLINT=TRUE +RUN_MDFORMAT=TRUE #################################### # Overwrite linter versions if set # @@ -83,6 +84,24 @@ else echo "- pylint version not overwritten in code workflow" fi +if [ $(yq '.jobs.lint.with | has("mdformat-version")' $CODE_LINTING_WORKFLOW) == true ] +then + DEFAULT_MDFORMAT_VERSION=$(cat /requirements.txt | grep mdformat== | cut -d "=" -f 3) + NEW_MDFORMAT_VERSION=$(yq '.jobs.lint.with."mdformat-version"' $CODE_LINTING_WORKFLOW) + if [ "$(echo $NEW_MDFORMAT_VERSION | tr '"' 'x')" = "xx" ] + then + RUN_MDFORMAT=FALSE + echo "- mdformat configured to be skipped (empty string)" + elif [ $DEFAULT_MDFORMAT_VERSION != $NEW_MDFORMAT_VERSION ] + then + # sed would fail with Permission denied + # sed -i "s+$DEFAULT_MDFORMAT_VERSION+$NEW_MDFORMAT_VERSION+g" /requirements.txt + echo "- Warning: overwritten mdformat version will not be installed!" + fi +else + echo "- mdformat version not overwritten in code workflow" +fi + echo ################### @@ -94,6 +113,7 @@ echo echo "flake8: `flake8 --version`" echo "pylint: `pylint --version`" echo "black: `black --version`" +echo "mdformat: `mdformat --version`" ######## # lint # @@ -134,7 +154,7 @@ then if [ $? -ne 0 ] then RETURNCODE=1 - FAILINGSTEP="$FAILINGSTEP PYLINT" + FAILINGSTEP="$FAILINGSTEP PYLINT (run 'pylint .')" fi echo @@ -150,7 +170,7 @@ then pylint --rc-file=.pylintrc_allowed_to_fail . else echo - echo "PYLINT configured to be skipped" + echo "PYLINT configured to be skipped (run 'pylint --rc-file=.pylintrc_allowed_to_fail .')" fi if [ $RUN_BLACK != "FALSE" ] @@ -161,13 +181,28 @@ then if [ $? -ne 0 ] then RETURNCODE=1 - FAILINGSTEP="$FAILINGSTEP BLACK" + FAILINGSTEP="$FAILINGSTEP BLACK (run 'black --check --diff --line-length 79 .')" fi else echo echo "BLACK configured to be skipped" fi +if [ $RUN_MDFORMAT != "FALSE" ] +then + echo + echo "MDFORMAT:" + mdformat --check . + if [ $? -ne 0 ] + then + RETURNCODE=1 + FAILINGSTEP="$FAILINGSTEP MDFORMAT (run 'mdformat --check .')" + fi +else + echo + echo "MDFORMAT configured to be skipped" +fi + if [ $RETURNCODE -ne 0 ] then echo "Failing steps: ${FAILINGSTEP}"