Skip to content

Commit

Permalink
github integration
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-yj-yang committed Apr 20, 2023
1 parent 63dfc3e commit 18b0513
Show file tree
Hide file tree
Showing 25 changed files with 1,074 additions and 1,757 deletions.
30 changes: 14 additions & 16 deletions CanvasGroupy/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.__init__': ( 'gh_group_creation.html#githubgroup.__init__',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.cd_cmd': ('gh_group_creation.html#cd_cmd', 'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.check_gh_auth': ( 'gh_group_creation.html#check_gh_auth',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.command': ('gh_group_creation.html#command', 'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.delete_directory': ( 'gh_group_creation.html#delete_directory',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.git_add_all': ( 'gh_group_creation.html#git_add_all',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.git_commit': ( 'gh_group_creation.html#git_commit',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.git_creat_repo_cmd': ( 'gh_group_creation.html#git_creat_repo_cmd',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.git_manage_access_cmd': ( 'gh_group_creation.html#git_manage_access_cmd',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.git_rename_cmd': ( 'gh_group_creation.html#git_rename_cmd',
'CanvasGroupy/gh_group.py')}}}
'CanvasGroupy.gh_group.GitHubGroup.add_collaborators': ( 'gh_group_creation.html#githubgroup.add_collaborators',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.auth_github': ( 'gh_group_creation.html#githubgroup.auth_github',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.create_group_repo': ( 'gh_group_creation.html#githubgroup.create_group_repo',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.create_repo': ( 'gh_group_creation.html#githubgroup.create_repo',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.get_repo': ( 'gh_group_creation.html#githubgroup.get_repo',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.rename_files': ( 'gh_group_creation.html#githubgroup.rename_files',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.set_org': ( 'gh_group_creation.html#githubgroup.set_org',
'CanvasGroupy/gh_group.py')}}}
2 changes: 1 addition & 1 deletion CanvasGroupy/canvas_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def set_group_category(self,
self.group_category = self.group_categories[category_name]
except KeyError:
raise KeyError(f"{category_name} did not found in the group categories."
f"\n Try to create one with CanvasGroup.create_group_category")
f"Try to create one with CanvasGroup.create_group_category")
return self.group_category

def get_course(self):
Expand Down
183 changes: 96 additions & 87 deletions CanvasGroupy/gh_group.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,108 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_gh_group_creation.ipynb.

# %% auto 0
__all__ = ['GitHubGroup', 'command', 'check_gh_auth', 'git_creat_repo_cmd', 'git_manage_access_cmd', 'cd_cmd', 'git_rename_cmd',
'git_commit', 'git_add_all', 'delete_directory']
__all__ = ['GitHubGroup']

# %% ../nbs/02_gh_group_creation.ipynb 3
from github import Github
import github
import json

# %% ../nbs/02_gh_group_creation.ipynb 4
class GitHubGroup():
class GitHubGroup:
def __init__(self,
token="",
org=""
credentials_fp="", # the file path to the credential json
org="", # the organization name
):
return ...
self.github = None
self.org = None

if credentials_fp != "":
self.auth_github(credentials_fp)
if org != "":
self.set_org(org)

# %% ../nbs/02_gh_group_creation.ipynb 8
def command(cmd: str # command to call in bash
) -> (bool, str): # success, output
"Execute the given command in shell"
success = False
try:
output = subprocess.check_output(cmd, shell=True, stderr = subprocess.STDOUT)
success = True
except subprocess.CalledProcessError as e:
output = e.output
except Exception as e:
# check_call can raise other exceptions, such as FileNotFoundError
output = str(e)
print('> '+cmd)
print(output.decode())
return success, output
def auth_github(self,
credentials_fp: str # the personal access token generated at GitHub Settings
):
"Authenticate GitHub account with the provided credentials"
with open(credentials_fp, "r") as f:
token = json.load(f)["GitHub Token"]
self.github = Github(token)
# check authorization
_ = self.github.get_user().get_repos()[0]

def set_org(self,
org: str # the target organization name
):
"Set the target organization for repo creation"
self.org = self.github.get_organization(org)

# %% ../nbs/02_gh_group_creation.ipynb 9
def check_gh_auth() -> bool: # whether you have authenticate
"Check whether you have gh auth configured"
msg = command("gh auth status")[1]
if "You are not logged into any GitHub hosts" in str(msg):
return False
return True

# %% ../nbs/02_gh_group_creation.ipynb 10
def git_creat_repo_cmd(repo_name:str, # the name of the created repository
repo_org: str, # the GitHub organization name
template_repo:str, # the template repository that the new repo will use
private_repo=True # the visibility of the repository
) -> str: # the command of repo creation
"Generate the appropriate command for creating GitHub repository"
cmd = "gh repo create"
if template_repo is not None:
cmd = f"{cmd} --template {template_repo}"
if private_repo:
cmd = f"{cmd} --private"
cmd = f"{cmd} --clone {repo_org}/{repo_name}"
return cmd

# %% ../nbs/02_gh_group_creation.ipynb 12
def git_manage_access_cmd(repo_name:str, # the name of the created repository
repo_org: str, # the GitHub organization name that the repo belongs to
collaorator_id: str, # the GitHub id of the collaborator, or team name if `add_team=True`
permission="push", # the permission to that collaborator
add_team=False # add access to github org's team
) -> str: # the command of repo creation
"GitHub CLI Command for modifying access priviliges of repo"
if add_team:
return (f"gh api -X PUT -f permission={permission} --silent "
f"/orgs/{repo_org}/teams/{collaorator_id}/repos/{repo_org}/{repo_name}")
cmd = f"gh api -X PUT -f permission={permission} --silent repos/{repo_org}/{repo_name}/{collaorator_id}"
return cmd


# %% ../nbs/02_gh_group_creation.ipynb 15
def cd_cmd(target_dir: str # the target directory we want to change to
) -> str: # the command of cd
"Unix command for change directory"
return f"cd {target_dir}"

# %% ../nbs/02_gh_group_creation.ipynb 16
def git_rename_cmd(source: str, # the original name of the file
target: str # the target name of the file
) -> str: # the command of git rename file
"GitHub mv command to rename file in the directory"
return f"git mv {source} {target}"

# %% ../nbs/02_gh_group_creation.ipynb 17
def git_commit(commit_msg: str, # Commit Message
) -> str: # the command for git commit
"GitHub commit command"
return f'git commit -m "{commit_msg}"'

# %% ../nbs/02_gh_group_creation.ipynb 18
def git_add_all():
return "git add -A"

# %% ../nbs/02_gh_group_creation.ipynb 19
def delete_directory(dir_name: str # the directory that we want to delete
) -> str: # the command for delete dir
return f"rm -rf {dir_name}"
def create_repo(self,
repo_name: str, # repository name
repo_template="", # template repository that new repo will use. If empty string, an empty repo will be created. Put in the format of "<owner>/<repo>"
private=True, # visibility of the created repository
description="", # description for the GitHub repository
personal_account=False, # create repos in personal GitHub account
) -> github.Repository.Repository:
"Create a repository, either blank, or from a template"
if self.org is None and personal_account:
raise ValueError("Organization is not set")
if personal_account:
parent = self.github.get_user()
else:
parent = self.org
if repo_template == "":
return parent.create_repo(
name=repo_name,
private=private,
description=description
)
# create from template
return parent.create_repo_from_template(
name=repo_name,
repo=self.get_repo(repo_template),
private=private,
description=description,
)

def get_repo(self,
repo_full_name: str # full name of the target repository
) -> github.Repository.Repository:
"To get a repository by its name"
return self.github.get_repo(repo_full_name)

def rename_files(self,
repo: github.Repository.Repository, # the repository that we want to rename file
og_filename: str, # old file name
new_filename: str # new file name
):
"Rename the file by delete the old file and commit the new file"
file = repo.get_contents(og_filename)
repo.create_file(new_filename, "rename files", file.decoded_content)
repo.delete_file(og_filename, "delete old files", file.sha)

def add_collaborators(self,
repo: github.Repository.Repository, # target repository
collaborator:str, # GitHub username of the collaborator
permission:str # `pull`, `push` or `admin`
):
"add collaborators to the repository"
repo.add_to_collaborators(collaborator, permission)

def create_group_repo(self,
repo_name: str, # group repository name
collaborators: [str], # list of collaborators GitHub id
permission: str, # the permission of collaborators
rename_files=dict(), # dictionary of files renames {<og_name>:<new_name>}
repo_template="", # If empty string, an empty repo will be created. Put in the format of "<owner>/<repo>"
private=True, # visibility of the created repository
description="", # description for the GitHub repository
) -> github.Repository.Repository:
"Create a Group Repository"
repo = self.create_repo(repo_name, repo_template, private, description)
for og_name, new_name in rename_files.values():
self.rename_files(repo, og_name, new_name)
for collaborator in collaborators:
self.add_collaborators(repo, collaborator)
return repo
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# CanvasGroupy
CanvasGroupy
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Expand Down
Loading

0 comments on commit 18b0513

Please sign in to comment.