# If a PR doesn't have a milestone assigned when it's merged, assign it the
# one that's scheduled next.
name: Assign Latest Milestone
on:
  pull_request:
    types: [closed]

jobs:
  sync:
    name: Assign Latest Milestone
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        name: Run script
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            if (!context.payload.pull_request.merged) {
              console.log('PR was not merged, skipping.');
              return;
            }
            if (!!context.payload.pull_request.milestone) {
              console.log('PR has existing milestone, skipping.');
              return;
            }
            if (context.payload.pull_request.user.type === 'Bot') {
              console.log('Ignoring Bot PR');
              return;
            }
            milestones = await github.issues.listMilestones({
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'open',
              sort: 'due_on',
              direction: 'asc'
            })

            if (milestones.data.length === 0) {
              console.log('There are no milestones, skipping.');
              return;
            }

            await github.issues.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.pull_request.number,
              milestone: milestones.data[0].number
            });

# If a PR doesn't have a milestone assigned when it's merged, assign it the
# one that's scheduled next.
name: Assign Latest Milestone
on:
  pull_request_target:
    types: [closed]
    branches: [main]

permissions:
  pull-requests: write
  issues: write

jobs:
  sync:
    name: Assign Latest Milestone
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        name: Run script
        with:
          script: |
            if (!context.payload.pull_request.merged) {
              console.log('PR was not merged, skipping.');
              return;
            }
            if (!!context.payload.pull_request.milestone) {
              console.log('PR has existing milestone, skipping.');
              return;
            }
            if (context.payload.pull_request.user.type === 'Bot') {
              console.log('Ignoring Bot PR');
              return;
            }
            milestones = await github.rest.issues.listMilestones({
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'open',
              sort: 'due_on',
              direction: 'asc'
            })

            if (milestones.data.length === 0) {
              console.log('There are no milestones, skipping.');
              return;
            }

            console.log(`Adding to milestone: ${milestones.data[0].number}`);
            result = await github.rest.issues.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.number,
              milestone: milestones.data[0].number
            });
            console.log(result);

Fix assign milestone for better permissions
CI: Fix assign milestone for better permissions
