-
Notifications
You must be signed in to change notification settings - Fork 282
/
check-conflict-repos
executable file
·143 lines (131 loc) · 8.67 KB
/
check-conflict-repos
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
#!/usr/bin/env bash
#
# Check Conflicts 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 GetNames() {
# Get name of collaborators from day 1
COLLABS=$(curl --fail -s -S -k -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/collaborators?per_page=100" | jq '.[] | .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 to get collabs!"
echo "ERROR:[$COLLABS]"
exit 1
fi
# Loop through collabs
for i in "${COLLABS[@]}"; do
if
# Check for element
ContainsElement "$i" "$OWNERS_ARRAY"
then
# Check the repo
CheckRepo "$i"
fi
done
}
function CheckRepo() {
# Name of the student repo
STUDENT=$1
# Clean it up
STUDENT=$(echo "$STUDENT" | (sed -e 's/^"//' -e 's/"$//'))
STUDENT_REPO="conflict-practice-$STUDENT"
# start student file
echo "---" >"conflict-activity-$STUDENT.json"
echo "githubHandle: $STUDENT" >>"conflict-activity-$STUDENT.json"
# check to see if pull requests created by script have been merged
for i in {1..3}; do
PR_STATUS=$(curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$STUDENT_REPO/pulls/$i/merge") >>log.out 2>&1
if echo "$PR_STATUS" | grep -iq "204"; then
echo "PR $i status is 204"
echo "pr$i: merged" >>"conflict-activity-$STUDENT.json"
if [ "$i" == "1" ]; then
# Search for conflict markers
START_MARK1=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/index.html" | grep -c -i '<<<<<<<') >>log.out 2>&1
echo "search for <<<<<<< found $START_MARK1"
DIV_MARK1=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/index.html" | grep -c -i '=======') >>log.out 2>&1
echo "search for ======= found $DIV_MARK1"
END_MARK1=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/index.html" | grep -c -i '>>>>>>>') >>log.out 2>&1
echo "search for >>>>>>> found $END_MARK1"
START_MARK2=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/play/manual/manual.html" | grep -c -i '<<<<<<<') >>log.out 2>&1
echo "search for <<<<<<< found $START_MARK2"
DIV_MARK2=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/play/manual/manual.html" | grep -c -i '=======') >>log.out 2>&1
echo "search for ======= found $DIV_MARK2"
END_MARK2=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/play/manual/manual.html" | grep -c -i '>>>>>>>') >>log.out 2>&1
echo "search for >>>>>>> found $END_MARK2"
START_MARK3=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/play/mini/mini_bored.html" | grep -c -i '<<<<<<<') >>log.out 2>&1
echo "search for <<<<<<< found $START_MARK3"
DIV_MARK3=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/play/mini/mini_bored.html" | grep -c -i '=======') >>log.out 2>&1
echo "search for ======= found $DIV_MARK3"
END_MARK3=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/play/mini/mini_bored.html" | grep -c -i '>>>>>>>') >>log.out 2>&1
echo "search for >>>>>>> found $END_MARK3"
# Make sure play/manual/manual.html still exists
CHECK_FILE=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://api.github.com/search/code?q=manual.html+in:path+repo:githubschool/$STUDENT_REPO" | jq .total_count) >>log.out 2>&1
# echo "search for play/manual/manual.html returned $CHECK_FILE"
{
echo "The answers are represent starting conflict markets, middle conflict markers, ending conflict markers, and if the deleted file is present (null = not present, 0 = present with another unnecessary file, 1 = correct files present)."
echo "correct answers would be: 0,0,0,0,0,0,0,0,0,1"
echo "checks$i:$START_MARK1,$DIV_MARK1,$END_MARK1,$START_MARK2,$DIV_MARK2,$END_MARK2,$START_MARK3,$DIV_MARK3,$END_MARK3,$CHECK_FILE"
} >>"conflict-activity-$STUDENT.json"
elif [ "$i" == "2" ]; then
# Search for conflict markers
START_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/index.css" | grep -c -i '<<<<<<<') >>log.out 2>&1
echo "search for <<<<<<< found $START_MARK"
DIV_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/index.css" | grep -c -i '=======') >>log.out 2>&1
echo "search for ======= found $DIV_MARK"
END_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/css/index.css" | grep -c -i '>>>>>>>') >>log.out 2>&1
echo "search for >>>>>>> found $END_MARK"
echo "correct answers would be: 0,0,0" >>"conflict-activity-$STUDENT.json"
echo "checks$i:$START_MARK,$DIV_MARK,$END_MARK" >>"conflict-activity-$STUDENT.json"
else
# Search for conflict markers
START_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/README.md" | grep -c -i '<<<<<<<') >>log.out 2>&1
echo "search for <<<<<<< found $START_MARK"
DIV_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/README.md" | grep -c -i '=======') >>log.out 2>&1
echo "search for ======= found $DIV_MARK"
END_MARK=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://raw.githubusercontent.com/githubschool/$STUDENT_REPO/main/README.md" | grep -c -i '>>>>>>>') >>log.out 2>&1
echo "search for >>>>>>> found $END_MARK"
echo "correct answers would be: 0,0,0" >>"conflict-activity-$STUDENT.json"
echo "checks$i:$START_MARK,$DIV_MARK,$END_MARK" >>"conflict-activity-$STUDENT.json"
fi
else
echo "PR $i has not been merged"
echo "pr$i: incomplete" >>"conflict-activity-$STUDENT.json"
fi
done
echo "---" >>"conflict-activity-$STUDENT.json"
# send graded file to original class repo
# first, encode file as base64
# shellcheck disable=SC2002
NEW_FILE=$(cat "conflict-activity-$STUDENT.json" | 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/conflict-activity-$STUDENT.json" | grep "^Status:") >>log.out 2>&1
if echo "$STATUS" | grep -iq "404"; then
# Create the file on the repo
curl -s -S -i -u "$TOKEN_OWNER:$TEACHER_PAT" -d "{ \"path\": \"_grades/conflict-activity-$STUDENT.json\", \"message\": \"Graded conflict activity for $STUDENT\", \"content\": \"$NEW_FILE\"}" -X PUT "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/conflict-activity-$STUDENT.json" >>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/conflict-activity-$STUDENT.json" | 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/conflict-activity-$STUDENT.json\", \"message\": \"Graded conflict activity for $STUDENT\", \"content\": \"$NEW_FILE\", \"sha\": $SHA}" -X PUT "https://$INSTANCE_URL/repos/$CLASS_ORG/$COLLAB_REPO/contents/_grades/conflict-activity-$STUDENT.json" >>log.out 2>&1
fi
# Remove the local file
rm "conflict-activity-$STUDENT.json"
}
# Get name of collaborators from day 1
OWNERS_ARRAY=$(curl -s -S -u "$TOKEN_OWNER:$TEACHER_PAT" -X GET "https://$INSTANCE_URL/orgs/$CLASS_ORG/members?role=admin" | jq '.[] | .login') >>log.out 2>&1
# Get the user names
GetNames