Created
May 2, 2021 23:03
-
-
Save vanessaaleung/a076863c7c226f36de64df01b44e4092 to your computer and use it in GitHub Desktop.
create_git_branch
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
def create_branch(owner: str, repo: str, token: str, base: str, new_branch: str) -> None: | |
"""Create a new branch""" | |
url = "https://api.github.com/repos/{}/{}/git/refs".format(owner, repo) | |
headers = { | |
"Authorization": "token {}".format(token), | |
"Content-Type": "application/json" | |
} | |
try: | |
get_response = requests.get(url+"/heads/"+base, headers=headers) | |
sha = json.loads(get_response.content)["object"]["sha"] | |
payload = { | |
"ref": "refs/heads/"+new_branch, | |
"sha": sha | |
} | |
requests.post(url, headers=headers, json=payload) | |
logging.info("New branch {} created".format(new_branch)) | |
except Exception as e: | |
logging.error(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment