Skip to content

Instantly share code, notes, and snippets.

@looneym
Created May 14, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save looneym/a448c9b3b62baaf918bd285103fe373c to your computer and use it in GitHub Desktop.
Save looneym/a448c9b3b62baaf918bd285103fe373c to your computer and use it in GitHub Desktop.
Create a branch in a remote repo using the Github API. Uses token auth.
import requests
import os
import json
TOKEN = os.environ['GH']
NEW_BRANCH_NAME = ''
HASH = ''
AUTHOR= ''
REPO = ''
url = 'https://api.github.com/repos/{}/{}/git/refs'.format(AUTHOR, REPO)
payload = \
{
"ref": "refs/heads/{}".format(NEW_BRANCH_NAME),
"sha": "{}".format(HASH)
}
headers = \
{
'Content-Type': 'application/json',
'Authorization': 'token {}'.format(TOKEN)
}
r = requests.post(url=url, data=json.dumps(payload), headers=headers)
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment