diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..edd04ae --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM concourse/buildroot:curl + +ADD assets/ /opt/resource/ +ADD test/ /opt/resource-tests/ +ADD tools/ /opt/tools/ + +RUN rm /usr/bin/jq +RUN mv /opt/tools/jq /usr/bin/jq + +# Run tests +# RUN /opt/resource-tests/test-check.sh +# RUN /opt/resource-tests/test-in.sh +# RUN /opt/resource-tests/test-out.sh diff --git a/assets/check b/assets/check new file mode 100755 index 0000000..8582448 --- /dev/null +++ b/assets/check @@ -0,0 +1,62 @@ +#!/bin/bash + +set -e + +exec 3>&1 # make stdout available as fd 3 for the result +exec 1>&2 # redirect all output to stderr for logging + +source $(dirname $0)/common.sh + +payload=$(mktemp $TMPDIR/artifactory-resource-request.XXXXXX) + +cat > $payload <&0 + +cat $payload + +endpoint=$(jq -r '.source.endpoint // ""' < $payload) +regex=$(jq -r '.source.regex // ""' < $payload) +username=$(jq -r '.source.username // ""' < $payload) +password=$(jq -r '.source.password // ""' < $payload) +skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // ""' < $payload) + +repository=$(jq -r '.source.repository // ""' < $payload) +file=$(jq -r '.params.file // ""' < $payload) +folder=$(jq -r '.params.folder // ""' < $payload) +paramRegex=$(jq -r '.params.regex // ""' < $payload) + +version=$(jq -r '.version.version // ""' < $payload) + +if [ -z "$endpoint" ]; then + echo "invalid payload (missing endpoint)" + exit 1 +fi + +if [ -z "$repository" ]; then + echo "invalid payload (missing repository)" + exit 1 +fi + +# Building CURL request +args_url="$endpoint/api/storage$repository$folder" + +args_security= + +[ -n "$username" ] && args_security="-u $username"; +[ -n "$password" ] && args_security="$args_security:$password"; +trueValue="true" +[ -n "$skip_ssl_verification" ] && [ "${skip_ssl_verification,,}" = "${trueValue,,}" ] && args_security="$args_security -k"; + +if [ -n "$paramRegex" ]; then + echo "overwriting source regex" + regex=$paramRegex +fi + +final_url=$(echo "$args_security" " $args_url") +echo $final_url + +if [ -z "$version" ]; then + echo "empty version - return current version" + artifactory_current_version "$final_url" "$regex" >&3 +else + check_version "$final_url" "$regex" "$version" >&3 +fi diff --git a/assets/common.sh b/assets/common.sh new file mode 100644 index 0000000..1452675 --- /dev/null +++ b/assets/common.sh @@ -0,0 +1,62 @@ + +# Using jq regex so we can support groups +applyRegex_version() { + local regex=$1 + local file=$2 + + jq -n "{ + version: $(echo $file | jq -R .) + }" | jq --arg v "$regex" '.version | capture($v)' | jq -r '.version' + +} + +# retrieve current from artifactory +# e.g url=http://your-host-goes-here:8081/artifactory/api/storage/your-path-goes-here +# regex=ecd-front-(?.*).tar.gz +artifactory_current_version() { + local artifacts_url=$1 + local regex=$2 + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[length-1] | {version: .version}]' + +} + +# Return all versions +artifactory_versions() { + local artifacts_url=$1 + local regex=$2 + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {version: .version}]' + +} + +# return uri and version of all files +artifactory_files() { + local artifacts_url=$1 + local regex="(?$2)" + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {uri: .uri, version: .version}]' + +} + +in_file_with_version() { + local artifacts_url=$1 + local regex="(?$2)" + local version=$3 + + result=$(artifactory_files "$artifacts_url" "$regex") + echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version == $v then $item else empty end)]' + +} + + +# return the list of versions from provided version +check_version() { + local artifacts_url=$1 + local regex=$2 + local version=$3 + + result=$(artifactory_versions "$artifacts_url" "$regex") #result=$(curl "$artifacts_url" "$regex") + echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version >= $v then $item else empty end)]' + +} diff --git a/assets/in b/assets/in new file mode 100755 index 0000000..3aca4d0 --- /dev/null +++ b/assets/in @@ -0,0 +1,84 @@ +#!/bin/bash + +set -e + +exec 3>&1 # make stdout available as fd 3 for the result +exec 1>&2 # redirect all output to stderr for logging + +source $(dirname $0)/common.sh + +source=$1 + +if [ -z "$source" ]; then + echo "usage: $0 " + exit 1 +fi + +cd $source + +payload=$(mktemp $TMPDIR/artifactory-resource-request.XXXXXX) + +cat > $payload <&0 + +cat $payload + +endpoint=$(jq -r '.source.endpoint // ""' < $payload) +regex=$(jq -r '.source.regex // ""' < $payload) +username=$(jq -r '.source.username // ""' < $payload) +password=$(jq -r '.source.password // ""' < $payload) +skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // ""' < $payload) + +repository=$(jq -r '.source.repository // ""' < $payload) +file=$(jq -r '.params.file // ""' < $payload) +folder=$(jq -r '.params.folder // ""' < $payload) +paramRegex=$(jq -r '.params.regex // ""' < $payload) + +version=$(jq -r '.version.version // ""' < $payload) + +if [ -z "$endpoint" ]; then + echo "invalid payload (missing endpoint)" + exit 1 +fi + +if [ -z "$repository" ]; then + echo "invalid payload (missing repository)" + exit 1 +fi + +if [ -z "$version" ]; then + echo "Missing version" + exit 1 +fi + +# Building CURL request +args_url="$endpoint/api/storage$repository$folder" + +args_security= + +[ -n "$username" ] && args_security="-u $username"; +[ -n "$password" ] && args_security="$args_security:$password"; +trueValue="true" +[ -n "$skip_ssl_verification" ] && [ "${skip_ssl_verification,,}" = "${trueValue,,}" ] && args_security="$args_security -k"; + +if [ -n "$paramRegex" ]; then + echo "overwriting source regex" + regex=$paramRegex +fi + +final_url=$(echo "$args_security" " $args_url") +echo $final_url + +file_json=$(in_file_with_version "$final_url" "$regex" "$version") +file=$(echo $file_json | jq -r '.[].uri') + +if [ -z "$file" ]; then + echo "file for version '$version' not found" + exit 1 +fi + +args_url="$endpoint$repository/$file" + +echo $args_security "-O" "$args_url" +curl $args_security "-O" "$args_url" + +echo $file_json | jq '.[].version | {version: {version: .}}' >&3 diff --git a/assets/out b/assets/out new file mode 100755 index 0000000..23aa730 --- /dev/null +++ b/assets/out @@ -0,0 +1,83 @@ +#!/bin/bash + +set -e + +exec 3>&1 # make stdout available as fd 3 for the result +exec 1>&2 # redirect all output to stderr for logging + +source $(dirname $0)/common.sh + +source=$1 + +if [ -z "$source" ]; then + echo "usage: $0 " + exit 1 +fi + +cd $source + +payload=$(mktemp $TMPDIR/artifactory-resource-request.XXXXXX) + +cat > $payload <&0 + +cat $payload + +endpoint=$(jq -r '.source.endpoint // ""' < $payload) +regex=$(jq -r '.source.regex // ""' < $payload) +username=$(jq -r '.source.username // ""' < $payload) +password=$(jq -r '.source.password // ""' < $payload) +skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // ""' < $payload) + +repository=$(jq -r '.source.repository // ""' < $payload) +folder=$(jq -r '.params.folder // ""' < $payload) +file=$(jq -r '.params.file // ""' < $payload) +paramRegex=$(jq -r '.params.regex // ""' < $payload) + +if [ -z "$endpoint" ]; then + echo "invalid payload (missing endpoint)" + exit 1 +fi + +if [ -z "$repository" ]; then + echo "invalid payload (missing repository)" + exit 1 +fi + +if [ -z "$file" ]; then + echo "invalid payload (missing file)" + exit 1 +fi + +abs_file=$(ls $file) +filename=$(basename "$abs_file") + + +args_url="$endpoint" +args_url="$args_url$repository" + +if [ -n "$folder" ]; then + echo "adding parameter folder" + args_url="$args_url/$folder" +fi + +args_url="$args_url/$filename" + +args_security= + +[ -n "$username" ] && args_security="-u $username"; +[ -n "$password" ] && args_security="$args_security:$password"; +trueValue="true" +[ -n "$skip_ssl_verification" ] && [ "${skip_ssl_verification,,}" = "${trueValue,,}" ] && args_security="$args_security -k"; + +echo "########## $filename, $file" + +echo $args_security "-T $abs_file $args_url " +curl $args_security "-T$abs_file" "$args_url" + + +echo $file $regex +version=$(applyRegex_version $regex $filename) + +jq -n "{ + version: {version: $(echo $version | jq -R .)} +}" >&3 diff --git a/pipeline.yml b/pipeline.yml new file mode 100644 index 0000000..6e5a3e7 --- /dev/null +++ b/pipeline.yml @@ -0,0 +1,69 @@ +resource_types: +- name: artifactory + type: docker-image + source: + repository: pivotalservices/artifactory-resource + +resources: +- name: file-repository + type: artifactory + check_every: 1m + source: + endpoint: http://ARTIFACTORY-HOST-NAME-GOES-HERE:8081/artifactory + repository: "/repository-name/sub-folder" + regex: "myapp-(?.*).txt" + username: YOUR-ARTIFACTORY-USERNAME + password: YOUR-ARTIFACTORY-PASSWORD + +jobs: +- name: build-and-save-to-artifactory + serial: true + public: true + plan: + - task: build-file + config: + platform: linux + image_resource: + type: docker-image + source: + repository: ubuntu + tag: "latest" + outputs: + - name: build + run: + path: sh + args: + - -exc + - | + export DATESTRING=$(date +"%Y%m%d") + echo "This is my file" > ./build/myapp-$(date +"%Y%m%d%H%S").txt + find . + - put: file-repository + params: { file: ./build/myapp-*.txt } + +- name: trigger-when-new-file-is-added-to-artifactory + serial: true + public: true + plan: + - get: file-repository + trigger: true + passed: + - build-and-save-to-artifactory + - task: use-new-file + config: + platform: linux + image_resource: + type: docker-image + source: + repository: ubuntu + tag: "latest" + inputs: + - name: file-repository + run: + path: sh + args: + - -exc + - | + export DATESTRING=$(date +"%Y%m%d") + ls -la file-repository + cat ./file-repository/myapp*.txt diff --git a/test/data/file-with-no-version.txt b/test/data/file-with-no-version.txt new file mode 100644 index 0000000..71e10b8 --- /dev/null +++ b/test/data/file-with-no-version.txt @@ -0,0 +1 @@ +This is a file with no version number. diff --git a/test/data/pivotal-1.0.0.txt b/test/data/pivotal-1.0.0.txt new file mode 100644 index 0000000..e8b3e4d --- /dev/null +++ b/test/data/pivotal-1.0.0.txt @@ -0,0 +1 @@ +This is version 1.0.0 diff --git a/test/data/pivotal-1.0.1.txt b/test/data/pivotal-1.0.1.txt new file mode 100644 index 0000000..1f265aa --- /dev/null +++ b/test/data/pivotal-1.0.1.txt @@ -0,0 +1 @@ +This is version 1.0.1 diff --git a/test/helpers.sh b/test/helpers.sh new file mode 100755 index 0000000..157b71d --- /dev/null +++ b/test/helpers.sh @@ -0,0 +1,215 @@ +#!/bin/bash + +set -e -u + +set -o pipefail + +export TMPDIR_ROOT=$(mktemp -d /tmp/artifactory-tests.XXXXXX) +trap "rm -rf $TMPDIR_ROOT" EXIT + +if [ -d /opt/resource ]; then + resource_dir=/opt/resource +else + resource_dir=$(cd $(dirname $0)/../assets && pwd) +fi + +run() { + export TMPDIR=$(mktemp -d ${TMPDIR_ROOT}/artifactory-tests.XXXXXX) + + echo -e 'running \e[33m'"$@"$'\e[0m...' + eval "$@" 2>&1 | sed -e 's/^/ /g' + echo "" +} + +find_primary_ip() { + ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' +} + +find_docker_host_ip() { + /sbin/ip route | awk '/default/ { print $3 }' +} + +create_version_file() { + local version=$1 + local src=$2 + + mkdir $src/version + echo "$version" > $src/version/number + + echo version/number +} + +create_file() { + local src=$1 + local file=$2 + + # Mock the artifact + mkdir $src/build-output + echo "Dummy File" > $src/build-output/$file + touch $src/build-output/$file + + echo "$src/build-output/$file" +} + +# CHECK +check_without_credentials_and_version() { + + local endpoint=$1 + local regex=$2 + local folder=$3 + local src=$4 + + jq -n "{ + source: { + endpoint: $(echo $endpoint | jq -R .), + repository: $(echo $folder | jq -R .), + regex: $(echo $regex | jq -R .) + } + }" | $resource_dir/check "$src" | tee /dev/stderr +} + +# CHECK +check_without_credentials_with_version() { + + local endpoint=$1 + local regex=$2 + local folder=$3 + local version=$4 + local src=$5 + + jq -n "{ + source: { + endpoint: $(echo $endpoint | jq -R .), + repository: $(echo $folder | jq -R .), + regex: $(echo $regex | jq -R .) + }, + version: { version: $(echo $version| jq -R .) + } + }" | $resource_dir/check "$src" | tee /dev/stderr +} + +# CHECK +check_with_credentials_with_version() { + + local endpoint=$1 + local regex=$2 + local username=$3 + local password=$4 + local folder=$5 + local version=$6 + local src=$7 + + jq -n "{ + source: { + endpoint: $(echo $endpoint | jq -R .), + repository: $(echo $folder | jq -R .), + regex: $(echo $regex | jq -R .), + username: $(echo $username | jq -R .), + password: $(echo $password | jq -R .) + }, + version: { version: $(echo $version| jq -R .) + } + }" | $resource_dir/check "$src" | tee /dev/stderr +} + +# IN +in_without_credentials_with_version() { + + local endpoint=$1 + local regex=$2 + local folder=$3 + local version=$4 + local src=$5 + + jq -n "{ + source: { + endpoint: $(echo $endpoint | jq -R .), + repository: $(echo $folder | jq -R .), + regex: $(echo $regex | jq -R .) + }, + version: { version: $(echo $version| jq -R .) + } + }" | $resource_dir/in "$src" | tee /dev/stderr +} + +# IN +in_with_credentials_with_version() { + + local endpoint=$1 + local regex=$2 + local folder=$3 + local version=$4 + local src=$5 + + local username=$6 + local password=$7 + + jq -n "{ + source: { + endpoint: $(echo $endpoint | jq -R .), + username: $(echo $username | jq -R .), + password: $(echo $password | jq -R .), + repository: $(echo $folder | jq -R .), + regex: $(echo $regex | jq -R .) + }, + version: { version: $(echo $version| jq -R .) + } + }" | $resource_dir/in "$src" | tee /dev/stderr +} + +# OUT +deploy_without_credentials() { + + local endpoint=$1 + local regex=$2 + local repository=$3 + local file=$(create_file "$6" "$4") + local version=$5 + local src=$6 + + local version_file=$(create_version_file "$version" "$src") + + jq -n "{ + params: { + file: $(echo $file | jq -R .), + version_file: $(echo $version_file | jq -R .) + }, + source: { + endpoint: $(echo $endpoint | jq -R .), + repository: $(echo $repository | jq -R .), + regex: $(echo $regex | jq -R .) + } + }" | $resource_dir/out "$src" | tee /dev/stderr +} + +# OUT +deploy_with_credentials() { + + local endpoint=$1 + local regex=$2 + local repository=$3 + local file=$(create_file "$6" "$4") + local version=$5 + local src=$6 + + local username=$7 + local password=$8 + + local version_file=$(create_version_file "$version" "$src") + + cat < +# export ART_USER= +# export ART_PWD= +# sample curl commands for artifactory API +# curl -u $ART_USER:$ART_PWD -X PUT "http://host:8081/artifactory/path-to-file" -T ./local-path-to-file +# Artifactory docker image: https://www.jfrog.com/confluence/display/RTF/Running+with+Docker + +set -e + +source $(dirname $0)/helpers.sh + +it_can_list_releases_from_artifactory() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + + check_without_credentials_and_version $endpoint $regex $folder $src + +} + +it_can_list_releases_from_artifactory_with_version() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="20161109222826" + + check_without_credentials_with_version $endpoint $regex $folder $version $src + +} + +it_can_list_releases_from_protected_artifactory_with_version() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + artifactory_ip=$ART_IP + TMPDIR=/tmp + + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="20161109222826" + local username="${ART_USER}" + local password="${ART_PWD}" + + check_with_credentials_with_version $endpoint $regex $username $password $folder $version $src + +} + +run it_can_list_releases_from_artifactory +run it_can_list_releases_from_artifactory_with_version +run it_can_list_releases_from_protected_artifactory_with_version diff --git a/test/test-in.sh b/test/test-in.sh new file mode 100755 index 0000000..496b996 --- /dev/null +++ b/test/test-in.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +set -e + +source $(dirname $0)/helpers.sh + +it_can_get_version_from_artifactory() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="20161109222826" + + in_without_credentials_with_version $endpoint $regex $folder $version $src + +} + +it_cant_get_version_from_artifactory() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="NONE" + + in_without_credentials_with_version $endpoint $regex $folder $version $src + +} + +it_can_get_version_from_artifactory_with_credentials() { + + # local local_ip=$(find_docker_host_ip) + #local_ip="localhost" + + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="20161109222826" + local username="${ART_USER}" + local password="${ART_PWD}" + + in_with_credentials_with_version $endpoint $regex $folder $version $src $username $password + +} + +#run it_can_get_version_from_artifactory +run it_can_get_version_from_artifactory_with_credentials + +# check for exit code > 0 +#run it_cant_get_version_from_artifactory diff --git a/test/test-out.sh b/test/test-out.sh new file mode 100755 index 0000000..4ea08aa --- /dev/null +++ b/test/test-out.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +set -e + +source $(dirname $0)/helpers.sh + +it_can_deploy_release_to_artifactory() { + + # local local_ip=$(find_docker_host_ip) + #local_ip=localhost + + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + local folder="/generic/ecd-front" + local version="20161109222826" + local username="${ART_USER}" + local password="${ART_PWD}" + + local repository="/generic/ecd-front" + local file="ecd-front-(?.*).tar.gz" + + local version=20161109222826 + + deploy_without_credentials $endpoint $regex $repository $file $version $src +} + +it_can_deploy_release_to_artifactory_with_credentials() { + + artifactory_ip=$ART_IP + TMPDIR=/tmp + + local src=$(mktemp -d $TMPDIR/in-src.XXXXXX) + local endpoint="http://${artifactory_ip}:8081/artifactory" + local regex="ecd-front-(?.*).tar.gz" + + local username="${ART_USER}" + local password="${ART_PWD}" + + local repository="/generic/ecd-front" + local file="ecd-front-(?.*).tar.gz" + + local version=20161109222826 + + deploy_with_credentials $endpoint $regex $repository $file $version $src $username $password +} + +run it_can_deploy_release_to_artifactory_with_credentials +run it_can_deploy_release_to_artifactory diff --git a/test/test-regex.sh b/test/test-regex.sh new file mode 100755 index 0000000..c1edcdd --- /dev/null +++ b/test/test-regex.sh @@ -0,0 +1,129 @@ +#!/bin/bash + +# parse file +applyRegex() { + local regex=$1 + local file=$2 + + if [[ $file =~ $regex ]]; + then + version="${BASH_REMATCH[1]}" + echo "${version}" + else + echo "$file doesn't match" >&2 # this could get noisy if there are a lot of non-matching files + exit 1 + fi + echo "${version}" +} + +# Use jq regex since it supports grouping +applyRegex_version() { + local regex=$1 + local file=$2 + + jq -n "{ + version: $(echo $file | jq -R .) + }" | jq --arg v "$regex" '.version | capture($v)' | jq -r '.version' + +} + +# retrieve all versions from artifactory +artifactory_artifacts() { + local artifacts_url=$1 + local regex=$2 + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' + +} + +# retrieve current from artifactory +artifactory_current_version() { + local artifacts_url=$1 + local regex=$2 + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[length-1] | {version: .version}]' + +} + +# check provided version returning all version +artifactory_versions() { + local artifacts_url=$1 + local regex=$2 + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {version: .version}]' + +} + +check_version() { + local artifacts_url=$1 + local regex=$2 + local version=$3 + + result=$(artifactory_versions "$artifacts_url" "$regex") + echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version >= $v then $item else empty end)]' + +} + +# check provided version returning all version +artifactory_files() { + local artifacts_url=$1 + local regex="(?$2)" + + curl $1 | jq --arg v "$regex" '[.children[].uri | capture($v)]' | jq 'sort_by(.version)' | jq '[.[] | {uri: .uri, version: .version}]' + +} + +in_file_with_version() { + local artifacts_url=$1 + local regex="(?$2)" + local version=$3 + + result=$(artifactory_files "$artifacts_url" "$regex") + echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version == $v then $item else empty end)]' + +} + +check_file_with_version() { + local artifacts_url=$1 + local regex=$2 + local version=$3 + + result=$(artifactory_versions "$artifacts_url" "$regex") + echo $result | jq --arg v "$version" '[foreach .[] as $item ([]; $item ; if $item.version == $v then $item else empty end)]' + +} + + +version=$(applyRegex_version "carshare-(?admin|api|customer)-(?.*).tar.gz" "carshare-api-1.0.0-rc.0.tar.gz") +echo "version -> $version" + +url=http://localhost:8081/artifactory/api/storage/UrbanActive/Products/Maven/admin +echo "Testing retrieving artifactis with version group" +echo $(artifactory_artifacts "$url" "carshare-(admin|api|customer)-(?.*).tar.gz") + +echo "Testing retrieving artifactis with module and version group" +echo $(artifactory_artifacts "$url" "carshare-(?admin|api|customer)-(?.*).tar.gz") + +echo "Testing retrieving current version" +echo $(artifactory_current_version "$url" "carshare-(admin|api|customer)-(?.*).tar.gz") + +echo "Testing check version" +result=$(artifactory_versions "$url" "carshare-(admin|api|customer)-(?.*).tar.gz") +echo $result + +result='[ { "version": "1.0.0-rc.0" }, { "version": "1.0.0.2" }, { "version": "1.0.0.3" } ]' + +echo "Testing check by version output" +echo $result | jq '[foreach .[] as $item ([]; $item ; if $item.version >= "1.0.0.2" then $item else empty end)]' + +echo "Testing artifactory files" +result=$(artifactory_files "$url" "carshare-(admin|api|customer)-(?.*).tar.gz") +echo $result + +echo "Testing in with good version" +result=$(in_file_with_version "$url" "carshare-(admin|api|customer)-(?.*).tar.gz" "1.0.0.2") +echo $result + +echo "############### Testing check by version output function" +url="-u admin:password http://192.168.1.224:8081/artifactory/api/storage/libs-snapshot-local/Pivotal" +echo $(check_version "$url" "carshare-(admin|api|customer)-(?.*).tar.gz" "1.0.0.2") diff --git a/tools/jq b/tools/jq new file mode 100755 index 0000000..939227e Binary files /dev/null and b/tools/jq differ