diff --git a/Dockerfile b/Dockerfile index 3cc1300..a322122 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ RUN apk --no-cache add \ ca-certificates=20190108-r0 \ curl=7.61.1-r3 \ git=2.15.4-r0 \ + git-lfs>1.1.2 \ jq=1.5-r5 \ openssh-client=7.5_p1-r10 @@ -12,7 +13,8 @@ RUN apk --no-cache add \ RUN git config --global user.email "git@localhost" && \ git config --global user.name "git" -COPY scripts/install_git_lfs.sh install_git_lfs.sh -RUN ./install_git_lfs.sh +# No longer needed as git-lfs is installed with apk +#COPY scripts/install_git_lfs.sh install_git_lfs.sh +#RUN ./install_git_lfs.sh COPY assets /opt/resource diff --git a/README.md b/README.md index c393f49..6555ca4 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ It will accept a regular expression as determined by [egrep](http://linuxcommand * `only_when_mergeable`: *Optional (default: false).* If enabled only pull requests which are mergeable (all tasks done, required number of approvers reached, ...) will be built. +* `max_pr_to_check`: *Optional (default: 999).* Max number of PRs to check, ordered in descending order by PR number. + * `only_when_asked`: *Optional (default: false).* Only build pull request when explicitly asked for, using rebuild_phrase. * `rebuild_when_target_changed`: *Optional (default: false).* Rebuild pull requests when target branch changed instead of only when source branch changed. diff --git a/assets/check b/assets/check index 3f6788c..4507b38 100755 --- a/assets/check +++ b/assets/check @@ -41,6 +41,7 @@ sleep_between_fetches=$(jq -r '.source.sleep_between_fetches // "0"' < "$payload sleep_between_fetches=$(echo "$sleep_between_fetches" | sed 's/[^0-9\.]//g') rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload") rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload") +max_pr_to_check=$(jq -r '.source.max_pr_to_check // "999"' < "$payload") CURRENT_VERSION_DATE=$(jq -r '.version.date // "0"' < "$payload") configure_git_ssl_verification "$skip_ssl_verification" @@ -60,7 +61,8 @@ fi # collect all pull requests from uri REMOTES=$(git ls-remote "$uri") set +e -PULL_REQUESTS=$(echo "$REMOTES" | grep -E "/pull\\-requests/[0-9]+/${prq_branch}") +PULL_REQUESTS=$(echo "$REMOTES" | grep -E "/pull\\-requests/[0-9]+/${prq_branch}" | \ + sort -t / -k 3 -n -r | head -n $max_pr_to_check) set -e diff --git a/assets/helpers/git.sh b/assets/helpers/git.sh index d598fca..d5433b7 100644 --- a/assets/helpers/git.sh +++ b/assets/helpers/git.sh @@ -96,14 +96,17 @@ pullrequest_metadata() { # $1: pull request number # $2: pull request repository # $3: skip ssl verification + # $4: soucre_commmit + # $5: target_commmit - 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}') + # No longer working with vSphere 7 + #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}') jq -n "[]" | \ add_pullrequest_metadata_basic "$1" "$2" "$3" | \ - add_pullrequest_metadata_commit "source" "$source_commit" | \ - add_pullrequest_metadata_commit "target" "$target_commit" + add_pullrequest_metadata_commit "source" "$4" | \ + add_pullrequest_metadata_commit "target" "$5" } configure_credentials() { diff --git a/assets/in b/assets/in index 869e5f5..9c7eb39 100755 --- a/assets/in +++ b/assets/in @@ -65,7 +65,13 @@ if test "$depth" -gt 0 2> /dev/null; then depthflag="--depth $depth" fi -branch="pull-requests/${prq_id}/merge" +# see comments in "check" resource +branch="pull-requests/${prq_id}" +if [ "$rebuild_when_target_changed" == "true" ]; then + branch="$branch/merge" +else + branch="$branch/from" +fi log "Cloning $uri in $destination" git clone $depthflag "$uri" "$destination" @@ -118,20 +124,6 @@ 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 - log "Unable to determine source commit from merge commit $ref. Please verify depth configuration." - exit 1 -fi - -if [ -z "$target_commit" ]; 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}" @@ -147,9 +139,26 @@ prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_id" if [ "$prq" != "NO_SUCH_PULL_REQUEST" ] && \ [ "$prq" != "ALREADY_MERGED" ] && \ [ "$prq" != "DECLINED" ]; then - branch=$(echo "$prq" | jq -r '.fromRef.displayId') + from_branch=$(echo "$prq" | jq -r '.fromRef.displayId') fi +# calculate source and target commit +source_commit=$(echo "$prq" | jq -r '.fromRef.latestCommit') +target_commit=$(echo "$prq" | jq -r '.toRef.latestCommit') +if [ -z "$source_commit" ]; then + log "Unable to determine source commit from merge commit $ref. Please verify depth configuration." + exit 1 +fi + +if [ -z "$target_commit" ]; then + log "Unable to determine target commit from merge commit $ref. Please verify depth configuration." + exit 1 +fi + +if [[ "$branch" == */from ]]; then + log "Merge in changes from $target_commit to pullrequest/*/from, to emulate pullrequest/*/merge" + git merge $target_commit +fi # expose configuration of pull request that can be used in container git config --add pullrequest.id $prq_id @@ -157,9 +166,9 @@ git config --add pullrequest.source $source_commit git config --add pullrequest.target $target_commit git config --add pullrequest.merge $ref git config --add pullrequest.date "$prq_date" -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" "$source_commit" "$target_commit" ) }" >&3 diff --git a/assets/out b/assets/out index d8db180..f9b67c7 100755 --- a/assets/out +++ b/assets/out @@ -123,10 +123,6 @@ case "$status" in exit 1 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}') - # determine repository name for calling REST api repo_name=$(basename "$uri" | sed "s/.git$//") repo_project=$(basename $(dirname "$uri")) @@ -135,6 +131,12 @@ repo_project=$(basename $(dirname "$uri")) uri_parser "$uri" repo_host="${uri_schema}://${uri_address}"$(getBasePathOfBitbucket) +# calculate source and target commit +prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification") +source_commit=$(echo "$prq" | jq -r '.fromRef.latestCommit') +target_commit=$(echo "$prq" | jq -r '.toRef.latestCommit') + + # include ATC_EXTERNAL_URL in build status key, different sources should have different builds build_key="$BUILD_TEAM_NAME-$BUILD_PIPELINE_NAME-$BUILD_JOB_NAME-$ATC_EXTERNAL_URL" build_name="Concourse $BUILD_TEAM_NAME: $BUILD_PIPELINE_NAME - $BUILD_JOB_NAME - #$BUILD_NAME" @@ -189,5 +191,5 @@ jq -n "{ hash: \"$prq_hash\", date: \"$PULL_REQUEST_DATE\" }, - metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification") + metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification" "$source_commit" "$target_commit" ) }" >&3 diff --git a/scripts/install_git_lfs.sh b/scripts/install_git_lfs.sh index ea3998e..ff6e22b 100755 --- a/scripts/install_git_lfs.sh +++ b/scripts/install_git_lfs.sh @@ -1,4 +1,5 @@ #!/bin/sh +## Why is this needed instead of inistalling from apk? set -eu