Created
February 11, 2022 04:13
-
-
Save verdimrc/ebed23d5b010703c6205788ff2073e63 to your computer and use it in GitHub Desktop.
gh-example.py
This file contains hidden or 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
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