如何通过GitHubAPI获取项目徽章?

How to get project badge via GitHub API?

我为自己建立了一个简历页面,并使用 GitHub API 列出了我所有的项目。有些项目是带有 rtfd build passing 徽章的文档,有些是 python 带有 travis-ci 和 pep-lint 徽章的项目。

现在,我想像项目一样显示徽章,我应该如何使用API?

我的页面在这里:http://gh.windrunner.info/resume/#/github

您也可以使用不同的 API 和 https://github-shields.com/

参见“How to embed live Github PR status in your blogs & docs

Consider the PR https://github.com/cloudfoundry/bosh/pull/715.
The URL doesn't indicate if the PR is open/merged/closed.

The cloudfoundry/bosh/pull/715 portion of the URL is copied directly into the following base URL:

https://github-shields.com/github/ + cloudfoundry/bosh/pull/715 + .svg gives a URL that redirects to the PR.

https://github-shields.com/github/cloudfoundry/bosh/pull/715.svg

As an image URL it gives cloudfoundry/bosh/pull/715

Awesome, it was merged!


对于项目的状态,OP kxxoling reports having found shields.io:

https://img.shields.io/badge/<SUBJECT>-<STATUS>-<COLOR>.svg

it indicates how to get the status of a badge.
If there none badge added for that project, it will return a inaccessible badge like this: https://img.shields.io/travis/kxxoling/z42-doc.svg =>

对于像 https://github.com/kxxoling/z42-doc 这样的项目(其中确实有徽章),您需要获取自述文件,然后在其中搜索可能的徽章。在不知道您喜欢使用哪种语言的情况下,我将写一些 pseudo-code

首先,您需要检索 GitHub 识别为要在您的 home-page 上呈现的自述文件。你可以通过

 GET /repos/kxxoling/z42-doc/readme
 Host: https://api.github.com
 Accept: application/vnd.github.v3.raw

如果您更愿意解析 HTML,请将最后 header 中的 "raw" 更改为 "html",例如,

 GET /repos/kxxoling/z42-doc/readme
 Host: https://api.github.com
 Accept: application/vnd.github.v3.html

有了 README 的内容,现在您只需要解析它以获得特定于您为 README 选择的 mark-up 语言的链接或指令。您可以使用正则表达式或您选择的 HTML/XML 解析库来解析它们(如果您从 GitHub 检索呈现的内容)。