name: Welcome Good First Issue

on:
  issues:
    types: [labeled]

permissions:
  issues: write

jobs:
  comment-on-good-first-issue:
    if: github.event.label.name == 'good first issue'
    runs-on: ubuntu-latest
    steps:
      - name: Check if welcome comment already exists
        id: check_comment
        uses: actions/github-script@v7
        with:
          result-encoding: string
          script: |
            const issueNumber = context.issue.number;
            const comments = await github.rest.issues.listComments({
              ...context.repo,
              issue_number: issueNumber
            });

            const alreadyCommented = comments.data.some(
              (comment) =>
                comment.body.includes('<!-- auto-comment:good-first-issue -->')
            );

            return alreadyCommented ? 'true' : 'false';

      - name: Leave welcome comment
        if: steps.check_comment.outputs.result == 'false'
        uses: actions/github-script@v7
        with:
          script: |
            const repoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}`;

            await github.rest.issues.createComment({
              ...context.repo,
              issue_number: context.issue.number,
              body: "🙌 **Hey there, future contributor!** 🙌\n\n" +
                    "This issue has been labeled as **good first issue**, which means it's a great place to get started with the OpenHands project.\n\n" +
                    "If you're interested in working on it, feel free to! No need to ask for permission.\n\n" +
                    "Be sure to check out our [development setup guide](" + repoUrl + "/blob/main/Development.md) to get your environment set up, and follow our [contribution guidelines](" + repoUrl + "/blob/main/CONTRIBUTING.md) when you're ready to submit a fix.\n\n" +
                    "Feel free to join our developer community on [Slack](https://all-hands.dev/joinslack). You can ask for [help](https://openhands-ai.slack.com/archives/C078L0FUGUX), [feedback](https://openhands-ai.slack.com/archives/C086ARSNMGA), and even ask for a [PR review](https://openhands-ai.slack.com/archives/C08D8FJ5771).\n\n" +
                    "🙌 Happy hacking! 🙌\n\n" +
                    "<!-- auto-comment:good-first-issue -->"
            });
