Commit e7a8b5d6 authored by Henrik Skov Midtiby's avatar Henrik Skov Midtiby

Initial commit.

parents
config.py
env/
Currently I need to have the students create user accounts on source.coderefinery.org prior to being able to invite them into projects.
For each student I need their username and their id
import requests
import pprint
import git
import config
def gitlab_url_api4(path):
return 'https://source.coderefinery.org/api/v4' + path
def get_commits(repository_id):
url = gitlab_url_api4("/projects/%d/repository/commits" % repository_id)
return requests.get(url,
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def get_project_details(project_id):
url = gitlab_url_api4("/projects/%d" % project_id)
return requests.get(url,
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def create_a_new_repository(repo_name):
return requests.post(gitlab_url_api4('/projects?name=%s' % repo_name),
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def create_a_new_repository_in_group(repo_name, group_id):
return requests.post(gitlab_url_api4('/projects?name=%s' % repo_name),
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def add_user_to_project(project_id, user_id):
return requests.post(
gitlab_url_api4('/projects/%d/members?user_id=%d&access_level=30' %
(project_id, user_id)),
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def push_git_repository_content_to_project(path_to_git_repository, project_id):
repo = git.Repo(path_to_git_repository)
resp = get_project_details(project_id)
temp = resp.json()
ssh_url_to_git_repo = temp['ssh_url_to_repo']
repo.git.push(ssh_url_to_git_repo, 'master')
def fetch_and_show_commits_example():
print("fetch_and_show_commits_example")
# Fetch commits from repository 692 -
# camera-calibration-using-extended-chessboard
resp = get_commits(692)
print(resp)
for item in resp.json():
print(item['title'])
# pprint.pprint(item)
def create_a_new_repository_example():
print("create_a_new_repository_example")
resp = create_a_new_repository('henrik-tester')
print(resp)
pprint.pprint(resp.json())
if resp.status_code == 200:
print("Project created succesfully")
project_id = resp.json()['id']
print("project_id: %d" % project_id)
else:
print("Project creation failed")
def add_user_to_project_example():
print("add_user_to_project_example")
resp = add_user_to_project(701, 646)
print(resp)
pprint.pprint(resp.json())
def push_git_repository_content_to_project_example():
print("push_git_repository_content_to_project_example")
push_git_repository_content_to_project('hw01', 701)
def create_assignment_project(
project_name,
student_id,
path_to_git_repository):
print("create_a_new_repository_example")
resp = create_a_new_repository(project_name)
print(resp)
#pprint.pprint(resp.json())
assert(resp.status_code == 201)
print("Project created succesfully")
project_id = resp.json()['id']
print("project_id: %d" % project_id)
print("Add student to project")
resp = add_user_to_project(project_id, student_id)
print(resp)
#pprint.pprint(resp.json())
print("Push content of local repository to project")
push_git_repository_content_to_project(path_to_git_repository, project_id)
def create_assignment_project_example():
create_assignment_project(
project_name='hw01-henrikmidtiby4',
student_id=646,
path_to_git_repository='hw01')
# fetch_and_show_commits_example()
# create_a_new_repository_example()
# project id: 701
# user id: 646 (henrikmidtiby@gmail.com)
# add_user_to_project_example()
# push_git_repository_content_to_project_example()
create_assignment_project_example()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment