Created
September 6, 2016 14:30
-
-
Save nottrobin/a18f9e33286f9db4b83e48af6d285e29 to your computer and use it in GitHub Desktop.
With Github API v3, create branch, commit a change to a file and open a pull request
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
#! /usr/bin/env python | |
from base64 import b64decode | |
from github import Github | |
with open('access-token.txt') as token_file: | |
token = token_file.read().strip() | |
api = Github(token) | |
site = api.get_repo('nottrobin/gh-cms-example-site') | |
def create_branch(branch_name): | |
return site.create_git_ref( | |
'refs/heads/{branch_name}'.format(**locals()), | |
site.get_branch('master').commit.sha | |
) | |
def commit_change(branch_name): | |
index_file = site.get_contents('index.html') | |
index_content = b64decode(index_file.content) | |
updated_content = index_content.replace('<html>', '<html lang="en">') | |
return site.update_file( | |
path='/index.html', | |
message='Add language to markup', | |
content=updated_content, | |
sha=index_file.sha, | |
branch=branch_name | |
) | |
def create_pull_request(branch_name): | |
return site.create_pull( | |
title="Add language to markup", | |
body=( | |
"# Description\n\nAdd `lang=en` to the HTML tag.\n" | |
"# QA\n\nThis will literally change nothing of significance." | |
), | |
base="master", | |
head=branch_name | |
) | |
branch_name = "add-language" | |
create_branch(branch_name) | |
commit_change(branch_name) | |
pull = create_pull_request(branch_name) | |
print pull.html_url |
I ran into the same issue, but after reading the code, I realized the repo needed a file called index.html
with the content of '<html>'. Also if you have your default branch to something other, like 'main' it will also fail.
I made a few mods for my needs so it creates a new file instead:
f = open("myfile.txt", "a")
f.write("some file content")
f.close()
site.create_file( path='myfile.txt', message='Adding a file', content='some file content', branch=branch_name)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried it but says
Traceback (most recent call last):
File "api_demo.py", line 17, in
site = api.get_repo(os.getenv('repo'))
File "/home/cardona/.local/lib/python3.8/site-packages/github/MainClass.py", line 321, in get_repo
headers, data = self.__requester.requestJsonAndCheck("GET", url)
File "/home/cardona/.local/lib/python3.8/site-packages/github/Requester.py", line 400, in requestJsonAndCheck
return self.__check(
File "/home/cardona/.local/lib/python3.8/site-packages/github/Requester.py", line 425, in __check
raise self.__createException(status, responseHeaders, output)
github.GithubException.UnknownObjectException: 404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository"}
I have it configured it as an os.getenv both the repo and the token. But if I hardcode them I get the same error
Do you know what can it be?