Skip to content

Instantly share code, notes, and snippets.

@naosim
Created July 16, 2019 22:46
Show Gist options
  • Save naosim/2144551711a66de2ac6563d1d9b4f097 to your computer and use it in GitHub Desktop.
Save naosim/2144551711a66de2ac6563d1d9b4f097 to your computer and use it in GitHub Desktop.
【GAS】GithubApiライブラリ
function GithubApi(
accessToken,
owner,
repo
) {
var requestOptions = {
headers: { Authorization: 'token ' + accessToken }
}
function exec(url) {
if(url.indexOf('http') != 0) {
url = 'https://api.github.com' + url;
}
url = url.split(':owner').join(owner).split(':repo').join(repo);
var content = UrlFetchApp.fetch(url, requestOptions).getContentText("UTF-8");
Logger.log(JSON.stringify(JSON.parse(content), null, ' '))
return JSON.parse(content);
}
function trees(treeSha) {
var url = '/repos/:owner/:repo/git/trees/:tree_sha';
return exec(url.split(':tree_sha').join(treeSha));
}
function getBlobContent(url) {
var c = exec(url)['content'];
return Utilities.newBlob(Utilities.base64Decode(c)).getDataAsString()
}
return {
exec: exec,
trees: trees,
getBlobContent: getBlobContent,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment