Skip to content

Commit

Permalink
Use name property to find current job in GitHub API response
Browse files Browse the repository at this point in the history
This updates the change made in d7e7736. Copied from
test-observability-action commit 518bc5e. Motivation as described in
that commit:

> I originally tried implementing this by passing a job-index input, whose
> value was the index to which the current job corresponds in the response
> from the "list jobs for a workflow run attempt" GitHub API. However,
> some experimentation then showed that that the order in which the jobs
> are listed in the action YAML file doesn’t necessarily match that in
> which they are returned from the API, so there was no way to calculate
> the value to pass for job-index. Hence, switched to using the job’s
> `name` instead.
  • Loading branch information
lawrence-forooghian committed Jan 23, 2024
1 parent 9d1163f commit 8db1987
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ jobs:
TEST_OBSERVABILITY_SERVER_AUTH_KEY: ${{ secrets.TEST_OBSERVABILITY_SERVER_AUTH_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Scripts/upload_test_results.sh --job-index ${{ strategy.job-index }}
Scripts/upload_test_results.sh --job-name "check (${{ matrix.platform }}, ${{ matrix.lane }})"
45 changes: 29 additions & 16 deletions Scripts/upload_test_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Options:
# -u / --upload-server-base-url <url>: Allows you to specify a URL to use as the upload server base URL. Defaults to https://test-observability.herokuapp.com.
# -i / --iteration <number>: If running the tests in a loop inside a single CI job, indicates which iteration of the loop is currently executing. Defaults to 1.
# -j / --job-index <number>: The index to which the current job corresponds in the response from the "list jobs for a workflow run attempt" GitHub API (https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#list-jobs-for-a-workflow-run-attempt). If you specify `GITHUB_TOKEN` but not `--job-index`, and the response from this API contains more than one job, the script will fail.
# -j / --job-name <name> (optional): The `name` property of the object corresponding to the current job in the response from the "list jobs for a workflow run attempt" GitHub API (https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#list-jobs-for-a-workflow-run-attempt). If there is more than one object in the response whose `name` property equals this value, the action will fail. If you specify `GITHUB_TOKEN` but not `--job-name`, and the response from this API contains more than one job, the script will fail.
#
# Optional environment variables:
#
Expand Down Expand Up @@ -103,7 +103,7 @@ while [[ "$#" -gt 0 ]]; do
case $1 in
-i|--iteration) iteration="$2"; shift ;;
-u|--upload-server-base-url) upload_server_base_url="$2"; shift ;;
-j|--job-index) job_index="$2"; shift ;;
-j|--job-name) job_name="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
Expand Down Expand Up @@ -217,32 +217,45 @@ then
temp_github_jobs_response_file=$(mktemp)
gh api "/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}/jobs" > $temp_github_jobs_response_file

number_of_jobs=$(jq '.jobs | length' < "${temp_github_jobs_response_file}")
matching_jobs_file=$(mktemp)

if [[ -z $job_index && $number_of_jobs -gt 1 ]]
if [[ -z $job_name ]]
then
echo -e "Got ${number_of_jobs} jobs from GitHub API but don’t know which one to pick. You need to provide a --job-index argument." 2>&1
exit 1
fi
number_of_jobs=$(jq '.jobs | length' < "${temp_github_jobs_response_file}")

if [[ -n $job_index ]]
then
if [[ $job_index -gt $number_of_jobs ]]
if [[ $number_of_jobs -eq 0 ]]
then
echo -e "Got no jobs from GitHub API." 2>&1
exit 1
fi

if [[ $number_of_jobs -gt 1 ]]
then
echo -e "The --job-index argument has value ${job_index}, but there are only ${number_of_jobs} jobs. This script does not currently handle pagination." 2>&1
echo -e "Got ${number_of_jobs} jobs from GitHub API but don’t know which one to pick. You need to provide a --job-name argument." 2>&1
exit 1
fi

jq '.jobs' < "${temp_github_jobs_response_file}" > "${matching_jobs_file}"
else
if [[ $number_of_jobs -eq 0 ]]
jq --arg job_name "${job_name}" '.jobs | map(select(.name == $job_name))' < "${temp_github_jobs_response_file}" > "${matching_jobs_file}"

number_of_matching_jobs=$(jq '. | length' < "${matching_jobs_file}")

if [[ $number_of_matching_jobs -eq 0 ]]
then
echo -e "The GitHub API response contains no job whose \`name\` is ${job_name}. This script does not currently handle pagination." 2>&1
exit 1
fi

if [[ $number_of_matching_jobs -gt 1 ]]
then
echo -e "The GitHub API response contains no jobs." 2>&1
echo -e "The GitHub API response contains multiple jobs whose \`name\` is ${job_name}." 2>&1
exit 1
fi
job_index=0
fi

github_job_api_url=$(jq --exit-status --raw-output ".jobs[${job_index}].url" < "${temp_github_jobs_response_file}")
github_job_html_url=$(jq --exit-status --raw-output ".jobs[${job_index}].html_url" < "${temp_github_jobs_response_file}")
github_job_api_url=$(jq --exit-status --raw-output ".[0].url" < "${matching_jobs_file}")
github_job_html_url=$(jq --exit-status --raw-output ".[0].html_url" < "${matching_jobs_file}")
fi

# 9. Create the JSON request body.
Expand Down

0 comments on commit 8db1987

Please sign in to comment.