Signup - EvanSHI888 #725
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: Signup Issue Automation | |
| on: | |
| issues: | |
| types: [closed] | |
| permissions: | |
| contents: write | |
| issues: write | |
| jobs: | |
| process-signup: | |
| runs-on: ubuntu-latest | |
| if: github.event.action == 'closed' && github.event.issue.state_reason == 'completed' && startsWith(github.event.issue.title, 'Signup - ') | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.PAT_WITH_INVITE_PERMISSIONS }} | |
| - name: Extract GitHub ID and process signup | |
| id: extract-info | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.PAT_WITH_INVITE_PERMISSIONS }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const issue = context.payload.issue; | |
| console.log('Processing signup issue:', issue.title); | |
| console.log('Issue body:', issue.body); | |
| // Extract GitHub ID from issue body | |
| const githubIdMatch = issue.body.match(/GitHub\s+ID\s*:\s*([^\s\n]+)/i); | |
| if (!githubIdMatch) { | |
| console.error('GitHub ID not found in issue body'); | |
| core.setFailed('GitHub ID not found in issue body'); | |
| return; | |
| } | |
| const githubId = githubIdMatch[1].trim(); | |
| console.log('Extracted GitHub ID:', githubId); | |
| // Validate GitHub username | |
| try { | |
| const { data: user } = await github.rest.users.getByUsername({ | |
| username: githubId | |
| }); | |
| console.log('GitHub user found:', user.login); | |
| } catch (error) { | |
| console.error('Invalid GitHub username:', githubId); | |
| core.setFailed(`Invalid GitHub username: ${githubId}`); | |
| return; | |
| } | |
| // Extract information from issue body for creating user file | |
| const nameMatch = issue.body.match(/\*\*(?:姓名|Name)[\s::]*\*\*\s*([^\n]+)/i) || | |
| issue.body.match(/(?:姓名|Name)[\s::]+([^\n]+)/i); | |
| const timezoneMatch = issue.body.match(/\*\*(?:时区|Timezone)[\s::]*\*\*\s*([^\n]+)/i) || | |
| issue.body.match(/(?:时区|Timezone)[\s::]+([^\n]+)/i); | |
| const contactMatch = issue.body.match(/\*\*(?:Telegram)[\s::]*\*\*\s*([^\n]+)/i) || | |
| issue.body.match(/(?:Telegram)[\s::]+([^\n]+)/i); | |
| const introMatch = issue.body.match(/\*\*(?:自我介绍|Self-introduction)[\s::]*\*\*\s*([^\n]+)/i) || | |
| issue.body.match(/(?:自我介绍|Self-introduction)[\s::]+([^\n]+)/i); | |
| const userName = nameMatch ? nameMatch[1].trim() : githubId; | |
| const userTimezone = timezoneMatch ? timezoneMatch[1].trim() : 'UTC+8'; | |
| const userContact = contactMatch ? contactMatch[1].trim() : ''; | |
| const userIntro = introMatch ? introMatch[1].trim() : ''; | |
| // Set output for next step | |
| core.setOutput('github_id', githubId); | |
| core.setOutput('user_name', userName); | |
| core.setOutput('user_timezone', userTimezone); | |
| core.setOutput('user_contact', userContact); | |
| core.setOutput('user_intro', userIntro); | |
| core.setOutput('issue_body', issue.body); | |
| - name: Create user markdown file | |
| env: | |
| GITHUB_ID: ${{ steps.extract-info.outputs.github_id }} | |
| USER_NAME: ${{ steps.extract-info.outputs.user_name }} | |
| USER_TIMEZONE: ${{ steps.extract-info.outputs.user_timezone }} | |
| USER_CONTACT: ${{ steps.extract-info.outputs.user_contact }} | |
| USER_INTRO: ${{ steps.extract-info.outputs.user_intro }} | |
| run: | | |
| # Create filename | |
| FILENAME="${GITHUB_ID}.md" | |
| # Check if file already exists | |
| if [ -f "$FILENAME" ]; then | |
| echo "File $FILENAME already exists, skipping creation" | |
| exit 0 | |
| fi | |
| # Get current date | |
| CURRENT_DATE=$(date +%Y.%m.%d) | |
| # Create the markdown file with formatted content based on template | |
| cat > "$FILENAME" << EOF | |
| --- | |
| timezone: ${USER_TIMEZONE} | |
| --- | |
| # ${USER_NAME} | |
| **GitHub ID:** ${GITHUB_ID} | |
| **Telegram:** ${USER_CONTACT} | |
| ## Self-introduction | |
| ${USER_INTRO} | |
| ## Notes | |
| <!-- Content_START --> | |
| # ${CURRENT_DATE} | |
| <!-- Content_END --> | |
| EOF | |
| echo "Created file: $FILENAME" | |
| # Configure git | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| # Add and commit the file | |
| git add "$FILENAME" | |
| git commit -m "Add signup file for user: $GITHUB_ID" | |
| git push | |
| - name: Comment on issue | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const issue_number = context.payload.issue.number; | |
| const githubId = '${{ steps.extract-info.outputs.github_id }}'; | |
| try { | |
| await github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number, | |
| body: `✅ Signup processed successfully! | |
| - ✅ User file \`${githubId}.md\` has been created | |
| Welcome to this Intensive CoLearning! 🎉` | |
| }); | |
| console.log(`Comment posted on issue #${issue_number}`); | |
| } catch (error) { | |
| console.error(`Error posting comment: ${error.message}`); | |
| core.setFailed(`Error posting comment: ${error.message}`); | |
| } |