generated from IntensiveCoLearning/template
-
Notifications
You must be signed in to change notification settings - Fork 65
184 lines (161 loc) · 6.82 KB
/
ReopBot.yml
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
name: Repo Management
on:
pull_request_target:
types: [closed]
schedule:
- cron: "0 0 * * *" # 每天午夜运行
push:
branches: [main] # 每次推送到main分支时也运行
permissions:
contents: write
pull-requests: write
jobs:
invite-contributor:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true
steps:
- name: Invite contributor
id: invite-contributor
uses: actions/github-script@v6
with:
github-token: ${{ secrets.PAT_WITH_INVITE_PERMISSIONS }}
script: |
const { owner, repo } = context.repo;
const username = context.payload.pull_request.user.login;
console.log(`Checking if ${username} is already a collaborator...`);
try {
const { data: permissionLevel } = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});
if (permissionLevel.permission === 'admin' || permissionLevel.permission === 'write') {
console.log(`${username} is already a collaborator with sufficient permissions.`);
return;
}
console.log(`${username} is a collaborator but needs permission update.`);
} catch (error) {
if (error.status !== 404) {
console.error(`Error checking collaborator status: ${error.message}`);
throw error;
}
console.log(`${username} is not a collaborator.`);
}
try {
console.log(`Inviting ${username} as a collaborator...`);
const response = await github.rest.repos.addCollaborator({
owner,
repo,
username,
permission: 'push'
});
if (response.status === 201) {
console.log(`Invitation sent to ${username} as a collaborator with push permission.`);
core.setOutput('invitation_sent', 'true');
} else if (response.status === 204) {
console.log(`${username}'s permissions updated to push.`);
core.setOutput('invitation_sent', 'false');
}
} catch (error) {
console.error(`Error inviting/updating collaborator: ${error.message}`);
core.setFailed(`Error inviting/updating collaborator: ${error.message}`);
}
- name: Comment on PR
if: steps.invite-contributor.outputs.invitation_sent == 'true'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;
const username = context.payload.pull_request.user.login;
try {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: `Thanks for your contribution, @${username}! You've been invited as a collaborator with push permissions. Please check your email for the invitation.`
});
console.log(`Comment posted on PR #${issue_number}`);
} catch (error) {
console.error(`Error posting comment: ${error.message}`);
core.setFailed(`Error posting comment: ${error.message}`);
}
update-readme:
runs-on: ubuntu-latest
if: github.event_name == 'schedule' || github.event_name == 'push'
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install PyGithub pytz
- name: Update README
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
START_DATE: ${{ vars.START_DATE }}
END_DATE: ${{vars.END_DATE }}
FILE_SUFFIX: ${{vars.FILE_SUFFIX}}
FIELD_NAME: ${{vars.FIELD_NAME}}
run: python sync_status_readme.py
- name: Check for changes
id: git-check
run: |
git diff --exit-code README.md || echo "modified=true" >> $GITHUB_OUTPUT
- name: Commit changes
if: steps.git-check.outputs.modified == 'true'
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add README.md
git commit -m "Update commit status table"
git push
notify-api:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true || github.event_name == 'push'
steps:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "16"
- name: Install axios
run: npm install axios
- name: Call API
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const axios = require('axios');
const commit_sha = context.sha;
const { owner, repo } = context.repo;
try {
const { data: commit } = await github.rest.repos.getCommit({
owner,
repo,
ref: commit_sha
});
// Get user details
const { data: user } = await github.rest.users.getByUsername({
username: commit.author.login
});
// Output information
console.log('Commit Author Information:');
console.log('------------------------');
console.log(`Name: ${commit.commit.author.name}`);
console.log(`Email: ${commit.commit.author.email}`);
console.log(`GitHub Username: ${commit.author.login}`);
console.log(`User ID: ${user.id}`);
console.log(`Account Type: ${user.type}`);
console.log(`Created At: ${user.created_at}`);
const response = await axios.get(`https://api.intensivecolearn.ing/api/programs/createByRepo/${owner}/${repo}`);
console.log('API response:', response.data);
const updateUserNotesResp = await axios.get(https://api.intensivecolearn.ing/api/programs/updateStudynotes?owner=${owner}&repo=${repo}&userGitId=${user.id}`);
console.log('updateUserNotesRespAPI response:', updateUserNotesResp.data);
} catch (error) {
console.error('Error calling API:', error.message);
core.setFailed(`Error calling API: ${error.message}`);
}