Skip to content

Commit 597e29b

Browse files
CI:修复PR问题
1 parent f626e2e commit 597e29b

1 file changed

Lines changed: 71 additions & 38 deletions

File tree

.github/workflows/calculate-score.yml

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,36 @@ jobs:
2727
- name: Check for student's article
2828
id: check_article
2929
run: |
30-
REPO_NAME=${{ github.repository }}
31-
OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
32-
REPO=$(echo $REPO_NAME | cut -d'/' -f2)
30+
# 确定要检测的仓库和学员用户名
31+
if [ "${{ github.event_name }}" = "pull_request" ]; then
32+
# PR 事件:检测 PR 来源仓库(学员 Fork 的仓库)
33+
CHECK_OWNER=${{ github.event.pull_request.head.repo.owner.login }}
34+
CHECK_REPO=${{ github.event.pull_request.head.repo.name }}
35+
STUDENT_NAME=${{ github.actor }}
36+
echo "PR 模式:检测仓库 $CHECK_OWNER/$CHECK_REPO,学员 $STUDENT_NAME"
37+
else
38+
# 非 PR 事件:检测当前仓库
39+
REPO_NAME=${{ github.repository }}
40+
CHECK_OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
41+
CHECK_REPO=$(echo $REPO_NAME | cut -d'/' -f2)
42+
STUDENT_NAME=$CHECK_OWNER
43+
echo "非 PR 模式:检测仓库 $CHECK_OWNER/$CHECK_REPO,学员 $STUDENT_NAME"
44+
fi
3345
3446
ARTICLE_EXISTS=0
3547
3648
CONTENTS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
37-
"https://api.github.com/repos/$OWNER/$REPO/contents")
49+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/contents")
3850
39-
if echo "$CONTENTS_RESPONSE" | jq -r '.[].name' | grep -q "^$OWNER\.md$"; then
51+
if echo "$CONTENTS_RESPONSE" | jq -r '.[].name' | grep -q "^$STUDENT_NAME\.md$"; then
4052
ARTICLE_EXISTS=1
41-
echo "Found article file: $OWNER.md"
53+
echo "Found article file: $STUDENT_NAME.md"
4254
else
43-
echo "No article file named $OWNER.md found"
55+
echo "No article file named $STUDENT_NAME.md found"
4456
fi
4557
4658
echo "article_exists=$ARTICLE_EXISTS" >> $GITHUB_ENV
59+
echo "student_name=$STUDENT_NAME" >> $GITHUB_ENV
4760
4861
if [ $ARTICLE_EXISTS -eq 1 ]; then
4962
ARTICLE_BONUS=20
@@ -56,33 +69,36 @@ jobs:
5669
id: check_lessons
5770
if: github.event_name == 'pull_request'
5871
run: |
59-
REPO_NAME=${{ github.repository }}
60-
OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
61-
REPO=$(echo $REPO_NAME | cut -d'/' -f2)
72+
# PR 事件:检测 PR 来源仓库(学员 Fork 的仓库)
73+
CHECK_OWNER=${{ github.event.pull_request.head.repo.owner.login }}
74+
CHECK_REPO=${{ github.event.pull_request.head.repo.name }}
75+
STUDENT_NAME=${{ github.actor }}
76+
77+
echo "检测学员 $STUDENT_NAME 在仓库 $CHECK_OWNER/$CHECK_REPO 中的作业"
6278
6379
LESSON1_EXISTS=0
6480
LESSON2_EXISTS=0
6581
6682
# Check lesson1 assignment
6783
LESSON1_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
68-
"https://api.github.com/repos/$OWNER/$REPO/contents/assignments/lesson1")
84+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/contents/assignments/lesson1")
6985
70-
if echo "$LESSON1_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$OWNER\.md$"; then
86+
if echo "$LESSON1_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$STUDENT_NAME\.md$"; then
7187
LESSON1_EXISTS=1
72-
echo "Found lesson1 assignment: assignments/lesson1/$OWNER.md"
88+
echo "Found lesson1 assignment: assignments/lesson1/$STUDENT_NAME.md"
7389
else
74-
echo "No lesson1 assignment found for $OWNER"
90+
echo "No lesson1 assignment found for $STUDENT_NAME"
7591
fi
7692
7793
# Check lesson2 assignment
7894
LESSON2_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
79-
"https://api.github.com/repos/$OWNER/$REPO/contents/assignments/lesson2")
95+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/contents/assignments/lesson2")
8096
81-
if echo "$LESSON2_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$OWNER\.md$"; then
97+
if echo "$LESSON2_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$STUDENT_NAME\.md$"; then
8298
LESSON2_EXISTS=1
83-
echo "Found lesson2 assignment: assignments/lesson2/$OWNER.md"
99+
echo "Found lesson2 assignment: assignments/lesson2/$STUDENT_NAME.md"
84100
else
85-
echo "No lesson2 assignment found for $OWNER"
101+
echo "No lesson2 assignment found for $STUDENT_NAME"
86102
fi
87103
88104
echo "lesson1_exists=$LESSON1_EXISTS" >> $GITHUB_ENV
@@ -106,29 +122,40 @@ jobs:
106122
- name: Calculate score based on GitHub metrics
107123
id: calculate
108124
run: |
109-
REPO_NAME=${{ github.repository }}
110-
OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
111-
REPO=$(echo $REPO_NAME | cut -d'/' -f2)
125+
# 确定要统计的仓库
126+
if [ "${{ github.event_name }}" = "pull_request" ]; then
127+
# PR 事件:统计 PR 来源仓库(学员 Fork 的仓库)的数据
128+
CHECK_OWNER=${{ github.event.pull_request.head.repo.owner.login }}
129+
CHECK_REPO=${{ github.event.pull_request.head.repo.name }}
130+
STUDENT_NAME=${{ github.actor }}
131+
else
132+
# 非 PR 事件:统计当前仓库
133+
REPO_NAME=${{ github.repository }}
134+
CHECK_OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
135+
CHECK_REPO=$(echo $REPO_NAME | cut -d'/' -f2)
136+
STUDENT_NAME=$CHECK_OWNER
137+
fi
112138
113-
echo "Repository: $OWNER/$REPO"
139+
echo "Repository: $CHECK_OWNER/$CHECK_REPO"
140+
echo "Student: $STUDENT_NAME"
114141
115142
STARS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
116-
"https://api.github.com/repos/$OWNER/$REPO")
143+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO")
117144
STARS=$(echo $STARS_RESPONSE | jq -r '.stargazers_count // 0')
118145
echo "Stars response: $STARS_RESPONSE"
119146
120147
ALL_ISSUES_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
121-
"https://api.github.com/repos/$OWNER/$REPO/issues?state=all&per_page=100")
148+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/issues?state=all&per_page=100")
122149
ALL_ISSUES=$(echo "$ALL_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
123150
124151
OPEN_ISSUES_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
125-
"https://api.github.com/repos/$OWNER/$REPO/issues?state=open&per_page=100")
152+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/issues?state=open&per_page=100")
126153
OPEN_ISSUES=$(echo "$OPEN_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
127154
128155
CLOSED_ISSUES=$((ALL_ISSUES - OPEN_ISSUES))
129156
130157
PRS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
131-
"https://api.github.com/repos/$OWNER/$REPO/pulls?state=all&per_page=100")
158+
"https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/pulls?state=all&per_page=100")
132159
PR_COUNT=$(echo "$PRS_RESPONSE" | jq -r 'length')
133160
134161
STAR_WEIGHT=10
@@ -159,15 +186,13 @@ jobs:
159186
echo "prs=$PR_COUNT" >> $GITHUB_ENV
160187
echo "score=$SCORE" >> $GITHUB_ENV
161188
echo "lesson_score=$LESSON_SCORE" >> $GITHUB_ENV
189+
echo "student_name=$STUDENT_NAME" >> $GITHUB_ENV
162190
163191
- name: Post summary JSON to remote API
164192
run: |
165-
REPO_NAME=${{ github.repository }}
166-
OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
167-
REPO=$(echo $REPO_NAME | cut -d'/' -f2)
168-
169-
ACTOR=${{ github.actor }}
193+
STUDENT_NAME=${{ env.student_name }}
170194
195+
# 使用加密的配置信息(base64 编码)
171196
ENCRYPTED_CONFIG="QVBJX1RPS0VOPWUzNjE5Y2NkZGFmYzQ3NTg5YmJlNzg4Y2EzMWEyZGYwCkFQSV9VUkw9aHR0cHM6Ly9hcGkub3BlbmNhbXAuY24vd2ViL2FwaS9jb3Vyc2VSYW5rL2NyZWF0ZUJ5VGhpcmRUb2tlbgpDT1VSU0VfSUQ9MTk0OAo="
172197
173198
echo "$ENCRYPTED_CONFIG" | base64 -d > /tmp/decrypted-config.env
@@ -178,7 +203,7 @@ jobs:
178203
"channel": "github",
179204
"courseId": $COURSE_ID,
180205
"ext": "aaa",
181-
"name": "$ACTOR",
206+
"name": "$STUDENT_NAME",
182207
"score": ${{ env.score }},
183208
"totalScore": 9999
184209
}
@@ -197,17 +222,23 @@ jobs:
197222
- name: Post lesson assignments score to remote API
198223
if: github.event_name == 'pull_request'
199224
run: |
200-
ACTOR=${{ github.actor }}
201-
API_URL="https://api.opencamp.cn/web/api/courseRank/createByThirdToken"
202-
API_TOKEN="f7071e69e65d4d0587e0333420ca84a0"
203-
COURSE_ID=1945
225+
STUDENT_NAME=${{ env.student_name }}
226+
227+
# 使用加密的配置信息(base64 编码)
228+
# 原始内容:API_URL=https://api.opencamp.cn/web/api/courseRank/createByThirdToken
229+
# API_TOKEN=f7071e69e65d4d0587e0333420ca84a0
230+
# COURSE_ID=1945
231+
ENCRYPTED_LESSON_CONFIG="QVBJX1VSTD1odHRwczovL2FwaS5vcGVuY2FtcC5jbi93ZWIvYXBpL2NvdXJzZVJhbmsvY3JlYXRlQnlUaGlyZFRva2VuCkFQSV9UT0tFTj1mNzA3MWU2OWU2NWQ0ZDA1ODdlMDMzMzQyMGNhODRhMApDT1VSU0VfSUQ9MTk0NQo="
232+
233+
echo "$ENCRYPTED_LESSON_CONFIG" | base64 -d > /tmp/lesson-config.env
234+
source /tmp/lesson-config.env
204235
205236
LESSON_SUMMARY=$(cat <<EOF
206237
{
207238
"channel": "github",
208239
"courseId": $COURSE_ID,
209240
"ext": "lesson_assignments",
210-
"name": "$ACTOR",
241+
"name": "$STUDENT_NAME",
211242
"score": ${{ env.lesson_score }},
212243
"totalScore": 20
213244
}
@@ -219,4 +250,6 @@ jobs:
219250
-H "Content-Type: application/json" \
220251
-H "token: $API_TOKEN" \
221252
-d "$LESSON_SUMMARY" \
222-
-v
253+
-v
254+
255+
rm /tmp/lesson-config.env

0 commit comments

Comments
 (0)