77 push :
88 branches :
99 - main
10+ pull_request :
11+ types : [opened, synchronize, reopened]
12+ branches :
13+ - main
1014
1115jobs :
1216 calculate-score :
@@ -23,23 +27,36 @@ jobs:
2327 - name : Check for student's article
2428 id : check_article
2529 run : |
26- REPO_NAME=${{ github.repository }}
27- OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
28- 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
2945
3046 ARTICLE_EXISTS=0
3147
3248 CONTENTS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
33- "https://api.github.com/repos/$OWNER/$REPO /contents")
49+ "https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO /contents")
3450
35- 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
3652 ARTICLE_EXISTS=1
37- echo "Found article file: $OWNER .md"
53+ echo "Found article file: $STUDENT_NAME .md"
3854 else
39- echo "No article file named $OWNER .md found"
55+ echo "No article file named $STUDENT_NAME .md found"
4056 fi
4157
4258 echo "article_exists=$ARTICLE_EXISTS" >> $GITHUB_ENV
59+ echo "student_name=$STUDENT_NAME" >> $GITHUB_ENV
4360
4461 if [ $ARTICLE_EXISTS -eq 1 ]; then
4562 ARTICLE_BONUS=20
@@ -48,64 +65,134 @@ jobs:
4865 fi
4966 echo "article_bonus=$ARTICLE_BONUS" >> $GITHUB_ENV
5067
68+ - name : Check for lesson assignments
69+ id : check_lessons
70+ if : github.event_name == 'pull_request'
71+ run : |
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 中的作业"
78+
79+ LESSON1_EXISTS=0
80+ LESSON2_EXISTS=0
81+
82+ # Check lesson1 assignment
83+ LESSON1_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
84+ "https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/contents/assignments/lesson1")
85+
86+ if echo "$LESSON1_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$STUDENT_NAME\.md$"; then
87+ LESSON1_EXISTS=1
88+ echo "Found lesson1 assignment: assignments/lesson1/$STUDENT_NAME.md"
89+ else
90+ echo "No lesson1 assignment found for $STUDENT_NAME"
91+ fi
92+
93+ # Check lesson2 assignment
94+ LESSON2_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
95+ "https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO/contents/assignments/lesson2")
96+
97+ if echo "$LESSON2_RESPONSE" | jq -r '.[].name' 2>/dev/null | grep -q "^$STUDENT_NAME\.md$"; then
98+ LESSON2_EXISTS=1
99+ echo "Found lesson2 assignment: assignments/lesson2/$STUDENT_NAME.md"
100+ else
101+ echo "No lesson2 assignment found for $STUDENT_NAME"
102+ fi
103+
104+ echo "lesson1_exists=$LESSON1_EXISTS" >> $GITHUB_ENV
105+ echo "lesson2_exists=$LESSON2_EXISTS" >> $GITHUB_ENV
106+
107+ if [ $LESSON1_EXISTS -eq 1 ]; then
108+ LESSON1_SCORE=10
109+ else
110+ LESSON1_SCORE=0
111+ fi
112+
113+ if [ $LESSON2_EXISTS -eq 1 ]; then
114+ LESSON2_SCORE=10
115+ else
116+ LESSON2_SCORE=0
117+ fi
118+
119+ echo "lesson1_score=$LESSON1_SCORE" >> $GITHUB_ENV
120+ echo "lesson2_score=$LESSON2_SCORE" >> $GITHUB_ENV
121+
51122 - name : Calculate score based on GitHub metrics
52123 id : calculate
53124 run : |
54- REPO_NAME=${{ github.repository }}
55- OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
56- 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
57138
58- echo "Repository: $OWNER/$REPO"
139+ echo "Repository: $CHECK_OWNER/$CHECK_REPO"
140+ echo "Student: $STUDENT_NAME"
59141
60142 STARS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
61- "https://api.github.com/repos/$OWNER/$REPO ")
143+ "https://api.github.com/repos/$CHECK_OWNER/$CHECK_REPO ")
62144 STARS=$(echo $STARS_RESPONSE | jq -r '.stargazers_count // 0')
63145 echo "Stars response: $STARS_RESPONSE"
64146
65147 ALL_ISSUES_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
66- "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")
67149 ALL_ISSUES=$(echo "$ALL_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
68150
69151 OPEN_ISSUES_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
70- "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")
71153 OPEN_ISSUES=$(echo "$OPEN_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
72154
73155 CLOSED_ISSUES=$((ALL_ISSUES - OPEN_ISSUES))
74156
75157 PRS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
76- "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")
77159 PR_COUNT=$(echo "$PRS_RESPONSE" | jq -r 'length')
78160
79161 STAR_WEIGHT=10
80162 ISSUE_WEIGHT=20
81163 PR_WEIGHT=30
82164
83165 ARTICLE_BONUS=${{ env.article_bonus }}
166+ LESSON1_SCORE=${{ env.lesson1_score || 0 }}
167+ LESSON2_SCORE=${{ env.lesson2_score || 0 }}
84168
85169 SCORE=$((STARS * STAR_WEIGHT + CLOSED_ISSUES * ISSUE_WEIGHT + PR_COUNT * PR_WEIGHT + ARTICLE_BONUS))
170+ LESSON_SCORE=$((LESSON1_SCORE + LESSON2_SCORE))
86171
87172 echo "=================== 学员成绩报告 ==================="
88173 echo "Stars: $STARS (权重: $STAR_WEIGHT) = $((STARS * STAR_WEIGHT)) 分"
89174 echo "已解决的Issues: $CLOSED_ISSUES (权重: $ISSUE_WEIGHT) = $((CLOSED_ISSUES * ISSUE_WEIGHT)) 分"
90175 echo "Pull Requests: $PR_COUNT (权重: $PR_WEIGHT) = $((PR_COUNT * PR_WEIGHT)) 分"
91- echo "个人文章提交: $ARTICLE_EXISTS (权重: 20) = $ARTICLE_BONUS 分"
176+ echo "个人文章提交: ${{ env.article_exists }} (权重: 20) = $ARTICLE_BONUS 分"
177+ echo "Lesson1作业: ${{ env.lesson1_exists }} (权重: 10) = $LESSON1_SCORE 分"
178+ echo "Lesson2作业: ${{ env.lesson2_exists }} (权重: 10) = $LESSON2_SCORE 分"
92179 echo "=================================================="
93180 echo "总分: $SCORE 分"
181+ echo "课程作业总分: $LESSON_SCORE 分"
94182 echo "更新时间: $(date)"
95183
96184 echo "stars=$STARS" >> $GITHUB_ENV
97185 echo "issues=$CLOSED_ISSUES" >> $GITHUB_ENV
98186 echo "prs=$PR_COUNT" >> $GITHUB_ENV
99187 echo "score=$SCORE" >> $GITHUB_ENV
188+ echo "lesson_score=$LESSON_SCORE" >> $GITHUB_ENV
189+ echo "student_name=$STUDENT_NAME" >> $GITHUB_ENV
100190
101191 - name : Post summary JSON to remote API
102192 run : |
103- REPO_NAME=${{ github.repository }}
104- OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
105- REPO=$(echo $REPO_NAME | cut -d'/' -f2)
106-
107- ACTOR=${{ github.actor }}
193+ STUDENT_NAME=${{ env.student_name }}
108194
195+ # 使用加密的配置信息(base64 编码)
109196 ENCRYPTED_CONFIG="QVBJX1RPS0VOPWUzNjE5Y2NkZGFmYzQ3NTg5YmJlNzg4Y2EzMWEyZGYwCkFQSV9VUkw9aHR0cHM6Ly9hcGkub3BlbmNhbXAuY24vd2ViL2FwaS9jb3Vyc2VSYW5rL2NyZWF0ZUJ5VGhpcmRUb2tlbgpDT1VSU0VfSUQ9MTk0OAo="
110197
111198 echo "$ENCRYPTED_CONFIG" | base64 -d > /tmp/decrypted-config.env
@@ -116,7 +203,7 @@ jobs:
116203 "channel": "github",
117204 "courseId": $COURSE_ID,
118205 "ext": "aaa",
119- "name": "$ACTOR ",
206+ "name": "$STUDENT_NAME ",
120207 "score": ${{ env.score }},
121208 "totalScore": 9999
122209 }
@@ -130,4 +217,39 @@ jobs:
130217 -d "$SUMMARY" \
131218 -v
132219
133- rm /tmp/decrypted-config.env
220+ rm /tmp/decrypted-config.env
221+
222+ - name : Post lesson assignments score to remote API
223+ if : github.event_name == 'pull_request'
224+ run : |
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
235+
236+ LESSON_SUMMARY=$(cat <<EOF
237+ {
238+ "channel": "github",
239+ "courseId": $COURSE_ID,
240+ "ext": "lesson_assignments",
241+ "name": "$STUDENT_NAME",
242+ "score": ${{ env.lesson_score }},
243+ "totalScore": 20
244+ }
245+ EOF
246+ )
247+
248+ curl -X POST "$API_URL" \
249+ -H "accept: application/json;charset=utf-8" \
250+ -H "Content-Type: application/json" \
251+ -H "token: $API_TOKEN" \
252+ -d "$LESSON_SUMMARY" \
253+ -v
254+
255+ rm /tmp/lesson-config.env
0 commit comments