From d8941d249bc71a5315d167ab6749d45bf1b414dc Mon Sep 17 00:00:00 2001 From: Thomas Detoux Date: Tue, 17 Apr 2018 16:31:19 -0400 Subject: [PATCH 1/9] Add the original branch name as a git config value to be used later in the pipeline --- README.md | 2 ++ assets/in | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 592fd6b..babdfdb 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,8 @@ It will allow you to build all versions instead of only the latest. Submodules are initialized and updated recursively. +Note: the name of the branch from which the pull request has been created is stored in the special git config `pullrequest.branch` so that you can use it as reference in your pipeline. + #### Parameters * `depth`: *Optional.* If a positive integer is given, *shallow* clone the repository using the `--depth` option. Using this flag voids your warranty. diff --git a/assets/in b/assets/in index 4e21009..1e4968d 100755 --- a/assets/in +++ b/assets/in @@ -132,12 +132,30 @@ if [ -z "$target_commit" ]; then 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') +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.merge $ref git config --add pullrequest.date "$prq_date" +git config --add pullrequest.branch "$branch" jq -n "{ version: $(jq '.version' < "$payload"), From 890dbf72d409126e595a1f9d0372a678578e27db Mon Sep 17 00:00:00 2001 From: Thomas Detoux Date: Wed, 25 Apr 2018 10:55:43 -0400 Subject: [PATCH 2/9] Add a custom comment after the comment made by the resource --- README.md | 7 +++++++ assets/helpers/bitbucket.sh | 13 ++++++++++--- assets/out | 17 +++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index babdfdb..a4daa35 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,13 @@ Set the status message on specified pull request. * [`on_success`](https://concourse.ci/on-success-step.html) and [`on_failure`](https://concourse.ci/on-failure-step.html) triggers may be useful for you when you wanted to reflect build result to the pull request (see the example below). +* `comment`: *Optional.* A custom comment that you want added to the status message. + Any occurence of `[[BRANCH]]` will be replace by the actual branch name form the + pull request. + +* `commentFile`: *Optional.* The path to a file that contains a custom comment to + add to the message. This allow the comment to be built by a previous task in the job. + ## Example pipeline ```yaml diff --git a/assets/helpers/bitbucket.sh b/assets/helpers/bitbucket.sh index 3f710e7..3a3cfd4 100755 --- a/assets/helpers/bitbucket.sh +++ b/assets/helpers/bitbucket.sh @@ -158,26 +158,33 @@ bitbucket_pullrequest_progress_comment() { # $2: hash of merge commit # $3: hash of source commit # $4: hash of target commit + # $5: custom comment local hash="$2" local progress_msg_end="" + local custom_comment="" + if [ "$hash" == "$3" ]; then progress_msg_end+=" into $4]" else progress_msg_end="] $3 into $4" fi + if [ -n "$5" ]; then + custom_comment="\n\n$5" + fi + local build_url="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")/builds/$(rawurlencode "$BUILD_NAME")" local build_result_pre=" \n\n **[" local build_result_post="]($build_url)** - Build #$BUILD_NAME" case "$1" in success) - echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}" ;; + echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}${custom_comment}" ;; failure) - echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}" ;; + echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}${custom_comment}" ;; pending) - echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}" ;; + echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}${custom_comment}" ;; esac } diff --git a/assets/out b/assets/out index 864ce29..8deae31 100755 --- a/assets/out +++ b/assets/out @@ -41,6 +41,8 @@ rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payloa path=$(jq -r '.params.path // ""' < "$payload") status=$(jq -r '.params.status // ""' < "$payload") +additionnal_comment=$(jq -r '.params.comment // ""' < "$payload") +additionnal_comment_file=$(jq -r '.params.commentFile // ""' < "$payload") configure_git_ssl_verification "$skip_ssl_verification" configure_git_global "${git_config_payload}" @@ -61,14 +63,20 @@ if [ -z "$status" ]; then fi cd "$source" + +if [ -n "$additionnal_comment_file" ]; then + additionnal_comment="$(<${additionnal_comment_file})" +fi + cd "$path" merge_commit=$(git rev-parse HEAD) ls_remote=$(git ls-remote "$uri") -# collect prq id from git config stored in git config (during get step) +# collect prq id and branch name from git config stored in git config (during get step) # included cat to catch error prq_number=$(git config --get pullrequest.id | cat) +branch=$(git config --get pullrequest.branch | cat) if [ -z "$prq_number" ]; then prqs=$(echo "$ls_remote" | grep -E "/pull\-requests/[0-9]+" | grep "$merge_commit" | cat) @@ -143,8 +151,13 @@ bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data" "" "" # use the pullrequest date stored in git config in get prq_verify_date=$(git config --get pullrequest.date | cat) +# Add branch name to additional comment +if [ -n "$additionnal_comment" ]; then + additionnal_comment="${additionnal_comment//\[\[BRANCH\]\]/$branch}" +fi + # add comment to pull request to track if build was started/finished -comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit") +comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit", "$additionnal_comment") comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]') commented="" skip_verify=false From 4ce297c3e2f1669511d61cca6299713ce9d79e07 Mon Sep 17 00:00:00 2001 From: laurentverbruggen Date: Sun, 27 May 2018 05:43:09 +0200 Subject: [PATCH 3/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4daa35..5cabf79 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -> This resource is no longer actively maintained. +> This resource is no longer actively maintained. Please refer to a maintained fork here: https://github.com/mmb/concourse-bitbucket-pullrequest-resource. Any pull requests here will not be merged. Instead do them on the forementioned fork. # Concourse Bitbucket Pull Request Resource From 9887424de8eca723c0ffa6372fcad0c93eb9f27d Mon Sep 17 00:00:00 2001 From: laurentverbruggen Date: Sun, 27 May 2018 05:44:48 +0200 Subject: [PATCH 4/9] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cabf79..d9189cc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -> This resource is no longer actively maintained. Please refer to a maintained fork here: https://github.com/mmb/concourse-bitbucket-pullrequest-resource. Any pull requests here will not be merged. Instead do them on the forementioned fork. +> This resource is no longer actively maintained. Please refer to a maintained fork here: https://github.com/mmb/concourse-bitbucket-pullrequest-resource.
+> Any pull requests should be done on the forementioned fork. They won't be merged here! # Concourse Bitbucket Pull Request Resource From 7f1a78867e70067ce46e5a4d78174aaaa21a5dd4 Mon Sep 17 00:00:00 2001 From: Gijs Boer Date: Mon, 30 Jul 2018 09:45:26 +0200 Subject: [PATCH 5/9] Add paths parameter --- README.md | 2 ++ assets/check | 6 +++++ assets/helpers/bitbucket.sh | 54 ++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02fe96b..e6118f4 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ If a match is found the pull request will be rebuilt. * `create_comments`: *Optional (default: false).* If true write comments with build status to pull requests. +* `paths`: *Optional (default "[]" - matches all).* If specified (as a list of regular expression patterns), only changes to the specified files will be built. + ## Behavior ### `check`: Search for pull requests to build. diff --git a/assets/check b/assets/check index b191f97..fac7223 100755 --- a/assets/check +++ b/assets/check @@ -40,6 +40,7 @@ only_when_asked=$(jq -r '.source.only_when_asked // "false"' < "$payload") rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload") rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload") CURRENT_VERSION_DATE=$(jq -r '.version.date // "0"' < "$payload") +paths=$(jq -r '.source.paths // []' < "${payload}") configure_git_ssl_verification "$skip_ssl_verification" configure_git_global "${git_config_payload}" @@ -137,6 +138,11 @@ if [ -n "$PULL_REQUESTS" ]; then continue fi + has_changes_in_specified_paths=$(does_pullrequest_include_changes_in_paths "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$paths" "" "$skip_ssl_verification") + if [ "${has_changes_in_specified_paths}" != "true" ]; then + continue + fi + # add prq to versions if [ "$skip_build" == "false" ]; then versions+=" + [{ id: \"$prq_number\", hash: \"$prq_hash\", date: \"$PULL_REQUEST_DATE\" }]" diff --git a/assets/helpers/bitbucket.sh b/assets/helpers/bitbucket.sh index e97f84a..b79a67f 100755 --- a/assets/helpers/bitbucket.sh +++ b/assets/helpers/bitbucket.sh @@ -82,7 +82,7 @@ bitbucket_request() { printf "DECLINED" return else - log "Bitbucket request ($request_url) failed: $(cat $request_result)" + log "Bitbucket request ($request_url) failed: $(cat ${request_result})" exit 1 fi @@ -229,3 +229,55 @@ bitbucket_pullrequest_update_comment_status() { log "Updating pull request comment (id: $6) for status on #$4 for $2/$3" bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments/$6" "" "{\"text\": \"$5\", \"version\": \"$7\" }" "" "$9" "$8" "PUT" } + +bitbucket_pullrequest_changes() { + # $1: host + # $2: project + # $3: repository id + # $4: pullrequest id + # $5: netrc file (default: $HOME/.netrc) + # $6: skip ssl verification + + log "Retrieving pull request changes #$4 for $2/$3" + bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/changes" "" "" "" "$6" "$5" +} + +does_pullrequest_include_changes_in_paths() { + # $1: host + # $2: project + # $3: repository id + # $4: pullrequest id + # $5: paths + # $6: netrc file (default: $HOME/.netrc) + # $7: skip ssl verification + + local paths=$5 + + if [ -z "${paths}" -o "${paths}" == "[]" ]; then + echo "true" + + return + fi + + set -e -o pipefail + local pull_request_changes + local changed_files + local changed_files_that_match_paths + + pull_request_changes=$(bitbucket_pullrequest_changes "$1" "$2" "$3" "$4" "$6" "$7") + changed_files=$(echo "${pull_request_changes}" | jq -r ".[] | .path.toString") + + for path in $(echo ${paths} | jq -r ".[]"); do + set +e + changed_files_that_match_paths=$(echo "${changed_files}" | grep "${path}") + set -e + + if [ -n "${changed_files_that_match_paths}" ]; then + echo "true" + + return + fi + done + + echo "false" +} From d69e08371442839279220c7a2f315d368a44d970 Mon Sep 17 00:00:00 2001 From: Gijs Boer Date: Mon, 30 Jul 2018 09:45:37 +0200 Subject: [PATCH 6/9] Fix package versions --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4a37cc4..34abd37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,8 @@ FROM alpine:3.7 RUN apk --no-cache add \ bash=4.4.19-r1 \ ca-certificates=20171114-r0 \ - curl=7.60.0-r1 \ - git=2.15.0-r1 \ + curl=7.61.0-r0 \ + git=2.15.2-r0 \ jq=1.5-r5 \ openssh-client=7.5_p1-r8 From c768a488db4f722d83f94a5e5f82e945a53a70cd Mon Sep 17 00:00:00 2001 From: Gijs Boer Date: Mon, 30 Jul 2018 09:48:09 +0200 Subject: [PATCH 7/9] Remove merge remnants --- README.md | 7 ------- assets/out | 5 ----- 2 files changed, 12 deletions(-) diff --git a/README.md b/README.md index e6118f4..0683d55 100644 --- a/README.md +++ b/README.md @@ -131,13 +131,6 @@ Set the status message on specified pull request. * `commentFile`: *Optional.* The path to a file that contains a custom comment to add to the message. This allow the comment to be built by a previous task in the job. -* `comment`: *Optional.* A custom comment that you want added to the status message. - Any occurence of `[[BRANCH]]` will be replace by the actual branch name form the - pull request. - -* `commentFile`: *Optional.* The path to a file that contains a custom comment to - add to the message. This allow the comment to be built by a previous task in the job. - ## Example pipeline ```yaml diff --git a/assets/out b/assets/out index d59244c..d8db180 100755 --- a/assets/out +++ b/assets/out @@ -160,11 +160,6 @@ if [ -n "$additionnal_comment" ]; then additionnal_comment="${additionnal_comment//\[\[BRANCH\]\]/$branch}" fi -# Add branch name to additional comment -if [ -n "$additionnal_comment" ]; then - additionnal_comment="${additionnal_comment//\[\[BRANCH\]\]/$branch}" -fi - # add comment to pull request to track if build was started/finished comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit", "$additionnal_comment") comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]') From 856549f85b2abdb4740f1e10b09b2bbd36107edd Mon Sep 17 00:00:00 2001 From: Gijs Boer Date: Thu, 2 Aug 2018 23:51:41 +0200 Subject: [PATCH 8/9] Don't pass in JSON contents as arguments, since large PRs will exceed argument length of the shell --- assets/helpers/bitbucket.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assets/helpers/bitbucket.sh b/assets/helpers/bitbucket.sh index b79a67f..be43b20 100755 --- a/assets/helpers/bitbucket.sh +++ b/assets/helpers/bitbucket.sh @@ -66,8 +66,9 @@ bitbucket_request() { if [ "$(jq -r '.isLastPage' < "$request_result")" == "false" ]; then local nextPage=$(jq -r '.nextPageStart' < "$request_result") - local nextResult=$(bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "start=${nextPage}&limit=${VALUES_LIMIT}") - jq -c '.values' < "$request_result" | jq -c ". + $nextResult" + bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "start=${nextPage}&limit=${VALUES_LIMIT}" > tmp.json + + jq -s -c '.[0].values + .[1]' "$request_result" "tmp.json" elif [ "$(jq -c '.values' < "$request_result")" != "null" ]; then jq -c '.values' < "$request_result" elif [ "$(jq -c '.errors' < "$request_result")" == "null" ]; then From 54eca50558cbbdf88efcb57cd55572fc90972a0d Mon Sep 17 00:00:00 2001 From: Gijs Boer Date: Thu, 2 Aug 2018 23:53:56 +0200 Subject: [PATCH 9/9] Add ignore_paths - to not trigger builds when files that are specified are changed --- README.md | 2 ++ assets/check | 3 ++- assets/helpers/bitbucket.sh | 52 +++++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0683d55..d09ad05 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ If a match is found the pull request will be rebuilt. * `paths`: *Optional (default "[]" - matches all).* If specified (as a list of regular expression patterns), only changes to the specified files will be built. +* `ignore_paths`: *Optional (default "[]" - matches nothing).* If specified (as a list of regular expression patterns), changes to the specified files will not be built, unless other paths have also been changed. + ## Behavior ### `check`: Search for pull requests to build. diff --git a/assets/check b/assets/check index fac7223..1441576 100755 --- a/assets/check +++ b/assets/check @@ -41,6 +41,7 @@ rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "fal rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload") CURRENT_VERSION_DATE=$(jq -r '.version.date // "0"' < "$payload") paths=$(jq -r '.source.paths // []' < "${payload}") +ignore_paths=$(jq -r '.source.ignore_paths // []' < "${payload}") configure_git_ssl_verification "$skip_ssl_verification" configure_git_global "${git_config_payload}" @@ -138,7 +139,7 @@ if [ -n "$PULL_REQUESTS" ]; then continue fi - has_changes_in_specified_paths=$(does_pullrequest_include_changes_in_paths "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$paths" "" "$skip_ssl_verification") + has_changes_in_specified_paths=$(does_pullrequest_include_changes_in_paths "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$paths" "$ignore_paths" "" "$skip_ssl_verification") if [ "${has_changes_in_specified_paths}" != "true" ]; then continue fi diff --git a/assets/helpers/bitbucket.sh b/assets/helpers/bitbucket.sh index be43b20..5d7dbf3 100755 --- a/assets/helpers/bitbucket.sh +++ b/assets/helpers/bitbucket.sh @@ -249,36 +249,66 @@ does_pullrequest_include_changes_in_paths() { # $3: repository id # $4: pullrequest id # $5: paths - # $6: netrc file (default: $HOME/.netrc) - # $7: skip ssl verification + # $6: ignore paths + # $7: netrc file (default: $HOME/.netrc) + # $8: skip ssl verification local paths=$5 + local ignore_paths=$6 - if [ -z "${paths}" -o "${paths}" == "[]" ]; then + if [ -z "${paths}" -o "${paths}" == "[]" ] && \ + [ -z "${ignore_paths}" -o "${ignore_paths}" == "[]" ]; then echo "true" return fi + if [ -z "${paths}" -o "${paths}" == "[]" ]; then + paths="[\".*\"]" + fi + + if [ -z "${ignore_paths}" ]; then + ignore_paths="[]" + fi + set -e -o pipefail local pull_request_changes local changed_files local changed_files_that_match_paths + local grep_arguments_for_paths - pull_request_changes=$(bitbucket_pullrequest_changes "$1" "$2" "$3" "$4" "$6" "$7") + pull_request_changes=$(bitbucket_pullrequest_changes "$1" "$2" "$3" "$4" "$7" "$8") changed_files=$(echo "${pull_request_changes}" | jq -r ".[] | .path.toString") + grep_arguments_for_paths=$(get_grep_arguments_for_list "${paths}") - for path in $(echo ${paths} | jq -r ".[]"); do + set +e + changed_files_that_match_paths="$(echo "${changed_files}" | grep ${grep_arguments_for_paths})" + set -e + + local grep_arguments_for_ignore_paths + grep_arguments_for_ignore_paths=$(get_grep_arguments_for_list "${ignore_paths}") + + if [ -n "${grep_arguments_for_ignore_paths}" ]; then set +e - changed_files_that_match_paths=$(echo "${changed_files}" | grep "${path}") + changed_files_that_match_ignore_paths=$(echo "${changed_files_that_match_paths}" | grep -v ${grep_arguments_for_ignore_paths}) set -e + else + changed_files_that_match_ignore_paths="${changed_files_that_match_paths}" + fi + + if [ -n "${changed_files_that_match_ignore_paths}" ]; then + echo "true" + else + echo "false" + fi +} - if [ -n "${changed_files_that_match_paths}" ]; then - echo "true" +get_grep_arguments_for_list() { + grep_arguments_for_ignore_paths="" - return - fi + for element in $(echo $1 | jq -r ".[]"); do + grep_arguments_for_ignore_paths="${grep_arguments_for_ignore_paths} -e ${element}" done - echo "false" + echo "${grep_arguments_for_ignore_paths}" }