commit 831eb193aa8e0e213864831a8c6592ed070d94a8 Author: James Eagan Date: Mon Mar 6 17:33:36 2023 +0100 Initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81c2109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +gitlab-token.txt +gitlab-commands.txt +*~ diff --git a/gitlab-clone-student-depots.py b/gitlab-clone-student-depots.py new file mode 100644 index 0000000..7a22094 --- /dev/null +++ b/gitlab-clone-student-depots.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +# Script to clone Git repositories in a Gitlab group. +# +# Minimally tested. Seems to work. Use at your own risk. +# +# By James Eagan +# https://james.eagan.fr + +import csv +import gitlab +import sys +import subprocess +import os.path + +GITLAB_URL='https://gitlab.telecom-paris.fr' +PRIVATE_TOKEN=sys.argv[3] if len(sys.argv) > 3 else '' + +def getProjectsInGroupId(groupId): + api = gitlab.Gitlab(GITLAB_URL, PRIVATE_TOKEN) + group = api.groups.get(groupId) + projects = group.projects.list(all=True) + return (api.projects.get(project.id) for project in projects) + +def cloneProject(project, targetDir): + name = project.attributes['name'] + url = project.attributes['ssh_url_to_repo'] + print("Cloning {} ({})").format(name, project.id) + target = os.path.join(targetDir, name) + subprocess.call(['git', 'clone', '--quiet', url, target]) + +if __name__ == '__main__': + if len(sys.argv) != 4: + print("Usage: python {} ".format(sys.argv[0])) + print(" groupId: The GitLab id for the group") + print(" dir: The directory into which to clone the projects") + print(" token: A GitLab API token as created at ") + print(" {}/-/profile/personal_access_tokens".format(GITLAB_URL)) + print(" Be sure to enable API scope.") + sys.exit(1) + + projects = getProjectsInGroupId(sys.argv[1]) + for project in projects: + cloneProject(project, sys.argv[2]) \ No newline at end of file diff --git a/gitlab-create-student-depots.py b/gitlab-create-student-depots.py new file mode 100644 index 0000000..40c9d49 --- /dev/null +++ b/gitlab-create-student-depots.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +# Script to create Git repositories in a group from a list of names and +# email addresses in a CSV file (such as exported from SynapseS). +# +# Minimally tested. Seems to work. Use at your own risk. +# +# By James Eagan +# https://james.eagan.fr + +import csv +import gitlab +import sys + +GITLAB_URL='https://gitlab.telecom-paris.fr' +PRIVATE_TOKEN=sys.argv[3] if len(sys.argv) > 3 else '' + +def readNames(fileName): + result = [] + with open(fileName) as fd: + sneakPeek = fd.read(1024) + fd.seek(0) + sniffer = csv.Sniffer() + dialect = sniffer.sniff(sneakPeek) + hasHeader = sniffer.has_header(sneakPeek) + + reader = csv.reader(fd, dialect) + # reader = csv.reader(fd, delimiter=';') + if hasHeader: + next(reader) + for row in reader: + result.append((row[0], row[1], row[2])) + return result + +def lookupUser(email, gitlab): + return gitlab.users.list(search=email)[0] + +def createProject(name, groupId, gitlab): + return gitlab.projects.create({'name': name, + 'namespace_id': groupId}) + +def addProjectMember(project, userId, accessLevel): + return project.members.create({'user_id': userId, + 'access_level': accessLevel}) + +def makeProjectsInGroupId(groupId, csvFile): + api = gitlab.Gitlab(GITLAB_URL, PRIVATE_TOKEN) + for (lastName, firstName, email) in readNames(csvFile): + try: +# if email.endswith('@telecom-paris.fr'): +# email = email.split('@')[0] + "@telecom-paristech.fr" + user = lookupUser(email, api) + print("::: Creating project for {} {} <{}>…".format( + lastName, firstName, email, user.id)) + print("::: Found {} ({})".format(user.name, user.id)) + project = createProject('{} {}'.format(lastName, firstName), + groupId, api) + member = addProjectMember(project, user.id, + gitlab.MAINTAINER_ACCESS) + except IndexError as e: + print >> sys.stderr, "!!! Could not create project for {}: User not found ({})".format(email, e) + except Exception as e: + # print("!!! Could not create user for {}: {}".format(email, e), file=sys.stderr) + print >> sys.stderr, "!!! Error creating project for {}: {}".format(email, e) + +if __name__ == '__main__': + if len(sys.argv) != 4: + print("*** Usage: python {} ".format(sys.argv[0])) + print("*** csv: a CSV file exported from Synapses.") + print("*** The first three columns are assumed to be ") + print("*** last name, first name, email address.") + print("*** groupId: The GitLab id where all the projects will") + print("*** be added.") + print("*** token: A GitLab API token as created at ") + print("*** {}/-/profile/personal_access_tokens".format(GITLAB_URL)) + print("*** Be sure to enable API scope.") + sys.exit(1) + makeProjectsInGroupId(sys.argv[2], sys.argv[1])