Closed7

GitHub Contents API

雪猫雪猫

ファイル取得 API

GET /repos/{owner}/{repo}/contents/{path}

ブランチを指定するときはクエリパラメーターに ref

https://api.github.com/repos/SnowCait/rest-api-description/contents/index.html?ref=swagger-ui

Response
{
  "name": "index.html",
  "path": "index.html",
  "sha": "c89370f97b2118122a41edb2e6ab5e22ea93d894",
  "size": 2855,
  "url": "https://api.github.com/repos/SnowCait/rest-api-description/contents/index.html?ref=swagger-ui",
  "html_url": "https://github.com/SnowCait/rest-api-description/blob/swagger-ui/index.html",
  "git_url": "https://api.github.com/repos/SnowCait/rest-api-description/git/blobs/c89370f97b2118122a41edb2e6ab5e22ea93d894",
  "download_url": "https://raw.githubusercontent.com/SnowCait/rest-api-description/swagger-ui/index.html",
  "type": "file",
  "content": "PCEtLSBIVE1MIGZvciBzdGF0aWMgZGlzdHJpYnV0aW9uIGJ1bmRsZSBidWls\nZCAtLT4KPCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KICA8aGVh\nZD4KICAgIDxtZXRhIGNoYXJzZXQ9IlVURi04Ij4KICAgIDx0aXRsZT5Td2Fn\nZ2VyIFVJPC90aXRsZT4KICAgIDxiYXNlIGhyZWY9Ii4vc3dhZ2dlci11aS8i\nPgogICAgPGxpbmsgcmVsPSJzdHlsZXNoZWV0IiB0eXBlPSJ0ZXh0L2NzcyIg\naHJlZj0iLi9zd2FnZ2VyLXVpLmNzcyIgPgogICAgPGxpbmsgcmVsPSJpY29u\nIiB0eXBlPSJpbWFnZS9wbmciIGhyZWY9Ii4vZmF2aWNvbi0zMngzMi5wbmci\nIHNpemVzPSIzMngzMiIgLz4KICAgIDxsaW5rIHJlbD0iaWNvbiIgdHlwZT0i\naW1hZ2UvcG5nIiBocmVmPSIuL2Zhdmljb24tMTZ4MTYucG5nIiBzaXplcz0i\nMTZ4MTYiIC8+CiAgICA8c3R5bGU+CiAgICAgIGh0bWwKICAgICAgewogICAg\nICAgIGJveC1zaXppbmc6IGJvcmRlci1ib3g7CiAgICAgICAgb3ZlcmZsb3c6\nIC1tb3otc2Nyb2xsYmFycy12ZXJ0aWNhbDsKICAgICAgICBvdmVyZmxvdy15\nOiBzY3JvbGw7CiAgICAgIH0KCiAgICAgICosCiAgICAgICo6YmVmb3JlLAog\nICAgICAqOmFmdGVyCiAgICAgIHsKICAgICAgICBib3gtc2l6aW5nOiBpbmhl\ncml0OwogICAgICB9CgogICAgICBib2R5CiAgICAgIHsKICAgICAgICBtYXJn\naW46MDsKICAgICAgICBiYWNrZ3JvdW5kOiAjZmFmYWZhOwogICAgICB9Cgog\nICAgICAuc3dhZ2dlci11aSAudG9wYmFyIC5kb3dubG9hZC11cmwtd3JhcHBl\nciBzZWxlY3QgewogICAgICAgIHdpZHRoOiAxMDAlOwogICAgICAgIG1hcmdp\nbjogMDsKICAgICAgICBib3JkZXI6IDJweCBzb2xpZCAjNjJhMDNmOwogICAg\nICAgIGJvcmRlci1yYWRpdXM6IDRweCAwIDAgNHB4OwogICAgICAgIG91dGxp\nbmU6IG5vbmU7CiAgICAgICAgbWluLXdpZHRoOiAxMDBweDsKICAgICAgICBw\nYWRkaW5nOiA4cHggMTBweDsKICAgICAgICBib3gtc2l6aW5nOiBib3JkZXIt\nYm94OwogICAgICAgIG92ZXJmbG93OiB2aXNpYmxlOwogICAgICAgIGZvbnQt\nZmFtaWx5OiBzYW5zLXNlcmlmOwogICAgICAgIGZvbnQtc2l6ZTogMTAwJTsK\nICAgICAgICBsaW5lLWhlaWdodDogMS4xNTsKICAgICAgfQogICAgPC9zdHls\nZT4KICA8L2hlYWQ+CgogIDxib2R5PgogICAgPGRpdiBpZD0ic3dhZ2dlci11\naSI+PC9kaXY+CgogICAgPHNjcmlwdCBzcmM9Ii4vc3dhZ2dlci11aS1idW5k\nbGUuanMiIGNoYXJzZXQ9IlVURi04Ij4gPC9zY3JpcHQ+CiAgICA8c2NyaXB0\nIHNyYz0iLi9zd2FnZ2VyLXVpLXN0YW5kYWxvbmUtcHJlc2V0LmpzI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Rz\nQnlDbGFzc05hbWUoJ2Rvd25sb2FkLXVybC1pbnB1dCcpWzBdCiAgICAgIGNv\nbnN0IHNlbGVjdCA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoJ3NlbGVjdCcp\nCiAgICAgIHNlbGVjdC5jbGFzc0xpc3QgPSBpbnB1dC5jbGFzc0xpc3QKICAg\nICAgdmVyc2lvbnMuZm9yRWFjaCAodmVyc2lvbiA9PiB7CiAgICAgICAgY29u\nc3QgdXJsID0gYCR7bG9jYXRpb24uaHJlZn1kZXNjcmlwdGlvbnMvJHt2ZXJz\naW9ufS8ke3ZlcnNpb259Lmpzb25gCiAgICAgICAgY29uc3Qgb3B0aW9uID0g\nZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnb3B0aW9uJykKICAgICAgICBvcHRp\nb24ubGFiZWwgPSB1cmwKICAgICAgICBvcHRpb24udmFsdWUgPSB1cmwKICAg\nICAgICBzZWxlY3QuYXBwZW5kQ2hpbGQob3B0aW9uKQogICAgICB9KQogICAg\nICBzZWxlY3QuYWRkRXZlbnRMaXN0ZW5lcignY2hhbmdlJywgKGV2ZW50KSA9\nPiB7CiAgICAgICAgY29uc3QgdXJsID0gZXZlbnQudGFyZ2V0LnZhbHVlCiAg\nICAgICAgdWkuc3BlY0FjdGlvbnMudXBkYXRlVXJsKHVybCkKICAgICAgICB1\naS5zcGVjQWN0aW9ucy5kb3dubG9hZCh1cmwpCiAgICAgIH0pCiAgICAgIGlu\ncHV0LnBhcmVudEVsZW1lbnQucmVwbGFjZUNoaWxkKHNlbGVjdCwgaW5wdXQp\nCiAgICAgIGRvY3VtZW50LmdldEVsZW1lbnRzQnlDbGFzc05hbWUoJ2Rvd25s\nb2FkLXVybC1idXR0b24nKVswXS5yZW1vdmUoKQogICAgfQogIDwvc2NyaXB0\nPgogIDwvYm9keT4KPC9odG1sPgo=\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/SnowCait/rest-api-description/contents/index.html?ref=swagger-ui",
    "git": "https://api.github.com/repos/SnowCait/rest-api-description/git/blobs/c89370f97b2118122a41edb2e6ab5e22ea93d894",
    "html": "https://github.com/SnowCait/rest-api-description/blob/swagger-ui/index.html"
  }
}

contentencoding でエンコードされたファイルの中身が入っている。

雪猫雪猫

base64 デコード

WindowOrWorkerGlobalScope.atob() - Web API | MDN

JavaScript
const url = 'https://api.github.com/repos/SnowCait/rest-api-description/contents/index.html?ref=swagger-ui'
fetch(url)
.then(response => response.json())
.then(json => {
    const file = atob(json.content)
    console.log(file)
})
Node.js (Probot)
const params = context.repo({
  path: 'index.html',
  ref: 'swagger-ui',
});
const response = await context.octokit.repos.getContent(params);
const file = Buffer.from(response.data.content, response.data.encoding).toString();
console.log(file)
雪猫雪猫

gist-it

HTML に埋め込む場合は gist-it.appspot.com というサービスがある。
Gist にある埋め込み機能の GitHub 版。

<script src="https://gist-it.appspot.com/github/SnowCait/rest-api-description/blob/swagger-ui/index.html"></script>

クエリパラメーターに slice を付けると範囲を絞れる。

<script src="https://gist-it.appspot.com/github/SnowCait/rest-api-description/blob/swagger-ui/index.html?slice=84:100"></script>

https://qiita.com/sikeda107/items/c35fb9327ab7f3f46cf5
https://yasushiito.hatenablog.com/entry/2019/04/26/073000

Markdown では使えなかった。

<script src="https://gist-it.appspot.com/github/SnowCait/rest-api-description/blob/swagger-ui/index.html?slice=84:100"></script>

雪猫雪猫

API は認証を通せるのでプライベートリポジトリの内容も取得できる。
他はパブリックリポジトリのみ。

このスクラップは2021/03/09にクローズされました