on:
  issues:
    types: [opened]

permissions:
  contents: read

jobs:
  issue_triage:
    name: Issue Opened Triage
    permissions:
      contents: read
      issues: write  # for github/issue-labeler to create or remove labels
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
    - uses: github/issue-labeler@98b5412841f6c4b0b3d9c29d53c13fad16bd7de2 # v3.2
      with:
        repo-token: "${{ secrets.GITHUB_TOKEN }}"
        configuration-path: .github/labeler-issue-triage.yml
        enable-versioned-regex: 0
  label-question:
    name: Redirect to discuss
    permissions:
      contents: read
      issues: write  # for actions/github-script to create comments and add labels
    if: contains(github.event.issue.labels.*.name, 'question')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `
                Hi 👋 thanks for reaching out.

                For general questions we recommend reaching out to the [community forum](https://discuss.hashicorp.com/c/packer) for greater visibility.
                As the GitHub issue tracker is only watched by a small subset of maintainers and is really reserved for bugs and enhancements, you'll have a better chance of finding someone who can help you in the forum.
                We'll mark this issue as needs-reply to help inform maintainers that this question is awaiting a response.
                If no activity is taken on this question within 30 days it will be automatically closed.

                If you find the forum to be more helpful or if you've found the answer to your question elsewhere please feel free to post a response and close the issue.`
            })

      - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
        with:
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ['needs-reply']
            })

