# This bot updates the issue with number DEPRECATION_TRACKER_ISSUE
# with the PR number that issued the deprecation.

# It runs on commits to main, and will trigger if the PR linked to a merged commit has the "Deprecate" label
name: Deprecations Bot

on:
  push:
    branches:
      - main


permissions: {}

jobs:
  deprecation_update:
    permissions:
      contents: read
      issues: write
    runs-on: ubuntu-24.04
    env:
      DEPRECATION_TRACKER_ISSUE: 56596
    steps:
    - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
      id: update-deprecation-issue
      with:
        script: |
          body = await github.rest.issues.get({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: ${{ env.DEPRECATION_TRACKER_ISSUE }},
          })
          body = body["data"]["body"];
          linkedPRs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
            owner: context.repo.owner,
            repo: context.repo.repo,
            commit_sha: '${{ github.sha }}'
          })
          linkedPRs = linkedPRs["data"];
          console.log(linkedPRs);
          if (linkedPRs.length > 0) {
            console.log("Found linked PR");
            linkedPR = linkedPRs[0]
            isDeprecation = false
            for (label of linkedPR["labels"]) {
              if (label["name"] == "Deprecate") {
                isDeprecation = true;
                break;
              }
            }

            PR_NUMBER = linkedPR["number"];

            body += ("\n- [ ] #" + PR_NUMBER);
            if (isDeprecation) {
              console.log("PR is a deprecation PR. Printing new body of issue");
              console.log(body);
              github.rest.issues.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: ${{ env.DEPRECATION_TRACKER_ISSUE }},
                body: body
              })
            }
          }
