1+ name : Calculate Student Score
2+
3+ on :
4+ schedule :
5+ - cron : ' 0 0 * * *'
6+ workflow_dispatch :
7+ push :
8+ branches :
9+ - main
10+
11+ jobs :
12+ calculate-score :
13+ runs-on : ubuntu-latest
14+ steps :
15+ - name : Checkout repository
16+ uses : actions/checkout@v3
17+
18+ - name : Setup jq
19+ run : |
20+ sudo apt-get update
21+ sudo apt-get install -y jq
22+
23+ - name : Check for student's article
24+ id : check_article
25+ run : |
26+ REPO_NAME=${{ github.repository }}
27+ OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
28+ REPO=$(echo $REPO_NAME | cut -d'/' -f2)
29+
30+ ARTICLE_EXISTS=0
31+
32+ CONTENTS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
33+ "https://api.github.com/repos/$OWNER/$REPO/contents")
34+
35+ if echo "$CONTENTS_RESPONSE" | jq -r '.[].name' | grep -q "^$OWNER\.md$"; then
36+ ARTICLE_EXISTS=1
37+ echo "Found article file: $OWNER.md"
38+ else
39+ echo "No article file named $OWNER.md found"
40+ fi
41+
42+ echo "article_exists=$ARTICLE_EXISTS" >> $GITHUB_ENV
43+
44+ if [ $ARTICLE_EXISTS -eq 1 ]; then
45+ ARTICLE_BONUS=20
46+ else
47+ ARTICLE_BONUS=0
48+ fi
49+ echo "article_bonus=$ARTICLE_BONUS" >> $GITHUB_ENV
50+
51+ - name : Calculate score based on GitHub metrics
52+ id : calculate
53+ run : |
54+ REPO_NAME=${{ github.repository }}
55+ OWNER=$(echo $REPO_NAME | cut -d'/' -f1)
56+ REPO=$(echo $REPO_NAME | cut -d'/' -f2)
57+
58+ echo "Repository: $OWNER/$REPO"
59+
60+ STARS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
61+ "https://api.github.com/repos/$OWNER/$REPO")
62+ STARS=$(echo $STARS_RESPONSE | jq -r '.stargazers_count // 0')
63+ echo "Stars response: $STARS_RESPONSE"
64+
65+ 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")
67+ ALL_ISSUES=$(echo "$ALL_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
68+
69+ 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")
71+ OPEN_ISSUES=$(echo "$OPEN_ISSUES_RESPONSE" | jq -r 'map(select(.pull_request == null)) | length')
72+
73+ CLOSED_ISSUES=$((ALL_ISSUES - OPEN_ISSUES))
74+
75+ PRS_RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
76+ "https://api.github.com/repos/$OWNER/$REPO/pulls?state=all&per_page=100")
77+ PR_COUNT=$(echo "$PRS_RESPONSE" | jq -r 'length')
78+
79+ STAR_WEIGHT=10
80+ ISSUE_WEIGHT=20
81+ PR_WEIGHT=30
82+
83+ ARTICLE_BONUS=${{ env.article_bonus }}
84+
85+ SCORE=$((STARS * STAR_WEIGHT + CLOSED_ISSUES * ISSUE_WEIGHT + PR_COUNT * PR_WEIGHT + ARTICLE_BONUS))
86+
87+ echo "=================== 学员成绩报告 ==================="
88+ echo "Stars: $STARS (权重: $STAR_WEIGHT) = $((STARS * STAR_WEIGHT)) 分"
89+ echo "已解决的Issues: $CLOSED_ISSUES (权重: $ISSUE_WEIGHT) = $((CLOSED_ISSUES * ISSUE_WEIGHT)) 分"
90+ echo "Pull Requests: $PR_COUNT (权重: $PR_WEIGHT) = $((PR_COUNT * PR_WEIGHT)) 分"
91+ echo "个人文章提交: $ARTICLE_EXISTS (权重: 20) = $ARTICLE_BONUS 分"
92+ echo "=================================================="
93+ echo "总分: $SCORE 分"
94+ echo "更新时间: $(date)"
95+
96+ echo "stars=$STARS" >> $GITHUB_ENV
97+ echo "issues=$CLOSED_ISSUES" >> $GITHUB_ENV
98+ echo "prs=$PR_COUNT" >> $GITHUB_ENV
99+ echo "score=$SCORE" >> $GITHUB_ENV
100+
101+ - name : Post summary JSON to remote API
102+ 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 }}
108+
109+ ENCRYPTED_CONFIG="QVBJX1RPS0VOPWUzNjE5Y2NkZGFmYzQ3NTg5YmJlNzg4Y2EzMWEyZGYwCkFQSV9VUkw9aHR0cHM6Ly9hcGkub3BlbmNhbXAuY24vd2ViL2FwaS9jb3Vyc2VSYW5rL2NyZWF0ZUJ5VGhpcmRUb2tlbgpDT1VSU0VfSUQ9MTk0OAo="
110+
111+ echo "$ENCRYPTED_CONFIG" | base64 -d > /tmp/decrypted-config.env
112+ source /tmp/decrypted-config.env
113+
114+ SUMMARY=$(cat <<EOF
115+ {
116+ "channel": "github",
117+ "courseId": $COURSE_ID,
118+ "ext": "aaa",
119+ "name": "$ACTOR",
120+ "score": ${{ env.score }},
121+ "totalScore": 9999
122+ }
123+ EOF
124+ )
125+
126+ curl -X POST "$API_URL" \
127+ -H "accept: application/json;charset=utf-8" \
128+ -H "Content-Type: application/json" \
129+ -H "token: $API_TOKEN" \
130+ -d "$SUMMARY" \
131+ -v
132+
133+ rm /tmp/decrypted-config.env
0 commit comments