Skip to content

Commit

Permalink
feat(script): create pull request against develop script (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Mar 19, 2023
1 parent c115626 commit 1a4a964
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ You want to contribute to this project, and we're thankful for that ❤. But fir
## 🤝 How To Contribute

1. [**Fork**](https://docs.github.com/en/free-pro-team@latest/github/getting-started-with-github/fork-a-repo) the project.
2. Create a new topic branch on your local forked copy. Its name must respect the git flow branch naming convention. You can name it `feat/[NAME_OF_YOUR_FEATURE]` or `fix/[NAME_OF_YOUR_FIX]`. To create a new branch for your feature or your fix, please run `npm run create-branch` and follow the steps.
2. Create a new topic branch on your local forked copy. Its name must respect the git flow branch naming convention. You can name it `feat/[NAME_OF_YOUR_FEATURE]` or `fix/[NAME_OF_YOUR_FIX]`. To create a new branch for your feature or your fix, please run `npm run script:create-branch` and follow the steps.
3. Before committing any change, install *Husky hooks* with `npm i`. Hooks will ensure that your branch name, commit, code quality and tests respect the project conventions and rules.
4. Push your topic branch up to your fork. Don't forget to add [**tests**](https://github.com/antoinezanardi/werewolves-assistant-api-next/tree/main/tests). Please read the ESLint rules and the commit messages format in the dedicated sections below.
5. [**Open a Pull Request**](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests) with a clear title and description against the `develop` branch.
5. [**Open a Pull Request**](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests) with a clear title and description against the `develop` branch. You can use the `npm run script:create-pull-request` command to automatically create a PR against the `develop` branch from the current branch.

## ✅ ESLint rules for TypeScript files

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ You can check all metrics at the top of this README.
npm run gource
```

### 🔀 Create git branch with a conventional name
```shell
npm run script:create-branch
```

### ⤴️ Create pull request against the `develop` branch from current branch
```shell
npm run script:create-pull-request
```

## <a name="license">©️ License</a>

This project is licensed under the [MIT License](http://opensource.org/licenses/MIT).
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.4.0",
"description": "About Werewolves Assistant API provides over HTTP requests a way of manage Werewolves games to help the game master.",
"scripts": {
"create-branch": "./scripts/create-git-branch.sh",
"prepare": "husky install",
"prebuild": "rimraf dist",
"build": "NODE_ENV=production nest build",
Expand Down Expand Up @@ -31,6 +30,8 @@
"test:e2e:watch": "NODE_ENV=test jest --watch --config config/jest/jest-e2e.ts",
"test:e2e:cov": "NODE_ENV=test rimraf tests/e2e/coverage && jest --coverage --config config/jest/jest-e2e.ts",
"test:e2e:debug": "NODE_ENV=test node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand --config config/jest/jest-e2e.ts",
"script:create-branch": "./scripts/create-git-branch.sh",
"script:create-pull-request": "./scripts/create-pull-request.sh",
"gource": "gource --load-config config/gource/gource.config"
},
"repository": {
Expand Down
39 changes: 19 additions & 20 deletions scripts/create-git-branch.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
#!/bin/bash
PS3="Are you starting a feature or a fix ? ";
select option in "feat" "fix";
do
PS3="Are you starting a feature or a fix ? "
select option in "feat" "fix"; do
case $option in
feat|fix)
SELECTED_OPTION=$option;
break;
;;
*)
echo "Invalid option: \"$REPLY\". Please choose between 1 or 2 you ding-dong.";
;;
feat | fix)
SELECTED_OPTION=$option
break
;;
*)
echo "Invalid option: \"$REPLY\". Please choose between 1 or 2 you ding-dong."
;;
esac
done

while true; do
echo "Please provide your branch name, it must be kebab-case (like: 'my-feature') : ";
read -r FEATURE_NAME;
echo "Please provide your branch name, it must be kebab-case (like: 'my-feature') : "
read -r FEATURE_NAME
if [[ -z "$FEATURE_NAME" ]]; then
echo "You must provide a branch name.";
continue;
echo "You must provide a branch name."
continue
fi
if ! [[ "$FEATURE_NAME" =~ ^[a-z]+(-[a-z]+)*$ ]]; then
echo "Your branch name must be in kebab-case.";
continue;
echo "Your branch name must be in kebab-case."
continue
fi
break;
break
done

BRANCH_NAME="$SELECTED_OPTION/$FEATURE_NAME";
git checkout -b "$BRANCH_NAME";
echo "You're all set 🚀";
BRANCH_NAME="$SELECTED_OPTION/$FEATURE_NAME"
git checkout -b "$BRANCH_NAME"
echo "You're all set 🚀"
47 changes: 47 additions & 0 deletions scripts/create-pull-request.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

base_branch=${1:-develop}
current_branch=$(git rev-parse --abbrev-ref HEAD)

if [ "$(git rev-list --count "$base_branch".."$current_branch")" -gt 0 ]; then
echo "Opening pull request for $current_branch against $base_branch..."
else
echo "❌ Current branch ($current_branch) has no commits yet. Please make at least one commit."
exit 1
fi

remote_url=$(git config --get remote.origin.url)

if [[ "$remote_url" =~ github\.com\/(.+)\/(.+)\.git$ ]]; then
username=${BASH_REMATCH[1]}
repository=${BASH_REMATCH[2]}
elif [[ "$remote_url" =~ git@github\.com:(.+)\/(.+)\.git$ ]]; then
username=${BASH_REMATCH[1]}
repository=${BASH_REMATCH[2]}
else
echo "❌ Unable to extract username and repository from the remote URL: $remote_url"
exit 1
fi

if [ "$current_branch" = "$base_branch" ]; then
echo "❌ The current branch is already the same as the base branch."
exit 1
fi

pr_url=$(curl -s "https://api.github.com/repos/$username/$repository/pulls?head=$username:$current_branch&base=$base_branch" | grep -Eo "https://github.com/$username/$repository/pull/[0-9]+" | head -n 1)

if [ -n "$pr_url" ]; then
echo "❌ A pull request already exists for $current_branch against $base_branch: $pr_url"
exit 1
else
git push -u origin "$current_branch"

if [[ "$OSTYPE" == "darwin"* ]]; then
open "https://github.com/$username/$repository/compare/$base_branch...$current_branch?expand=1"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
xdg-open "https://github.com/$username/$repository/compare/$base_branch...$current_branch?expand=1"
else
echo "❌ Unsupported OS for automatically open the pull request creation page"
exit 1
fi
fi

0 comments on commit 1a4a964

Please sign in to comment.