From aeb53138dedec7ae3c6e640ec952dfb199ec467b Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Tue, 31 Oct 2023 20:38:03 +0900 Subject: [PATCH] Added support for Bitbucket --- hooks/common/post-flow-feature-publish | 36 ++++++++++++++++++ hooks/common/post-flow-hotfix-publish | 36 ++++++++++++++++++ hooks/common/post-flow-release-publish | 36 ++++++++++++++++++ hooks/common/pre-flow-feature-finish | 51 ++++++++++++++++++++++++++ hooks/common/pre-flow-hotfix-finish | 51 ++++++++++++++++++++++++++ hooks/common/pre-flow-release-finish | 51 ++++++++++++++++++++++++++ 6 files changed, 261 insertions(+) diff --git a/hooks/common/post-flow-feature-publish b/hooks/common/post-flow-feature-publish index 32de746..f857bff 100755 --- a/hooks/common/post-flow-feature-publish +++ b/hooks/common/post-flow-feature-publish @@ -63,6 +63,42 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then echo "Do not delete the feature branch after the PR is merged. It will be deleted automatically when the feature is finished." echo "" 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" + verbose "Creating a PR 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 + PR_STATE=$(gh pr view $BRANCH --json state --jq .state) + die_on_error "There is no Pull Request for branch $BRANCH, please publish this feature before finishing it." + if [[ $PR_STATE != MERGED ]]; then + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${DEVELOP_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${DEVELOP_BRANCH}:${DEVELOP_BRANCH} + die_on_error "Failed to fetch $DEVELOP_BRANCH from $ORIGIN" + else + die "gh is not available, so I cannot check pull request states on $ORIGIN ($ORIGIN_URL)" + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${DEVELOP_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${DEVELOP_BRANCH}:${DEVELOP_BRANCH} + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${DEVELOP_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${DEVELOP_BRANCH}:${DEVELOP_BRANCH} + die_on_error "Failed to fetch $DEVELOP_BRANCH from $ORIGIN" else die "$ORIGIN ($ORIGIN_URL) is not a github repository, so I cannot check pull request states" fi diff --git a/hooks/common/pre-flow-hotfix-finish b/hooks/common/pre-flow-hotfix-finish index 9c7de44..e6a344c 100755 --- a/hooks/common/pre-flow-hotfix-finish +++ b/hooks/common/pre-flow-hotfix-finish @@ -45,6 +45,57 @@ if [[ $ORIGIN_URL =~ ^git@github\.com ]]; then else die "gh is not available, so I cannot check pull request states on $ORIGIN ($ORIGIN_URL)" fi +elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then + if command -v gh &> /dev/null; then + PR_STATE=$(gh pr view $BRANCH --json state --jq .state) + die_on_error "There is no Pull Request for branch $BRANCH, please publish this hotfix before finishing it." + if [[ $PR_STATE != MERGED ]]; then + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN" + else + die "gh is not available, so I cannot check pull request states on $ORIGIN ($ORIGIN_URL)" + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN" else die "$ORIGIN ($ORIGIN_URL) is not a github repository, so I cannot check pull request states" fi diff --git a/hooks/common/pre-flow-release-finish b/hooks/common/pre-flow-release-finish index 99a89ec..16d2960 100755 --- a/hooks/common/pre-flow-release-finish +++ b/hooks/common/pre-flow-release-finish @@ -45,6 +45,57 @@ if [[ $ORIGIN_URL =~ ^git@github\.com ]]; then else die "gh is not available, so I cannot check pull request states on $ORIGIN ($ORIGIN_URL)" fi +elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then + if command -v gh &> /dev/null; then + PR_STATE=$(gh pr view $BRANCH --json state --jq .state) + die_on_error "There is no Pull Request for branch $BRANCH, please publish this release before finishing it." + if [[ $PR_STATE != MERGED ]]; then + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN" + else + die "gh is not available, so I cannot check pull request states on $ORIGIN ($ORIGIN_URL)" + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + 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" + verbose "Checking Pull Request on repository $repo for $BRANCH" + PR_STATE=$(curl -sS \ + --header "Authorization: Bearer $(git config bitbucket.api.username)" \ + https://api.bitbucket.org/2.0/repositories/${repo}/pullrequests?state=ALL |\ + jq -r --arg BRANCH $BRANCH ' + .values |= sort_by(.updated_on) | + last(.values[] | 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 + die "There is a Pull Request that is not yet merged, please wait (Status: $PR_STATE)" + fi + verbose "Fetching ${MASTER_BRANCH} from $ORIGIN" + git fetch $ORIGIN ${MASTER_BRANCH}:${MASTER_BRANCH} + die_on_error "Failed to fetch $MASTER_BRANCH from $ORIGIN" else die "$ORIGIN ($ORIGIN_URL) is not a github repository, so I cannot check pull request states" fi