Skip to content

Commit

Permalink
global: add gitlint for commit message checks
Browse files Browse the repository at this point in the history
* closes cernanalysispreservation#2033

Signed-off-by: Ilias Koutsakis <[email protected]>
  • Loading branch information
Lilykos committed Jan 29, 2021
1 parent 3996a65 commit 25003b9
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 99 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/commit-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

---
name: Commit Check

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
CommitCheckJob:
name: Commit Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
# fetch-depth is needed to get the full list of commits
# necessary for commit msg linting when a PR has 2 commits or more
# to avoid getting ALL the commits, we get current commits + origin
fetch-depth: ${{ github.event.pull_request.commits }} + 1
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup python 3.6
uses: actions/setup-python@v2
with:
python-version: 3.6.9
- name: Checking commit quality (messages, signatures, etc)
env:
GIT_LAST: ${{ github.event.after }}
GIT_ORIGIN: ${{ github.event.pull_request.base.sha }}
run: |
sh ./scripts/ci/prebuild.sh
11 changes: 11 additions & 0 deletions .gitlint
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[general]
ignore=body-is-missing
extra-path=gitlint_rules/
debug=true
verbosity=3

[title-max-length]
line-length=50

[body-max-line-length]
line-length=72
61 changes: 61 additions & 0 deletions gitlint_rules/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from gitlint.rules import CommitRule, RuleViolation

COMMIT_SUBJECTS = [
"alice",
"atlas",
"cms",
"lhcb",
"ui",
"access",
"deposits",
"auth",
"devops",
"docker",
"docs",
"files",
"fixtures",
"global",
"mail",
"records",
"repos",
"schemas",
"scripts",
"search",
"services",
"tests",
"workflows"
]


class SignedOffBy(CommitRule):
"""
This rule will enforce that each commit contains a "Signed-off-by" line.
"""
name = "body-requires-signed-off-by"
id = "CAP1"

def validate(self, commit):
for line in commit.message.body:
if line.startswith("Signed-off-by"):
return

msg = "Body does not contain a 'Signed-Off-By' line"
return [RuleViolation(self.id, msg, line_nr=1)]


class ApprovedSubject(CommitRule):
"""
This rule will enforce that each commit starts with one of the approved
subjects, as presented in the list above.
"""
name = "approved-subject-in-title"
id = "CAP2"

def validate(self, commit):
for subject in COMMIT_SUBJECTS:
if commit.message.title.startswith(f'{subject}: '):
return

msg = f"Subject not approved, please start with one of: " \
f"{COMMIT_SUBJECTS}"
return [RuleViolation(self.id, msg, line_nr=1)]
50 changes: 50 additions & 0 deletions scripts/ci/commit-msg-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
#
# -*- coding: utf-8 -*-
#
# This file is part of CERN Analysis Preservation Framework.
# Copyright (C) 2021 CERN.
#
# CERN Analysis Preservation Framework is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# CERN Analysis Preservation Framework is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CERN Analysis Preservation Framework; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

handle_test_result(){
EXIT_CODE=$1
RESULT="$2"
if [ $EXIT_CODE -eq 0 ]; then
echo "Commit check was succesfull."
else
echo "Errors in commit message."
fi

# Print RESULT if not empty
if [ -n "$RESULT" ] ; then
echo "$RESULT"
fi
}

run_git_check(){
RESULT=$(gitlint --commits $GIT_ORIGIN..$GIT_LAST 2>&1)
local exit_code=$?
handle_test_result $exit_code "$RESULT"
return $exit_code
}

pip install gitlint
run_git_check
32 changes: 28 additions & 4 deletions scripts/ci/prebuild.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
#!/bin/bash
#
# -*- coding: utf-8 -*-
#
# This file is part of CERN Analysis Preservation Framework.
# Copyright (C) 2021 CERN.
#
# CERN Analysis Preservation Framework is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# CERN Analysis Preservation Framework is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CERN Analysis Preservation Framework; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

echo "RUNNING PREBUILD SCRIPTS"
# if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
# ./scripts/helpers/check_commit_subject.sh -r $TRAVIS_COMMIT_RANGE
# fi
# 1st step: commit message

echo "RUNNING COMMIT MESSAGE CHECKS"
sh ./scripts/ci/commit-msg-check.sh
92 changes: 0 additions & 92 deletions scripts/helpers/check_commit_subject.sh

This file was deleted.

6 changes: 3 additions & 3 deletions tests/unit/schemas/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_schema_stringify(db):
db.session.add(schema)
db.session.commit()

assert str(schema) == 'cms-analysis-v1.0.2'
assert str(schema) == 'cms-analysis-v1.0.21111'


def test_deposit_path_and_index(db):
Expand All @@ -93,13 +93,13 @@ def test_deposit_path_and_index(db):
'deposit_schema': {},
'major': 1,
'minor': 0,
'patch': 2
'patch': 5
})
db.session.add(schema)
db.session.commit()

assert schema.deposit_path == 'deposits/records/cms-analysis-v1.0.2.json'
assert schema.deposit_index == 'deposits-records-cms-analysis-v1.0.2'
assert schema.deposit_index == 'depo-records-cms-analysis-v1.0.2'


def test_record_path_and_index(db):
Expand Down

0 comments on commit 25003b9

Please sign in to comment.