GtiHubのAPIについてまとめています。
目次
GitHubのAPI(REST API)でウェブからgit操作が可能

GitHubには、git機能のREST APIが用意されています。ウェブアプリ等から、GitHubへのgit操作が可能です。
シンプルにGitHubのAPIを呼び出してみましょう。下記は、Linux(ubuntu)で実行した例です。ユーザoctocatの組織情報を取得します。curlコマンドに-Iオプションを指定しているため、ヘッダ情報も一緒に出力します。
関連)octocat (The Octocat) (ユーザoctocaのリポジトリ)
$ curl -I https://api.github.com/users/octocat/orgs HTTP/2 200 server: GitHub.com date: Fri, 25 Jun 2021 05:13:05 GMT content-type: application/json; charset=utf-8 content-length: 5 cache-control: public, max-age=60, s-maxage=60 vary: Accept, Accept-Encoding, Accept, X-Requested-With etag: "992aaf92b487506974e4305db4a64326866f3ed48b2ba424cef8fbe4e28f72b0" x-github-media-type: github.v3; format=json access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset access-control-allow-origin: * strict-transport-security: max-age=31536000; includeSubdomains; preload x-frame-options: deny x-content-type-options: nosniff x-xss-protection: 0 referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin content-security-policy: default-src 'none' x-ratelimit-limit: 60 x-ratelimit-remaining: 59 x-ratelimit-reset: 1624601585 x-ratelimit-resource: core x-ratelimit-used: 1 accept-ranges: bytes x-github-request-id: AC92:23F8:427FB7:4B5724:60D565E1
https://api.github.com/users/ユーザ名/orgsにアクセスすることで、指定したユーザの組織情報が取得できるんですね。ヘッダ情報によると、「content-type: application/json; charset=utf-8」とあり、utf-8文字コードで、データの形式はjsonです。
参考)GitHubのREST API – GitHub Docs
ユーザ認証つきで、GitHubのAPIにアクセスする
publicリポジトリにはパスワードなしでアクセス可能ですが、プライベートリポジトリへのアクセスにはユーザ認証が必要です。curlに-uオプションを指定すると、接続時にユーザ名とパスワードを指定可能です。
$ curl -i -u your_username https://api.github.com/users/octocat Enter host password for user 'your_username':
しかし、アクセスするたびにパスワード入力するのは面倒ですね。そこで、個人アクセストークンを使って以下のようにアクセスすれば、パスワード入力は不要です。
$ curl -i -u username:$token https://api.github.com/users/octocat
GitHub公式サイトによれば、トークンはパスワードと同じ扱いのため、直接記述するのは避け、$tokenなどの環境変数に入れて使用することが推奨されています。
個人アクセストークンの取得方法は、こちらの記事を参照してください。
【関連記事】
▶GitHubのログイン時にパスワード入力を省略する httpsとssh接続を解説
GitHubのAPIでリポジトリ情報を取得
REST APIで特定ユーザのリポジトリ情報を取得するには以下のように指定します。ユーザ名googleのリポジトリを取得しています。
$ curl https://api.github.com/users/google/repos
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 4348 0 4348 0 [
{
0 "id": 170908616,
7722 "node_id": "MDEwOlJlcG9zaXRvcnkxNzA5MDg2MTY=",
"name": ".github",
0 "full_name": "google/.github",
--: "private": false,
-- "owner": {
:-- "login": "google",
-- "id": 1342004,
:--:-- "node_id": "MDEyOk9yZ2FuaXphdGlvbjEzNDIwMDQ=",
--:--:- "avatar_url": "https://avatars.githubusercontent.com/u/1342004?v=4",
- "gravatar_id": "",
7709 "url": "https://api.github.com/users/google",
"html_url": "https://github.com/google",
"followers_url": "https://api.github.com/users/google/followers",
"following_url": "https://api.github.com/users/google/following{/other_
user}",
"gists_url": "https://api.github.com/users/google/gists{/gist_id}",
"starred_url": "https://api.github.com/users/google/starred{/owner}{/re
po}",
"subscriptions_url": "https://api.github.com/users/google/subscriptions
",
"organizations_url": "https://api.github.com/users/google/orgs",
"repos_url": "https://api.github.com/users/google/repos",
"events_url": "https://api.github.com/users/google/events{/privacy}",
"received_events_url": "https://api.github.com/users/google/received_ev
ents",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/google/.github",
:
:
特定のリポジトリのissuesを取得するには以下のようにします。リポジトリgoogle-research/google-researchのissuesを取得しています。
APIの最初の方には、リポジトリに関する情報が出力されており、続いてissuesが出力されています。
$ curl https://api.github.com/repos/google-research/google-research/issues
:
(リポジトリに関する情報)
:
"id": 929181867,
"node_id": "MDU6SXNzdWU5MjkxODE4Njc=",
"number": 743,
"title": "UFlow Training on custom data set failed with error : train_ds
= train_datasets[0] IndexError: list index out of range ",
"user": {
"login": "infi2021",
"id": 86414621,
"node_id": "MDQ6VXNlcjg2NDE0NjIx",
"avatar_url": "https://avatars.githubusercontent.com/u/86414621?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/infi2021",
"html_url": "https://github.com/infi2021",
"followers_url": "https://api.github.com/users/infi2021/followers",
"following_url": "https://api.github.com/users/infi2021/following{/othe
r_user}",
"gists_url": "https://api.github.com/users/infi2021/gists{/gist_id}",
"starred_url": "https://api.github.com/users/infi2021/starred{/owner}{/
repo}",
"subscriptions_url": "https://api.github.com/users/infi2021/subscriptio
ns",
"organizations_url": "https://api.github.com/users/infi2021/orgs",
"repos_url": "https://api.github.com/users/infi2021/repos",
"events_url": "https://api.github.com/users/infi2021/events{/privacy}",
"received_events_url": "https://api.github.com/users/infi2021/received_
events",
"type": "User",
"site_admin": false
},
:
:
同様に、特定のリポジトリのプルリクエストのうち、ステータスがopenのもののみ取得するには、以下のようにします。
リポジトリgoogle-research/google-researchのプルリクエストのうち、state=openの情報が出力されます。
$ curl https://api.github.com/repos/google-research/google-research/pulls?state=open
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0[
{
"url": "https://api.github.com/repos/google-research/google-research/pull
s/741",
"id": 675732821,
"node_id": "MDExOlB1bGxSZXF1ZXN0Njc1NzMyODIx",
"html_url": "https://github.com/google-research/google-research/pull/741"
,
"diff_url": "https://github.com/google-research/google-research/pull/741.
diff",
"patch_url": "https://github.com/google-research/google-research/pull/741
.patch",
"issue_url": "https://api.github.com/repos/google-research/google-researc
h/issues/741",
"number": 741,
"state": "open",
"locked": false,
"title": "Update pendulum_collect_data.ipynb",
"user": {
"login": "katelynnashley95icloudcom",
"id": 85616681,
"node_id": "MDQ6VXNlcjg1NjE2Njgx",
"avatar_url": "https://avatars.githubusercontent.com/u/85616681?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/katelynnashley95icloudcom",
"html_url": "https://github.com/katelynnashley95icloudcom",
"followers_url": "https://api.github.com/users/katelynnashley95icloudco
m/followers",
:
:
GitHub APIの使用例

GitHubのAPIを使ったサイトで有名なのは、github1s.comです。
vscodeのオープンソース版(code-oss)と、GitHub REST APIを組み合わせて作られており、ブラウザ上でvscodeを使ってGitHub上のコードを読みやすくするサービスです。
参考)github1s、恐ろしい子。githubのコードをVS Codeで開く、1秒で。 – Qiita
【関連記事】
▶GitHubをvscodeと連携 デフォルトエディタへの設定と拡張機能
GitHub APIのまとめ


- GitHubのREST APIは、git操作をウェブ経由で実行できる
- GitHubのREST APIは、json形式、utf-8文字コードで出力される
- github1s.comなどGitHub REST APIを使って作られたウェブサービスあり