diff --git a/scripts/check b/scripts/check index 09484de..4b2c705 100755 --- a/scripts/check +++ b/scripts/check @@ -20,6 +20,7 @@ uri="$(jq -r '.source.uri // ""' < "${payload}")" private_token="$(jq -r '.source.private_token // ""' < "${payload}")" no_ssl="$(jq -r '.source.no_ssl // ""' < "${payload}")" version_sha="$(jq -r '.version.sha // ""' < "${payload}")" +version_project_id="$(jq -r '.version.project_id // ""' < "${payload}")" if [[ "${uri}" == *"git@"* ]]; then gitlab_host="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\1/p')" @@ -50,12 +51,13 @@ new_versions='' for i in $(seq 0 $((num_mrs - 1))); do mr="$(echo "${open_mrs}" | jq -r '.['"$i"']')" + mr_project_id="$(echo "${mr}" | jq -r '.source_project_id')" 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}\"}" + new_versions="${new_versions},{\"sha\":\"${mr_sha}\",\"project_id\":\"${mr_project_id}\"}" fi fi done @@ -63,8 +65,8 @@ 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}\"}]" +if [ "${new_versions}" == '[]' ] && ! [ -z "${version_sha}" ] && ! [ -z "${version_project_id}" ]; then + new_versions="[{\"sha\":\"${version_sha}\",\"project_id\":\"${version_project_id}\"}]" fi jq -n "${new_versions}" >&3 diff --git a/scripts/in b/scripts/in index 8fe0fd6..999b311 100755 --- a/scripts/in +++ b/scripts/in @@ -27,8 +27,11 @@ uri="$(jq -r '.source.uri // ""' < "${payload}")" username="$(jq -r '.source.username // ""' < "${payload}")" password="$(jq -r '.source.password // ""' < "${payload}")" private_key="$(jq -r '.source.private_key // ""' < "${payload}")" +private_token="$(jq -r '.source.private_token // ""' < "${payload}")" +no_ssl="$(jq -r '.source.no_ssl // ""' < "${payload}")" version="$(jq -r '.version // ""' < "${payload}")" commit_sha="$(echo "${version}" | jq -r '.sha // ""')" +commit_project_id="$(echo "${version}" | jq -r '.project_id // ""')" if [[ ! -z "${private_key}" ]]; then gitlab_host="$(echo "${uri}" | sed -rn 's/.*git@(.*):([0-9]*\/+)?(.*)\.git/\1/p')" @@ -49,7 +52,26 @@ else echo "default login ${username} password ${password}" > "${HOME}/.netrc" # Save credentials for git push below fi -git clone "${uri}" "${destination}" +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' +fi + +ssh_project_uri="$(curl -s -H "private-token: ${private_token}" "${protocol}://${gitlab_host}/api/v4/projects/${commit_project_id}" \ + | jq -r '.ssh_url_to_repo')" + +git clone "${ssh_project_uri}" "${destination}" cd "${destination}" diff --git a/scripts/out b/scripts/out index 227ec5f..efc90ee 100755 --- a/scripts/out +++ b/scripts/out @@ -65,14 +65,27 @@ cd "${path_to_repo}" commit_sha="$(git rev-parse HEAD)" +# Find project id of merge request +version_project_id=-1 +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')" +for i in $(seq 0 $((num_mrs - 1))); do + mr="$(echo "${open_mrs}" | jq -r '.['"$i"']')" + mr_project_id="$(echo "${mr}" | jq -r '.source_project_id')" + mr_sha="$(echo "${mr}" | jq -r '.sha')" + if [ "${mr_sha}" == "${commit_sha}" ]; then + version_project_id="$mr_project_id" + fi +done + curl \ --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}" + "${protocol}://${gitlab_host}/api/v4/projects/${version_project_id}/statuses/${commit_sha}" -version="{\"sha\":\"${commit_sha}\"}" +version="{\"sha\":\"${commit_sha}\",\"project_id\":\"${version_project_id}\"}" jq -n "{ version: ${version},