diff --git a/.github/scripts/validate-pr-number.js b/.github/scripts/validate-pr-number.js new file mode 100644 index 00000000000000..d65dfd7e525270 --- /dev/null +++ b/.github/scripts/validate-pr-number.js @@ -0,0 +1,36 @@ +// @ts-check + +const { readFileSync } = require('node:fs'); + +module.exports = main; + +/** + * + * @param {{filePath:string}} options + * @returns {number} + */ +function main(options) { + return validatePrNumber(options.filePath); +} + +/** + * + * @param {string} filePath + * @returns {number} + */ +function validatePrNumber(filePath) { + try { + const content = readFileSync(filePath, 'utf-8').trim(); + const prNumber = Number(content); + + if (isNaN(prNumber) || !Number.isInteger(prNumber)) { + throw new Error('The ID in pr.txt is not a valid PR number.'); + } + + console.info('✅ PR ID valid'); + return prNumber; + } catch (err) { + console.error(`Error: ${err.message}`); + process.exit(1); + } +} diff --git a/.github/workflows/pr-website-deploy-comment.yml b/.github/workflows/pr-website-deploy-comment.yml index cb10d9b5f6a6bb..d636e5ad9ddcde 100644 --- a/.github/workflows/pr-website-deploy-comment.yml +++ b/.github/workflows/pr-website-deploy-comment.yml @@ -19,14 +19,18 @@ jobs: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }} outputs: - pr_number: ${{ steps.pr_number.outputs.id }} + pr_number: ${{ steps.pr_number.outputs.result }} website_url: ${{ steps.website_url.outputs.id }} permissions: id-token: write steps: - - name: Download WebSite artifacts - uses: actions/download-artifact@v4 + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github + + - uses: actions/download-artifact@v4 with: name: pr-website-artifacts path: ./website @@ -41,13 +45,18 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} - name: Load PR number + uses: actions/github-script@v7 id: pr_number - run: echo "id=$(cat pr.txt)" >> $GITHUB_OUTPUT - working-directory: ./results + with: + script: | + const run = require('./.github/scripts/validate-pr-number'); + const result = run({filePath:'results/pr.txt'}); + return result; + result-encoding: string - name: Load WEBSITE URL id: website_url - run: echo "id=${{env.DEPLOY_HOST_URL}}pull/${{steps.pr_number.outputs.id}}/" >> $GITHUB_OUTPUT + run: echo "id=${{env.DEPLOY_HOST_URL}}pull/${{steps.pr_number.outputs.result}}/" >> $GITHUB_OUTPUT - name: Login via Azure CLI uses: azure/login@v2 @@ -67,7 +76,7 @@ jobs: --destination '$web' \ --source ./website \ --account-name ${{ env.AZURE_STORAGE }} \ - --destination-path pull/${{steps.pr_number.outputs.id}} \ + --destination-path pull/${{steps.pr_number.outputs.result}} \ --auth-mode login \ --overwrite