Skip to content

Instantly share code, notes, and snippets.

@ilg-ul
Created March 10, 2017 14:27
Show Gist options
  • Save ilg-ul/568a6806d5e97fcc1384d7acda4ffe36 to your computer and use it in GitHub Desktop.
Save ilg-ul/568a6806d5e97fcc1384d7acda4ffe36 to your computer and use it in GitHub Desktop.
Bintray test script to upload Eclipse p2 repositories
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Safety settings (see https://gist.github.com/ilg-ul/383869cbb01f61a51c4d).
if [[ ! -z ${DEBUG} ]]
then
set -x # Activate the expand mode if DEBUG is anything but empty.
fi
set -o errexit # Exit if command failed.
set -o pipefail # Exit if pipe failed.
set -o nounset # Exit if variable not set.
# Remove the initial space and instead use '\n'.
IFS=$'\n\t'
# -----------------------------------------------------------------------------
# Script initially inspired by:
# https://github.com/vogellacompany/bintray-publish-p2-updatesite
API=https://api.bintray.com
echo
echo "User: ${BINTRAY_USER}"
echo "API key: ${BINTRAY_API_KEY}"
echo "Owner: ${BINTRAY_OWNER}"
work_folder_path="${HOME}/tmp/bintray-test"
# -----------------------------------------------------------------------------
# $1 = repo name
do_remove_repo()
{
local repo="$1"
echo "Delete repo '${repo}'"
curl \
--request DELETE \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
"${API}/repos/${BINTRAY_OWNER}/${repo}"
echo
}
# $1 = repo name
do_create_repo()
{
local repo="$1"
echo "Create generic repo '${repo}'"
curl \
--request POST \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
"${API}/repos/${BINTRAY_OWNER}/${repo}"
echo
}
# POST /packages/:subject/:repo
# {
# "name": "my-package",
# "desc": "This package...",
# "labels": ["persistence", "database"],
# "licenses": ["Apache-2.0", "GPL-3.0"],
# "custom_licenses": ["my-license-1", "my-license-2"],
# "vcs_url": "https://github.com/bintray/bintray-client-java.git",
# "website_url": "http://jfrog.com",
# "issue_tracker_url": "https://github.com/bintray/bintray-client-java/issues",
# "github_repo": "bintray/bintray-client-java",
# "github_release_notes_file": "RELEASE.txt",
# "public_download_numbers": false,
# "public_stats": true
# }
# $1 = repo name
# $2 = package name
do_create_package()
{
local repo="$1"
local package="$2"
echo "Create package '${repo}/${package}'"
local tmp_path="$(mktemp)"
# Note: EOF is not quoted to allow local substitutions.
cat <<__EOF__ >"${tmp_path}"
{
"name": "${package}",
"licenses": ["EPL-1.0"],
"public_download_numbers": true,
"vcs_url": "https://github.com/",
"public_download_numbers": true,
"public_stats": true
}
__EOF__
curl \
--request POST \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
--header "Content-Type: application/json" \
--data @"${tmp_path}" \
"${API}/packages/${BINTRAY_OWNER}/${repo}"
echo
rm "${tmp_path}"
}
# POST /packages/:subject/:repo/:package/versions
# {
# "name": "1.1.5",
# "released": "ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)", (optional)
# "desc": "This version...",
# "github_release_notes_file": "RELEASE.txt", (optional)
# "github_use_tag_release_notes": true, (optional)
# "vcs_tag": "1.1.5" (optional)
# }
# $1 = repo
# $2 = package
# $3 = version
do_create_version()
{
local repo="$1"
local package="$2"
local version="$3"
echo "Create version '${repo}/${package}/${version}'"
local tmp_path="$(mktemp)"
# Note: EOF is not quoted to allow local substitutions.
cat <<__EOF__ >"${tmp_path}"
{
"name": "${version}"
}
__EOF__
curl \
--request POST \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
--header "Content-Type: application/json" \
--data @"${tmp_path}" \
"${API}/packages/${BINTRAY_OWNER}/${repo}/${package}/versions"
rm "${tmp_path}"
echo
}
# This option is not present in the documentation explictly (the
# documentation shows only how to upload to a version), but it is
# used in some examples, so I'm not sure this is legal.
# $1 = file absolute path
# $2 = repo
# $3 = file relative path
do_upload_to_repo()
{
local file_absolute_path="$1"
local repo="$2"
local file_relative_path="$3"
echo "Upload '${file_relative_path}' to '/${repo}/'"
curl \
--request PUT \
--upload-file "${file_absolute_path}" \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
"${API}/content/${BINTRAY_OWNER}/${repo}/${file_relative_path}?publish=1?override=1"
echo
}
# PUT /content/:subject/:repo/:package/:version/:file_path[?publish=0/1][?override=0/1][?explode=0/1]
# $1 = file absolute path
# $2 = repo
# $3 = package
# $4 = version
# $5 = file relative path
do_upload_to_pv()
{
local file_absolute_path="$1"
local repo="$2"
local package="$3"
local version="$4"
local file_relative_path="$5"
echo "Upload '${file_relative_path}' to '/${repo}/${package}/${version}/'"
curl \
--request PUT \
--upload-file "${file_absolute_path}" \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
"${API}/content/${BINTRAY_OWNER}/${repo}/${package}/${version}/${file_relative_path}?publish=1?override=1?explode=0"
echo
}
# X-Bintray-Package: :package
# X-Bintray-Version: :version
# [X-Bintray-Publish: 0/1]
# [X-Bintray-Override: 0/1]
# [X-Bintray-Explode: 0/1]
# PUT /content/:subject/:repo/:file_path
# $1 = file absolute path
# $2 = repo
# $3 = package
# $4 = version
# $5 = file relative path
do_upload_to_pv_header()
{
local file_absolute_path="$1"
local repo="$2"
local package="$3"
local version="$4"
local file_relative_path="$5"
echo "Upload '${file_relative_path}' to '/${repo}/${package}/${version}/'"
curl \
--request PUT \
--upload-file "${file_absolute_path}" \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
--header "X-Bintray-Package: ${package}" \
--header "X-Bintray-Version: ${version}" \
--header "X-Bintray-Publish: 1" \
--header "X-Bintray-Override: 1" \
--header "X-Bintray-Explode: 0" \
"${API}/content/${BINTRAY_OWNER}/${repo}/${file_relative_path}"
echo
}
# PUT /content/:subject/:repo/:file_path;bt_package=:package;bt_version=:version[;publish=0/1][;override=0/1][;explode=0/1]
# $1 = file absolute path
# $2 = repo
# $3 = package
# $4 = version
# $5 = file relative path
do_upload_to_pv_matrix()
{
local file_absolute_path="$1"
local repo="$2"
local package="$3"
local version="$4"
local file_relative_path="$5"
echo "Upload '${file_relative_path}' to '/${repo}/${package}/${version}/'"
curl \
--request PUT \
--upload-file "${file_absolute_path}" \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
"${API}/content/${BINTRAY_OWNER}/${repo}/${file_relative_path};bt_package=${package};bt_version=${version};publish=1;override=1;explode=0"
echo
}
# $1 = repo
# $2 = package
# $3 = version
# $4 = file relative path
do_put_dl()
{
local repo="$1"
local package="$2"
local version="$3"
local file_relative_path="$4"
curl \
--request PUT \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
--header "Content-Type: application/json" \
--data "{ \"list_in_downloads\": true }" \
"${API}/file_metadata/${BINTRAY_OWNER}/${repo}/${package}/${version}/${file_relative_path}"
echo
}
# POST /content/:subject/:repo/:package/:version/publish
# $1 = repo
# $2 = package
# $3 = version
do_publish()
{
local repo="$1"
local package="$2"
local version="$3"
echo "Publish '${repo}/${package}/${version}'"
curl \
--request POST \
--user ${BINTRAY_USER}:${BINTRAY_API_KEY} \
--header "Content-Type: application/json" \
-d "{ \"publish_wait_for_secs\": -1, \"discard\": \"false\" }" \
"${API}/content/${BINTRAY_OWNER}/${repo}/${package}/${version}/publish"
echo
}
# -----------------------------------------------------------------------------
do_test1()
{
echo
echo "Test 1"
echo "Publish to repo url, without package/version created."
echo
local repo="repo1"
local package="pack1"
do_remove_repo "${repo}"
do_create_repo "${repo}"
# -----
local p2_folder_path="${v1_folder_path}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_repo "$(pwd)/${f}" "${repo}" "${f}"
done
# This version is not published correctly; some files are accepted to be
# uploaded to the repo url, but most are not.
# Upload 'artifacts.xml.xz' to '/repo1/'
# {"message":"Invalid file path and name"}
# Upload 'content.xml.xz' to '/repo1/'
# {"message":"Invalid file path and name"}
# Upload 'features/ilg.gnuarmeclipse.codered_1.1.1.201701141320.jar' to '/repo1/'
# {"message":"Invalid file path and name"}
# ...
# -----
local p2_folder_path="${v2_folder_path}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_repo "$(pwd)/${f}" "${repo}" "${f}"
done
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
}
do_test2()
{
echo
echo "Test 2"
echo "Publish to repo url, with package/version created."
echo
local repo="repo2"
local package="pack2"
do_remove_repo "${repo}"
do_create_repo "${repo}"
do_create_package "${repo}" "${package}"
# -----
local version="${v1}"
local p2_folder_path="${v1_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_repo "$(pwd)/${f}" "${repo}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# This version is not published correctly; some files are accepted to be
# uploaded to the repo url, but most are not.
# Upload 'artifacts.xml.xz' to '/repo1/'
# {"message":"Invalid file path and name"}
# Upload 'content.xml.xz' to '/repo1/'
# {"message":"Invalid file path and name"}
# Upload 'features/ilg.gnuarmeclipse.codered_1.1.1.201701141320.jar' to '/repo1/'
# {"message":"Invalid file path and name"}
# ...
# -----
local version="${v2}"
local p2_folder_path="${v2_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_repo "$(pwd)/${f}" "${repo}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
echo "Check '/${repo}/${package}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/" --verbose
echo
echo "Check '/${repo}/${package}/${version}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/${version}/" --verbose
echo
}
do_test3()
{
echo
echo "Test 3"
echo "Publish to repo/package/version."
echo
local repo="repo3"
local package="pack3"
do_remove_repo "${repo}"
do_create_repo "${repo}"
do_create_package "${repo}" "${package}"
# -----
local version="${v1}"
local p2_folder_path="${v1_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# This version is not published correctly; some files are published to
# the repo url, some are in the version folder.
# -----
local version="${v2}"
local p2_folder_path="${v2_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
echo "Check '/${repo}/${package}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/" --verbose
echo
echo "Check '/${repo}/${package}/${version}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/${version}/" --verbose
echo
}
do_test4()
{
echo
echo "Test 4"
echo "Publish to repo/package/version using header params."
echo
local repo="repo4"
local package="pack4"
do_remove_repo "${repo}"
do_create_repo "${repo}"
do_create_package "${repo}" "${package}"
# -----
local version="${v1}"
local p2_folder_path="${v1_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_header "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# This version is published correctly, it can be downloaded
# from the repo url.
# -----
local version="${v2}"
local p2_folder_path="${v2_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_header "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
echo "Check '/${repo}/${package}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/" --verbose
echo
echo "Check '/${repo}/${package}/${version}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/${version}/" --verbose
echo
}
do_test5()
{
echo
echo "Test 5"
echo "Publish to repo/package/version using matrix params."
echo
local repo="repo5"
local package="pack5"
do_remove_repo "${repo}"
do_create_repo "${repo}"
do_create_package "${repo}" "${package}"
# -----
local version="${v1}"
local p2_folder_path="${v1_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_matrix "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# This version is published correctly, it can be downloaded
# from the repo url.
# -----
local version="${v2}"
local p2_folder_path="${v2_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_matrix "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
echo "Check '/${repo}/${package}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/" --verbose
echo
echo "Check '/${repo}/${package}/${version}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/${version}/" --verbose
echo
}
do_test6()
{
echo
echo "Test 6"
echo "Publish to repo/package/version using matrix params and republish."
echo
local repo="repo6"
local package="pack6"
do_remove_repo "${repo}"
do_create_repo "${repo}"
do_create_package "${repo}" "${package}"
# -----
local version="${v1}"
local p2_folder_path="${v1_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_matrix "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# This version is published correctly, it can be downloaded
# from the repo url.
# -----
local version="${v2}"
local p2_folder_path="${v2_folder_path}"
do_create_version "${repo}" "${package}" "${version}"
cd ${p2_folder_path}
for f in *.* */*
do
do_upload_to_pv_matrix "$(pwd)/${f}" "${repo}" "${package}" "${version}" "${f}"
done
do_publish "${repo}" "${package}" "${version}"
# The second version is no longer correct, two files are missing.
# Upload 'artifacts.xml.xz' to '/repo6/pack6/3.3.1-201702251311/'
# {"message":"Unable to upload files: An artifact with the path 'artifacts.xml.xz' already exists under another version"}
# Upload 'content.xml.xz' to '/repo6/pack6/3.3.1-201702251311/'
# {"message":"Unable to upload files: An artifact with the path 'content.xml.xz' already exists under another version"}
# -----
echo "Check '/${repo}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/" --verbose
echo
echo "Check '/${repo}/${package}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/" --verbose
echo
echo "Check '/${repo}/${package}/${version}/' page"
curl -L "https://dl.bintray.com/${BINTRAY_OWNER}/${repo}/${package}/${version}/" --verbose
echo
}
# -----------------------------------------------------------------------------
mkdir -p "${work_folder_path}"
cd "${work_folder_path}"
# The following archived Eclipse p2 repositories will be used:
# https://github.com/gnuarmeclipse/plug-ins/releases/download/v3.2.1-201701141320/ilg.gnuarmeclipse.repository-3.2.1-201701141320.zip
# https://github.com/gnuarmeclipse/plug-ins/releases/download/v3.3.1-201702251311/ilg.gnuarmeclipse.repository-3.3.1-201702251311.zip
url_base="https://github.com/gnuarmeclipse/plug-ins/releases/download"
v1="3.2.1-201701141320"
v2="3.3.1-201702251311"
v1_folder_name="ilg.gnuarmeclipse.repository-${v1}"
v1_folder_path="${work_folder_path}/${v1_folder_name}"
v1_archive_name="${v1_folder_name}.zip"
v1_archive_path="${work_folder_path}/${v1_archive_name}"
v1_url="${url_base}/v${v1}/${v1_archive_name}"
# Download and unpack v1
if [ ! -d "${v1_folder_path}" ]
then
if [ ! -f "${v1_archive_path}" ]
then
curl -L "${v1_url}" -o "${v1_archive_path}"
fi
unzip "${v1_archive_path}" -d "${v1_folder_path}"
fi
v2_folder_name="ilg.gnuarmeclipse.repository-${v2}"
v2_folder_path="${work_folder_path}/${v2_folder_name}"
v2_archive_name="${v2_folder_name}.zip"
v2_archive_path="${work_folder_path}/${v2_archive_name}"
v2_url="${url_base}/v${v2}/${v2_archive_name}"
# Download and unpack v2
if [ ! -d "${v2_folder_path}" ]
then
if [ ! -f "${v2_archive_path}" ]
then
curl -L "${v2_url}" -o "${v2_archive_path}"
fi
unzip "${v2_archive_path}" -d "${v2_folder_path}"
fi
# -----------------------------------------------------------------------------
# set +o errexit # Do not exit if command fails.
do_test1
do_test2
do_test3
do_test4
do_test5
do_test6
exit
# -----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment