Skip to content

Instantly share code, notes, and snippets.

@verdimrc
Created February 11, 2022 04:13
Show Gist options
  • Save verdimrc/ebed23d5b010703c6205788ff2073e63 to your computer and use it in GitHub Desktop.
Save verdimrc/ebed23d5b010703c6205788ff2073e63 to your computer and use it in GitHub Desktop.
gh-example.py
import rich.pretty
rich.pretty.install()
import os
from github3 import login
gh = login(username="username", token=os.environ["token"])
repo = gh.repository("username", "reponame")
repo.create_file(
f"src/README1.md",
message="haha hehe",
content="some yaml string here".encode("utf-8"),
committer={"name": "User Name", "email": "[email protected]"},
author={"name": "User Name", "email": "[email protected]"},
)
"""
{
'content': <Contents [src/README1.md]>,
'commit': <Commit [long_string_1]>,
'Last-Modified': '',
'ETag': '"long_string_2"'
}
"""
try:
repo.create_file(
f"src/README1.md",
message="haha hehe",
content="some yaml string here".encode("utf-8"),
committer={"name": "User Name", "email": "[email protected]"},
author={"name": "User Name", "email": "[email protected]"},
)
except Exception as e:
if e.code == 422 and '"sha" wasn\'t supplied' in e.msg:
# Looks like an existing file. Try to update instead.
repo.file_contents("src/README1.md").update(
message="hihi hoho",
content="another yaml string".encode("utf-8"),
committer={
"name": "User Name",
"email": "[email protected]",
},
author={
"name": "User Name",
"email": "[email protected]",
},
)
else:
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment