Skip to content

Commit

Permalink
feat(scripts): shell scripts for git flow (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Jan 7, 2024
1 parent 7470c78 commit 9efd083
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .run/Create Branch.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Create Branch" type="js.build_tools.npm" focusToolWindowBeforeRun="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="script:create-branch" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/Create Pull Request.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Create Pull Request" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="script:create-pull-request" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
"postinstall": "nuxt prepare",
"script:create-branch": "scripts/create-git-branch.sh",
"script:create-pull-request": "scripts/create-pull-request.sh"
},
"devDependencies": {
"nuxt": "^3.9.0",
Expand Down
32 changes: 32 additions & 0 deletions scripts/create-git-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
PS3="What kind of job are your starting ? "
select option in "feat" "fix" "docs" "style" "refactor" "test" "perf" "build" "ci" "chore" "revert";
do
case $option in
feat | fix | docs | style | refactor | test | perf | build | ci | chore | revert)
SELECTED_OPTION=$option
break
;;
*)
echo "❌ Invalid option: \"$REPLY\". Please choose between 1 to 11."
;;
esac
done

while true; do
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
fi
if ! [[ "$FEATURE_NAME" =~ ^[a-z0-9]+(-[a-z0-9]+)*$ ]]; then
echo "❌ Your branch name must be in kebab-case."
continue
fi
break
done

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

github_user=$(git config github.user)

while true; do
if [[ -z "$github_user" ]]; then
read -r -p "Enter your GitHub username: " github_user
git config github.user "$github_user"
elif ! curl -s "https://api.github.com/users/$github_user" | grep -q '"login":'; then
echo "The GitHub username \"$github_user\" does not exist or is invalid."
read -r -p "Enter your GitHub username: " github_user
git config github.user "$github_user"
else
break
fi
done

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
fi

git push -u origin "$current_branch"

word_map=("feat:🚀 feature" "fix:🐛 bug" "chore:🧹 chore" "docs:📖 docs" "style:🎨 style" "refactor:🔩 refactor" "perf:⚡️ perf" "test:✅ test" "ci:🔁 ci")

first_commit_message=$(git log --format=%B -n 1 "$base_branch".."$current_branch")
first_word=$(echo "$first_commit_message" | cut -d'(' -f1)

label=""
for pair in "${word_map[@]}"; do
key=${pair%%:*}
value=${pair#*:}
if [ "$key" = "$first_word" ]; then
label=$value
break
fi
done

open_pr_url="https://github.com/$username/$repository/compare/$base_branch...$current_branch?expand=1&assignee=$github_user"
encoded_label=$(ruby -e 'require "erb"; puts ERB::Util.url_encode(ARGV[0])' "$label")
encoded_title=$(ruby -e 'require "erb"; puts ERB::Util.url_encode(ARGV[0])' "$first_commit_message")
encoded_open_pr_url="$open_pr_url&labels=$encoded_label&title=$encoded_title"
if [[ "$OSTYPE" == "darwin"* ]]; then
open "$encoded_open_pr_url"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
xdg-open "$encoded_open_pr_url"
else
echo "❌ Unsupported OS for automatically open the pull request creation page"
exit 1
fi
7 changes: 7 additions & 0 deletions scripts/transform-cucumber-report-as-env-variables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

report_file="tests/acceptance/reports/junit.xml"

num_testcases=$(grep -c "<testcase" "$report_file")

echo "CUCUMBER_SCENARIOS_COUNT=$num_testcases"
26 changes: 26 additions & 0 deletions scripts/transform-jest-results-as-env-variables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
output=$(cat)

tests_count=$(echo "$output" | awk '/Tests:/{print $2}')

statements_count=$(echo "$output" | awk '/Statements/{print $(NF-1)}')
branches_count=$(echo "$output" | awk '/Branches/{print $(NF-1)}')
functions_count=$(echo "$output" | awk '/Functions/{print $(NF-1)}')
lines_count=$(echo "$output" | awk '/Lines/{print $(NF-1)}' | tr -d '\nLine')

statements_percent=$(echo "$output" | awk '/Statements/{print $3}')
branches_percent=$(echo "$output" | awk '/Branches/{print $3}')
functions_percent=$(echo "$output" | awk '/Functions/{print $3}')
lines_percent=$(echo "$output" | awk '/Lines/{print $3}' | tr -d '\nLine')

echo "JEST_TESTS_COUNT=$tests_count"

echo "JEST_STATEMENTS_COUNT=$statements_count"
echo "JEST_BRANCHES_COUNT=$branches_count"
echo "JEST_FUNCTIONS_COUNT=$functions_count"
echo "JEST_LINES_COUNT=$lines_count"

echo "JEST_STATEMENTS_PERCENT=$statements_percent"
echo "JEST_BRANCHES_PERCENT=$branches_percent"
echo "JEST_FUNCTIONS_PERCENT=$functions_percent"
echo "JEST_LINES_PERCENT=\"${lines_percent:1}\""

0 comments on commit 9efd083

Please sign in to comment.