无法签出通过github API创建的标记

2024-09-29 19:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循了Github create tagAPI参考中的说明:

1) create a tag from a sha
2) create a reference for that commit

我两个都做了。引用当然只是一个分支,我可以在我的终端上很好地查看它,也可以在githubweb上很好地查看它。你知道吗

但是,标签不见了,既没有Github web,也没有git checkout标签/标签名

我的代码如下:

import json
import requests
from requests.auth import HTTPBasicAuth
url = 'https://api.github.com/repos/MYORG/MYREPO/git/tags'
params = dict(ref="refs/heads/MyNewTag",    sha="SHA")
res = requests.post(url, data=json.dumps(params), auth=HTTPBasicAuth('user', 'token'))
# this works and returns a 201 with its own sha
url = 'https://api.github.com/repos/MYORG/MYREPO/git/refs', params = dict(ref="refs/heads/MyNewBranch", sha="SHA")
res = requests.post(url, data=json.dumps(params), auth=HTTPBasicAuth('user', 'token'))
#this also works and returns a 201

但是正如我所说的,这个标签不在githubweb中,我也不能在本地查看它。我错过了什么?你知道吗


Tags: fromimportgitgithubauthjsonurlcreate
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:15

问题出在我创建的ref中。它应该是ref/tags/MYNewTag。其代码为:

import requests
from requests.auth import HTTPBasicAuth

#create a release branch from the desired commit
url = 'https://api.github.com/repos/MYORG/MYREPO/git/refs'
params = dict(ref="refs/heads/MyNewBranch", sha="SHA")
res = requests.post(url, data=json.dumps(params), auth=HTTPBasicAuth('user', 'token'))

#you should check for res.status_code first to avoid erorr
sha = res.json()['object']['sha']
# create a tag from the desired sha (same one that was used to create the release branch)
url = 'https://api.github.com/repos/MYORG/MYREPO/git/tags'
params = dict(tag="MyNewTag", message="testing tagging with API", object=sha, type="commit",
       tagger={name=name, email=email, data=date})
res = requests.post(url, data=json.dumps(params), auth=HTTPBasicAuth('user', 'token'))

#now get the sha of the new tag
sha = res.json()['object']['sha'] # again don't forget check status_code
url = 'https://api.github.com/repos/ttcpaselect/cpaselect/git/refs'
params = dict(ref="refs/tags/MyNewTag", sha=sha)
res = requests.post(url, data=json.dumps(params), auth=HTTPBasicAuth('user', 'token'))

现在,您可以在执行git fetch之后在本地repo中签出标记,如下所示:

git fetch  all
git checkout tags/MyNewTag

相关问题 更多 >

    热门问题