Commit 9d5b689f authored by Henrik Skov Midtiby's avatar Henrik Skov Midtiby

Remove dead code.

parent f878035e
import requests
import pprint
import git
import config
# The file config.py contains a single line with the content
# PersonalAcessToken = "value of personal access token"
def gitlab_url_api4(path):
return 'https://source.coderefinery.org/api/v4' + path
def get_commits(repository_id):
"""
Fetch commits from the project with id "project_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):
"""
Fetch project details about the project with id "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):
"""
Create a new repository with the specified 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):
"""
Create a new project with the name "repo_name".
The project is placed in the namespace with id "group_id".
"""
return requests.post(gitlab_url_api4('/projects?name=%s&namespace_id=%d' %
(repo_name, group_id)),
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def add_user_to_project(project_id, user_id):
"""
Add user with id "user_id" to the project with id "project_id".
Give developer access rights to the added user.
"""
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):
"""
Push the local git repository located at "path_to_git_repository"
to the project with id "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 and show commits from repository 692 -
# https://source.coderefinery.org/sdu-uas-center/perception/camera-calibration-with-large-chessboards
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,
group_id,
path_to_git_repository):
"""
Create a new project with the name 'project_name' in the namespace
provided by the group with id 'group_id'.
Add the student with id 'student_id' to the project.
Push the current content of the local git repository at the location
'path_to_git_repository' to the project.
"""
print("create_a_new_repository_example")
resp = create_a_new_repository_in_group(project_name, group_id)
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, # henrikmidtiby@gmail.com
group_id=715, # sdu-uas-center/gitlab-experiments
path_to_git_repository='hw01')
def create_a_new_repository_in_group_example():
print("create_a_new_repository_in_group_example")
resp = create_a_new_repository_in_group('hw01-henrikmidtiby5', 453)
print(resp)
pprint.pprint(resp.json())
def create_a_new_repository_in_group_example_two():
print("create_a_new_repository_in_group_example")
resp = create_a_new_repository_in_group('hw01-henrikmidtiby6', 715)
print(resp)
pprint.pprint(resp.json())
def get_user_details(username):
"""
Fetch details about the user with username "username".
"""
url = gitlab_url_api4("/users?username=%s" % username)
return requests.get(url,
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def get_user_details_example():
print("get_user_details")
resp = get_user_details("henrikmidtiby")
print(resp)
pprint.pprint(resp.json())
def autocomplete_user(username):
url = "https://source.coderefinery.org/autocomplete/users.json?search=%s&active=true" % username
#url = gitlab_url_api4("/users?username=%s" % username)
return requests.get(url,
headers={'Authorization': 'Bearer {}'.format(config.PersonalAcessToken)})
def autocomplete_user_example():
resp = autocomplete_user("hesc")
print(resp)
pprint.pprint(resp.json())
# Todos
# * Look up username to get the userid of the user (henrikmidtiby@gmail.com, # 646)
# * Look up class name to get the groupid of the class (SDU UAS Center, # 453)
# * Create a group for a new class
# * Load list of assignments / projects and create repositories for these
# within a specified class.
# * Load list of students and assignments, and add students to the specific
# assignments (within a class).
# * Load list of assignments, download / clone all repositories to this
# computer.
# List of assignments
#assignment_name class
#hw1-adam test2
#hw1-birger test2
#hw2-adam test2
#hw2-birger test2
# List of students to add to a project
#student_id assignment_name class
#101 hw1-birger test2
#105 hw1-adam test2
#105 hw2-adam test2
# 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()
# create_a_new_repository_in_group_example()
# create_a_new_repository_in_group_example_two()
#get_user_details_example()
autocomplete_user_example()
# SDU UAS Center - group_id = 453
# gitlab-experiments - group_id = 715
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