set -eu
# Supply AZURE_DEVOPS_EXT_PAT, AZURE_DEVOPS_TEST_ORG and AZURE_DEVOPS_TEST_PROJECT.
# Set PROOF_DIR to a fresh fixture directory containing repo.json from az repos create.
repo=$(python3 -c 'import json,os; print(json.load(open(os.environ["PROOF_DIR"]+"/repo.json"))["remoteUrl"])')
id=$(python3 -c 'import json,os; print(json.load(open(os.environ["PROOF_DIR"]+"/repo.json"))["id"])')
mkdir ${PROOF_DIR}/seed
git -C ${PROOF_DIR}/seed init -q -b main
cd ${PROOF_DIR}/seed
git config user.name Scenario
git config user.email scenario@example.invalid
git commit -q --allow-empty -m Main
git switch -q -c fix
git commit -q --allow-empty -m 'Original worktree commit A'
git tag original-a
git commit -q --allow-empty -m 'PR head B'
git branch cached-b
git switch -q -c release/fix original-a
git commit -q --allow-empty -m 'Unrelated release commit C'
git switch -q main
git remote add origin "$repo"
git -c credential.helper='!f() { echo username=pat; echo "password=$AZURE_DEVOPS_EXT_PAT"; }; f' push origin main fix release/fix
az repos pr create --organization "$AZURE_DEVOPS_TEST_ORG" --project "$AZURE_DEVOPS_TEST_PROJECT" --repository "$id" --source-branch fix --target-branch main --title 'Repro PR from fix' --query pullRequestId -o tsv > ${PROOF_DIR}/pr-id
git -c credential.helper='!f() { echo username=pat; echo "password=$AZURE_DEVOPS_EXT_PAT"; }; f' push origin :fix
