From 6a6904bb7c7bdcaed7b8982871b5f42a5d993cfa Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sat, 4 Sep 2021 07:55:06 +0300 Subject: [PATCH] Add a GitHub Python submodule. Under the rcfiles module. Similar to the GitLab submodule. --- Documents/bin/rcfiles/github.py | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Documents/bin/rcfiles/github.py diff --git a/Documents/bin/rcfiles/github.py b/Documents/bin/rcfiles/github.py new file mode 100644 index 0000000..f6bf094 --- /dev/null +++ b/Documents/bin/rcfiles/github.py @@ -0,0 +1,42 @@ +"""A bunch of convenience functions to deal with GitHub.""" + +import os +import github3 # pylint: disable=import-error + + +def connect(): + """Return a GitHub session.""" + try: + token = os.environ["GITHUB_TOKEN"] + except KeyError: + raise Exception("GITHUB_TOKEN environment variable not set.") + return github3.login(token=token) + + +def github_me(conn): + """Return my GitHub account name.""" + return conn.me().login + + +def create_repo( # pylint: disable=too-many-arguments + conn, name, description="", homepage="", has_issues=False, has_wiki=False +): + """Create a new GitHub repository under the login namespace.""" + return conn.create_repository( + name, description, homepage, has_issues=has_issues, has_wiki=has_wiki + ) + + +def get_repo(conn, name): + """Return the GitHub repo object with the given name.""" + me = github_me(conn) # pylint: disable=invalid-name + for i in conn.repositries_by(me, type="owner"): + if i.name == name: + return i + raise Exception(f"Could not find the {name} GitHub repository.") + + +def read_only_github(conn, name): + """Make a GitHub repository read-only.""" + repo = get_repo(conn, name) + repo.edit(name, archived=True) -- GitLab