HEYBlog about Technology

京都の学生エンジニアのエンジニアブログ

日曜日とhubotとGithub

hubotを育てていた日曜日だった。

hubotとは、Github社が開発して、チャットbot開発・実行フレームワークのことです。 モダンな開発環境を構築している企業等々様々なところで利用されています。

HipChatやSlackやChatWorkとの連携も簡単にできる!

日曜日にやっていたことは、

hubot pullreq <repository> base <base_branch> compare <head_branch> title <title>

ってhubotに言えば、Github上でpullreqを作ってくれるやつを作った。

それを実現した流れを以下に書きますー。

まずはGithubAPIのtokenを取りましょう

Macのローカル環境下で試してみたよ。

curl -i https://api.github.com/authorizations -d '{"note":"hubot_local_test","scopes":["repo"]}' -u "yourgithubaccount"

これで

{
  "id": xxxxxx,
  "url": "https://api.github.com/authorizations/cccccc",
  "app": {
    "name": "hubot local (API)",
    "url": "https://developer.github.com/v3/oauth_authorizations/",
    "client_id": "00000000000000000000"
  },
  "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "note": "hubot local",
  "note_url": null,
  "created_at": "2014-06-28T11:06:43Z",
  "updated_at": "2014-06-28T11:06:43Z",
  "scopes": [
    "repo"
  ]
}

が返ってくるから、これのtokenをコピーして

export HUBOT_GITHUB_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

する。

hubotの導入等々はこちらで。

http://qiita.com/kmdsbng/items/fdc069048b5f0d07295e

scripts/github.coffee

# Description:
#   Githubに関連するhubotスクリプト
#
# Commands:
#   hubot pullreq <repository> base <base_branch> compare <head_branch> title <title>

module.exports = (robot) ->

  github = require('githubot')
  url_api_base = "https://api.github.com"

  robot.respond /pullreq\s+for\s+(.*)\s+base\s+(.*)\s+compare\s+(.*)\s+title\s+(.*)/i, (msg)->
    repo = github.qualified_repo msg.match[1]
    base_branch = msg.match[2]
    head_branch = msg.match[3]
    title = msg.match[4]
    data = { title: title, head: head_branch, base: base_branch }
    url = "#{url_api_base}/repos/#{repo}/pulls"
    github.post url, data, (pullreq) ->
      msg.send pullreq.html_url

ちょっとだけ解説を入れますと

 repo = github.qualified_repo msg.match[1]

このように書くと

msg.matchに入っているのが

testとかだと、kohey18/testに変換してくれる。

あとはGithubAPI通りですー!

ちょっとテンションあがって

hubot show <assignee> issues for <repository>

ってコマンドでそのレポジトリに対応する自分のassignされたIssueが出るのも作ってみました。 またどこかで公開します。

https://developer.github.com/

  • 書き方はこの辺参照

https://github.com/github/hubot-scripts/blob/master/src/scripts/github-issues.coffee

  • githubotのpackage

https://github.com/iangreenleaf/githubot

今週も頑張りましょう!