on:   # yamllint disable-line rule:truthy
  workflow_run:
    workflows: [Check semver breaks]
    types: [completed]

name: Check semver breaks - Label and Comment PR

jobs:
  Download:
    name: Download, Unzip and Add Labels/Comments
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      pull-requests: write
    # only run if CI passes on the "Check semver breaks" workflow
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - name: "Download artifact"
        uses: actions/github-script@v7
        with:
          script: |
            // get all artifacts from the workflow run
            let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: context.payload.workflow_run.id,
            });

            // find the artifact that starts with 'semver-break'
            let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
              return artifact.name.startsWith('semver-break');
            })[0];

            // if no artifact found, exit
            if (!matchArtifact) {
              console.log('No semver-break artifact found');
              process.exit(0);
            }

            // otherwise download the artifact
            let download = await github.rest.actions.downloadArtifact({
              owner: context.repo.owner,
              repo: context.repo.repo,
              artifact_id: matchArtifact.id,
              archive_format: 'zip',
            });

            // write the artifact to the workspace
            let fs = require('fs');
            fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/semver-break.zip`, Buffer.from(download.data));
      - name: "Unzip artifact"
        if: ${{ hashFiles('semver-break.zip') != '' }}
        run: unzip -n semver-break.zip
      - name: "Comment and add label on PR - Semver break"
        uses: actions/github-script@v7
        if: ${{ hashFiles('semver-break') != '' }}
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            // sanitize and get the PR number from the semver-break file
            const fs = require('fs');
            let issue_number = parseInt(fs.readFileSync('semver-break', 'utf8'), 10);

            // assure that is not NaN using Number.isNaN
            // since does not coerce the value to a number like isNaN
            if (Number.isNaN(issue_number)) {
              console.log('PR_NUMBER is not a number');
              process.exit(1);
            }

            // comment on the PR
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue_number,
              body: ':rotating_light: API BREAKING CHANGE DETECTED\n\nTo see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.'
            });

            // add the label to the PR
            await github.rest.issues.addLabels({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue_number,
              labels: ['API break']
            });
