Skip to content
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

Update the Bold Commerce Gitlab MR resource with changes from forked repo #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ resources:
* `private_token`: Your GitLab user's private token (required, can be found in your profile settings)
* `private_key`: The private SSH key for SSH auth when pulling

Example:
Example:

```yaml
private_key: |
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
<Lots more text>
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
-----END RSA PRIVATE KEY-----
```
```yaml
private_key: |
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
<Lots more text>
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
-----END RSA PRIVATE KEY-----
```

* `username`: The username for HTTP(S) auth when pulling
* `password`: The password for HTTP(S) auth when pulling
Expand All @@ -61,6 +61,7 @@ Updates the merge request's `merge_status` which displays nicely in the GitLab U

* `repository`: The path of the repository of the merge request's source branch (required)
* `status`: The new status of the merge request (required, can be either `pending`, `running`, `success`, `failed`, or `canceled`)
* `build_label`: The label of the build in GitLab (optional, defaults to `"Concourse"`)

## Example

Expand Down
42 changes: 24 additions & 18 deletions scripts/check
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,50 @@ private_token="$(jq -r '.source.private_token // ""' < "${payload}")"
no_ssl="$(jq -r '.source.no_ssl // ""' < "${payload}")"
version_sha="$(jq -r '.version.sha // ""' < "${payload}")"

gitlab_host="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\2/p')"
project_path="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\3/p')"
project_path_encoded="${project_path//\//%2F}" # url encode "/"
project_path_encoded="${project_path_encoded//./%2E}" # url encode "."
protocol="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\1/p')"
if [[ "${uri}" == *"git@"* ]]; then
gitlab_host="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\1/p')"
port="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\2/p')"
port=${port///} # remove trailing slash
project_path="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\3/p')"
protocol='https'
else
gitlab_host="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\2/p')"
project_path="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\3/p')"
protocol="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\1/p')"
fi

if [ "${no_ssl}" == 'true' ]; then
protocol='http'
protocol='http'
fi

version_updated_at=0
if [ ! -z "${version_sha}" ]; then
version_updated_at=`curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v3/projects/${project_path_encoded}/repository/commits/${version_sha}" \
| jq '.committed_date|.[:19]|strptime("%Y-%m-%dT%H:%M:%S")|mktime'`
version_updated_at="$(curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v4/projects/$(urlencode "${project_path}")/repository/commits/${version_sha}" \
| jq '.committed_date|.[:19]|strptime("%Y-%m-%dT%H:%M:%S")|mktime')"
fi

open_mrs="$(curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v3/projects/${project_path_encoded}/merge_requests?state=opened&order_by=updated_at")"
open_mrs="$(curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v4/projects/$(urlencode "${project_path}")/merge_requests?state=opened&order_by=updated_at")"
num_mrs="$(echo "${open_mrs}" | jq 'length')"

new_versions=''

for i in $(seq 0 $((num_mrs - 1))); do
mr="$(echo "${open_mrs}" | jq -r '.['"$i"']')"
mr_sha="$(echo "${mr}" | jq -r '.sha')"
if [ "${mr_sha}" != "null" ]; then
mr_updated_at=`curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v3/projects/${project_path_encoded}/repository/commits/${mr_sha}" \
| jq '.committed_date|.[:19]|strptime("%Y-%m-%dT%H:%M:%S")|mktime'`
if [ "${mr_updated_at}" -gt "${version_updated_at}" ] || [ -z "${version_sha}" ]; then
new_versions="${new_versions},{\"sha\":\"${mr_sha}\"}"
mr="$(echo "${open_mrs}" | jq -r '.['"$i"']')"
mr_sha="$(echo "${mr}" | jq -r '.sha')"
if [ "${mr_sha}" != "null" ]; then
mr_updated_at="$(curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v4/projects/$(urlencode "${project_path}")/repository/commits/${mr_sha}" \
| jq '.committed_date|.[:19]|strptime("%Y-%m-%dT%H:%M:%S")|mktime')"
if [ "${mr_updated_at}" -gt "${version_updated_at}" ] || [ -z "${version_sha}" ]; then
new_versions="${new_versions},{\"sha\":\"${mr_sha}\"}"
fi
fi
fi
done

new_versions="${new_versions#','}" # remove trailing comma
new_versions="[${new_versions}]" # make JSON array

if [ "${new_versions}" == '[]' ] && ! [ -z "${version_sha}" ]; then
new_versions="[{\"sha\":\"${version_sha}\"}]"
new_versions="[{\"sha\":\"${version_sha}\"}]"
fi

jq -n "${new_versions}" >&3
4 changes: 2 additions & 2 deletions scripts/common.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ configure_ssl_verification() {
}

urlencode() {
echo -n "${1}" | jq -s -R -r @uri
}
echo -n "$1" | jq -s -R -r @uri
}
31 changes: 19 additions & 12 deletions scripts/in
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ source "$(dirname "$0")/common.sh"
destination="$1"

if [ -z "${destination}" ]; then
echo "Usage: $0 <path/to/destination>" >&2
exit 1
echo "Usage: $0 <path/to/destination>" >&2
exit 1
fi

payload="$(mktemp "${TMPDIR}/gitlab-merge-request-resource.XXXXXX")"
Expand All @@ -31,15 +31,22 @@ version="$(jq -r '.version // ""' < "${payload}")"
commit_sha="$(echo "${version}" | jq -r '.sha // ""')"

if [[ ! -z "${private_key}" ]]; then
gitlab_host="$(echo "${uri}" | sed -rn 's/git@(.*):(.*)\.git/\1/p')"

id_rsa="${HOME}/.ssh/id_rsa"
mkdir -p "${HOME}/.ssh/"
echo "${private_key}" > "${id_rsa}"
chmod 500 "${id_rsa}"
ssh-keyscan -t rsa "${gitlab_host}" > "${HOME}/.ssh/known_hosts"
gitlab_host="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\1/p')"
port="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\2/p')"
port=${port///} # remove trailing slash

id_rsa="${HOME}/.ssh/id_rsa"
mkdir -p "${HOME}/.ssh/"
echo "${private_key}" > "${id_rsa}"
chmod 500 "${id_rsa}"

if [[ ! -z "${port}" ]]; then
ssh-keyscan -t rsa -p "${port}" "${gitlab_host}" > "${HOME}/.ssh/known_hosts"
else
ssh-keyscan -t rsa "${gitlab_host}" > "${HOME}/.ssh/known_hosts"
fi
else
echo "default login ${username} password ${password}" > "${HOME}/.netrc" # Save credentials for git push below
echo "default login ${username} password ${password}" > "${HOME}/.netrc" # Save credentials for git push below
fi

git clone "${uri}" "${destination}"
Expand All @@ -49,6 +56,6 @@ cd "${destination}"
git reset --hard "${commit_sha}"

jq -n "{
version: ${version},
metadata: $(git_metadata)
version: ${version},
metadata: $(git_metadata)
}" >&3
46 changes: 23 additions & 23 deletions scripts/out
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ source "$(dirname "$0")/common.sh"
destination="$1"

if [ -z "${destination}" ]; then
echo "Usage: $0 <path/to/destination>" >&2
exit 1
echo "Usage: $0 <path/to/destination>" >&2
exit 1
fi

payload="$(mktemp "${TMPDIR}/gitlab-merge-request-resource.XXXXXX")"
Expand All @@ -30,51 +30,51 @@ no_ssl="$(jq -r '.source.no_ssl // ""' < "${payload}")"
params="$(jq -r '.params // ""' < "${payload}")"
path_to_repo="$(echo "${params}" | jq -r '.repository // ""')"
new_status="$(echo "${params}" | jq -r '.status // ""')"
build_label="$(echo "${params}" | jq -r '.build_label // "Concourse"')"

if [ -z "${path_to_repo}" ]; then
echo "please specify a repository" >&2
exit 1
echo "please specify a repository" >&2
exit 1
fi
if [ -z "${new_status}" ]; then
echo "please specify a status" >&2
exit 1
echo "please specify a status" >&2
exit 1
fi

gitlab_host=''
project_path=''
protocol='https'
if [[ ! -z "${private_key}" ]]; then
gitlab_host="$(echo "${uri}" | sed -rn 's/git@(.*):(.*)\.git/\1/p')"
project_path="$(echo "${uri}" | sed -rn 's/git@(.*):(.*)\.git/\2/p')"
gitlab_host="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\1/p')"
port="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\2/p')"
port=${port///} # remove trailing slash
project_path="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\3/p')"
else
gitlab_host="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\2/p')"
project_path="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\3/p')"
protocol="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\1/p')"
gitlab_host="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\2/p')"
project_path="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\3/p')"
protocol="$(echo "${uri}" | sed -rn 's/(https?):\/\/([^\/]*)\/(.*)\.git/\1/p')"
fi
if [ "${no_ssl}" == 'true' ]; then
protocol='http'
protocol='http'
fi

project_path_encoded="${project_path//\//%2F}" # url encode "/"
project_path_encoded="${project_path_encoded//./%2E}" # url encode "."

target_url="${ATC_EXTERNAL_URL}/teams/$(urlencode ${BUILD_TEAM_NAME})/pipelines/$(urlencode ${BUILD_PIPELINE_NAME})/jobs/$(urlencode ${BUILD_JOB_NAME})/builds/$(urlencode ${BUILD_NAME})"
target_url="${ATC_EXTERNAL_URL}/teams/$(urlencode "${BUILD_TEAM_NAME}")/pipelines/$(urlencode "${BUILD_PIPELINE_NAME}")/jobs/$(urlencode "${BUILD_JOB_NAME}")/builds/$(urlencode "${BUILD_NAME}")"

cd "${destination}"
cd "${path_to_repo}"

commit_sha="$(git rev-parse HEAD)"

curl \
--request POST \
--header "PRIVATE-TOKEN: ${private_token}" \
--header "Content-Type: application/json" \
--data "{\"state\":\"${new_status}\",\"name\":\"Concourse\",\"target_url\":\"${target_url}\"}" \
"${protocol}://${gitlab_host}/api/v3/projects/${project_path_encoded}/statuses/${commit_sha}"
--request POST \
--header "PRIVATE-TOKEN: ${private_token}" \
--header 'Content-Type: application/json' \
--data "{\"state\":\"${new_status}\",\"name\":\"${build_label}\",\"target_url\":\"${target_url}\"}" \
"${protocol}://${gitlab_host}/api/v4/projects/$(urlencode "${project_path}")/statuses/${commit_sha}"

version="{\"sha\":\"${commit_sha}\"}"

jq -n "{
version: ${version},
metadata: $(git_metadata)
version: ${version},
metadata: $(git_metadata)
}" >&3