Skip to content

Commit

Permalink
Added support for Bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas committed Oct 31, 2023
1 parent cbea70d commit aeb5313
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 0 deletions.
36 changes: 36 additions & 0 deletions hooks/common/post-flow-feature-publish
Original file line number Diff line number Diff line change
Expand Up @@ -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 @- <<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"
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 "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 @- <<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"
else
echo "Create a PR manually at $ORIGIN_URL"
echo "Do not delete the feature branch after the PR is merged. It will be deleted automatically when the feature is finished."
Expand Down
36 changes: 36 additions & 0 deletions hooks/common/post-flow-hotfix-publish
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
echo "Create PR at: https://github.com/$repo/compare/$BRANCH?expand=1"
echo "Do not delete the hotfix branch after the PR is merged. It will be deleted automatically when the hotfix is finished."
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 @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
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"
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 @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
else
echo "Create a PR manually at $ORIGIN_URL"
echo "Do not delete the hotfix branch after the PR is merged. It will be deleted automatically when the hotfix is finished."
Expand Down
36 changes: 36 additions & 0 deletions hooks/common/post-flow-release-publish
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,42 @@ elif [[ $ORIGIN_URL =~ ^https://github\.com ]]; then
echo "Do not delete the release branch after the PR is merged. It will be deleted automatically when the release 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 @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
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"
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 @- <<EOM
{
"title": "Merge $BRANCH",
"description": "Feature $VERSION. Do not delete the feature branch after the merge.",
"destination": { "branch": { "name": "$MASTER_BRANCH" } },
"source": { "branch": { "name": "$BRANCH" } }
}
EOM
die_on_error "Failed to create a Pull Request"
else
echo "Create a PR manually at $ORIGIN_URL"
echo "Do not delete the release branch after the PR is merged. It will be deleted automatically when the release is finished."
Expand Down
51 changes: 51 additions & 0 deletions hooks/common/pre-flow-feature-finish
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down
51 changes: 51 additions & 0 deletions hooks/common/pre-flow-hotfix-finish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions hooks/common/pre-flow-release-finish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aeb5313

Please sign in to comment.