-
Notifications
You must be signed in to change notification settings - Fork 36
160 lines (128 loc) · 5.69 KB
/
Copy pathsignup-automation.yml
File metadata and controls
160 lines (128 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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}`);
}