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

Conversation

asidhu0
Copy link
Contributor

@asidhu0 asidhu0 commented Jul 1, 2024

Isort is used to sort Python imports and black is used to format Python code. These two formatters will run on the code on every push to main and on any branch associated with an active pull request. It will then automatically push these changes to the current branch. If you'd like this behavior to change, please let me know and I can do that.

The automation since the isort workflow will be triggered on a push to main or an active pull request and after it finishes it will call the black workflow and black will be able to run. The two formatters run sequentially and not in parallel because one of the workflows may change the code and push that code to the current branch. And if the other workflow is also making changes there will be merge conflicts.

Also, since the workflows run on an active pull request, every time you push to an active pull request it would be nice to pull before working on it again since the formatters may make auto styling changes. Otherwise, merge conflicts can happen.

@asidhu0 asidhu0 linked an issue Jul 1, 2024 that may be closed by this pull request
@asidhu0 asidhu0 requested a review from youngdjn July 1, 2024 15:28
Copy link
Collaborator

@youngdjn youngdjn left a comment

Choose a reason for hiding this comment

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

This looks great! What I can't decide is whether it should apply the formatting on PR creation (and new commits to a PR), or just when the PR is merged to main. My instinct is the latter, so we don't have to be vigilant about the need to pull changes on a dev branch (to get the formatting changes) after every push to the branch. But if you feel strongly otherwise, I'll defer to you.

@asidhu0
Copy link
Contributor Author

asidhu0 commented Jul 1, 2024

This looks great! What I can't decide is whether it should apply the formatting on PR creation (and new commits to a PR), or just when the PR is merged to main. My instinct is the latter, so we don't have to be vigilant about the need to pull changes on a dev branch (to get the formatting changes) after every push to the branch. But if you feel strongly otherwise, I'll defer to you.

I don't have a strong preference for either. However, if we were to integrate some kind of testing to run on every pull request, I would recommend having the formatters run on open active PRs also so then the tests will run on the actual code that will be pushed to main.

But for now, I think it would be simpler to have it run only on main.

@youngdjn
Copy link
Collaborator

youngdjn commented Jul 1, 2024

Great point on the testing. How about for now, we have it only apply to main, and if/when we implement testing, we switch to have it apply to PRs as well. Could you add a comment to the action .yml noting how we would make that switch?

Comment on lines +14 to +22
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
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.

Comment on lines +12 to +20
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
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.

@asidhu0 asidhu0 requested a review from youngdjn July 1, 2024 17:55
Copy link
Collaborator

@youngdjn youngdjn left a comment

Choose a reason for hiding this comment

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

Thanks, sounds good to keep the 'determine branch name' step in. I had one suggested change for the comment you added, and also would like to request that you add a comment at the top of the 'determine branch name' step explaining that it has no effect currently given this workflow only applies to the main branch, but will be important if/when we additionally apply this workflow to pull requests.

@@ -1,10 +1,10 @@
name: Code Formatter

# Add `pull_request:` to apply on active PRs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please change this to:

# Add 'pull_request:' beneath 'on:' to apply on active PRs

@asidhu0 asidhu0 requested a review from youngdjn July 1, 2024 18:18
Copy link
Collaborator

@youngdjn youngdjn left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks!

@asidhu0 asidhu0 merged commit aaa321e into main Jul 1, 2024
@asidhu0 asidhu0 deleted the as_automatic-style-formatting branch July 9, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add automatic style formatting
2 participants