From 8d7437a34b402fc8f7623d27a977e8960012e65e Mon Sep 17 00:00:00 2001 From: Vladimir Komissarov Date: Wed, 30 Jan 2019 17:53:52 +0700 Subject: [PATCH] add ability to push sources --- README.md | 18 ++++++++++------ assets/out | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c516553..3ee26db 100644 --- a/README.md +++ b/README.md @@ -96,15 +96,21 @@ commit on the branch. All `params` and `source` configuration of the original resource will be respected. -### `out`: Update build task status. +### `out`: Update the PR. -This updates the build status of the pull request commit. +Behavior depends on the value of parameter `action`, where is the values are: -#### Parameters +* `change-build-status`: Change the commit build status. -* Parameters except the `name` will be respected the [Bitbucket documentation](https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/). -* `name`: `Deprecated`. Parameter is deprecated and has been left only for backward compatibility. -* `repository`: `Optional`. The path of the source repository for pushing or changing build status. + * `action`: `Required`. For this behavior should be `change-build-status`. + * Parameters except the `name` will be respected the [Bitbucket documentation](https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/). + * `name`: `Deprecated`. Parameter is deprecated and has been left only for backward compatibility. + * `repository`: `Optional`. The path of the source repository for changing build status. + +* `push`: Push the commit to pull request branch. + + * `action`: `Required`. For this behavior should be `push`. + * `repository`: `Optional`. The path of the source repository for pushing. ## Troubleshooting diff --git a/assets/out b/assets/out index ae24f87..6bd506b 100755 --- a/assets/out +++ b/assets/out @@ -18,12 +18,12 @@ username=$(jq -r '.source.username // ""' < "${payload}") password=$(jq -r '.source.password // ""' < "${payload}") project=$(jq -r '.source.project // ""' < "${payload}") repository=$(jq -r '.source.repository // ""' < "${payload}") -dir=$(jq -r '.params.repository // .source.dir // .source.repository // ""' < "${payload}") +path=$(jq -r '.params.repository // .source.dir // .source.repository // ""' < "${payload}") params=$(jq -rc '.params' < "${payload}") +action=$(jq -rc '.action // ""' <<< "${params}") -cd "${dir}" - -pr=$(cat pull-request-info) +path="$(realpath "${PWD}"/"${path}")/" +pr=$(cat "${path}pull-request-info") eval_param() { eval echo "$(jq -r "${1}" <<< "${params}")" @@ -79,4 +79,55 @@ change_build_status() { }' >&3 } -change_build_status +push() { + git=$(jq -r '.source.git // ""' < "${payload}") + + git_payload=$(jq -nc \ + --argjson git "${git}" \ + --argjson pr "${pr}" \ + --arg repository "${path}" ' + { + source: ( + $git * { + branch: $pr.feature_branch + } + ), + params: { + repository: $repository + } + } + ') + + git_payload_out=$(/opt/git-resource/out "${1}" <<< "${git_payload}") + + jq -n --argjson pr "${pr}" --argjson git "${git_payload_out}" \ + '{ + version: { + id: $pr.id|tostring, + branch: $pr.feature_branch, + commit: $git.version.ref + }, + metadata: $git.metadata + }' >&3 +} + +case "${action}" in + # case for backward compatibility + "") + echo "warning: please setup the \`action\` parameter" + change_build_status + ;; + + "change-build-status") + change_build_status + ;; + + "push") + push "${@%/}" + ;; + + *) + echo "error: incorrect params.action (${action})" + exit 1 + ;; +esac