diff --git a/.github/workflows/notify-on-an-bdu-label.yml b/.github/workflows/notify-on-an-bdu-label.yml index 31f378bb4a..eae9b878b0 100644 --- a/.github/workflows/notify-on-an-bdu-label.yml +++ b/.github/workflows/notify-on-an-bdu-label.yml @@ -7,52 +7,50 @@ on: permissions: pull-requests: read + contents: read jobs: - check-label-timing: - name: Check if label was added after PR creation + notify-bdu: + name: Notify Teams when A/N BDU label is added runs-on: ubuntu-latest - outputs: - should-notify: ${{ steps.check.outputs.should-notify }} + timeout-minutes: 5 steps: - - name: Check label timing - id: check + - name: Check if label was added after PR creation + id: check_label_timing uses: actions/github-script@v7 with: script: | - const pr = context.payload.pull_request; - const timeline = await github.rest.issues.listEventsForTimeline({ + const prCreatedAt = new Date('${{ github.event.pull_request.created_at }}'); + + // Get PR timeline to find when label was added + const timeline = await github.rest.issues.listEvents({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: pr.number + issue_number: context.issue.number, }); - - const createdTime = new Date(pr.created_at); - const labelEvent = timeline.data.find(event => - event.event === 'labeled' && event.label.name === 'A/N BDU' + + // Find the most recent 'labeled' event for "A/N BDU" + const labelEvent = timeline.data.reverse().find(event => + event.event === 'labeled' && + event.label.name === 'A/N BDU' ); - - if (labelEvent) { - const labelTime = new Date(labelEvent.created_at); - // If label was added more than 1 second after PR creation, notify - const shouldNotify = (labelTime - createdTime) > 1000; - core.setOutput('should-notify', shouldNotify.toString()); + + if (!labelEvent) { + core.setOutput('should_notify', 'false'); + return; + } + + const labelAddedAt = new Date(labelEvent.created_at); + + // Only proceed if label was added AFTER PR was created + if (labelAddedAt > prCreatedAt) { + core.setOutput('should_notify', 'true'); } else { - core.setOutput('should-notify', 'false'); + core.setOutput('should_notify', 'false'); } - notify-bdu: - name: Notify Teams when A/N BDU label is added - needs: check-label-timing - runs-on: ubuntu-latest - timeout-minutes: 5 - if: >- - github.event.action == 'labeled' && - github.event.label.name == 'A/N BDU' && - github.event.pull_request.state == 'open' && - needs.check-label-timing.outputs.should-notify == 'true' - steps: - name: Send Teams notification + if: steps.check_label_timing.outputs.should_notify == 'true' uses: Alfresco/alfresco-build-tools/.github/actions/send-teams-notification@1d671f8f10336861c89c67be57c6648d98876103 # v18.19.0 with: webhook-url: ${{ secrets.TEAMS_NOTIFICATION_ADF_BDU_WEBHOOK }} @@ -65,3 +63,4 @@ jobs: - **Repository:** ${{ github.repository }} - **PR:** [#${{ github.event.pull_request.number }} ${{ github.event.pull_request.title }}](${{ github.event.pull_request.html_url }}) - **Author:** ${{ github.event.pull_request.user.login }} + \ No newline at end of file