Change Default branch for GITHUB using API.

霸气de小男生 提交于 2019-12-20 06:41:43

问题


I need to create a branch dev which is other than master branch. Also need to update dev to default branch using GITHUB API.

Please share details if anyone know which API to call or a way to do it, programmatically. I understand with help of UI we can do it.

Thanks Ashish


回答1:


Following the guide here: https://developer.github.com/v3/repos/#edit , default_branch input should make what you want

default_branch (string): Updates the default branch for this repository.

So, you should submit a PATCH request like:

PATCH /repos/:owner/:repo

{"default_branch": "dev"}



回答2:


I don't have enough reputation to reply to the Adam's comment above but the problem is name is a required field. The JSON should actually be:

PATCH /repos/:owner/:repo { "name":":repo" "default_branch": "dev" }




回答3:


You can use requests library:

import requests
access_token = "your_access_token"
headers = {'Authorization': f'token {access_token}', 
           'Content-Type':'application/json'}
data={"name":"knowledge-engine", "default_branch": "development"}
owner = "username"
repo_name = "repo_name"
url = f"https://api.github.com/repos/{owner}/{repo_name}"
requests.patch(url, data=json.dumps(data), headers=headers)
<Response [200]>

Docs:

  • https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
  • http://docs.python-requests.org/en/master/
  • https://developer.github.com/v3/repos/#edit


来源:https://stackoverflow.com/questions/52776313/change-default-branch-for-github-using-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!