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