-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63dfc3e
commit 18b0513
Showing
25 changed files
with
1,074 additions
and
1,757 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# CanvasGroupy | ||
CanvasGroupy | ||
================ | ||
|
||
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! --> | ||
|
||
|
Oops, something went wrong.