Skip to content

Commit

Permalink
224-feat: Add automatic validation PR workflow names (#394)
Browse files Browse the repository at this point in the history
* 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
ansivgit authored Jul 24, 2024
1 parent ae45d73 commit 45331bb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/auto-pr-title.yml
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\"}"
1 change: 0 additions & 1 deletion .husky/pre-commit

This file was deleted.

29 changes: 29 additions & 0 deletions .husky/pre-push
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
44 changes: 44 additions & 0 deletions .husky/prepare-commit-msg
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
20 changes: 17 additions & 3 deletions readme/git-pullrequest-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,40 @@ Pick a name for your branch that tells us what it's for. Use this format:
- `<issue number>`: This is the number of the task or issue in Github Projects you're working on.
- `short-description`: This is a quick summary of what you're doing. Use hyphens instead of spaces.

❗Please note - the `short-description` shouldn't contain numbers.

Example: `feat/123-add-login-button`

## Commit Messages

Your commit messages should be short, but they should tell us what changes you made. Use this format:

`<type of work>: <summary>`

Your commit message will be automatically converted to the following format:

`<type of work>: <issue number> - <summary>`

- `<type of work>`: This shows what kind of changes you made.
- `<issue number>`: This is the number of the task or issue in Github Projects you're working on.
- `<summary>`: This is a quick summary of what changes you made.

Example: `doc: 159 - add login button functionality`
❗Please note - the `summary` shouldn't contain numbers.

Example:
your commit message `docs: add login button functionality`
will be automatically converted to `docs: 159 - add login button functionality`


Please note that at RS School, we strive to follow the [Conventional Commits 1.0.0](https://www.conventionalcommits.org/en/v1.0.0/). In case of uncertainty, feel free to refer to this source that inspired us in writing these guidelines.

## Pull Requests

When you finish your work in a branch, make a pull request against the main branch. Use the same format as commit messages for your pull request title.
When you finish your work in a branch, make a pull request against the main branch. Initially, the pull request will be named after your branch. However, be aware that once the pull request is opened, an automatic script will rename it according to a predefined template, overwriting the title you initially provided.

<issue number>-<type of work>: <Title>

If necessary, you can adjust the pull request' <Title> after PR has been created. Below is the final look of your pull request title.

Example: `123-feat: Complete addition of login button functionality`.

Expand All @@ -45,7 +59,7 @@ Please use English for all discussions, pull requests, issues, and comments.

## Note

Always get the latest updates from the main branch before you push your own changes or make a pull request.
Always get the latest updates from the main branch and `npm install` before you push your own changes or make a pull request.

## Conclusion

Expand Down

0 comments on commit 45331bb

Please sign in to comment.