---
lang:ja-jp
###### tags: `Program` `open`
---
# GitHub REST API まとめ
[TOC]
## 注意
一部の項目ではHeaderにTokenが必要になる。これに関して、古いdocumentでは誤った情報が記載されているので注意。
```javascript=
const header_auth={"Authorization":`token ${envs.GitHubToken}`,
"Accept":"application/vnd.github.v3+json"};
```
また、ファイル内容はatob/btoaによるdecode/encodeが必要(base64)。
## Repo List取得
```javascript=
// obtain repo
GitHubName="ProjectIgnis"
GitHubRepo="BabelCDB"
header_auth={"Accept":"application/vnd.github.v3+json"};
git_url=`https://api.github.com/repos/${GitHubName}/${GitHubRepo}`;
res_get=await fetch(git_url, { method:"GET", headers:header_auth}).then(d=>d.json());
```
## ファイル情報取得・DL
ファイル内容はbase64でencodeされているので、`atob()`でdecodeする。
```javascript=
const git_url=gitFile.git_url;
const res = await fetch(git_url).then(d=>d.json());
const data = atob(res.content);
```
大容量ファイルの場合、git/blobs/を使う。
```javascript=
const repoInfo=defaultRepoInfo.MyRepo;
const header_auth = { "Accept": "application/vnd.github.v3+json" };
const git_content_url = `https://api.github.com/repos/${repoInfo.user}/${repoInfo.repo}/contents/data`;
const res_content = await fetch(git_content_url, { method: "GET", headers: header_auth }).then(d=>d.json());
const sha=res_content.filter(d=>d.path===repoInfo.path)[0].sha
const git_data_url = `https://api.github.com/repos/${repoInfo.user}/${repoInfo.repo}/git/blobs/${sha}`;
const content = await fetch(git_data_url, { method: "GET", headers: header_auth }).then(d=>d.json())
.then(res=>atob(res.content));
```
## 検索
検索文字列`q`はurlの後ろにつなげる。
```javascript=
// search
GitHubName="ProjectIgnis"
GitHubRepo="BabelCDB"
header_auth={"Accept":"application/vnd.github.v3+json"};
q="filename:*.cdb -filename:*-rush*.cdb -filename:*-skills*.cdb -filename:*unofficial*.cdb -filename:*goat*.cdb"
git_search_url=`https://api.github.com/search/code`;
search_query=`q=${q}+repo:${GitHubName}/${GitHubRepo}`;
res_get=await fetch(git_search_url+"?"+search_query, { method:"GET", headers: header_auth}).then(d=>d.json());
const searchedFiles=res_get.items;
```
## ファイル内容更新・作成
ファイルの更新にはTokenに加えて、shaと呼ばれる符号が必要になる。
これは[ファイル情報取得](https://hackmd.io/n0g1imhOTXqBKr3xzktVWw#%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E6%83%85%E5%A0%B1%E5%8F%96%E5%BE%97%E3%83%BBDL)で得られる。
また、ファイル内容はbase64でencodeが必要->`btoa()`
```javascript=
//check file
const header_auth={"Authorization":`token ${envs.GitHubToken}`, "Accept":"application/vnd.github.v3+json"};
const git_url=`https://api.github.com/repos/${envs.GitHubName}/${envs.GitHubRepo}/contents/Backup/${note_title}_${note_id}.md`;
const res_get=await fetch(git_url, { method:"GET", headers:header_auth}).then(d=>d.json());
let sha=null;
if (/200|302/.test(res_get["status"])){
console.log("connection error");
return;
} else if (res_get["sha"]) sha = res_get["sha"];
// create or update file
let put_body={message:"auto backup", content:btoa(note_md)};
const put_header={...header_auth ,"Content-Type": "application/json"};
if (sha) put_body={...put_body, "sha":sha}
const res_put=await fetch(git_url, { method:"PUT", body:JSON.stringify(put_body), headers:put_header})
.then(d=>d.json());
if (res_put.status!=200) console.log("can't push");
```