Limit Introductions in Welcome Category #493
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Limit Introductions in Welcome Category | |
| on: | |
| discussion: | |
| types: [created, category_changed] | |
| permissions: | |
| contents: read | |
| discussions: write | |
| jobs: | |
| limit-welcome-discussions: | |
| runs-on: ubuntu-latest | |
| # Run when a discussion is created in the category, or moved into the category | |
| if: > | |
| github.event.discussion.category.name == 'A Welcome to GitHub' && | |
| (github.event.action == 'created' || github.event.changes.category != null) | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.11" | |
| - name: Check if GitHub employee (member of org "github") | |
| id: check_employee | |
| env: | |
| ORG_MEMBERS_TOKEN: ${{ secrets.READ_GITHUB_ORG_MEMBERS_TOKEN }} | |
| USERNAME: ${{ github.event.discussion.user.login }} | |
| run: python .github/workflows/scripts/check_employee.py | |
| - name: Check for prior discussion in Welcome category | |
| id: check_prior | |
| if: steps.check_employee.outputs.is_employee != 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| OWNER: ${{ github.repository_owner }} | |
| REPO: ${{ github.event.repository.name }} | |
| CURRENT_DISCUSSION_NUMBER: ${{ github.event.discussion.number }} | |
| AUTHOR_LOGIN: ${{ github.event.discussion.user.login }} | |
| IS_EMPLOYEE: ${{ steps.check_employee.outputs.is_employee }} | |
| run: python .github/workflows/scripts/limit_welcome_category_discussions.py | |
| - name: Post comment and close duplicate discussion | |
| if: steps.check_employee.outputs.is_employee != 'true' && steps.check_prior.outputs.has_prior == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const discussionId = context.payload.discussion.node_id; | |
| // Post a comment explaining the restriction | |
| await github.graphql(` | |
| mutation($discussionId: ID!, $body: String!) { | |
| addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { | |
| comment { id } | |
| } | |
| } | |
| `, { | |
| discussionId, | |
| body: "👋 Thanks for being part of our community! We noticed you've already created an introduction in the **A Welcome to GitHub** category. We only allow **one introduction per person** in this category (including closed ones).\n\nIf you have questions or would like to start a new conversation, please navigate to another category.\n\nThis discussion has been closed." | |
| }); | |
| // Close the duplicate discussion | |
| await github.graphql(` | |
| mutation($discussionId: ID!) { | |
| closeDiscussion(input: {discussionId: $discussionId, reason: DUPLICATE}) { | |
| discussion { closed } | |
| } | |
| } | |
| `, { | |
| discussionId | |
| }); | |
| console.log("Duplicate introduction discussion closed."); |