forked from KTH/github-canvas-integration-devops
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_task.py
More file actions
188 lines (150 loc) · 7.29 KB
/
Copy pathupdate_task.py
File metadata and controls
188 lines (150 loc) · 7.29 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: update_task.py
import json, os, sys
from utils.course import Course
from github import Github
import argparse
# ENVs for updating task
CANVAS_TOKEN = os.getenv("CANVAS_TOKEN")
CANVAS_COURSE_ID = os.getenv("CANVAS_COURSE_ID")
GH_TOKEN = os.getenv("GH_TOKEN")
GH_REPO_FULLNAME = os.getenv("GH_REPO_FULLNAME")
GITHUB_CONTRIBUTION_PATH = "./contributions"
CANVAS_URL = "https://canvas.kth.se"
github_repo = Github(GH_TOKEN).get_repo(GH_REPO_FULLNAME)
course = Course(CANVAS_URL, CANVAS_TOKEN, CANVAS_COURSE_ID)
# Arguments
MODE = ''
PR_NUMBER = 0
README_SECTIONS = ['']
# Get sub directories of a given path
def get_sub_directory(path):
categories = dict()
for dirpath, dirnames, filenames in os.walk(path, topdown=True):
for category in dirnames:
categories[category] = {"path": os.path.join(dirpath, category)}
break
return categories
# Create new groups and remove deleted groups
def update_groups(canvas_groups_category_id, github_groups):
groups_canvas = course.list_groups(canvas_groups_category_id)
print("Canvas groups : " + str(len(groups_canvas)))
for github_group in github_groups:
if github_group not in groups_canvas:
print("Creating group : " + github_group)
id_group = course.create_group(canvas_groups_category_id, github_group)
members = github_group.split("-")
for member in members:
print("Adding : " + member)
id_member = course.get_user_id(member)
if id_member is None:
raise Exception("User {0} not found !".format(member))
course.add_group_member(id_group, id_member)
print("Cleaning groups ...")
deleted_groups = {k: v for k, v in groups_canvas.items() if k not in github_groups}
for group_name in deleted_groups:
print("Deleting group : " + group_name)
r = course.delete_group(deleted_groups[group_name])
print(r)
# Check if the group is valid
# No same member in same group set
# Member registered in canvas
def check_groups(canvas_groups_category_id, task_name, github_groups):
groups_canvas = course.list_groups(canvas_groups_category_id)
registered_user = []
for group_canvas in groups_canvas:
members = group_canvas.split("-")
registered_user += members
for github_group in github_groups:
if task_name == 'executable-tutorial' or task_name == 'feedback':
# check that "task X" is in the README
fname = github_groups[github_group]["path"] + '/README.md'
content = open(fname).read().lower()
assert "task 1" in content \
or "task 2" in content \
or "task 3" in content , "deadline should be stated exactly as 'task x', eg 'task 1' in " + fname
members = github_group.split("-")
for member in members:
if member in registered_user and github_group not in groups_canvas:
if PR_NUMBER > 0:
github_repo.get_pull(PR_NUMBER).create_issue_comment(
"Student " + member + " already registered for this task" + "\n\n" +
"If not from your group, fetch the upstream.")
raise Exception("User {0} already registered for {1} !".format(member, task_name))
id_member = course.get_user_id(member)
if id_member is None:
if PR_NUMBER > 0:
github_repo.get_pull(PR_NUMBER).create_issue_comment(
"Missing student registration :" + member + "\n\n" +
"If not from your group, fetch the upstream.")
raise Exception("User {0} not found !".format(member))
sections = get_sections(github_groups[github_group]["path"] + '/README.md')
if not sections == README_SECTIONS:
if PR_NUMBER > 0:
github_repo.get_pull(PR_NUMBER).create_issue_comment("Readme is not correctly formatted\n" +
"Need exactly: " + str(README_SECTIONS) + "\n\n" +
"Got: " + str(sections))
raise Exception("Readme is not correctly formatted")
# Check if the readme contains the required info
def get_sections(path):
f = open(path, "r")
file = f.readlines()
sections = [s.strip('#').strip() for s in file if s.startswith('#')]
return sections
# Mapping from github task name to canvas group set id
def task_to_group_category_id(task_name, canvas_groups_set):
mapping = {
"presentation": canvas_groups_set["Presentations"],
"scientific-paper": canvas_groups_set["Scientific Papers"],
"demo": canvas_groups_set["Demos"],
"open-source": canvas_groups_set["Open-source contributions"],
"executable-tutorial": canvas_groups_set["Executable Tutorials"],
"feedback": canvas_groups_set["Feedback"]
}
return mapping.get(task_name, Exception("Groupset mapping"))
# Parse arguments of the script
def parse_args():
global MODE
global PR_NUMBER
global README_SECTIONS
parser = argparse.ArgumentParser()
parser.add_argument('--mode', dest='mode', type=str, help='Is only check')
parser.add_argument('--pr', dest='pr', type=int, help='Pull request number', default=0)
# To handle spaces, use double quote eg. --sections "Assignment Proposal"
parser.add_argument('--sections', dest='sections', type=str, nargs='+', help='List readme sections',
default=['Assignment Proposal', 'Title', 'Names and KTH ID', 'Deadline', 'Category',
'Description'])
args = parser.parse_args()
MODE = args.mode
PR_NUMBER = args.pr
README_SECTIONS = args.sections
def raise_exception(error):
if PR_NUMBER > 0:
github_repo.get_pull(PR_NUMBER).create_issue_comment(error)
raise Exception(error)
def main():
parse_args()
# Get GitHub tasks and canvas group set
github_tasks = get_sub_directory(GITHUB_CONTRIBUTION_PATH)
canvas_groups_set = course.get_group_categories()
for task_name in github_tasks:
print("Getting tasks : " + task_name)
github_groups = dict()
# Get GitHbs groups and check with canvas group set
print("TASK_NAME", task_name)
print("CANVAS_GROUPS_SET", canvas_groups_set)
canvas_groups_category_id = task_to_group_category_id(task_name, canvas_groups_set)
if task_name == 'presentation' or task_name == 'demo' or task_name == 'scientific-paper':
weeks = get_sub_directory(github_tasks[task_name]["path"])
for week in weeks:
if not week.startswith('week'):
raise_exception("The group folder should be inside a week folder !")
github_groups.update(get_sub_directory(os.path.join(github_tasks[task_name]["path"], week)))
else:
github_groups = get_sub_directory(github_tasks[task_name]["path"])
check_groups(canvas_groups_category_id, task_name, github_groups)
# If not check mode, sync the groups with canvas
if MODE != 'check':
update_groups(canvas_groups_category_id, github_groups)
main()