name: Auto Assign Bump Issues
on:
    pull_request:
        types: [ opened ]

jobs:
    auto-assign:
        runs-on: ubuntu-latest
        permissions:
            pull-requests: write
            contents: write
        steps:
            -   name: Check if PR title starts with "Bump"
                uses: actions/github-script@v7
                with:
                    script: |
                        const prTitle = context.payload.pull_request.title;
                        return prTitle.startsWith('Bump');
                id: check_bump_pr

            -   name: Assign the PR
                if: steps.check_bump_pr.outputs.result == 'true'
                uses: actions-ecosystem/action-add-assignees@v1
                with:
                    github_token: ${{ secrets.github_token }}
                    assignees: henri-tremblay
            -   name: Get open milestones
                if: steps.check_bump_pr.outputs.result == 'true'
                uses: actions/github-script@v7
                with:
                    script: |
                        const { data: milestones } = await github.rest.issues.listMilestones({
                          owner: context.repo.owner,
                          repo: context.repo.repo,
                          state: 'open'
                        });

                        if (milestones.length === 0) {
                          core.setFailed('No open milestones found');
                        } else {
                          return milestones[0].number;
                        }

                id: get_open_milestone

            -   name: Assign the PR to the milestone
                if: steps.get_open_milestone.outputs.result
                uses: actions/github-script@v7
                with:
                    script: |
                        const milestoneNumber = parseInt("${{ steps.get_open_milestone.outputs.result }}", 10);
                        const prNumber = context.issue.number;
                        core.info(`Updating pull request ${prNumber} with milestone ${milestoneNumber}`);
                        const response = await github.rest.issues.update({
                          owner: context.repo.owner,
                          repo: context.repo.repo,
                          issue_number: prNumber,
                          milestone: milestoneNumber
                        });

