forked from githubtraining/training-manual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-caption-repo
executable file
·134 lines (115 loc) · 4.88 KB
/
check-caption-repo
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
#!/usr/bin/env bash
#
# Check Caption Repo
# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"
#################################################################
# NOTE: You must have a personal access token (PAT) #
# saved to your environment variables to use this script. #
# We recommend a dedicated service account (e.g. githubteacher) #
#################################################################
COLLAB_REPO=$1
function ContainsElement() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 1; done
return 0
}
function GetStudents() {
for i in "${COLLABS[@]}"; do
if
ContainsElement "$i" "$OWNERS_ARRAY"
then
CreateStudentFile "$i"
fi
done
# Check the repo
CheckRepo
}
function CreateStudentFile() {
# Read in the var
STUDENT=$1
# Get the Student name
STUDENT=$(echo "$STUDENT" | (sed -e 's/^"//' -e 's/"$//'))
echo "---" >"caption-activity-$STUDENT.yml"
echo "githubHandle: $STUDENT" >>"caption-activity-$STUDENT.yml"
}
function CheckRepo() {
# Iterate through the PRs and find out who authored each one
for i in "${PULLS_NUMBERS[@]}"; do
# Give credit to the author
PR_AUTHOR=$(curl --fail -k -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/pulls/$i" | jq .user.login) >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed curl!"
echo "ERROR:[$PR_AUTHOR]"
exit 1
fi
# Check the return
if
ContainsElement "$PR_AUTHOR" "$OWNERS_ARRAY"
then
PR_AUTHOR=$(echo "$PR_AUTHOR" | (sed -e 's/^"//' -e 's/"$//'))
echo "prAuth: https://$INSTANCE_URL/$CLASS_ORG/$COLLAB_REPO/pull/$i" >>"caption-activity-$PR_AUTHOR.yml"
fi
# Get authors of reviews on this PR
REVIEW_AUTH=$(curl --fail -k -s -S -H "Accept: application/vnd.github.v3+json" -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/pulls/$i/reviews" | jq '.[] | .user.login') >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
echo "ERROR! Failed curl!"
echo "ERROR:[$REVIEW_AUTH]"
exit 1
fi
# Iterate through reviewers and give credit
for z in "${REVIEW_AUTH[@]}"; do
# Give credit to the author
if
ContainsElement "$z" "$OWNERS_ARRAY"
then
z=$(echo "$z" | (sed -e 's/^"//' -e 's/"$//'))
echo "REVIEW_AUTH: https://$INSTANCE_URL/$CLASS_ORG/$COLLAB_REPO/pull/$i" >>"caption-activity-$z.yml"
fi
done
done
# Send the files
SendFiles
}
function SendFiles() {
for i in "${COLLABS[@]}"; do
if
ContainsElement "$i" "$OWNERS_ARRAY"
then
i=$(echo "$i" | (sed -e 's/^"//' -e 's/"$//'))
echo "---" >>"caption-activity-$i.yml"
# Send graded file to original class repo
# First, encode file as base64
# shellcheck disable=SC2002
NEW_FILE=$(cat "caption-activity-$i.yml" | base64)
# Does the file already exist?
STATUS=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$i.yml" | grep "^Status:") >>log.out 2>&1
if echo "$STATUS" | grep -iq "404"; then
# Create the file on the repo
curl --fail -k -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"_grades/caption-activity-$i.yml\", \"message\": \"Graded caption activity for $i\", \"content\": \"$NEW_FILE\"}" -X PUT "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$i.yml" >>log.out 2>&1
else
# Get the SHA
SHA=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$i.yml" | jq .sha) >>log.out 2>&1
echo "Replacing the file at sha: $SHA"
# Replace the file
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"_grades/caption-activity-$i.yml\", \"message\": \"Graded caption activity for $i\", \"content\": \"$NEW_FILE\", \"sha\": $SHA}" -X PUT "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$i.yml" >>log.out 2>&1
fi
# Delete the temporary grading file
rm "caption-activity-$i.yml"
fi
done
}
# Get name of collaborators from day 1
{
COLLABS=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/collaborators?per_page=100" | jq '.[] | .login')
OWNERS_ARRAY=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/orgs/$CLASS_ORG/members?role=admin" | jq '.[] | .login')
PULLS_NUMBERS=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/pulls?per_page=100" | jq '.[] | .number')
} >>log.out 2>&1
# Get the students
GetStudents