From d6982d82cef48006b16f09ae0d361be7e8504f57 Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Wed, 1 Jul 2020 10:20:33 +0200 Subject: [PATCH 1/6] Introduce 'debug' configuration parameter to enable bash tracing --- README.md | 3 +++ assets/check | 8 +++++++- assets/in | 7 ++++++- assets/out | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 801b97e..0014749 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/assets/check b/assets/check index 4f71355..da5a33c 100755 --- a/assets/check +++ b/assets/check @@ -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" @@ -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]" diff --git a/assets/in b/assets/in index f1ef0e9..3a4a550 100755 --- a/assets/in +++ b/assets/in @@ -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" @@ -149,7 +155,6 @@ if [ "$prq" != "ERROR" ]; then 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 diff --git a/assets/out b/assets/out index f0f5db6..66b3fe5 100755 --- a/assets/out +++ b/assets/out @@ -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" From 29e8c795d270b7e9053100133f2432588fdf8010 Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Wed, 1 Jul 2020 14:56:54 +0200 Subject: [PATCH 2/6] Use Bitbucket API wherever possible instead of git revs --- assets/helpers/git.sh | 6 ++-- assets/in | 69 +++++++++++++++++++++++++++---------------- assets/out | 7 +++-- 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/assets/helpers/git.sh b/assets/helpers/git.sh index 6bca9b6..54e79ce 100644 --- a/assets/helpers/git.sh +++ b/assets/helpers/git.sh @@ -94,9 +94,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" | \ diff --git a/assets/in b/assets/in index 3a4a550..a1c9214 100755 --- a/assets/in +++ b/assets/in @@ -32,7 +32,7 @@ cat > "$payload" <&0 enable_debug=$(jq -r '.source.debug // false' < ${payload}) if [ "${enable_debug}" = "true" ]; then - echo "Enable debug mode" + log "Enable debug mode" set -x fi @@ -54,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}" @@ -72,14 +93,17 @@ 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 +# TODO cleanup +#branch="pull-requests/${prq_id}/merge" +#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}" ref=$(git rev-parse HEAD) @@ -126,45 +150,38 @@ if [ "$disable_git_lfs" != "true" ]; then 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}') +# TODO cleanup +#source_commit=$(git rev-list --parents -1 $ref | awk '{print $3}') +#echo "Using source commit: ${source_commit}" + +# TODO cleanup +#target_commit=$(git rev-list --parents -1 $ref | awk '{print $2}') +#echo "Using target commit: ${target_commit}" -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" jq -n "{ version: $(jq '.version' < "$payload"), - metadata: $(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification") + metadata: $(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification" "$prq_from" "$prq_target") }" >&3 diff --git a/assets/out b/assets/out index 66b3fe5..5fa5999 100755 --- a/assets/out +++ b/assets/out @@ -118,9 +118,10 @@ case "$status" in exit 1 esac +# TODO # 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 | cat) +target_commit=$(git config --get pullrequest.target | cat) # determine repository name for calling REST api repo_name=$(basename "$uri" | sed "s/.git$//") @@ -202,5 +203,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 From b0540afada170fc473e32fc680370759e6b334d4 Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Tue, 21 Jul 2020 09:17:46 +0200 Subject: [PATCH 3/6] Remove commented code --- assets/in | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/assets/in b/assets/in index a1c9214..f0da379 100755 --- a/assets/in +++ b/assets/in @@ -97,11 +97,6 @@ log "Cloning $uri in $destination" git clone $depthflag "$uri" "$destination" cd "$destination" -# TODO cleanup -#branch="pull-requests/${prq_id}/merge" -#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}" @@ -149,15 +144,6 @@ if [ "$disable_git_lfs" != "true" ]; then git submodule foreach "git lfs fetch && git lfs checkout" fi -# calculate source and target commit -# TODO cleanup -#source_commit=$(git rev-list --parents -1 $ref | awk '{print $3}') -#echo "Using source commit: ${source_commit}" - -# TODO cleanup -#target_commit=$(git rev-list --parents -1 $ref | awk '{print $2}') -#echo "Using target commit: ${target_commit}" - if [ -z "$prq_from" ]; then log "Unable to determine source commit from merge commit $ref. Please verify depth configuration." exit 1 From 4c7b9d9a9f4b791d5036ea1c9cd9c84f6d4bf50d Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Tue, 21 Jul 2020 09:22:07 +0200 Subject: [PATCH 4/6] Remove unnecessary piped cat calls. --- assets/out | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/assets/out b/assets/out index 5fa5999..f67fa30 100755 --- a/assets/out +++ b/assets/out @@ -118,10 +118,9 @@ case "$status" in exit 1 esac -# TODO # 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 config --get pullrequest.source | cat) -target_commit=$(git config --get pullrequest.target | cat) +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$//") From 7abe9f712212ba92ca603235d56f808f62517f0e Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Tue, 16 Mar 2021 10:43:00 +0100 Subject: [PATCH 5/6] Switch over to the official alpine image with tagged version --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a0e34ce..06c6a9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM gliderlabs/alpine:edge +FROM alpine:3.13.2 RUN apk --update add \ ca-certificates \ From 8c0f010fe6be02577ce30d0bcf336518b16533cb Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Tue, 16 Nov 2021 11:11:58 +0100 Subject: [PATCH 6/6] Add target branch to git config meta data --- assets/in | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/in b/assets/in index b745ecf..aa2a6d1 100755 --- a/assets/in +++ b/assets/in @@ -166,6 +166,7 @@ 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 "$from_branch" +git config --add pullrequest.targetbranch "$prq_target_branch" jq -n \ --argjson version "$(jq '.version' < "$payload")" \