Trigger deadlines and set protected tags

This commit is contained in:
Tim Schubert 2018-03-30 15:10:53 +02:00
parent 3123a697ce
commit bc20842d63

View file

@ -22,13 +22,10 @@ class Deadline(yaml.YAMLObject):
def trigger(self, project): def trigger(self, project):
"""Create protected tag on ref""" """Create protected tag on ref"""
try:
project.tags.create({ project.tags.create({
'tag_name': self.tag, 'tag_name': self.tag,
'ref': self.ref 'ref': self.ref
}) })
except gitlab.exceptions.GitlabHttpError as e:
log.warn(e)
def test(self): def test(self):
return self.time <= datetime.datetime.now() return self.time <= datetime.datetime.now()
@ -59,10 +56,8 @@ class Course(yaml.YAMLObject):
def sync_projects(self, gl): def sync_projects(self, gl):
found = self.group.projects.list(search=self.base) found = self.group.projects.list(search=self.base)
if len(found) > 0: if len(found) == 0:
return found[0] gl.projects.create({
return gl.projects.create({
'name': self.base, 'name': self.base,
'namespace_id': self.group.id, 'namespace_id': self.group.id,
'visibility': 'internal' 'visibility': 'internal'
@ -91,7 +86,7 @@ class Student():
found = gl.users.list(search=self.user) found = gl.users.list(search=self.user)
if len(found) > 0: if len(found) > 0:
return found return found[0]
return gl.users.create({ return gl.users.create({
'email': self.email, 'email': self.email,
@ -104,26 +99,30 @@ class Student():
}) })
def fork_project(gl, group, base, user): def sync_project(gl, course, student):
"""Create user projects as forks from course/solutions in namespace of """Create user projects as forks from course/solutions in namespace of
course and add user as developer (NOT master) user should not be able course and add user as developer (NOT master) user should not be able
to modify protected TAG or force-push on protected branch users can to modify protected TAG or force-push on protected branch users can
later invite other users into their projects""" later invite other users into their projects"""
# fork course base project (e.g. solutions) base = course.group.projects.list(search=course.base)[0]
fork = base.forks.create({ base = gl.projects.get(base.id)
'name': user['name'],
'namespace': group.namspace, project = gl.projects.create({
'visibility': 'private' 'namespace_id': course.group.id,
'name': student.user.username,
'path': student.user.username
}) })
# add student as member of project # add student as member of project
fork.members.create({ project.members.create({
'user_id': user, 'user_id': student.user.id,
'access_level': gitlab.DEVELOPER_ACCESS 'access_level': gitlab.DEVELOPER_ACCESS
}) })
return fork project.create_fork_relation(base.id)
return project
def deadlines(gl, course, args): def deadlines(gl, course, args):
@ -131,7 +130,11 @@ def deadlines(gl, course, args):
for deadline in course.deadlines: for deadline in course.deadlines:
if deadline.test(): if deadline.test():
deadline.trigger(course) for project in course.group.projects.list():
try:
deadline.trigger(project)
except gitlab.exceptions.GitlabHttpError as e:
log.warn(e)
def sync(gl, conf, args): def sync(gl, conf, args):
@ -143,14 +146,15 @@ def sync(gl, conf, args):
for course in conf['courses']: for course in conf['courses']:
course.group = course.sync_group(gl) course.group = course.sync_group(gl)
project = course.sync_projects(gl) course.sync_projects(gl)
with open(args.students[0], encoding='latin1') as csvfile: with open(args.students[0], encoding='latin1') as csvfile:
for student in Student.from_csv(csvfile): for student in Student.from_csv(csvfile):
try: try:
student.sync_user(gl, conf['ldap']) student.user = student.sync_user(gl, conf['ldap'])
except gitlab.exceptions.GitlabCreateError: sync_project(gl, course, student)
log.warn('Failed to create %s' % student.user) except gitlab.exceptions.GitlabCreateError as e:
log.warn(e)
def parseconf(conf): def parseconf(conf):