#!/bin/sh -e

# Usage:
#  openqa-clone-custom-git-refspec GITHUB_PR_URL OPENQA_JOB_URL [CUSTOM_TEST_VAR_1=foo] [CUSTOM_TEST_VAR_2=bar]
#
# Example:
#  openqa-clone-custom-git-refspec https://github.com/os-autoinst/os-autoinst-distri-opensuse/pull/6529 https://openqa.opensuse.org/tests/835060 DESKTOP=textmode
#
set -o pipefail
if [ -n $GITHUB_TOKEN ]; then
    AUTHENTICATED_REQUEST=" -u $GITHUB_TOKEN:x-oauth-basic "
    echo "Github oauth token provided, peforming authenticated requests"
fi
if [ -z ${repo_name+x} ] || [ -z ${pr+x} ]; then
    pr_url="${1:?"Need 'pr_url' as parameter pointing to a github pull request or 'repo_name' (sending repo) and 'pr' variables, e.g. either 'https://github.com/os-autoinst/os-autoinst-distri-opensuse/pull/1234' 'me/os-autoinst-distri-opensuse' and '1234'"}"
    target_repo_part=${pr_url%%/pull*}
    pr="${pr_url##*/}"
fi
if [ -z ${host+x} ] || [ -z ${job+x} ]; then
    job_url="${2:?"Need 'job_url' as parameter pointing to the openQA job to clone or 'host' and 'job' variables, e.g. either 'https://openqa.opensuse.org/tests/123456' or 'https://openqa.opensuse.org' and '123456'"}"
    host=${job_url%%/t*}
    job=${job_url##*/}
fi

function throw_json_error {
    echo "in contents queried from $1:"
    echo "$2"
    exit 2
}

if [ -z ${branch+x} ] || [ -z ${repo_name+x} ]; then
    pr_url=${target_repo_part/github.com/api.github.com/repos}/pulls/$pr
    pr_content=$(curl ${AUTHENTICATED_REQUEST} -s "$pr_url")
    label=$(echo $pr_content | jq -r '.head.label') || throw_json_error "$pr_url" "$pr_content"
    if [ "${label}" == "null" ]; then
        echo "Github API rate limit might have been exceeded. If this is the case, generate one at
        https://github.com/settings/tokens and then export it as an environment variable"
        echo 'export GITHUB_TOKEN=foobar'
        echo "Github reply: " $(echo ${pr_content} | jq -r '.message')
        exit 1
    fi
    repo_name="${repo_name:-"${label%:*}/${target_repo_part##*/}"}"
    branch="${branch:-"${label##*:}"}"
    repo="${repo:-"https://github.com/${repo_name}.git"}"
fi

if [ -z ${testsuite+x} ] || [ -z ${needles_dir+x} ] || [ -z ${productdir+x} ]; then
    json_url=${host}/tests/${job}/file/vars.json
    json_data=$(curl -s "${json_url}")
    testsuite="${testsuite:-"$(echo $json_data | jq -r '.TEST')"}" || throw_json_error "$json_url" "$json_data"
    needles_dir="${needles_dir:-"$(echo $json_data | jq -r '.NEEDLES_DIR')"}" || throw_json_error "$json_url" "$json_data"
    old_productdir=$(echo $json_data | jq -r '.PRODUCTDIR') || throw_json_error "$json_url" "$json_data"
    old_casedir=$(echo $json_data | jq -r '.CASEDIR') || throw_json_error "$json_url" "$json_data"
    productdir="${productdir:-"${repo_name##*/}${old_productdir##$old_casedir}"}"
fi
repo_branch="${repo_branch:-"$repo_name#$branch"}"
test="${test:-"$testsuite@$repo_branch"}"
build="${build:-"$repo_name#$pr"}"
casedir="${casedir:-"$repo#$branch"}"
GROUP="${GROUP:-0}"
dry_run="${dry_run:-""}"
$dry_run openqa-clone-job --skip-chained-deps --within-instance $host $job _GROUP=$GROUP TEST=$test BUILD=$build CASEDIR=$casedir PRODUCTDIR=$productdir NEEDLES_DIR=$needles_dir "${@:3}"
