Skip to main content

GitHub

Goal

Integrate codee format with GitHub to automatically check if Fortran code is properly formatted and fail the workflow if any formatting issues are found.

Prerequisites

Ensure you have:

  • codee installed and accessible in your GitHub runners.

Create a GitHub Actions workflow

GitHub Actions can be used to run codee format automatically on pull requests and ensure that all contributions follow a consistent formatting style.

In your repository, create the following directory structure if it does not exist:

mkdir
mkdir -p .github/workflows/

Then, create a new workflow file:

touch
touch .github/workflows/check-formatting.yml

Define the GitHub actions workflow

GitHub action to stop the pipeline if the code does not follow the Coding Guidelines

Add the following content to .github/workflows/check-formatting.yml:

name: Check Fortran Formatting

on:
pull_request:

jobs:
format:
name: Check formatting
runs-on: self-hosted

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get modified Fortran files
id: modified-files
run: |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV

- name: Run `codee format` on modified files
if: env.MODIFIED_FILES != ''
run: codee format $MODIFIED_FILES

- name: Fail if there are uncommitted changes
run: git diff --quiet || (echo "Formatting issues detected. Run 'codee format' locally before committing." && exit 1)

How it works

  • The workflow runs whenever a pull request is opened or updated.
  • It runs codee format on all modified Fortran source files.
  • If codee format makes any changes, the workflow fails, requiring contributors to fix formatting before merging.

GitHub action to apply Codee format changes

Warning

Be cautious when following this guide as there can always be issues in formatting that could cause problems in your code. Always review before merging

GitHub actions can also be used to automatically use codee format to format Fortran code from the contributors.

Add the following content to .github/workflows/format-code.yml:

name: Codee format changes

on:
pull_request:

permissions:
contents: write
pull-requests: write

jobs:
format:
name: Check formatting
runs-on: self-hosted

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}

- name: Get modified Fortran files
id: modified-files
run: |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV

- name: Run `codee format` on modified files
if: env.MODIFIED_FILES != ''
run: git codee-format --diff ${{ github.base_ref }} HEAD

- name: Commit changes
if: env.MODIFIED_FILES != ''
run: |
if ! git diff --quiet; then
# Push with the github-actions user. More information at:
# https://github.com/actions/checkout?tab=readme-ov-file#push-a-commit-using-the-built-in-token
git commit -c user.name="github-actions[bot]" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" -am "Apply codee format formatting fixes"
git push --set-upstream origin ${{ github.head_ref }}
else
echo "No changes detected."
exit 0
fi

How it works

  • The workflow runs whenever a pull request is opened or updated.
  • It runs codee format on all modified Fortran source files.
  • If codee format makes any changes, it commits those changes and pushes them to the current branch for review.

Troubleshooting

  • If the action does not trigger, verify the on: section.
  • If formatting does not apply, check if your files match the specified glob patterns (*.f90, *.F90, etc.).