Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link development branches to issues and remove bugfix and feature wrappers over branch #88

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ curl -sSL https://raw.githubusercontent.com/SituDevelopment/automation/main/inst
## Tools

- `branch`
- Creates a new Git branch in the given repository and publishes it to GitHub.
- `bugfix-branch`
- Creates a new Git bug fix branch in the given repository and publishes it to GitHub.
- Creates a new development branch for a GitHub issue.
- `clone-mongo-db`
- Clones a MongoDB database.
- `create-pr`
- Creates a GitHub pull request with the given properties.
- `feature-branch`
- Creates a new Git feature branch in the given repository and publishes it to GitHub.
- `find-by-extension`
- Determines the existence and echoes the names of files with the given file extension(s).

Expand Down
84 changes: 49 additions & 35 deletions branch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash
#
#
# Creates a new Git branch in the given repository and publishes it to GitHub.
# Creates a new development branch for a GitHub issue.
# If the VS Code command: `code` is available, the project will be opened in VS Code.
#
#
# Usage:
# branch [-r] [-o <owner>] [-b <base-branch>] <repository> <branch-name>
# branch [-r] [-o <owner>] [-b <base-branch>] [-d <dev-branch>] <repository> <issue-number>
# branch -h
#
# Options:
Expand All @@ -18,25 +18,30 @@
# -b <base-branch> Name of the branch from which to take the new branch.
# Defaults to the repository's default branch.
#
# -d <dev-branch> Name of the new branch to create.
# Defaults to being auto-generated by the issue number and title.
#
# <repository> Name of the repository.
#
# <branch-name> Name of the new branch to create.
# <issue-number> Number of the issue for which the development branch is being created.
#
# -h Display help and exit.
#
#
# Exit codes:
# 0 - success
# 1 - incorrect usage
# 2 - git error
# 3 - VS Code error
# 2 - GitHub error
# 3 - git error
# 4 - VS Code error


# exit codes
SUCCESS=0
INCORRECT_USAGE=1
GIT_ERROR=2
VS_CODE_ERROR=3
GITHUB_ERROR=2
GIT_ERROR=3
VS_CODE_ERROR=4


#######################################
Expand All @@ -49,10 +54,10 @@ VS_CODE_ERROR=3
showhelp() {
cat << EOF
Usage:
branch [-r] [-o <owner>] [-b <base-branch>] <repository> <branch-name>
branch [-r] [-o <owner>] [-b <base-branch>] [-d <dev-branch>] <repository> <issue-number>
branch -h

Creates a new Git branch in the given repository and publishes it to GitHub.
Creates a new development branch for a GitHub issue.


Options:
Expand All @@ -64,9 +69,12 @@ Options:
-b <base-branch> Name of the branch from which to take the new branch.
Defaults to the repository's default branch.

-d <dev-branch> Name of the new branch to create.
Defaults to being auto-generated by the issue number and title.

<repository> Name of the repository.

<branch-name> Name of the new branch to create.
<issue-number> Number of the issue for which the development branch is being created.

-h Display help and exit.
EOF
Expand Down Expand Up @@ -97,11 +105,12 @@ assert_success() {


# assign repository and branch variables according to given arguments
while getopts ": r o: b: h" flag; do
while getopts ": r o: b: d: h" flag; do
case "${flag}" in
r) reuse=" --reuse-window" ;;
o) owner="${OPTARG}" ;;
b) base="${OPTARG}" ;;
d) dev="--name ${OPTARG}" ;;
h) showhelp; exit ${SUCCESS} ;;
?) showhelp; exit ${INCORRECT_USAGE} ;;
esac
Expand All @@ -114,43 +123,48 @@ if [[ $# -ne 2 ]]; then
fi

repo=$1
branch=$2
issue=$2

# set default values for unset variables
reuse=${reuse:=""}
owner=${owner:="SituDevelopment"}
base=${base:=$(bkt --discard-failures -- gh api /repos/${owner}/${repo}/branches?protected=true --jq '.[0].name')}

assert_success "Cloning base branch of repository..." \
"git clone [email protected]:${owner}/${repo} --branch '${base}' '/var/www/development/${repo}-${branch}'" \
"Error cloning repository" \
${GIT_ERROR} \
"Cloned base branch of repository\n"
dev=${dev:=""}


eval "cd '/var/www/development/${repo}-${branch}'"
# if not provided, construct branch name
if [[ -z "${dev}" ]]; then
prefixes=(bug chore feature)
issue_labels=$(gh issue view ${issue} --json labels --jq '.labels[].name')

branch_name=$(gh issue view ${issue} --json title --jq '.title')
branch_name=$(echo ${branch_name} | sed -e 's| |-|g') # convert to kebab-case
branch_name=$(echo ${branch_name} | sed -e 's|[^a-zA-Z0-9-]||g') # remove non-kebab-case characters
branch_name=$(echo ${branch_name} | sed -E 's|[A-Z][a-z]+|\L&|g') # convert non-initialisms to lowercase

assert_success "Creating branch..." \
"git branch '${branch}'" \
"Error creating branch" \
${GIT_ERROR} \
"Created branch\n"

for prefix in "${prefixes[@]}"; do
if [[ $issue_labels == *"${prefix}"* ]]; then
dev="--name ${prefix}/${branch_name}"
break
fi
done
fi

assert_success "Publishing branch to remote repository..." \
"git push --set-upstream origin '${branch}'" \
"Error publishing branch to remote repository" \
${GIT_ERROR} \
"Published branch to remote repository\n"
# commands predefined to ensure shell expansion works correctly
branch_command="gh issue develop '${issue}' --base '${base}' ${dev}"
replacement_command="sed -e s|github.com/${owner}/${repo}/tree/||g"

assert_success "Creating development branch..." \
"branch=$(eval ${branch_command} | ${replacement_command})" \
"Error creating development branch" \
"${GITHUB_ERROR}" \
"Created development branch\n"

assert_success "Checking out to branch..." \
"git checkout '${branch}'" \
"Error checking out to branch" \
assert_success "Cloning repository..." \
"git clone [email protected]:${owner}/${repo} --branch '${branch}' '/var/www/development/${repo}-${branch}'" \
"Error cloning repository" \
${GIT_ERROR} \
"Checked out branch\n"

"Cloned repository\n"

assert_success "Checking for VS Code command..." \
"which code" \
Expand Down
16 changes: 10 additions & 6 deletions branch-man
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.TH BRANCH 1 "November 2023" "3.0" "Automation Manual"
.TH BRANCH 1 "January 2024" "4.0" "Automation Manual"

.SH NAME
branch \- create a new branch
branch \- create a new development branch

.SH SYNOPSIS
branch [-r] [-o <owner>] [-b <base-branch>] <repository> <new-branch>
branch [-r] [-o <owner>] [-b <base-branch>] <repository> <issue-number>

branch -h

.SH DESCRIPTION
Creates a new Git branch in the given repository and publishes it to GitHub.
Creates a new development branch for a GitHub issue.

If the VS Code command: `code` is available, the project will be opened in VS Code.

Expand All @@ -26,13 +26,17 @@ Owner of the repository. Defaults to "SituDevelopment".
-b <base-branch>
Name of the branch from which to take the new branch. Defaults to the repository's default branch.

.TP
-d <dev-branch>
Name of the new branch to create. Defaults to being auto-generated by the issue number and title.

.TP
<repository>
Name of the repository.

.TP
<new-branch>
Name of the new branch to create.
<issue-number>
Number of the issue for which the development branch is being created.

.TP
-h
Expand Down
30 changes: 0 additions & 30 deletions bugfix-branch

This file was deleted.

42 changes: 0 additions & 42 deletions bugfix-branch-man

This file was deleted.

30 changes: 0 additions & 30 deletions chore-branch

This file was deleted.

42 changes: 0 additions & 42 deletions chore-branch-man

This file was deleted.

Loading
Loading