#!/bin/bash -e

# Usage:
#  openqa-clone-custom-git-refspec GITHUB_PR_URL OPENQA_JOB_URL [CUSTOM_TEST_VAR_1=foo] [CUSTOM_TEST_VAR_2=bar]
#  openqa-clone-custom-git-refspec GITHUB_BRANCH_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
#  openqa-clone-custom-git-refspec https://github.com/coolgw/os-autoinst-distri-opensuse/tree/nfs 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" ]] || [[ -z "$pr" ]]; then
    first_arg="${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' or 'branch'"}"
    if [[ $first_arg == *"pull"* ]]; then
        pr_url=$first_arg
        target_repo_part=${pr_url%%/pull*}
        pr="${pr_url##*/}"
    elif [[ $first_arg  == *"tree"* ]]; then
        #maybe a branch_url is given
        branch_url=$first_arg
        forked_repo_part=${branch_url%%/tree*}
        branch=${branch_url##*/}
        repo_name=${forked_repo_part##*github.com/}
        casedir="${casedir:-"${forked_repo_part}.git#${branch}"}"
        build="${build:-"$repo_name#$branch"}"
    fi

fi
if [[ -z "$host" ]] || [[ -z "$job" ]]; 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

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

if [[ -z "$branch" ]] || [[ -z "$repo_name" ]]; 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" ]] || [[ -z "$needles_dir" ]] || [[ -z "$productdir" ]]; 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"
    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}"}"
    old_needledir=$(echo "$json_data" | jq -r '.NEEDLES_DIR | select (.!=null)') || throw_json_error "$json_url" "$json_data"
    needles_dir="${needles_dir:-"$old_needledir"}"
    needles_dir="${needles_dir:-"$old_productdir/needles"}"
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}"
