如何从GitHub API获取提交的所有标记 [英] How to get all tags of a commit from the GitHub API

查看:9
本文介绍了如何从GitHub API获取提交的所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

在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身份验证
  • 官方接口使用失败

这篇关于如何从GitHub API获取提交的所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆