-
Notifications
You must be signed in to change notification settings - Fork 282
/
grade-client
executable file
·207 lines (168 loc) · 6.76 KB
/
grade-client
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
#
# Grade Client 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) #
#################################################################
# GLOBALS
COLLAB_ORG='' # Name of the Org
COLLAB_REPO='' # name of the Repo
MASTER_ISSUE='' # Issue number to check
OPEN_PR_ARRAY=() # Array of open PR's
COMMENTORS_ARRAY=() # Array of all commentors
function GetInputVars() {
echo "-----------------------------------"
echo "What is the name of the GitHub ORG? (ex. github-training)"
# Read the collab Org
read -r COLLAB_ORG
# Clean any whitespace that may be entered
COLLAB_ORG_WHITESPACE="$(echo -e "$COLLAB_ORG" | tr -d '[:space:]')"
COLLAB_ORG=$COLLAB_ORG_WHITESPACE
echo "-----------------------------------"
echo "What is the name of the Github REPO? (ex. training-class)"
# Read the collab REPO
read -r COLLAB_REPO
# Clean any whitespace that may be entered
COLLAB_REPO_WHITESPACE="$(echo -e "$COLLAB_REPO" | tr -d '[:space:]')"
COLLAB_REPO=$COLLAB_REPO_WHITESPACE
echo "-----------------------------------"
echo "What is the number of the GitHub ISSUE? (ex. 1)"
# Read the collab ISSUE
read -r MASTER_ISSUE
# Validate we got a number
REGEX='^[0-9]+$'
if ! [[ $MASTER_ISSUE =~ $REGEX ]]; then
echo "ERROR: [$MASTER_ISSUE] is not a number!"
exit 1
fi
}
function GetIssueCommenters() {
# Get array of all commentors in an issue
IFS=" " read -ra COMMENTORS_ARRAY <<<"$(
curl --fail -s -S -k -X GET --url "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/issues/$MASTER_ISSUE/comments" \
-H 'accept: application/vnd.github.v3+json' -H "authorization: Bearer $TEACHER_PAT" -H 'content-type: application/json' --data '{}' | jq '.[] | .user.login'
)" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to gain list of commentors!"
echo "ERROR:[${COMMENTORS_ARRAY[*]}]"
exit 1
fi
# Get the length of the commentor array
COMMENTS_FOUND="${#COMMENTORS_ARRAY[@]}"
echo "Found:[$COMMENTS_FOUND] comments on issue"
# Warn on no comments
if [ "$COMMENTS_FOUND" -eq 0 ]; then
echo "WARN! no comments found on issue!"
fi
}
function GetOpenPRS() {
# Get a list of all open PRs on the main repo
IFS=" " read -ra OPEN_PR_ARRAY <<<"$(
curl -s -S -k -X GET --url "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/pulls" \
-H "authorization: Bearer $TEACHER_PAT" -H 'content-type: application/json' \
--data '{ "state": "open", "sort": "created", "direction": "desc" }' | jq '.[] | .user.login'
)" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to gain list of open PR's!"
echo "ERROR:[${OPEN_PR_ARRAY[*]}]"
exit 1
fi
# Get the length of the Open-PR array
COMMENTS_FOUND="${#OPEN_PR_ARRAY[@]}"
echo "Found:[$COMMENTS_FOUND] open PR's"
}
function CreateObjects() {
# Clean the arrays of duplicates
COMMENTORS_ARRAY_CLEAN="$(echo "${COMMENTORS_ARRAY[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
OPEN_PR_ARRAY_CLEAN="$(echo "${OPEN_PR_ARRAY[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
CONCAT_ARRAY=("${COMMENTORS_ARRAY_CLEAN[@]}" "${OPEN_PR_ARRAY_CLEAN[@]}")
# Store Intersection ARRAY
INTERSECTION_ARRAY="$(printf '%s\n' "${CONCAT_ARRAY[@]}" | awk '!($0 in seen){seen[$0];next} 1')"
####################
# Debug statements #
####################
# echo "DEBUG: COMMENTORS_ARRAY_CLEAN:[${COMMENTORS_ARRAY_CLEAN[*]}]"
# echo "DEBUG: OPEN_PR_ARRAY_CLEAN:[${OPEN_PR_ARRAY_CLEAN[*]}]"
# echo "DEBUG: INTERSECTION_ARRAY:[${INTERSECTION_ARRAY[*]}]"
# Users who have open PR and a commentor
for USER in "${INTERSECTION_ARRAY[@]}"; do
# create the success file _grades/_date
# Remove whitespace
USER=$(echo "$USER" | (sed -e 's/^"//' -e 's/"$//'))
echo "---" >>"caption-activity-$USER.yml"
# Send graded file to original class repo
# first, encode file as base64
# shellcheck disable=SC2002
NEW_FILE=$(cat "caption-activity-$USER.yml" | base64)
# Does the file already exist?
STATUS=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$USER.yml" | grep "^Status:") >>log.out 2>&1
if echo "$STATUS" | grep -iq "404"; then
# Create the file on the repo
echo "Creating file for:[$USER]"
curl --fail -k -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"_grades/caption-activity-$USER.yml\", \"message\": \"Graded caption activity for $USER\", \"content\": \"$NEW_FILE\"}" -X PUT "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$USER.yml" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to create file!"
exit 1
fi
else
# Get the sha
SHA=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$USER.yml" | jq .sha) >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to get SHA!"
echo "ERROR:[$SHA]"
exit 1
else
# We need to go replace the file
echo "Replacing the file:[caption-activity-$USER.yml] at sha:[$SHA]"
fi
# Replace the file
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"_grades/caption-activity-$USER.yml\", \"message\": \"Graded caption activity for $USER\", \"content\": \"$NEW_FILE\", \"sha\": $SHA}" -X PUT "https://$INSTANCE_URL/repos/$COLLAB_ORG/$COLLAB_REPO/contents/_grades/caption-activity-$USER.yml" >>log.out 2>&1
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to replace the file!"
exit 1
fi
fi
# and delete the temporary grading file
rm -f "caption-activity-$USER.yml"
# Load the error code
ERROR_CODE=$?
# Check the shell for errors
if [ $ERROR_CODE -ne 0 ]; then
# Error
echo "ERROR! Failed to remove the file!"
exit 1
fi
done
}
# Get the user Input Info
GetInputVars
# Get all open PRs
GetOpenPRS
# Get Commenters on Issue
GetIssueCommenters
# Create Objects
CreateObjects