forked from githubtraining/training-manual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpire-repo
executable file
·96 lines (80 loc) · 3.6 KB
/
expire-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
#!/usr/bin/env bash
#
# Expire 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) #
#################################################################
function GetPages() {
# Get the number of pages
# shellcheck disable=SC1102
NUM_PAGES=$( (curl -s -S -I -H "Authorization: token $TEACHER_PAT" -X GET "https://$INSTANCE_URL/orgs/$CLASS_ORG/repos" | grep "^Link:") | (sed -n 's/.*=\([0-9]*\)>; rel=\"last\".*/\1/p'))
# Check the return
if [ -z "$NUM_PAGES" ]; then
NUM_PAGES=1
echo "There is only $NUM_PAGES page"
else
echo "There are $NUM_PAGES pages"
fi
# Go through the pages
while [ $NUM_PAGES -gt 0 ]; do
echo "Getting results for page $NUM_PAGES"
# Get the repos
GetRepos "$NUM_PAGES"
((NUM_PAGES = NUM_PAGES - 1))
done
}
function GetRepos() {
# Read in the page number
PAGE_NUM=$1
# get repos in $CLASS_ORG org that start with conflict-practice or github-games
REPO_LIST=$(curl -s -S -H "Authorization: token $TEACHER_PAT" -X GET "https://$INSTANCE_URL/orgs/$CLASS_ORG/repos?page=$PAGE_NUM" | jq '.[] | .name')
for i in "${REPO_LIST[@]}"; do
i=$(echo "$i" | (sed -e 's/^"//' -e 's/"$//'))
if echo "$i" | grep -iq -e "conflict-practice-" -e "github-games-"; then
echo "Let's see when $i was created"
# Get the date
CheckDates "$i"
fi
done
}
function CheckDates() {
# Pull in the vars
REPO_NAME=$1
# Get created time
# shellcheck disable=SC1102
CREATED=$( (curl -s -S -H "Authorization: token $TEACHER_PAT" -X GET "https://$INSTANCE_URL/repos/$CLASS_ORG/$REPO_NAME" | jq .created_at) | (sed -e 's/^"//' -e 's/Z"$//')) >>log.out 2>&1
# if they were created more than 10 days ago, expire them
if [[ "$COMP_DATE" > "$CREATED" ]]; then
echo "EXPIRED: $REPO_NAME was created on $CREATED"
BeNice "$REPO_NAME"
else
echo "KEEP: $REPO_NAME was created on $CREATED"
fi
}
function BeNice() {
# Pull in the vars
REPO_NAME=$1
# Get the user
USER=$(echo "$REPO_NAME" | (sed -n -e 's/.*-//p'))
# Check the name
if echo "$REPO_NAME" | grep -iq -e "github-games-"; then
TEMPLATE="github-games [here](https://github.com/githubtraining/github-games)"
else
TEMPLATE="conflict-practice (our conflict practice activity) [here](https://github.com/githubtraining/conflict-practice)"
fi
curl -s -S -i -H "Authorization: token $TEACHER_PAT" -d "{ \"title\": \"Thank you for attending GitHub Training\", \"body\": \"@$USER We hope you enjoyed the :octocat: class and we look forward to seeing you in another [class or webinar](https://services.github.com/training/#public) soon. To keep things tidy in the githubschool organization, we are going to remove your practice repository.\n\nIf you would like to revisit these activities in the future, you can access the template repository for $TEMPLATE.\n\nStop by and see us sometime at [services.github.com](https://services.github.com), or take our newest self-paced courses at [lab.github.com](https://lab.github.com). :wave: Until we see you again, good luck in your GitHub adventures :rocket:.\"}" -X POST "https://$INSTANCE_URL/repos/$CLASS_ORG/$REPO_NAME/issues" >>log.out 2>&1
# Delete the repo
./remove-repo "$REPO_NAME"
}
# Check the OS
if [ "$OSTYPE" = "msys" ]; then
COMP_DATE=$(date --date='10 days ago' +%Y-%m-%dT%H:%M:%S)
else
COMP_DATE=$(date -v-10d +%Y-%m-%dT%H:%M:%S)
fi
# Get the pages
GetPages