name: Issue Labeled

on:
  issues:
    types: [labeled]

permissions: {}

jobs:
  invalid-template-label:
    name: Re-validate invalid template label
    if: "github.event.label.name == 'flag: invalid template'"
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
    steps:
      - name: Checkout base branch
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Install scripts
        run: cd .github/scripts && npm install --ignore-scripts

      - name: Handle invalid template label
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: node --experimental-strip-types .github/scripts/issue-template-label-action.ts

  issue-labeled:
    name: Handle issue label automation
    if: "github.event.label.name != 'flag: invalid template'"
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
      pull-requests: write
      actions: read
      checks: read
      statuses: read
    steps:
      - name: Handle issue label automation
        uses: actions/github-script@v9
        with:
          script: |
            const { owner, repo } = context.repo;
            const issue_number = context.payload.issue.number;
            const login = context.payload.issue.user.login;
            const label = context.payload.label.name;

            const comment = async (body) => {
              await github.rest.issues.createComment({ owner, repo, issue_number, body });
            };

            const close = async (state_reason) => {
              await github.rest.issues.update({
                owner,
                repo,
                issue_number,
                state: 'closed',
                state_reason,
              });
            };

            const lock = async (lock_reason) => {
              await github.rest.issues.lock({ owner, repo, issue_number, lock_reason });
            };

            const handlers = {
              'status: can not reproduce': async () => {
                await comment(
                  `> This is a templated message

            Hello @${login},

            Thank you for reporting this bug, however we are unable to reproduce the issue you described given the information we have on hand. Can you please create a fresh project that you are able to reproduce the issue in, provide clear steps to reproduce this issue, and either upload this fresh project to a new GitHub repo or compress it into a \`.zip\` and upload it on this issue?

            We would greatly appreciate your assistance with this, by working in a fresh project it will cut out any possible variables that might be unrelated.
            Please note that issues labeled with \`status: can not reproduce\` will be closed in 14 days if there is no activity.

            Thank you!`
                );
              },

              'flag: v3-unsupported': async () => {
                await comment(
                  `> This is a templated message

            Hello @${login},

            Thank you for reporting this potential bug report, please keep in mind that we are no longer accepting bug reports for Strapi v3.
            The recommended action we suggest for v3 users is to utilize the various migration resources to upgrade your package from Strapi v3 to Strapi v4:

            - [Code Migration Guide](https://docs.strapi.io/developer-docs/latest/update-migration-guides/migration-guides/v4/code-migration.html) and/or [Codemods project](https://github.com/strapi/codemods)
            - [Data Migration Guide](https://docs.strapi.io/developer-docs/latest/update-migration-guides/migration-guides/v4/data-migration.html) and/or [Data Migration Scripts](https://github.com/strapi/migration-scripts)
            - [Plugin Migration Guide](https://docs.strapi.io/developer-docs/latest/update-migration-guides/migration-guides/v4/plugin-migration.html)

            Please see our [Security file](https://github.com/strapi/strapi/security) for more information about supported versions.
            For now this issue is marked as closed.

            Thank You`
                );
                await close('not_planned');
              },

              'flag: v4-unsupported': async () => {
                await comment(
                  `> This is a templated message

            Hello @${login},

            Thank you for reporting this potential bug report, please keep in mind that we are no longer accepting low/medium severity bug reports for Strapi v4.
            We have confirmed that this issue is a low/medium severity bug and is not reproducable in Strapi 5. We advise upgrading to Strapi 5 to resolve this issue.

            The recommended action we suggest for v4 users is to utilize the various migration resources to upgrade from Strapi 4 to Strapi 5:

            - [Updating documentation](https://docs.strapi.io/dev-docs/migration/v4-to-v5/introduction-and-faq)
            - [Step by Step Guide](https://docs.strapi.io/dev-docs/migration/v4-to-v5/step-by-step)
            - [Upgrade Tool](https://docs.strapi.io/dev-docs/upgrade-tool)
            - [Breaking Changes list](https://docs.strapi.io/dev-docs/migration/v4-to-v5/breaking-changes)
            - [Specific Resources](https://docs.strapi.io/dev-docs/migration/v4-to-v5/additional-resources/introduction)

            Please see our [Security file](https://github.com/strapi/strapi/security) for more information about supported versions.
            For now this issue is marked as closed.

            Thank You`
                );
                await close('not_planned');
              },

              'issue: feature request': async () => {
                await comment(
                  `> This is a templated message

            Hello @${login},

            First thank you for reporting this feature need.
            To manage feature requests and the Strapi roadmap, we are using Canny.
            You will be able to access the Public Roadmap here: https://feedback.strapi.io.

            In your message, please mention the URL of this thread in case some messages are posted there. But the most important is to have your feedback posted on our feedback/roadmap site.
            The product team is reading EVERY comment, that really helps us to develop the project in the right direction. We are keeping all feature requests and project insights in one place, our feedback website.

            In order to keep our GitHub issues clean and for valid bug reports this issue will be marked as closed, but please feel free to continue the discussion with other community members here.

            Thank you for your insight and have a good day.`
                );
                await close('completed');
              },

              'flag: question': async () => {
                await comment(
                  `> This is a templated message

            Hello @${login},

            Thank you for your interest in Strapi! However, this issue appears to be a question rather than a bug report. GitHub Issues are reserved for reproducible bug reports.

            Please ask your question in our [GitHub Discussions](https://github.com/strapi/strapi/discussions) where the community and team can help you out.

            You can also check out our [Discord](https://discord.strapi.io) for real-time help.

            Thank you!`
                );
                await close('not_planned');
                await lock('off_topic');
              },
            };

            const handler = handlers[label];

            if (!handler) {
              core.info(`No automation configured for label "${label}"`);
              return;
            }

            core.info(`Running automation for label "${label}" on issue #${issue_number}`);
            await handler();
