-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add format files github action (#1682)
Co-authored-by: Arjun Suresh <[email protected]>
- Loading branch information
1 parent
518b454
commit 41fa8aa
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#================================================================ | ||
# HEADER | ||
#================================================================ | ||
# DESCRIPTION | ||
# This is a script containing some commands to automatically | ||
# format the c/c++ code contained in one folder. | ||
# This will help to maintain good quality code in the github | ||
# repository. | ||
# SET UP | ||
# You need to allow the correct permissions for this file. | ||
# This can be done by running: | ||
# chmod u+x <path to file> | ||
# REQUIREMENTS | ||
# clang-format | ||
#================================================================ | ||
# END_OF_HEADER | ||
#================================================================ | ||
|
||
|
||
# Checks all the modified c/c++ files, format them and adds them | ||
# to the commit. | ||
for FILE in $(git diff upstream/$1 --name-only | grep -E '.*\.(cc|cpp|h|hpp)$') | ||
do | ||
clang-format -i -style=file $FILE | ||
git add $FILE | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#================================================================ | ||
# HEADER | ||
#================================================================ | ||
# DESCRIPTION | ||
# This is a script containing some commands to automatically | ||
# format the c/c++ code contained in one folder. | ||
# This will help to maintain good quality code in the github | ||
# repository. | ||
# SET UP | ||
# You need to allow the correct permissions for this file. | ||
# This can be done by running: | ||
# chmod u+x <path to file> | ||
# REQUIREMENTS | ||
# clang-format | ||
#================================================================ | ||
# END_OF_HEADER | ||
#================================================================ | ||
|
||
|
||
# Checks all the modified c/c++ files, format them and adds them | ||
# to the commit. | ||
for FILE in $(git diff upstream/$1 --name-only | grep -E '.*\.py$') | ||
do | ||
autopep8 --in-place -a $FILE | ||
git add $FILE | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
name: "format" | ||
on: | ||
pull_request: | ||
branches: [ master ] | ||
types: [opened, closed, synchronize] | ||
|
||
|
||
env: | ||
python_version: "3.9" | ||
repo: "mlcommons/inference" | ||
|
||
jobs: | ||
format-code: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.head_ref || github.ref_name }} | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
- name: Set up Python ${{ env.python_version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ env.python_version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python3 -m pip install autopep8 | ||
- name: Grant permissions | ||
run: | | ||
chmod 777 "${GITHUB_WORKSPACE}/.github/scripts/format-cpp.sh" | ||
chmod 777 "${GITHUB_WORKSPACE}/.github/scripts/format-py.sh" | ||
- name: Format Codebase | ||
if: ${{ github.event.pull_request.base.repo.full_name == env.repo }} | ||
run: | | ||
git remote add upstream ${{ github.event.pull_request.base.repo.clone_url }} | ||
git fetch upstream ${{ github.event.pull_request.base.ref }} | ||
".github/scripts/format-cpp.sh" "${{ github.event.pull_request.base.ref }}" | ||
".github/scripts/format-py.sh" "${{ github.event.pull_request.base.ref }}" | ||
- name: Commit | ||
run: | | ||
cd ${GITHUB_WORKSPACE} | ||
HAS_CHANGES=$(git diff --cached --name-only) | ||
if [ ${#HAS_CHANGES} -gt 0 ]; then | ||
git log | ||
git config --global user.email "${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com" | ||
git config --global user.name "${GITHUB_ACTOR}" | ||
git commit -m '[Automated Commit] Format Codebase' | ||
git push origin ${{ github.head_ref || github.ref_name }} | ||
fi | ||