Skip to content

Make resource compatible with Bitbucket 7 #4

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

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gliderlabs/alpine:edge
FROM alpine:3.13.2

RUN apk --update add \
ca-certificates \
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ It will accept a regular expression as determined by [egrep](http://linuxcommand
* `rebuild_phrase`: *Optional (default: test this please).* Regular expression as determined by [egrep](http://linuxcommand.org/man_pages/egrep1.html) will match all comments in pull request overview.
If a match is found the pull request will be rebuilt.

* `debug`: *Optional (default: false).* If set to `true` the bash tracing (`set -x`) will be enabled for the `in`, `out` and `check` scripts.

## Behavior

### `check`: Search for pull requests to build.
Expand Down Expand Up @@ -131,6 +133,7 @@ resources:
username: {{bitbucket-username}}
password: {{bitbucket-password}}
uri: https://bitbucket.exemple.net/scm/exemple-project/exemple-repo.git
debug: true

jobs:
- name: test pull request
Expand Down
8 changes: 7 additions & 1 deletion assets/check
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ payload=$(tmpfile request)

cat > "$payload" <&0

enable_debug=$(jq -r '.source.debug // false' < ${payload})
if [ "${enable_debug}" = "true" ]; then
echo "Enable debug mode"
set -x
fi

log "Configuring git credentials"
load_pubkey "$payload"

Expand Down Expand Up @@ -68,7 +74,7 @@ pull_requests=$(jq -r '.[] | [.id, .fromRef.latestCommit, .toRef.latestCommit, .
if [ -n "$pull_requests" ]; then
# Check if the current version is still valid
if [[ "$current_change" -gt 0 ]] && (jq -e --argjson version "$current_version" '.[] | select(
.id == (($version.id//0)|tonumber)
.id == (($version.id//0)|tonumber)
and .fromRef.latestCommit == $version.from
and .toRef.latestCommit == $version.to)' > /dev/null <<< "$pull_requests_json"); then
versions="[$current_version]"
Expand Down
6 changes: 4 additions & 2 deletions assets/helpers/git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ pullrequest_metadata() {
# $1: pull request number
# $2: pull request repository
# $3: skip ssl verification
# $4: source_commit
# $5: target_commit

local source_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $3}')
local target_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $2}')
local source_commit=$4
local target_commit=$5

jq -n "[]" | \
add_pullrequest_metadata_basic "$1" "$2" "$3" | \
Expand Down
65 changes: 37 additions & 28 deletions assets/in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ payload=$(tmpfile request)

cat > "$payload" <&0

enable_debug=$(jq -r '.source.debug // false' < ${payload})
if [ "${enable_debug}" = "true" ]; then
log "Enable debug mode"
set -x
fi

load_pubkey "$payload"
configure_credentials "$payload"

Expand All @@ -48,6 +54,27 @@ prq_id=$(jq -r '.version.id // ""' < "$payload")
prq_date=$(jq -r '.version.date // ""' < "$payload")
prq_change=$(jq -r '.version.change // ""' < "$payload")

# parse uri and retrieve host
uri_parser "$uri"
repo_host="${uri_schema}://${uri_address}"
repo_host=${repo_host}$(getBasePathOfBitbucket)

# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
repo_project=$(basename $(dirname "$uri"))

# verify target branch of prq
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_id" "" "$skip_ssl_verification")

prq_from=$(echo "$prq" | jq -r '.fromRef.latestCommit')
prq_from_branch=$(echo "$prq" | jq -r '.fromRef.displayId')
prq_target=$(echo "$prq" | jq -r '.toRef.latestCommit')
prq_target_branch=$(echo "$prq" | jq -r '.toRef.displayId')

log "Detected branches"
log "From: ${prq_from_branch} (${prq_from})"
log "Target: ${prq_target_branch} (${prq_target})"

configure_git_ssl_verification "$skip_ssl_verification"
configure_git_global "${git_config_payload}"

Expand All @@ -66,14 +93,12 @@ if test "$depth" -gt 0 2> /dev/null; then
depthflag="--depth $depth"
fi

branch="pull-requests/${prq_id}/merge"

log "Cloning $uri in $destination"
git clone $depthflag "$uri" "$destination"
cd "$destination"

git fetch $depthflag origin "+refs/${branch}:refs/remotes/origin/${branch}"
git checkout -B $branch origin/$branch
git fetch $depthflag origin "${prq_from_branch}"
git checkout -B "${prq_from_branch}" "origin/${prq_from_branch}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behavior of the resource.

The original code would checkout the special commit refs/pull-requests/$id/merge which is an automatic merge of the branches involved in the pull request.

To maintain the same behavior, we should checkout the to_branch and immediately merge the from_branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to say I am not a git pro. In my understanding you want to do the following:

Pseudo:

git fetch […] from_branch
git fetch […] to_branch
git checktout -B to_branch
git merge from_branch

?? commit // What to do on merge conflict?

Can you please confirm my assumption or state the git calls to be integrated?


ref=$(git rev-parse HEAD)

Expand Down Expand Up @@ -119,49 +144,33 @@ if [ "$disable_git_lfs" != "true" ]; then
git submodule foreach "git lfs fetch && git lfs checkout"
fi

# calculate source and target commit
source_commit=$(git rev-list --parents -1 $ref | awk '{print $3}')
target_commit=$(git rev-list --parents -1 $ref | awk '{print $2}')

if [ -z "$source_commit" ]; then
if [ -z "$prq_from" ]; then
log "Unable to determine source commit from merge commit $ref. Please verify depth configuration."
exit 1
fi

if [ -z "$target_commit" ]; then
if [ -z "$prq_target" ]; then
log "Unable to determine target commit from merge commit $ref. Please verify depth configuration."
exit 1
fi

# parse uri and retrieve host
uri_parser "$uri"
repo_host="${uri_schema}://${uri_address}"
repo_host=${repo_host}$(getBasePathOfBitbucket)

# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
repo_project=$(basename $(dirname "$uri"))

# verify target branch of prq
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_id" "" "$skip_ssl_verification")

if [ "$prq" != "ERROR" ]; then
branch=$(echo "$prq" | jq -r '.fromRef.displayId')
from_branch=$(echo "$prq" | jq -r '.fromRef.displayId')
fi


# expose configuration of pull request that can be used in container
git config --add pullrequest.id $prq_id
git config --add pullrequest.source $source_commit
git config --add pullrequest.target $target_commit
git config --add pullrequest.source $prq_from
git config --add pullrequest.target $prq_target
git config --add pullrequest.merge $ref
git config --add pullrequest.date "$prq_date"
git config --add pullrequest.change "$prq_change"
git config --add pullrequest.branch "$branch"
git config --add pullrequest.branch "$from_branch"
git config --add pullrequest.targetbranch "$prq_target_branch"

jq -n \
--argjson version "$(jq '.version' < "$payload")" \
--argjson metadata "$(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification")" \
--argjson metadata "$(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification" "$prq_from" "$prq_target")" \
'{
version: $version,
metadata: $metadata
Expand Down
12 changes: 9 additions & 3 deletions assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ payload=$(tmpfile request)

cat > "$payload" <&0

enable_debug=$(jq -r '.source.debug // false' < ${payload})
if [ "${enable_debug}" = "true" ]; then
echo "Enable debug mode"
set -x
fi

load_pubkey "$payload"
configure_credentials "$payload"

Expand Down Expand Up @@ -113,8 +119,8 @@ case "$status" in
esac

# set build status on commit in bitbucket, this has to be the latest source commit or the pull request won't pick it up
source_commit=$(git rev-list --parents -1 $merge_commit | awk '{print $3}')
target_commit=$(git rev-list --parents -1 $merge_commit | awk '{print $2}')
source_commit=$(git config --get pullrequest.source)
target_commit=$(git config --get pullrequest.target)

# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
Expand Down Expand Up @@ -196,5 +202,5 @@ jq -n "{
date: \"$prq_verify_date\",
change: \"$prq_change\"
},
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification")
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification" "$source_commit" "$target_commit")
}" >&3