Add better managment of tags. Fixed getValueFromJson.

This commit is contained in:
Llewellyn van der Merwe 2024-01-22 10:07:29 +02:00
parent 5620954306
commit 8edfe5dc27
Signed by: Llewellyn
GPG Key ID: A9201372263741E7

View File

@ -225,7 +225,7 @@ function getConfigValue() {
# get what we can from the config if not already set in the .env file
# so to set the value globally use the .env option
# to set per project use the config file of the project
getValueFromJson "$1" "$2" || {
setValueFromJson "$1" "$2" "$VDM_CONFIG_DATA" || {
# give little heads-up if not blocked
"${3:-true}" && echo >&2 "[error] We require ($2) for $PROGRAM_NAME v${PROGRAM_V} to work, but it's not found in $VDM_PACKAGE_CONF_FILE."
# we had no success
@ -235,25 +235,50 @@ function getConfigValue() {
return 0
}
# Set value from JSON
function setValueFromJson() {
local target_var="$1"
local value
value=$(getValueFromJson "$2" "$3") || {
return 19
}
# Safely use eval to dynamically set the value
eval "$target_var"=\'"$value"\'
}
# Get value from JSON
function getValueFromJson() {
# some local values
local value_key
local json
# load the values
value_key="${2}"
# load the json object
json="${3:-$VDM_CONFIG_DATA}"
# get the value
# shellcheck disable=SC2034
val=$(echo "${json}" | jq -r "$value_key")
# check the value not to be null
[ "${val}" = 'null' ] && {
# we had no success
return 19
}
# set the dynamic variable $1=(MUST BE VALID VARIABLE NAME)
eval "$1"=\"\$val\"
return 0
local json_key="$1"
local json_data="$2"
local value
# Check if the necessary parameters are provided
if [[ -z "$json_key" ]]; then
# [error] Missing required key parameter.
return 19
fi
if [[ -z "$json_data" ]]; then
# [error] JSON data is empty.
return 19
fi
# Attempt to extract the value using jq
if ! value=$(echo "$json_data" | jq -r "$json_key"); then
# [error] Failed to parse JSON data.
return 19
fi
# Check if the value is 'null' or empty
if [[ "$value" == "null" || -z "$value" ]]; then
# [error] Key not found or null value.
return 19
fi
# Safely return the value
echo "$value"
}
# set the Package Path
@ -436,39 +461,36 @@ function clearFileEnv() {
# get the repository zip package
function getZipFile() {
# check the mode
local mode="tags"
local target="Key"
if [ "${VDM_MODE}" = 'tags' ] || [ "${VDM_MODE}" = 'releases' ]; then
# make API call
callGiteaAPI "${VDM_MODE}" "${VDM_OWNER}" "${VDM_REPO}" "${VDM_API}" "${VDM_TOKEN}" || return 11
# check the mode and availability
if [ -n "${VDM_API_BUCKET}" ] && [ "${VDM_MODE}" = 'tags' ]; then
# get file by tag
getFileByTag || return 11
elif [ -n "${VDM_API_BUCKET}" ] && [ "${VDM_MODE}" = 'releases' ]; then
# get file by release
getFileByRelease || return 11
else
return 11
fi
else
# make API call
callGiteaAPI "tags" "${VDM_OWNER}" "${VDM_REPO}" "${VDM_API}" "${VDM_TOKEN}" || return 11
# get the file by key/branch/tag
getFileByKey || return 11
mode="${VDM_MODE}"
target="${VDM_MODE^}"
elif [[ "${VDM_MODE}" == branch:* ]]; then
VDM_MODE="${VDM_MODE#branch:}"
fi
# success
callGiteaAPI "${mode}" "${VDM_OWNER}" "${VDM_REPO}" "${VDM_API}" "${VDM_TOKEN}" || return 11
if [ -n "${VDM_API_BUCKET}" ]; then
"getFileBy${target}" || return 11
else
return 11
fi
return 0
}
# get the file by tag
function getFileByTag() {
function getFileByTags() {
# get the name
getValueFromJson "VDM_ZIP_NAME" ".name" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ZIP_NAME" ".[0].name" "${VDM_API_BUCKET}" || {
_echo "[error] Tag name not found in VDM_API_BUCKET."
return 12
}
# get the message
getValueFromJson "VDM_ZIP_MESSAGE" ".message" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ZIP_MESSAGE" ".[0].message" "${VDM_API_BUCKET}" || {
_echo "[error] Tag message not found in VDM_API_BUCKET."
return 12
}
@ -491,23 +513,23 @@ function getFileByTag() {
}
# get the file by tag
function getFileByRelease() {
function getFileByReleases() {
# check that we got some file
local has_files=false
local i=0
# get the name
getValueFromJson "VDM_ZIP_NAME" ".tag_name" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ZIP_NAME" ".[0].tag_name" "${VDM_API_BUCKET}" || {
_echo "[error] Release name not found in VDM_API_BUCKET."
return 12
}
# get the message
getValueFromJson "VDM_ZIP_MESSAGE" ".name" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ZIP_MESSAGE" ".[0].name" "${VDM_API_BUCKET}" || {
_echo "[error] Release message not found in VDM_API_BUCKET."
return 12
}
# we may have multiple assets (but we only load the first ZIP file we get)
# because we can only have the ${VDM_ID} only once in the xml file
for asset_zip_file_name in $(echo "${VDM_API_BUCKET}" | jq -r '.assets[].name'); do
for asset_zip_file_name in $(echo "${VDM_API_BUCKET}" | jq -r '.[0].assets[].name'); do
# make sure we did not already get a file
$has_files && {
# increment
@ -523,7 +545,7 @@ function getFileByRelease() {
# download the zip file if not already set
if [ ! -f "${VDM_PACKAGE_SRC_DIR}/${VDM_OWNER}__${asset_zip_file_name}" ]; then
# get download link
getValueFromJson "VDM_ASSET_DOWNLOAD" ".assets[$i].browser_download_url" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ASSET_DOWNLOAD" ".[0].assets[$i].browser_download_url" "${VDM_API_BUCKET}" || {
_echo "[error] Asset [$i].browser_download_url for (${asset_zip_file_name}) not found in VDM_API_BUCKET."
# increment
((i++))
@ -558,7 +580,7 @@ function getFileByKey() {
# get the message
VDM_ZIP_MESSAGE="${VDM_MODE}"
# get the name
getValueFromJson "VDM_ZIP_VERSION" ".name" "${VDM_API_BUCKET}" || {
setValueFromJson "VDM_ZIP_VERSION" ".[0].name" "${VDM_API_BUCKET}" || {
_echo "[error] Tag name not found in VDM_API_BUCKET. (but ignored since we are getting file by key)"
VDM_ZIP_VERSION="${VDM_MODE}"
}
@ -630,30 +652,30 @@ function callGiteaAPI() {
local repo="${3}"
local api="${4}"
local token="${5}"
local zip_packages
local json_data
local message
# each time reset
unset VDM_API_BUCKET
# give message
_echo "[info] Getting ${mode} information from ${owner}/${repo} repository."
# get the zip packages
zip_packages=$(curl -s -H "Authorization: token ${token}" -H 'accept: application/json' -X 'GET' "${api}/repos/${owner}/${repo}/${mode}")
# get the json data
json_data=$(curl -s -H "Authorization: token ${token}" -H 'accept: application/json' -X 'GET' "${api}/repos/${owner}/${repo}/${mode}")
# check for error
if [[ "${zip_packages}" =~ '"errors"' ]] && [[ "${zip_packages}" =~ '"message"' ]]; then
if [[ "${json_data}" =~ '"errors"' ]] && [[ "${json_data}" =~ '"message"' ]]; then
# get the message
message=$(echo "${zip_packages}" | jq -r ".message")
message=$(echo "${json_data}" | jq -r ".message")
# check that we have tags (not ideal, but to catch wrong repo path)
_echo "[error] failed to get ${mode} from ${owner}/${repo} repository [${message}]."
# we an add more later
return 12
elif [ "${zip_packages}" = '[]' ]; then
elif [ "${json_data}" = '[]' ]; then
# check that we have tags (not ideal, but to catch wrong repo path)
_echo "[error] failed to get ${mode} from ${owner}/${repo} repository."
# we an add more later
return 12
fi
# get the latest
VDM_API_BUCKET=$(echo "${zip_packages}" | jq ".[0]")
VDM_API_BUCKET="${json_data}"
# success
export VDM_API_BUCKET
# success
@ -963,25 +985,23 @@ function setRepository() {
if $update_repo; then
# make API call
callGiteaAPI "tags" "${VDM_PACKAGE_OWNER}" "${VDM_PACKAGE_REPO}" "${VDM_PACKAGE_API}" "${VDM_PACKAGE_TOKEN}" || return 28
# get the name
if [ -n "${VDM_API_BUCKET}" ]; then
version_name=$(echo "${VDM_API_BUCKET}" | jq -r ".name")
else
return 28
fi
# check if tag exists
VDM_TAG_EXIST=$(echo "${VDM_API_BUCKET}" | jq -r --arg TAG "${VDM_PACKAGE_VERSION:-1.0.0}" 'any(.[]; .name == $TAG)')
# set update message
if [ "${VDM_PACKAGE_VERSION:-1.0.0}" != "${version_name}" ]; then
if $VDM_TAG_EXIST; then
message="Update - ${VDM_PACKAGE_VERSION:-1.0.0}"
else
message="Update"
fi
# get the repository last tag
makeGitCommit "${message}" "${VDM_PACKAGE_VERSION:-1.0.0}" "${version_name}" || return 28
makeGitCommit "${message}" "${VDM_PACKAGE_VERSION:-1.0.0}" "$VDM_TAG_EXIST" || return 28
# give little notice of progress
_echo "[info] Pushing changes to (${VDM_PACKAGE_OWNER}/${VDM_PACKAGE_REPO}) repository"
# make a normal push update
git push >/dev/null 2>&1 || return 30
git push --tags >/dev/null 2>&1 || return 30
if ! $VDM_TAG_EXIST; then
git push --tags >/dev/null 2>&1 || return 30
fi
else
# create new repo
setGitRepository || return 29
@ -1000,7 +1020,7 @@ function setRepository() {
function getExistingRepository() {
# little information of progress
_echo "[info] Getting (${VDM_PACKAGE_OWNER}/${VDM_PACKAGE_REPO}) repository"
if [ "${VDM_PACKAGE_REPO_BRANCH}" = 'default' ]; then
if [[ "${VDM_PACKAGE_REPO_BRANCH}" == 'default' ]]; then
# clone the existing repository
git clone "ssh://git@${VDM_PACKAGE_URL}/${VDM_PACKAGE_OWNER}/${VDM_PACKAGE_REPO}.git" >/dev/null 2>&1 || return 23
else
@ -1084,7 +1104,10 @@ function makeGitCommit() {
# set the commit message
git commit -am"${1}" >/dev/null 2>&1 || return 28
# check if the tag should be added
if [ "${2:-error}" != "error" ] && [ "${2:-error}" != "${3:-none}" ]; then
if "${3:-false}"; then
# little information of progress
_echo "[info] TAG (${2}) in (${VDM_PACKAGE_OWNER}/${VDM_PACKAGE_REPO}) already exists"
else
# little information of progress
_echo "[info] Adding TAG (${2}) to (${VDM_PACKAGE_OWNER}/${VDM_PACKAGE_REPO}) repository"
# add the new tag