Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add black and isort for auto style formatting #58

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Black Linter

# This allows Black be called from the isort workflow
on:
workflow_call:

jobs:
black-linter:
name: Run Black
runs-on: ubuntu-latest

steps:
- name: Determine Branch Name
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
else
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
fi
Comment on lines +12 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here as previous comment.


- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ env.BRANCH_NAME }}

- name: Pull latest changes
run: git pull origin ${{ env.BRANCH_NAME }}

- name: Run Black formatter
uses: psf/black@stable
with:
options: "--verbose"
src: "."
jupyter: true

- name: Push changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply black formatting changes
45 changes: 45 additions & 0 deletions .github/workflows/isort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Code Formatter

on:
push:
branches:
- main
pull_request:

jobs:
isort:
name: Run Isort
runs-on: ubuntu-latest

steps:
- name: Determine Branch Name
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
else
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
fi
Comment on lines +15 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for determining the branch name was only necessary if the workflow were to run on active PRs. This will not break the code but is no longer necessary. However, if we were to switch to have the workflow run on PRs then integrating that change will be very easy if we keep this code (addressed by the comment). Otherwise, it will take some work to change that behavior. Let me know if you think we should keep this code or not.


- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ env.BRANCH_NAME }}

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Run Isort
run: pip install isort==5.13.2 && isort .

- name: Push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Apply isort formatting changes

# Ensure Black runs after Isort has completed
black:
needs: isort
uses: ./.github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import metashape_workflow_functions as meta
import read_yaml

if(sys.stdin.isatty()):
if sys.stdin.isatty():
config_file = sys.argv[1]
else:
config_file = manual_config_file
Expand Down
Loading