Change Default branch for GITHUB using API.

2020-04-28 04:29发布

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

3条回答
Rolldiameter
2楼-- · 2020-04-28 05:27

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" }

查看更多
beautiful°
3楼-- · 2020-04-28 05:27

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:

查看更多
Luminary・发光体
4楼-- · 2020-04-28 05:30

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"}
查看更多
登录 后发表回答