Skip to content

Commit

Permalink
Use Bitbucket's CLI for Bitbucket's origins
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas committed Dec 5, 2023
1 parent bfde620 commit 3b62208
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 137 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ You can also change the _prefix_ used to tag the new release/hotfix (default is
git config gitflow.prefix.versiontag v
```

When using Pull Requests, if the origin is on [GitHub](https://github.com), the scripts will rely on the [Github's CLI (gh)](https://cli.github.com) and will fail if the tool is not present. If the origin is on [BitBucket](https://bitbucket.org), you must create a [repository access token](https://support.atlassian.com/bitbucket-cloud/docs/create-a-repository-access-token/) and store this access token in the git config:
When using Pull Requests, if the origin is on [GitHub](https://github.com), the scripts will rely on the [Github's CLI (gh)](https://cli.github.com) and will fail if the tool is not present.

```sh
git config bitbucket.api.access-token xxxxyyyy
```
If the origin is on [BitBucket](https://bitbucket.org), the scripts will rely on the [Bitbucket's CLI (bb)](https://bitbucket.org/gildas_cherruel/bb) and will fail if the tool is not present. You can configure which Bitbucket profile is used in your git config (`git config bitbucket.cli.profile myprofile`)

If the origin is on [GitLab](https://gitlab.com/about), the [GitLab CLI](https://gitlab.com/gitlab-org/cli) must be installed.
If the origin is on [GitLab](https://gitlab.com), the scripts will reply on the [GitLab's CLI (glab)](https://gitlab.com/gitlab-org/cli) and will fail if the tool is not present.

The scripts will also bump the Helm Chart version if it is present. You can configure the location of the chart with:
```bash
Expand Down
46 changes: 16 additions & 30 deletions hooks/common/post-flow-feature-publish
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,26 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$DEVELOP_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Feature $VERSION. Do not delete the feature branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $DEVELOP_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$DEVELOP_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Feature $VERSION. Do not delete the feature branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $DEVELOP_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^git@gitlab\.com ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@gitlab\.com://' -e 's/\.git$//')
if command -v glab &> /dev/null; then
Expand Down
46 changes: 16 additions & 30 deletions hooks/common/post-flow-hotfix-publish
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,26 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Hotfix $VERSION. Do not delete the hotfix branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Hotfix $VERSION. Do not delete the hotfix branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $MASTER_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Hotfix $VERSION. Do not delete the hotfix branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Hotfix $VERSION. Do not delete the hotfix branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $MASTER_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^git@gitlab\.com ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@gitlab\.com://' -e 's/\.git$//')
if command -v glab &> /dev/null; then
Expand Down
46 changes: 16 additions & 30 deletions hooks/common/post-flow-release-publish
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,26 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Release $VERSION. Do not delete the release branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Release $VERSION. Do not delete the release branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $MASTER_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Creating a Pull Request on repository $repo"
curl https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests \
--request POST \
--header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' \
--data @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Release $VERSION. Do not delete the release branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
bb ${profile:+--profile $profile} pr create \
--title "Merge $BRANCH" \
--description "Release $VERSION. Do not delete the release branch after the merge." \
--repository $repo \
--source $BRANCH \
--destination $MASTER_BRANCH
die_on_error "Failed to create a Pull Request"
elif [[ $ORIGIN_URL =~ ^git@gitlab\.com ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@gitlab\.com://' -e 's/\.git$//')
if command -v glab &> /dev/null; then
Expand Down
24 changes: 10 additions & 14 deletions hooks/common/pre-flow-feature-finish
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this feature before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand All @@ -92,15 +90,13 @@ elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
die_on_error "Failed to fetch $DEVELOP_BRANCH from $ORIGIN"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this feature before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand Down
24 changes: 10 additions & 14 deletions hooks/common/pre-flow-hotfix-finish
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this hotfix before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand All @@ -92,15 +90,13 @@ elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this hotfix before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand Down
24 changes: 10 additions & 14 deletions hooks/common/pre-flow-release-finish
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
fi
elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/^git@bitbucket\.org://' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this release before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand All @@ -92,15 +90,13 @@ elif [[ $ORIGIN_URL =~ ^git@bitbucket\.org ]]; then
die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN"
elif [[ $ORIGIN_URL =~ ^https://.*bitbucket\.org ]]; then
repo=$(echo $ORIGIN_URL | sed -e 's/https:\/\/.*bitbucket\.org\///' -e 's/\.git$//')
token=$(get_config bitbucket.api.access-token)
[[ -z $token ]] && die "Bitbucket's API Access Token is empty"
profile=$(git config bitbucket.cli.profile)
verbose "Checking Pull Request on repository $repo for $BRANCH"
PR_STATE=$(curl -sS \
--header "Authorization: Bearer $token" \
https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\
PR_STATE=$( \
bb ${profile:+--profile $profile} pr list --repository $repo |\
jq -r --arg BRANCH $BRANCH '
.values |= sort_by(.updated_on) |
last(.values[] | select(.source.branch.name == $BRANCH) | .state)'
. |= sort_by(.updated_on) |
last(.[] | select(.source.branch.name == $BRANCH) | .state)'
)
die_on_error "There is no Pull Request for branch $BRANCH, please publish this release before finishing it."
if [[ $PR_STATE != MERGED ]]; then
Expand Down

0 comments on commit 3b62208

Please sign in to comment.