Skip to content
Snippets Groups Projects
Commit e548298a authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
# Project-specific
# none (for now)
# Mac file finder metadata
.DS_Store
# Emacs backup file
*~
# Python files
*.pyc
*.pyo
__pycache__/
# JetBrains (IntelliJ IDEA, PyCharm, etc) files
.idea/
*.iml
*.iws
*.ipr
# Miscellaneous
tmp/
*.tmp
*.bak
*.swp
# CSCE 361 Scripts
These scripts are designed to automate the creation, population, and staffing of repositories for student assignments.
## Requires
- python-gitlab
- pip3 install python-gitlab
- documentation: https://python-gitlab.readthedocs.io
- (eventually, a Canvas package)
## Files
- config.py
- provides URLs and API keys
- *do not* commit API keys to repository
- you will need to replace `None` with your API keys (as strings)
- course.py
- provides namespace and group ID
- will need to update each semester
- gitlab_functions.py
- provides functions to query & update projects (including its members and issues)
- These functions are those we believe are useful for automating pair & team projects for CSCE 361
- These generally are wrappers for calls you *could* make directly from what python-gitlab provides, but these
functions don't require you to memorize the api
- includes an enumeration of the fields for User, Project, and Issue
- includes side-effect-free example function calls
- gitlab_demo.py
- includes commented-out functions demonstrating the creation of projects, adding users to projects, and
creating/updating the projects' issues
class Config:
# GitLab API Configuration
gitlab_url = 'https://git.unl.edu/'
gitlab_api_key = None
# Canvas API configuration
canvas_url = "https://canvas.unl.edu/api/v1/"
canvas_api_key = None
class Course:
# GitLab course information
gitlab_namespace = 'csce_361/sandbox'
# Canvas course information
canvas_course_id = None
from gitlab import Gitlab
import gitlab_functions
from config import Config
from course import Course
"""
def add_rubi(git_server):
team_number = 99
project_name = f'foo{team_number}'
project = gitlab_functions.create_project_in_group(git_server, Course.gitlab_namespace, project_name)
user = gitlab_functions.get_user_by_name(git_server, 'rubi.quinones')
gitlab_functions.add_user_to_project_as_maintainer(project, user)
team_number = 98
project_name = f'bar{team_number}'
project = gitlab_functions.create_project_in_group(git_server, Course.gitlab_namespace, project_name)
user = gitlab_functions.add_user_to_project_as_maintainer(git_server, project, 'rubi.quinones')
team_number = 97
project_name = f'baz{team_number}'
project = gitlab_functions.create_project_in_group(git_server, Course.gitlab_namespace, project_name)
gitlab_functions.add_user_to_project_as_maintainer(git_server, project, user)
"""
"""
def add_and_print_issues(git_server):
project_name = f'{Course.gitlab_namespace}/baz97'
project = gitlab_functions.get_project_by_path(git_server, project_name)
issue1 = gitlab_functions.create_issue(project, 'Larry', 'Larry Fine')
issue2 = gitlab_functions.create_issue(project, 'Moe', 'Moe Howard')
issue3 = gitlab_functions.create_issue(project, 'Curly', 'Curly Howard')
issue2.state_event = 'close'
issue2.save()
issues = gitlab_functions.get_issues(project)
for issue in issues:
if issue.closed_at is None:
print(f'Issue #{issue.iid} created at {issue.created_at} and updated at {issue.updated_at}')
else:
print(f'Issue #{issue.iid} created at {issue.created_at} and closed at {issue.closed_at}')
"""
if __name__ == '__main__':
git = Gitlab(Config.gitlab_url, private_token=Config.gitlab_api_key)
# add_rubi(git)
# add_and_print_issues(git)
import gitlab
from config import Config
def get_user_by_name(git_server, username):
return git_server.users.list(username=username)[0]
def get_user_by_id(git_server, user_id):
return git_server.users.get(user_id)
def get_project_by_id(git_server, project_id):
return git_server.projects.get(project_id)
def get_project_by_path(git_server, namespace_and_name):
return git_server.projects.get(namespace_and_name)
def get_projects_by_group(git_server, group_id):
return git_server.groups.get(group_id).projects.list()
def get_projects_by_keyword(git_server, search_term):
return git_server.projects.list(search=search_term)
def create_project(git_server, project_name):
git_server.projects.create({'name': project_name})
def create_project_in_group(git_server, group_name, project_name):
group_id = git_server.groups.get(group_name).id
return git_server.projects.create({'name': project_name, 'namespace_id': group_id})
def add_user_to_project_as_maintainer(git_server, project, user):
if isinstance(user, str):
git_user = get_user_by_name(git_server, user)
else:
git_user = user
return project.members.create({'user_id': git_user.id, 'access_level': gitlab.MAINTAINER_ACCESS})
def get_issues(project):
return project.issues.list()
def create_issue(project, title, description):
return project.issues.create({'title': title, 'description': description})
# user fields--
# id: gitlab User ID number
# name: plain-text human name
# username: gitlab username
# state
# avatar_url
# web_url: https URL to user's git site, would be the base path to any personal repositories they created
# project fields--
# id: Project ID number
# description
# name: project name without namespace
# name_with_namespace: project name with namespace, spaces around slashes
# path: path without namespace (may differ from name if name has spaces)
# path_with_namespace path with namespace, no spaces around slashes
# created_at
# default_branch
# tag_list
# ssh_url_to_repo ssh URL to clone repository
# http_url_to_repo https URL to clone repository
# web_url https URL to git site, should be http_url_to_repo without trailing '.git'
# readme_url https URL to README.md
# avatar_url
# star_count
# forks_count
# last_activity_at
# namespace namespace's JSON object
# _links JSON object with api/v4 links to self, issues, merge_requests,
# repo_branches, labels, events, members
# empty_repo
# archived
# visibility 'private', etc.
# resolve_outdated_diff_discussions
# container_registry_enabled
# issues_enabled
# merge_requests_enabled
# jobs_enabled
# snippets_enabled
# issues_access_level
# repository_access_level,
# wiki_access_level
# builds_access_level
# snippets_access_level
# shared_runners_enabled
# lfs_enabled
# creator_id gitlab User ID of user who created repository
# import_status
# import_error
# open_issues_count
# runners_token
# ci_default_git_depth
# public_jobs
# build_git_strategy
# build_timeout
# auto_cancel_pending_pipelines
# build_coverage_regex
# ci_config_path
# shared_with_groups
# only_allow_merge_if_pipeline_succeeds
# request_access_enabled
# only_allow_merge_if_all_discussions_are_resolved
# printing_merge_request_link_enabled
# merge_method
# auto_devops_enabled
# auto_devops_deploy_strategy
# permissions
# issue fields--
# id universally-unique identifier
# iid project-unique identifier
# project_id
# title
# description
# state opened or closed
# created_at format yyyy-mm-ddThh:mm:ss.sssUTCoffset (e.g., '2019-08-13T11:32:44.590-05:00')
# updated_at same date format
# closed_at same date format, or None (seems to prefer no UTC offset, e.g., '2019-08-13T11:32:44.590Z')
# closed_by user
# labels list of labels
# milestone
# assignees list of users
# author user
# assignee user
# user_notes_count
# merge_requests_count
# upvotes
# downvotes
# due_date same date format, or None
# confidential
# discussion_locked
# web_url https URL to issue's page
# time_stats
# task_completion_status
# has_tasks
# _links
# notes
# award_emoji
# project
# subscribed
if __name__ == '__main__':
git = gitlab.Gitlab(Config.gitlab_url, private_token=Config.gitlab_api_key)
print('getting a user, by name')
print(get_user_by_name(git, 'bohn'))
print('getting a user by user ID and printing only the user\'s name')
print(get_user_by_id(git, 519).name)
print('getting a project by its namespace/name')
print(get_project_by_path(git, 'csce_361/sandbox/HelloWorld'))
print('getting a project by project ID and printing only the project\'s description')
print(get_project_by_id(git, 5220).description)
print('getting a group by name')
print(git.groups.get('csce_361').name)
name = git.groups.get('csce_361/sandbox').full_path
gid = git.groups.get('csce_361/sandbox').id
print(f'{name}\t{id}')
print('getting a group by group ID')
print(get_projects_by_group(git, 1946)[0])
print('getting a list of projects that match a keyword and printing the first project in the list')
print(get_projects_by_keyword(git, "Homework")[0])
print('getting a list of projects in a group (by group ID) and printing the second project in the list')
print(get_projects_by_group(git, 1937)[1])
print('getting a list of project members (has only inherited members)')
print(get_project_by_id(git, 5220).members.list())
print('getting a list of project members, including inherited members')
print(get_project_by_id(git, 5220).members.all(all=True))
print('getting a list of project members (has assigned members), printing the third\'s name')
print(get_project_by_id(git, 5176).members.list()[2].name)
print('getting a list of issues and printing the first')
print(get_issues(get_project_by_id(git, 5215))[0])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment