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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: