spiff-arena/spiffworkflow-backend/bin/run_process_model_with_api

71 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
function error_handler() {
>&2 echo "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
exit "$2"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errtrace -o errexit -o nounset -o pipefail
script_dir="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
if [[ -z "${KEYCLOAK_BASE_URL:-}" ]]; then
export KEYCLOAK_BASE_URL="http://localhost:7002"
fi
if [[ -z "${BACKEND_BASE_URL:-}" ]]; then
export BACKEND_BASE_URL=http://localhost:7000
fi
process_model_identifier="${1:-}"
username="${2:-admin}"
password="${3:-admin}"
realm_name="${4:-spiffworkflow}"
if [[ -z "${1:-}" ]]; then
>&2 echo "usage: $(basename "$0") [process_model_identifier] [username: OPTONAL] [password: OPTONAL] [realm_name: OPTONAL]"
exit 1
fi
modified_process_model_identifier=$(tr '/' ':' <<<"$process_model_identifier")
function check_result_for_error() {
local result="$1"
error_code=$(jq '.error_code' <<<"$result")
if [[ -n "$error_code" && "$error_code" != "null" ]]; then
>&2 echo "ERROR: Failed to run process instance. Received error: $result"
exit 1
fi
}
function process_next_task() {
local next_task="$1"
if [[ -n "$next_task" && "$next_task" != "null" ]]; then
task_type=$(jq -r '.type' <<<"$next_task")
task_state=$(jq -r '.state' <<<"$next_task")
task_guid=$(jq -r '.id' <<<$"$next_task")
if grep -qE "Manual ?Task" <<<"$task_type" && [[ "${task_state}" == "READY" ]]; then
next_task=$(curl --silent -X PUT "${BACKEND_BASE_URL}/v1.0/tasks/${process_instance_id}/${task_guid}" -H "Authorization: Bearer $access_token")
check_result_for_error "$next_task"
process_next_task "$next_task"
elif [[ "$(jq '.ok' <<<"$next_task")" == "null" ]]; then
echo -e "\n\nThe next task is not a Manual Task and requires user input. It must be completed manually."
echo "$next_task"
fi
fi
}
access_token=$("${script_dir}/get_token" "$username" "$password" "$realm_name")
curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/login_with_access_token?access_token=${access_token}" -H "Authorization: Bearer $access_token" >/dev/null
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}" -H "Authorization: Bearer $access_token")
process_instance_id=$(jq -r '.id' <<<"$result")
if ! grep -qE '^[0-9]+$' <<<"$process_instance_id"; then
>&2 echo "ERROR: Did not receive valid process instance id when instantiating process model. result was ${result}"
exit 1
fi
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}/${process_instance_id}/run" -H "Authorization: Bearer $access_token")
check_result_for_error "$result"
next_task=$(jq '.next_task' <<<"$result")
process_next_task "$next_task"