Skip to content

Commit

Permalink
nopackage: New workflow: check commit messages (#1812)
Browse files Browse the repository at this point in the history
* nopackage: new workflow: check commit messages

* nopackage: updates to validate-commit-message

* Validate PR title as well
* Bugfix: only comment when issues are found

* nopackage: fix flake8 issues

* nopackage: process review remarks

* nopackage: make spack style happy

* nopackage: more workflow refactoring
  • Loading branch information
heerener authored Jan 24, 2023
1 parent c3b7622 commit c0e857d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/validate-commit-message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python

# requires: gitpython

import os
from argparse import ArgumentParser

from git import Repo

KEYWORDS = ['nopackage', 'deploy', 'docs']
EXISTING_PACKAGES = []


def prefix_invalid(prefix):
package = prefix.split(':')[0]
if package.strip() not in EXISTING_PACKAGES and package not in KEYWORDS:
return True

return False


def main(title):
repo = Repo('.')

faulty_commits = []

if prefix_invalid(title):
msg = '* Merge Request Title\n'
msg += f'> {title}\n\n'
msg += 'Merge request title needs to be compliant as well, '
msg += 'as it will be used for the merge/squash commit'
faulty_commits.append(msg)

for commit in repo.iter_commits():
print(f'Commit: {commit.message} (parents: {commit.parents})')
if len(commit.parents) > 1:
print('Not going beyond a merge commit')
break

prefix = commit.message.splitlines()[0]
if prefix_invalid(prefix):
quoted_commit_message = '\n'.join([f'> {line}' for
line in commit.message.splitlines()])
msg = f'* {commit.hexsha}\n'
msg += f'{quoted_commit_message}'
faulty_commits.append(msg)

if faulty_commits:
warning = 'These commits are not formatted correctly. '
warning += 'Please amend them to start with one of:\n'
warning += '* \\<package>: \n'
warning += f'* {", ".join(keyword + ":" for keyword in KEYWORDS)}\n\n'
warning += "### Faulty commits:\n"
faulty_commits.insert(0, warning)
with open('faulty_commits.txt', 'w') as fp:
fp.write('\n'.join(faulty_commits))
with open(os.environ['GITHUB_OUTPUT'], 'a') as fp:
fp.write("faulty-commits=true")


if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--title", required=True, help="PR title")

args = parser.parse_args()

for spack_repo in ['./var/spack/repos/builder.test',
'./var/spack/repos/builtin',
'./var/spack/repos/builtin.mock',
'./var/spack/repos/tutorial',
'./bluebrain/repo-bluebrain',
'./bluebrain/repo-patches']:
try:
EXISTING_PACKAGES.extend(next(os.walk(f'{spack_repo}/packages'))[1])
except StopIteration:
print(f'No packages under {spack_repo}')
pass

main(args.title)
42 changes: 42 additions & 0 deletions .github/workflows/validate-commit-message.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Commit message validation

on:
pull_request:

jobs:
validate-commit-messages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Validate commit messages
id: commit-message-validator
continue-on-error: true
run: |
echo "Deepening shallow clone to ${{ github.event.pull_request.commits }} commits"
echo "PR number is ${{ github.event.pull_request.number }}"
git fetch --deepen=$((${{ github.event.pull_request.commits }} - 1))
pip install gitpython
python ./.github/workflows/validate-commit-message.py --title "${{ github.event.pull_request.title }}" || true
- name: Show output
run: |
echo "Output: ${{ steps.commit-message-validator.outputs.faulty-commits }}"
- name: Add comment to PR
if: ${{ steps.commit-message-validator.outputs.faulty-commits == 'true' }}
uses: thollander/actions-comment-pull-request@v2
with:
filePath: faulty_commits.txt
reactions: eyes, confused, rocket

- name: Fail the action
if: ${{ steps.commit-message-validator.outputs.faulty-commits == 'true' }}
run: |
echo "Faulty commit messages found - failing"
exit 1

0 comments on commit c0e857d

Please sign in to comment.