【问题标题】:How to get all tags of a commit from the GitHub API如何从 GitHub API 获取提交的所有标签
【发布时间】:2015-01-31 07:02:21
【问题描述】:

如何从 GitHub API 获取提交的所有标签。

问题:我确实有一个文件。我从 API 中检索了文件的提交。现在我需要知道包含特定提交的所有标签。 GitHub 网站确实很好地列出了标签,但我在 API 上找不到方法。

【问题讨论】:

  • 我正在寻找相同的。我认为 github API 只是半生不熟。
  • 嘿伙计!我找到了诀窍。您必须实际使用参考。这是一个例子:api.github.com/repos/wisebrains/wise-archetypes/tags
  • @Theuserwithnohat 据我了解 topicstarter 他需要找出特定提交的所有标签,反之亦然。此外,您的 url 输出中存在的提交是标签本身的提交

标签: github-api


【解决方案1】:

在 Github Web UI 中,有一个内部调用可以检索与给定提交关联的分支名称和标签,如下所示:

你可以调用这个内部api并解析html中的标签:

GET https://github.com/{owner}/{repo}/branch_commits/{commit_sha}

示例:https://github.com/mui-org/material-ui/branch_commits/28b768913b75752ecf9b6bb32766e27c241dbc46

这是一个 脚本,它使用此解决方法检索特定提交的所有标签:

import requests
from bs4 import BeautifulSoup

owner = "mui-org"
repo = "material-ui"
commit_sha = "28b768913b75752ecf9b6bb32766e27c241dbc46"

r = requests.get(f"https://github.com/{owner}/{repo}/branch_commits/{commit_sha}")
soup = BeautifulSoup(r.text, "html.parser")
ul_list = soup.findAll("ul")

if len(ul_list) > 1:
    tag_ul = soup.findAll("ul")[1]
    tags = [ t.text for t in tag_ul.findAll("li") if t.text != "…"]
    print(tags)

try this on repl.it

这也是超快的,例如下面的代码检索与本次提交相关的1255个标签(此时):

import requests
from bs4 import BeautifulSoup

owner = "aosp-mirror"
repo = "platform_manifest"
commit_sha = "d8bad7851a80605389de1611638c2717242db2b4"

r = requests.get(f"https://github.com/{owner}/{repo}/branch_commits/{commit_sha}")
soup = BeautifulSoup(r.text, "html.parser")
ul_list = soup.findAll("ul")

if len(ul_list) > 1:
    tag_ul = soup.findAll("ul")[1]
    tags = [ t.text for t in tag_ul.findAll("li") if t.text != "…"]
    print(tags)
    print(len(tags))

缺点

  • 这不适用于私有存储库,因为您需要为此进行 Web 身份验证
  • 使用官方API失败

【讨论】:

    【解决方案2】:

    好像github偷偷提供了这样一个API url如下:https://api.github.com/repos/{{user}}/{{repository}}/tags 我想它适用于任何参考(注释等)。

    【讨论】:

    • 我没有投反对票,但我理解。首先,这不是秘密,而是well documented。其次,这列出了所有标签; OP 只想列出包含特定提交的标签。
    猜你喜欢
    • 2020-08-06
    • 2017-03-10
    • 2021-02-11
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多