ini-upload-action/action.yml

107 lines
2.9 KiB
YAML

name: HyperTuner INI upload
description: GitHub Action for INI upload.
author: Piotr Rogowski <piotr.rogowski0@gmail.com>
branding:
icon: cpu
color: blue
inputs:
api-url:
description: 'HyperTuner API URL'
required: true
username:
description: 'HyperTuner admin username'
required: true
password:
description: 'HyperTuner admin password'
required: true
path:
description: 'Path to INI file'
required: true
ecosystem:
description: 'Either "speeduino" or "rusefi"'
required: true
runs:
using: composite
steps:
- name: Upload
shell: bash
run: |
API_URL=${{ inputs.api-url }}
USERNAME=${{ inputs.username }}
PASSWORD=${{ inputs.password }}
INI_PATH=${{ inputs.path }}
ECOSYSTEM=${{ inputs.ecosystem }}
upload_url="${API_URL}/api/collections/iniFiles/records"
auth_url="${API_URL}/api/admins/auth-with-password"
echo "Parsing signature..."
signature=$(awk -F "=" '/signature/ {print $2}' ${INI_PATH} | tr -d '"' | cut -f1 -d";" | xargs)
echo "Signature: ${signature}"
echo "Compressing..."
filename="gzipped/${signature// /_}.ini"
mkdir gzipped
gzip --best --keep ${INI_PATH}
mv ${INI_PATH}.gz ${filename}
echo "Authenticating..."
token=$(curl --silent \
$auth_url \
--header 'Content-Type: application/json' \
--data-raw "{\"identity\": \"${USERNAME}\", \"password\": \"${PASSWORD}\"}" | jq -r '.token')
echo "Checking if file already exists..."
response=$(curl --silent \
$upload_url \
--get \
--header "Authorization: Bearer ${token}" \
--data-urlencode "filter=signature=\"${signature}\"")
found=$(echo $response | jq '.totalItems')
if [[ $found == 0 ]]; then
echo "Not found"
echo "Trying to create a new record..."
response=$(curl --silent \
$upload_url \
--request POST \
--header "Authorization: Bearer ${token}" \
--form "signature=\"${signature}\"" \
--form "ecosystem=\"${ECOSYSTEM}\"" \
--form file=@"${filename}")
else
echo "Record already exists, trying to update..."
id=$(echo $response | jq -r '.items[0].id')
response=$(curl --silent \
${upload_url}/${id} \
--request PATCH \
--header "Authorization: Bearer ${token}" \
--form "signature=\"${signature}\"" \
--form "ecosystem=\"${ECOSYSTEM}\"" \
--form file=@"${filename}")
fi
error=$(echo $response | jq -r '.code')
if [ -z "$error" ]
then
echo "Error: $response"
exit 1
else
echo "Success"
exit 0
fi