3
3

More than 5 years have passed since last update.

hubot-github-identifyを使ってみた

Posted at

hubotでgithub上のユーザやトークンを上手く取り扱うためのライブラリ、hubot-github-identifyの紹介です。

前提条件

以下が必要です。

  • Redis(永続ストアに使用)
  • hubotがデプロイされているURL

hubot-github-identifyとは

以下の機能を提供します。

  • チャット上のユーザ名とgithubアカウントを紐付ける機能
  • github上のアカウント名に対して、APIトークンを保持・管理する機能

例えば、hubot上からgithubにアクセスするときにhubot自身に登録されたトークンを使うのではなく、ユーザ固有のトークンを使うことができます。

また、チャット上のmitsukuni というアカウントがgithub上のkeyというアカウントである場合、githubにアクセスする際はmitsukuniからkeyへの変換も行うことが出来ます。

hubot-github-identifyはnpmで提供されているので、hubotのpackage.jsonに追記するだけで使いはじめることが出来ます。

使い方

環境変数の設定

環境変数にHUBOT_HOSTNAMEとRedisのアクセス先を登録します。
Redisは生のRedisのほかRedis To GoやRedis CloudのURLを指定することが出来ます。

export HUBOT_HOSTNAME=http://myawesomehubot.example.org:8888
export REDIS_URL=redis://localhost:6379

Githubユーザ名を登録する

hubotのいるチャットルームでhubot i am <github username>と話しかけます。hubotが知らないユーザだと次のように表示されてトークンの登録を促されます。Githubでトークンを取得して、hubotから提示されたURLで登録します。

mitsukuni > hubot i am tombell
Hubot > mitsukuni: Sorry, I don't know of key, maybe you need to register your GitHub username and API token with me at http://myawesomehubot.example.org:8888/github/identity

Githubユーザ名を忘れてもらう

Githubのアカウントを削除するにはhubot forget meと発言すると忘れられます。

mitsukuni > hubot forget me
Hubot > mitsukuni: Ok, I have no idea who you are anymore.

Githubユーザ名とトークンを忘れてもらう

mitsukuni > hubot forget me
Hubot > mitsukuni: Ok, I have no idea who you are anymore, or who you are on GitHub

hubotのスクリプトでユーザ毎のトークンを取得する

robotオブジェクトにidentityオブジェクトが追加されています。findTokenメソッドにチャットのユーザ名を渡すと、登録済みのGithubのトークンを取得することが出来ます。

module.exports = (robot) ->

  robot.respond /make some github api request for me/i, (res) ->
    # チャットのユーザ名
    username = res.envelope.user.name

    # Githubトークンを検索
    robot.identity.findToken username, (err, token) ->
      # エラーハンドル
      if err
        switch err.type
          when 'redis'
            res.reply "Oops: #{err}"
          when 'github user'
            res.reply "Sorry, you haven't told me your GitHub username."
      else
        # githubotオブジェクトを任意のトークンで初期化
        github = require("githubot")(robot, token: token)
        github.get "" ...
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3