-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
224-feat: Add automatic validation PR workflow names (#394)
* feat: 224 - add commit and branch names validation * feat: 224 - add github action for PR title * fix: 224 - github actions * fix: 224 - github actions regex * fix: 224 - clean code * fix: 224 - add documentation & fix minor bugs * fix: 224 - after review * fix: 224 - after review * fix: 224 - after review * fix: 224 - add characters to error commit msg
- Loading branch information
Showing
5 changed files
with
133 additions
and
4 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,43 @@ | ||
name: Auto PR Title | ||
|
||
on: | ||
pull_request: | ||
types: [opened] | ||
|
||
jobs: | ||
update-pr-title: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out the code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Extract branch number and update PR title | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: | | ||
PR_NUMBER="${{ github.event.number }}" | ||
BRANCH_NAME="${{ github.event.pull_request.head.ref }}" | ||
PR_TITLE="${{ github.event.pull_request.title }}" | ||
if [[ "$BRANCH_NAME" =~ ^(feat|fix|refactor|docs|chore|test)\/([0-9]{1,5})-([a-zA-Z0-9-]{2,})$ ]]; | ||
then | ||
TYPE="${BASH_REMATCH[1]}" | ||
NUMBER="${BASH_REMATCH[2]}" | ||
DESCRIPTION="${BASH_REMATCH[3]}" | ||
else | ||
echo "Branch name does not match the required pattern. Exiting." | ||
exit 0 | ||
fi | ||
FORMATTED_DESCRIPTION=$(echo "$DESCRIPTION" | sed -E 's/-/ /g' | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2)) substr($0,length($1)+1)}') | ||
NEW_TITLE="$NUMBER-$TYPE: $FORMATTED_DESCRIPTION" | ||
echo "Updating PR title to: $NEW_TITLE" | ||
curl -X PATCH \ | ||
-H "Authorization: token $GITHUB_TOKEN" \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \ | ||
-d "{\"title\":\"$NEW_TITLE\"}" |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,30 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | ||
|
||
BRANCH_REGEX="^(main|develop|((feat|fix|refactor|docs|chore|test)\/\d{1,5}-[a-zA-Z-]{2,}))$" | ||
|
||
if ! echo "$BRANCH_NAME" | grep -Eq "$BRANCH_REGEX"; | ||
then | ||
echo "-" | ||
echo "Error: Branch name '$BRANCH_NAME' does not match the required pattern." | ||
echo "-" | ||
echo "Allowed patterns are:" | ||
echo " - main" | ||
echo " - develop" | ||
echo " - feat/<number>-<description>" | ||
echo " - fix/<number>-<description>" | ||
echo " - refactor/<number>-<description>" | ||
echo " - docs/<number>-<description>" | ||
echo " - chore/<number>-<description>" | ||
echo " - test/<number>-<description>" | ||
echo "-" | ||
exit 1 | ||
fi | ||
|
||
echo "-" | ||
echo "Your branch name is OK 😎" | ||
echo "-" | ||
|
||
npm run test |
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,44 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
COMMIT_MSG_FILE=$1 | ||
|
||
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | ||
|
||
BRANCH_NUMBER=$(echo "$BRANCH_NAME" | grep -o '[0-9]\+') | ||
|
||
if [ ! -z "$BRANCH_NUMBER" ]; | ||
then | ||
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") | ||
|
||
MODIFIED_MSG=$(echo "$COMMIT_MSG" | sed "s/^\(.*: \)\(.*\)/\1$BRANCH_NUMBER - \2/") | ||
|
||
COMMIT_REGEX="^(feat|fix|refactor|docs|chore|test): [0-9]{1,5} \-( [a-zA-Z_'&:.-]+){2,}$" | ||
|
||
if ! echo "$MODIFIED_MSG" | grep -Eq "$COMMIT_REGEX"; | ||
then | ||
echo "-" | ||
echo "ERROR: Invalid commit message format. 😕" | ||
echo "-" | ||
echo "The commit message must have this format:" | ||
echo " - <commit type>: <what was done>" | ||
echo "-" | ||
echo "Allowed commit types: feat, fix, refactor, docs, chore, test" | ||
echo "Allowed characters in commit description: [a-zA-Z_'&:.-]" | ||
echo "-" | ||
echo "Example: feat: add login button" | ||
echo "-" | ||
exit 1 | ||
fi | ||
|
||
echo "$MODIFIED_MSG" > "$COMMIT_MSG_FILE" | ||
echo "-" | ||
echo "-" | ||
echo "-" | ||
echo "The name of your commit has been changed to '$MODIFIED_MSG' according to the styleguide 😎" | ||
echo "-" | ||
echo "-" | ||
echo "-" "precommit": "concurrently \"chmod +x .husky/pre-commit\" \"npx lint-staged\" \"npm:build\"", | ||
fi | ||
|
||
npm run precommit |
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