Skip to content

Commit f08983e

Browse files
committed
correctly get PR number for forks, and exit workflow if PR number or SHA is empty
1 parent b41c1dc commit f08983e

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

.github/workflows/upload-dev-build.yml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
inputs:
1010
pr_number:
1111
description: 'PR Number to deploy'
12-
required: true
12+
required: false
1313
run_id:
1414
description: 'The Run ID of the CI workflow that has the artifact'
1515
required: true
@@ -44,14 +44,35 @@ jobs:
4444
if [ "${{ github.event_name }}" == "workflow_run" ]; then
4545
SHA="${{ github.event.workflow_run.head_sha }}"
4646
else
47-
echo "Fetching latest SHA for PR #$PR_NUM..."
48-
SHA=$(gh pr view "$PR_NUM" --repo ${{ github.repository }} --json headRefOid --template '{{.headRefOid}}')
47+
echo "Fetching latest SHA for PR #${PR_NUM}..."
48+
if [ -n "${PR_NUM}" ]; then
49+
SHA=$(gh pr view "${PR_NUM}" --repo "${{ github.repository }}" --json headRefOid --template '{{.headRefOid}}')
50+
fi
51+
fi
52+
53+
# Validate that we have a SHA
54+
if [ -z "${SHA}" ]; then
55+
echo "Failed to get commit SHA, exiting"
56+
exit 1
57+
fi
58+
59+
# If PR_NUM is empty (this is the case for forks) get PR number using SHA
60+
if [ -z "${PR_NUM}" ]; then
61+
PR_NUM=$(gh pr list --search "sha:${SHA}" --state open --json number --jq '.[0].number')
62+
fi
63+
64+
# Validate that we have a PR number and that it is less than 5 characters
65+
if [ -z "${PR_NUM}" ] || [ ${#PR_NUM} -gt 5 ]; then
66+
echo "Failed to get PR number, exiting (PR_NUM=${PR_NUM})"
67+
exit 1
4968
fi
69+
5070
SHORT_SHA=${SHA::7}
5171
UPLOAD_DIR_NAME="upload"
5272
53-
echo "Using SHA: ${SHA}"
73+
echo "SHA: ${SHA}"
5474
echo "Short SHA: ${SHORT_SHA}"
75+
echo "PR number: ${PR_NUM}"
5576
mkdir -p "${UPLOAD_DIR_NAME}/pr-${PR_NUM}/latest"
5677
mkdir -p "${UPLOAD_DIR_NAME}/pr-${PR_NUM}/${SHORT_SHA}"
5778
# Copy all 3 artifacts (plotly.js, plotly.min.js, plot-schema.json) to /latest/
@@ -114,5 +135,5 @@ jobs:
114135
echo "### PR Build Uploaded" >> $GITHUB_STEP_SUMMARY
115136
echo "Builds for PR #${{ steps.setup-metadata.outputs.PR_NUM }} can be accessed at:" >> $GITHUB_STEP_SUMMARY
116137
echo "- Latest build for this PR: [$BASE/latest/plotly.min.js]($BASE/latest/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
117-
echo "- Build for this commit: [$BASE/${{ steps.setup-metadata.outputs.SHA }}/plotly.min.js]($BASE/${{ steps.setup-metadata.outputs.SHA }}/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
138+
echo "- Build for this commit: [$BASE/${{ steps.setup-metadata.outputs.SHA }}/plotly.min.js]($BASE/${{ steps.setup-metadata.outputs.SHORT_SHA }}/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
118139
echo "The above links should start working a minute or two after this job completes." >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)