From 3b62208307e168e008a1d265db695f85fdca1fe3 Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Wed, 6 Dec 2023 00:51:25 +0900 Subject: [PATCH] Use Bitbucket's CLI for Bitbucket's origins --- README.md | 8 ++--- hooks/common/post-flow-feature-publish | 46 +++++++++----------------- hooks/common/post-flow-hotfix-publish | 46 +++++++++----------------- hooks/common/post-flow-release-publish | 46 +++++++++----------------- hooks/common/pre-flow-feature-finish | 24 ++++++-------- hooks/common/pre-flow-hotfix-finish | 24 ++++++-------- hooks/common/pre-flow-release-finish | 24 ++++++-------- 7 files changed, 81 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 4462c9c..8616011 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/hooks/common/post-flow-feature-publish b/hooks/common/post-flow-feature-publish index de0c445..5141d1a 100755 --- a/hooks/common/post-flow-feature-publish +++ b/hooks/common/post-flow-feature-publish @@ -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 @- < /dev/null; then diff --git a/hooks/common/post-flow-hotfix-publish b/hooks/common/post-flow-hotfix-publish index fedf083..c28f7f0 100755 --- a/hooks/common/post-flow-hotfix-publish +++ b/hooks/common/post-flow-hotfix-publish @@ -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 @- < /dev/null; then diff --git a/hooks/common/post-flow-release-publish b/hooks/common/post-flow-release-publish index 53b6a3c..af0eb94 100755 --- a/hooks/common/post-flow-release-publish +++ b/hooks/common/post-flow-release-publish @@ -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 @- < /dev/null; then diff --git a/hooks/common/pre-flow-feature-finish b/hooks/common/pre-flow-feature-finish index 272a028..7f83cbc 100755 --- a/hooks/common/pre-flow-feature-finish +++ b/hooks/common/pre-flow-feature-finish @@ -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 @@ -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 diff --git a/hooks/common/pre-flow-hotfix-finish b/hooks/common/pre-flow-hotfix-finish index afe7654..99b9b06 100755 --- a/hooks/common/pre-flow-hotfix-finish +++ b/hooks/common/pre-flow-hotfix-finish @@ -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 @@ -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 diff --git a/hooks/common/pre-flow-release-finish b/hooks/common/pre-flow-release-finish index 26fe184..66e0260 100755 --- a/hooks/common/pre-flow-release-finish +++ b/hooks/common/pre-flow-release-finish @@ -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 @@ -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