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

Adds version pattern to check #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
FROM openjdk:8u151-jdk-alpine
FROM adoptopenjdk/openjdk8:latest

RUN apk add --no-cache curl tar bash jq libxml2-utils

# https://github.com/concourse/concourse/issues/2042
RUN unlink $JAVA_HOME/jre/lib/security/cacerts && \
cp /etc/ssl/certs/java/cacerts $JAVA_HOME/jre/lib/security/cacerts
RUN apt-get update \
&& apt-get install -y curl tar bash jq xqilla \
&& rm -rf /var/lib/apt/lists/*

ADD assets/ /opt/resource/
ADD test/ /opt/resource/test/
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Deploys and retrieve artifacts from a Maven Repository Manager.

* `artifact`: *Required.* The artifact coordinates in the form of _groupId:artifactId:type[:classifier]_

* `version`: *Optional.* A regular expression matching the entire version of an artifact. If specified only versions matching the regex will be considered. For example `1\.\0\.0-.*` would match all version like `1.0.0-build.123` but not `1.0.0` or `21.0.0-build.123`. Can be used to pin to latest of a major or minor release without bumping to the absolute latest release.

* `username`: *Optional.* Username for accessing an authenticated repository.

* `password`: *Optional.* Password for accessing an authenticated repository.
Expand Down Expand Up @@ -47,6 +49,11 @@ the repository.

Download the artifact from the repository.

### Files

* (artifact): Named artifact file.

* version: File containing the version of the artifact.

### `out`: Deploy artifact to a repository.

Expand Down
38 changes: 16 additions & 22 deletions assets/check
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fi
release_url=$(jq -r '.source.url //empty' < $payload)
snapshot_url=$(jq -r '.source.snapshot_url //empty' < $payload)
artifact=$(jq -r '.source.artifact //empty' < $payload)
version_pattern=$(jq -r '.source.version //".*"' < $payload)
version=$(jq -r '.version.version //empty' < $payload)
username=$(jq -r '.source.username //empty' < $payload)
password=$(jq -r '.source.password //empty' < $payload)
Expand Down Expand Up @@ -72,38 +73,31 @@ else
metadataUrl="$url/${groupId//.//}/$artifactId/maven-metadata.xml"
fi

metadata=$(mktemp)
set +e
metadata=$(curl --fail --silent --show-error $cert $auth $metadataUrl 2>&1)
curl -o $metadata --fail --silent --show-error $cert $auth $metadataUrl 2>&1
if [ $? -ne 0 ]; then
printf '\e[91m[ERROR]\e[0m %s\n' "$metadata"
printf '\e[91m[ERROR]\e[0m %s\n' "$(cat $metadata)"
printf '\e[91m[ERROR]\e[0m failed to download maven-metadata.xml from: %s\n' "$metadataUrl"
exit 2
fi
set -e

declare -a versions=( )
if [[ "$version" = *-SNAPSHOT ]]; then
if [[ -z "$classifier" ]]; then
versions[1]=$(echo $metadata | xmllint --xpath "/metadata/versioning/snapshotVersions/snapshotVersion[extension='$packaging' and not(classifier)]/value/text()" - 2>/dev/null)
if [[ -z "$classifier" ]]; then
versions=$(echo "/metadata/versioning/snapshotVersions/snapshotVersion[extension='$packaging' and not(classifier)]/value/text()" | xqilla -i $metadata /dev/stdin 2>/dev/null)
else
versions[1]=$(echo $metadata | xmllint --xpath "/metadata/versioning/snapshotVersions/snapshotVersion[extension='$packaging' and classifier='$classifier']/value/text()" - 2>/dev/null)
versions=$(echo "/metadata/versioning/snapshotVersions/snapshotVersion[extension='$packaging' and classifier='$classifier']/value/text()" | xqilla -i $metadata /dev/stdin 2>/dev/null)
fi
elif [ "$version" = "latest" ] || [ -z "$version" ]; then
versions[1]=$(echo $metadata | xmllint --xpath "/metadata/versioning/versions/version[last()]/text()" - 2>/dev/null)
else
itemsCount=$(echo $metadata | xmllint --xpath 'count(/metadata/versioning/versions/version)' - 2>/dev/null)
found=false
for (( i=1; i <= $itemsCount; i++ )); do

current=$(echo $metadata | xmllint --xpath "/metadata/versioning/versions/version[$i]/text()" - 2>/dev/null)
if [ "$found" = false ] && [ "$current" = "$version" ]; then
found=true
fi

if [ "$found" = true ]; then
versions[$i]=$current
fi
done
match_version_pattern="matches(text(), '^$version_pattern\$')"
versions=$(echo "/metadata/versioning/versions/((version[text()='$version']/(self::version|following-sibling::version)[$match_version_pattern])|(version[$match_version_pattern][last()]))/text()" | xqilla -i $metadata /dev/stdin 2>/dev/null)
fi

printf "%s\n" "${versions[@]}" | sed 's/.*/{ "version": "&" }/' | jq --slurp . >&3
rm -f $metadata

if [ -z "${versions}" ]; then
echo "[]" >&3
else
echo "$versions" | sed 's/.*/{ "version": "&" }/' | jq --slurp . >&3
fi
2 changes: 2 additions & 0 deletions assets/in
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ args="$args -Drepository.url=$url"

$resource_dir/mvnw dependency:copy $args

echo $version > $destination/version

jq -n \
--arg version "$version" \
'{
Expand Down
155 changes: 155 additions & 0 deletions test/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,162 @@ it_can_check_latest_from_three_versions() {
'
}

it_can_check_filtered_version_from_four_versions() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)
local version3=$(deploy_artifact $url $artifact '2.0.0-rc.1' $src)
local version4=$(deploy_artifact $url $artifact '21.0.0-rc.1' $src)

check_artifact_filtered $url $artifact 'latest' $src '1\.0\.0-.*' | \
jq -e \
--arg version $version2 \
'
. == [
{version: $version}
]
'
}

it_can_check_filtered_version_from_five_versions_interleaved() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)
local version3=$(deploy_artifact $url $artifact '2.0.0-rc.1' $src)
local version4=$(deploy_artifact $url $artifact '21.0.0-rc.1' $src)
local version5=$(deploy_artifact $url $artifact '1.0.0-rc.3' $src)

check_artifact_filtered $url $artifact 'latest' $src '1\.0\.0-.*' | \
jq -e \
--arg version $version5 \
'
. == [
{version: $version}
]
'
}

it_can_check_filtered_out_all_version() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)

check_artifact_filtered $url $artifact 'latest' $src '2\.0\.0-.*' | \
jq -e \
'
. == [
]
'
}

it_can_check_provided_version_is_not_latest_version_when_filtering() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)
local version3=$(deploy_artifact $url $artifact '1.0.0-rc.3' $src)

check_artifact_filtered $url $artifact $version2 $src '1\.0\.0-.*' | \
jq -e \
--arg version1 $version1 \
--arg version2 $version2 \
--arg version3 $version3 \
'
. == [
{version: $version2},
{version: $version3}
]
'
}

it_can_check_provided_version_is_latest_version_when_filtering() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)

check_artifact_filtered $url $artifact $version2 $src '1\.0\.0-.*' | \
jq -e \
--arg version1 $version1 \
--arg version2 $version2 \
'
. == [
{version: $version2}
]
'
}

it_can_check_filtered_version_gets_latest_version_if_input_version_is_missing() {

local src=$(mktemp -d $TMPDIR/check-src.XXXXXX)

local repository=$src/remote-repository
mkdir -p $repository

local url=file://$repository
local artifact=ci.concourse.maven:maven-resource:jar:standalone

local version1=$(deploy_artifact $url $artifact '1.0.0-rc.1' $src)
local version2=$(deploy_artifact $url $artifact '1.0.0-rc.2' $src)
local version3=$(deploy_artifact $url $artifact '2.0.0-rc.1' $src)
local version4=$(deploy_artifact $url $artifact '21.0.0-rc.1' $src)
local version5=$(deploy_artifact $url $artifact '1.0.0-rc.4' $src)

check_artifact_filtered $url $artifact '1.0.0-rc.3' $src '1\.0\.0-.*' | \
jq -e \
--arg version $version5 \
'
. == [
{version: $version}
]
'
}

run it_can_check_from_one_version
run it_can_check_from_three_versions
run it_can_check_latest_from_one_version
run it_can_check_latest_from_three_versions
run it_can_check_filtered_version_from_four_versions
run it_can_check_filtered_version_from_five_versions_interleaved
run it_can_check_filtered_out_all_version
run it_can_check_provided_version_is_not_latest_version_when_filtering
run it_can_check_provided_version_is_latest_version_when_filtering
run it_can_check_filtered_version_gets_latest_version_if_input_version_is_missing
2 changes: 2 additions & 0 deletions test/get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ it_can_get_artifact() {
'
.version == {version: $version}
'
local actual_version=$(cat $src/version)
[ "$version" == "$actual_version" ]
}

run it_can_get_artifact
24 changes: 24 additions & 0 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ check_artifact() {
}' | $resource_dir/check "$src" | tee /dev/stderr
}

check_artifact_filtered() {
local url=$1
local artifact=$2
local version=$3
local src=$4
local filter=$5

jq -n \
--arg url "$url" \
--arg artifact "$artifact" \
--arg version "$version" \
--arg filter "$filter" \
'{
source: {
url: $url,
artifact: $artifact,
version: $filter
},
version: {
version: $version
}
}' | $resource_dir/check "$src" | tee /dev/stderr
}

check_artifact_from_manager() {

local version=$1
Expand Down