diff --git a/.github/workflows/semver-checks-pr-label.yml b/.github/workflows/semver-checks-pr-label.yml new file mode 100644 index 000000000..2cbfffd64 --- /dev/null +++ b/.github/workflows/semver-checks-pr-label.yml @@ -0,0 +1,85 @@ +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-latest + 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 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' + }); + + // 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'] + }); diff --git a/.github/workflows/semver-checks.yml b/.github/workflows/semver-checks.yml index 4462f7efb..eb89381c8 100644 --- a/.github/workflows/semver-checks.yml +++ b/.github/workflows/semver-checks.yml @@ -22,15 +22,20 @@ jobs: run: cargo binstall cargo-semver-checks --no-confirm - name: "Run semver checker script" run: ./contrib/check-semver.sh - - name: "Add PR label to breaking changes" - uses: actions-ecosystem/action-add-labels@v1 + - name: Save PR number if: ${{ hashFiles('semver-break') != '' }} - with: - labels: "API break" - - name: Comment PR - uses: thollander/actions-comment-pull-request@v2 + env: + PR_NUMBER: ${{ github.event.number }} + run: | + # check if PR_NUMBER is a number + if ! [[ "$PR_NUMBER" =~ ^-?[0-9]+$ ]]; then + echo "$PR_NUMBER is not a number." + exit 1 + fi + echo "$PR_NUMBER" > ./semver-break + - name: "Save breaking state" if: ${{ hashFiles('semver-break') != '' }} + uses: actions/upload-artifact@v4 with: - message: | - :rotating_light: API BREAKING CHANGE DETECTED - + name: semver-break + path: semver-break