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

Unable to check out tag created via the github API

我按照此 Github create tag API 参考资料中的说明操作:

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

我都做了。引用当然只是一个分支,我可以在我的终端中查看它,也可以在 Github web 中查看它。

然而,标签无处可寻,Github 网络和 git 结帐都找不到 tags/tag-name

我的代码如下:

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

但正如我所说,该标签不在 Github 网络中,我也无法在本地查看。我错过了什么?

问题出在我创建的 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 提取后检出本地存储库中的标签:

git fetch --all
git checkout tags/MyNewTag