あいつの日誌β

働きながら旅しています。

Github API で自分が作業した内容をとりだすスクリプト

作業内容の報告書を書く時に URL が必要になったので書いた。

const axios = require('axios')
const qs = require('qs')
const moment = require('moment')

const BASE_URL = 'https://api.github.com/'
const TOKEN = 'YOUR_TOKEN'

async function getCommits(owner, repo, queryParams) {
  
  const q = qs.stringify(queryParams)

  return axios.create({
    baseURL: BASE_URL,
    timeout: 3000,
    headers: {
      'Authorization': `token ${TOKEN}`,
    },
  }).get(`/repos/${owner}/${repo}/commits?${q}`).catch(console.error)
}

async function main() {
  const res = await getCommits('okamuuu', 'practice-rails-app-docker', {
    author: 'okamuuu',
    since: moment().add(-90, 'days').format('YYYY-MM-DDTHH:MM:SSZ'),
    until: moment().format('YYYY-MM-DDTHH:MM:SSZ')
  });

  const commitObjects = res.data

  const commitsOf = {}
  commitObjects.forEach(obj => {
    const date = moment(obj.commit.author.date).format('YYYY-MM-DD');
    if (commitsOf[date] === undefined ) {
      commitsOf[date] = []
    }
    commitsOf[date].push(obj.html_url);
    // console.log(obj.commit.author.date)
    // console.log(obj.commit.url)
  })

  for (let date in commitsOf) {
    console.log(date)
    const commits = commitsOf[date]
    console.log(commits.join("\n"))
  }
}

main();

access_token の取得方法は Creating a personal access token - GitHub Docs を参照。

実行結果。

2018-08-21
https://github.com/okamuuu/practice-rails-app-docker/commit/f8eeac0d87e31e20bbfbca6d3b0c6e18c6225d62
https://github.com/okamuuu/practice-rails-app-docker/commit/2f3fe1f253962842cd010988714e1c98c007b5f3
https://github.com/okamuuu/practice-rails-app-docker/commit/c95d453762be8dc2aa95cab3af2ab4ca517f0a29
https://github.com/okamuuu/practice-rails-app-docker/commit/352e50785ae218f555d91cbe3d72eabed474faf4
https://github.com/okamuuu/practice-rails-app-docker/commit/69adab4e9c1b547a1a1a9c73764fe66c0ccf3671
https://github.com/okamuuu/practice-rails-app-docker/commit/111021a00eed75ca0b7beec0c249275c767d273c
https://github.com/okamuuu/practice-rails-app-docker/commit/397974b760a87cca2f4faeeb7d19fe70a8ec1d8f
https://github.com/okamuuu/practice-rails-app-docker/commit/1e45ad2fe316336e22a026c6bedde8db674d593a
https://github.com/okamuuu/practice-rails-app-docker/commit/6e6fb4a943e7074db71dbbec5398f7bc4c2c1f73
https://github.com/okamuuu/practice-rails-app-docker/commit/c4e7ca1f2618bf77743cbb780e3e8b632d7fd789
https://github.com/okamuuu/practice-rails-app-docker/commit/64bd5a4200eb4a46bd3ca08e641e49249b604466
https://github.com/okamuuu/practice-rails-app-docker/commit/79a497d98a4771e602abac7f5d0b4a810f95b354
https://github.com/okamuuu/practice-rails-app-docker/commit/cab4949876176986108a909da0fe8eecc6bc3fce
2018-08-20
https://github.com/okamuuu/practice-rails-app-docker/commit/4c5659e87401a12a010478747fb973772460fa1d
https://github.com/okamuuu/practice-rails-app-docker/commit/3ca6b776830ba71f4b981b40a10c9c47d62dac57
https://github.com/okamuuu/practice-rails-app-docker/commit/4d2e4cca9a358252d309dea41d19594dcdb76e19
https://github.com/okamuuu/practice-rails-app-docker/commit/ae721effdb9d81943e9b002d9db89a522de0e51e
https://github.com/okamuuu/practice-rails-app-docker/commit/960e652dd1af962543ef07e13ec932c9a0fd1893
https://github.com/okamuuu/practice-rails-app-docker/commit/b4ea70149edae79ae5edf49bb76269d6d3dbd9c0
https://github.com/okamuuu/practice-rails-app-docker/commit/6a2c20a69655bf4e3e87666d11655d75e5e9df96
https://github.com/okamuuu/practice-rails-app-docker/commit/0976b5773c743a301f1fc249aec87d9fc0c90ddc
https://github.com/okamuuu/practice-rails-app-docker/commit/96e3678374f3fd2a7f2689683abeb8440378ca99
https://github.com/okamuuu/practice-rails-app-docker/commit/6a72b60c7ab0bfeb34bd84fdf10e57e432af5a48
https://github.com/okamuuu/practice-rails-app-docker/commit/ea4d1f2547fe0e5932254cc9c64942fb8a7b0c0a
https://github.com/okamuuu/practice-rails-app-docker/commit/d3ffe90738e7219e294736ecc82cf63623022d6b

表示する内容は以下を参考にして各自で調整するとよいでしょう。

developer.github.com