From 617d0315e55b5105669216f7cc782c0d4982a713 Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 16:51:15 -0500 Subject: [PATCH 01/11] enforce jira ticketin git commit message --- .../workflows/commit_message_validation.yml | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 .github/workflows/commit_message_validation.yml diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml new file mode 100644 index 0000000..a529e9a --- /dev/null +++ b/.github/workflows/commit_message_validation.yml @@ -0,0 +1,61 @@ +name: Release + +on: [push] + +jobs: + commit_message_validation: + name: validate git commit messages + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: '${{ github.event.pull_request.base.ref }}' + + - name: Execute message validation + run: | + # Checkout branch + git checkout -q ${{ github.event.pull_request.head.ref }} + # Set variables + BASE_BRANCH=${{ github.event.pull_request.base.ref }} + msg_regex='MAT-[0-9]+\s.*' + # This list permits some commits + commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" + # Initialize invalidCommit as false, will be set to true by any invalid commits + invalidCommit=false + # Find hash of commit most common ancestor, e.g. where branch began + BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) + # Find all commits since common ancestor + BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) + # Check every commit message since ancestor for regex match + for commit in $BRANCH_COMMITS; do + # Check if ticket in static permit-list for non-Jira commit_sha + if $(git log --max-count=1 --format=%B $commit | grep -iqE "$commit_static_permit_list"); then + : #If commit matches regex, commit is valid, do nothing + elif git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]' | grep -iqE "$msg_regex"; then + : #If commit matches regex, commit is valid, do nothing + else + # If commit doesn't match regex, commit isn't valid, print commit info + echo "************" + printf "Invalid commit message: \"%s\" and hash: %s\n" "$(git log --max-count=1 --format=%B $commit)" "$commit" + echo "************" + + # Set this variable to trigger rejection if any commit fails regex + invalidCommit=true + fi + done + + # If any commit are invalid, print reject message + if [ "$invalidCommit" == true ]; then + echo "Your push was rejected because at least one commit message on this branch is invalid" + echo "Please fix the commit message(s) and push again." + echo "https://help.github.com/en/articles/changing-a-commit-message" + echo "************" + exit 1 + elif [ "$invalidCommit" == false ]; then + echo "************" + echo "All commits are valid" + echo "************" + exit 0 + fi \ No newline at end of file From 8bc04263cd7ce8f7d4ed20ee26975994e8c28a4b Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:06:01 -0500 Subject: [PATCH 02/11] debugging git action --- .../workflows/commit_message_validation.yml | 32 +------------------ 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index a529e9a..1f6ab22 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -28,34 +28,4 @@ jobs: BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) # Find all commits since common ancestor BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) - # Check every commit message since ancestor for regex match - for commit in $BRANCH_COMMITS; do - # Check if ticket in static permit-list for non-Jira commit_sha - if $(git log --max-count=1 --format=%B $commit | grep -iqE "$commit_static_permit_list"); then - : #If commit matches regex, commit is valid, do nothing - elif git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]' | grep -iqE "$msg_regex"; then - : #If commit matches regex, commit is valid, do nothing - else - # If commit doesn't match regex, commit isn't valid, print commit info - echo "************" - printf "Invalid commit message: \"%s\" and hash: %s\n" "$(git log --max-count=1 --format=%B $commit)" "$commit" - echo "************" - - # Set this variable to trigger rejection if any commit fails regex - invalidCommit=true - fi - done - - # If any commit are invalid, print reject message - if [ "$invalidCommit" == true ]; then - echo "Your push was rejected because at least one commit message on this branch is invalid" - echo "Please fix the commit message(s) and push again." - echo "https://help.github.com/en/articles/changing-a-commit-message" - echo "************" - exit 1 - elif [ "$invalidCommit" == false ]; then - echo "************" - echo "All commits are valid" - echo "************" - exit 0 - fi \ No newline at end of file + exit 0 \ No newline at end of file From f50a6adda3bf39d3b9196c5f34c6179842167b4a Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:07:37 -0500 Subject: [PATCH 03/11] debug git action --- .../workflows/commit_message_validation.yml | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index 1f6ab22..e43d7c8 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -18,14 +18,14 @@ jobs: # Checkout branch git checkout -q ${{ github.event.pull_request.head.ref }} # Set variables - BASE_BRANCH=${{ github.event.pull_request.base.ref }} - msg_regex='MAT-[0-9]+\s.*' - # This list permits some commits - commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" - # Initialize invalidCommit as false, will be set to true by any invalid commits - invalidCommit=false - # Find hash of commit most common ancestor, e.g. where branch began - BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) - # Find all commits since common ancestor - BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) +# BASE_BRANCH=${{ github.event.pull_request.base.ref }} +# msg_regex='MAT-[0-9]+\s.*' +# # This list permits some commits +# commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" +# # Initialize invalidCommit as false, will be set to true by any invalid commits +# invalidCommit=false +# # Find hash of commit most common ancestor, e.g. where branch began +# BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) +# # Find all commits since common ancestor +# BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) exit 0 \ No newline at end of file From 1f56d763836f83a03088fe98c25d6ab26e634141 Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:10:24 -0500 Subject: [PATCH 04/11] debug git action --- .github/workflows/commit_message_validation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index e43d7c8..ec74735 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -18,7 +18,7 @@ jobs: # Checkout branch git checkout -q ${{ github.event.pull_request.head.ref }} # Set variables -# BASE_BRANCH=${{ github.event.pull_request.base.ref }} + BASE_BRANCH=${{ github.event.pull_request.base.ref }} # msg_regex='MAT-[0-9]+\s.*' # # This list permits some commits # commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" From 7af39c85ab6d32b1cf0994e4b6e5a7ecf160b56b Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:11:39 -0500 Subject: [PATCH 05/11] debug git action --- .../workflows/commit_message_validation.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index ec74735..c0acb08 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -19,13 +19,13 @@ jobs: git checkout -q ${{ github.event.pull_request.head.ref }} # Set variables BASE_BRANCH=${{ github.event.pull_request.base.ref }} -# msg_regex='MAT-[0-9]+\s.*' -# # This list permits some commits -# commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" -# # Initialize invalidCommit as false, will be set to true by any invalid commits -# invalidCommit=false -# # Find hash of commit most common ancestor, e.g. where branch began -# BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) -# # Find all commits since common ancestor -# BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) + msg_regex='MAT-[0-9]+\s.*' + # This list permits some commits + commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" + # Initialize invalidCommit as false, will be set to true by any invalid commits + invalidCommit=false + # Find hash of commit most common ancestor, e.g. where branch began + BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) + # Find all commits since common ancestor + BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) exit 0 \ No newline at end of file From 0c8a24025164cc7a491d818e5fe45fc1db448bfe Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:12:50 -0500 Subject: [PATCH 06/11] git action debugging --- .github/workflows/commit_message_validation.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index c0acb08..b230309 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -23,9 +23,9 @@ jobs: # This list permits some commits commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" # Initialize invalidCommit as false, will be set to true by any invalid commits - invalidCommit=false - # Find hash of commit most common ancestor, e.g. where branch began - BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) - # Find all commits since common ancestor - BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) +# invalidCommit=false +# # Find hash of commit most common ancestor, e.g. where branch began +# BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) +# # Find all commits since common ancestor +# BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) exit 0 \ No newline at end of file From bc482c7a333ed410b1786f26bdab616e1da378f5 Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:14:59 -0500 Subject: [PATCH 07/11] debug git commit --- .github/workflows/commit_message_validation.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index b230309..61d4f0b 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -23,9 +23,9 @@ jobs: # This list permits some commits commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" # Initialize invalidCommit as false, will be set to true by any invalid commits -# invalidCommit=false -# # Find hash of commit most common ancestor, e.g. where branch began -# BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) + invalidCommit=false + # Find hash of commit most common ancestor, e.g. where branch began + BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) # # Find all commits since common ancestor # BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) exit 0 \ No newline at end of file From e65b9e2404f40241c64287fcc9b7cbb3e235adcc Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:17:17 -0500 Subject: [PATCH 08/11] debug git action --- .github/workflows/commit_message_validation.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index 61d4f0b..c0acb08 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -26,6 +26,6 @@ jobs: invalidCommit=false # Find hash of commit most common ancestor, e.g. where branch began BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) -# # Find all commits since common ancestor -# BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) + # Find all commits since common ancestor + BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) exit 0 \ No newline at end of file From 124eabe47a87631302d572325afcd5a00651fb0a Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:24:53 -0500 Subject: [PATCH 09/11] debug git actions --- .github/workflows/commit_message_validation.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index c0acb08..3a0aada 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -24,7 +24,10 @@ jobs: commit_static_permit_list="(^Merge pull request \#)|(^Merge branch)|(^Revert \")" # Initialize invalidCommit as false, will be set to true by any invalid commits invalidCommit=false + CURRENT_BRANCH=$(git branch | grep ^\* | cut -d "*" -f 2 | cut -d " " -f 2) # Find hash of commit most common ancestor, e.g. where branch began + echo $BASE_BRANCH + echo $CURRENT_BRANCH BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) # Find all commits since common ancestor BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) From 382f368815478925f017c28c752589a292694d53 Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:31:15 -0500 Subject: [PATCH 10/11] debug git actions --- .github/workflows/commit_message_validation.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index 3a0aada..a76ab07 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -1,6 +1,10 @@ -name: Release +name: validate git commit messages -on: [push] +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] jobs: commit_message_validation: From 4a3a97e346c85c1fcc5955a46b24fa44929bfdb9 Mon Sep 17 00:00:00 2001 From: adongare Date: Tue, 13 Feb 2024 17:34:04 -0500 Subject: [PATCH 11/11] MAT-6378 debug git action --- .../workflows/commit_message_validation.yml | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/.github/workflows/commit_message_validation.yml b/.github/workflows/commit_message_validation.yml index a76ab07..e328778 100644 --- a/.github/workflows/commit_message_validation.yml +++ b/.github/workflows/commit_message_validation.yml @@ -35,4 +35,34 @@ jobs: BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH}) # Find all commits since common ancestor BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD) - exit 0 \ No newline at end of file + # Check every commit message since ancestor for regex match + for commit in $BRANCH_COMMITS; do + # Check if ticket in static permit-list for non-Jira commit_sha + if $(git log --max-count=1 --format=%B $commit | grep -iqE "$commit_static_permit_list"); then + : #If commit matches regex, commit is valid, do nothing + elif git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]' | grep -iqE "$msg_regex"; then + : #If commit matches regex, commit is valid, do nothing + else + # If commit doesn't match regex, commit isn't valid, print commit info + echo "************" + printf "Invalid commit message: \"%s\" and hash: %s\n" "$(git log --max-count=1 --format=%B $commit)" "$commit" + echo "************" + + # Set this variable to trigger rejection if any commit fails regex + invalidCommit=true + fi + done + + # If any commit are invalid, print reject message + if [ "$invalidCommit" == true ]; then + echo "Your push was rejected because at least one commit message on this branch is invalid" + echo "Please fix the commit message(s) and push again." + echo "https://help.github.com/en/articles/changing-a-commit-message" + echo "************" + exit 1 + elif [ "$invalidCommit" == false ]; then + echo "************" + echo "All commits are valid" + echo "************" + exit 0 + fi \ No newline at end of file