Hubot | 自作Script | gitlab_ci_sample.coffee - GitLab CIとの連携を検証するサンプル

# Hubot | 自作Script | gitlab_ci_sample.coffee - GitLab CIとの連携を検証するサンプル

## 概要
GitLab CI との 連携を検証するサンプル script を作成します。
GitLab CI の Web hooks と、 Hubot の Web サーバー機能を利用します。

## 仕様
### filename
gitlab_ci_sample.coffee

### Hubot gitlab 検証用URL
* http://hubot_url:8080/gitlab_ci/hook

### 仕様補足
GitLab CI の Web Hook が返却してくる json のフォーマットについては下記参照

~~~json
  {
      "build_id": 2,
      "build_status": "failed",
      "build_started_at": "2014-05-05T18:01:02.563Z",
      "build_finished_at": "2014-05-05T18:01:07.611Z",
      "project_id": 1,
      "project_name": "Brightbox \/ Brightbox Cli",
      "gitlab_url": "http:\/\/localhost:3000\/brightbox\/brightbox-cli",
      "ref": "master",
      "sha": "a26cf5de9ed9827746d4970872376b10d9325f40",
      "before_sha": "34f57f6ba3ed0c21c5e361bbb041c3591411176c",
      "push_data": {
        "before": "34f57f6ba3ed0c21c5e361bbb041c3591411176c",
        "after": "a26cf5de9ed9827746d4970872376b10d9325f40",
        "ref": "refs\/heads\/master",
        "user_id": 1,
        "user_name": "Administrator",
        "project_id": 5,
        "repository": {
          "name": "Brightbox Cli",
          "url": "dzaporozhets@localhost:brightbox\/brightbox-cli.git",
          "description": "Voluptatibus quae error consectetur voluptas dolores vel excepturi possimus.",
          "homepage": "http:\/\/localhost:3000\/brightbox\/brightbox-cli"
        },
        "commits": [
          {
            "id": "a26cf5de9ed9827746d4970872376b10d9325f40",
            "message": "Release v1.2.2",
            "timestamp": "2014-04-22T16:46:42+03:00",
            "url": "http:\/\/localhost:3000\/brightbox\/brightbox-cli\/commit\/a26cf5de9ed9827746d4970872376b10d9325f40",
            "author": {
              "name": "Paul Thornthwaite",
              "email": "tokengeek@gmail.com"
            }
          },
          {
            "id": "34f57f6ba3ed0c21c5e361bbb041c3591411176c",
            "message": "Fix server user data update\n\nIncorrect condition was being used so Base64 encoding option was having\nopposite effect from desired.",
            "timestamp": "2014-04-11T18:17:26+03:00",
            "url": "http:\/\/localhost:3000\/brightbox\/brightbox-cli\/commit\/34f57f6ba3ed0c21c5e361bbb041c3591411176c",
            "author": {
              "name": "Paul Thornthwaite",
              "email": "tokengeek@gmail.com"
            }
          }
        ],
        "total_commits_count": 2,
      }
    }
~~~


## ソースコード

coffee:gitlab_ci_sample.coffee

~~~coffeescript
module.exports = (robot) ->
  say = (message) ->
    user = {
      room :
        id : 2
    }

    robot.send user, message

  robot.router.post "/gitlab_ci/hook", (req, res) ->
    json  = req.body
    say "GitLab CI issue"
    say "project_name = #{json['project_name']}"
    status = json['build_status']
    icon = status == 'failed' ? ':skull:' : ':angel:'
    say "build_status = #{icon} #{status}"
    say "branch = #{json['ref']}"
    say json['gitlab_url']

    res.send 200
~~~

## 前提
* Kandan 1.1
* Hubot 2.4.7
* GitLab 7.0
* GitLab CI 5.0.1
* GitLab CI Runner 5.0.1

※GitLab - GitLab CI - GitLab CI Runner は連携済み

* 対象リポジトリは自作 gem の gottani を GitLabに保存したもの

https://github.com/tbpgr/gottani

これをそのまま GitLab に放り込んでます。

## 手順
### gitlab_ci_sample.coffee を配置

gitlab_ci_sample.coffee を hubot サーバーの scripts 配下に保存する。

### Hubot の再起動
### ブラウザで GitLab CI の Web hooks を追加する

URL は http://hubot_path:8080/gitlab_ci/hook に設定。
gitlab/hook の部分は gitlab_ci_sample.coffee の

~~~coffeescript
robot.router.post "/gitlab_ci/hook", (req, res) ->
~~~

の部分に対応。

![hubot_gitlab_ci1.png](https://qiita-image-store.s3.amazonaws.com/0/43480/dda631d0-aef8-7995-90d4-dd1e99d3658d.png)

## 確認
* GitLab へ push します

~~~bash
$ git commit -m "test for gitlab-gitlab ci-kandan-hubot test"
$ git push origin master
~~~

* GitLab CI のテスト開始を確認

![hubot_gitlab_ci2.png](https://qiita-image-store.s3.amazonaws.com/0/43480/3e5d1a47-c2c5-faa4-40b4-36674f134821.png)

* GitLab CI のテスト終了を確認

![hubot_gitlab_ci3.png](https://qiita-image-store.s3.amazonaws.com/0/43480/3abc97c8-cad1-4628-c696-60c54236630d.png)

* Kandan で Hubot の反応を確認します(成功時)

![hubot_gitlab_ci4.png](https://qiita-image-store.s3.amazonaws.com/0/43480/c1b6625c-020f-4b08-7eb5-d45e38c9c7e2.png)

* Kandan で Hubot の反応を確認します(失敗時)

![hubot_gitlab_ci5.png](https://qiita-image-store.s3.amazonaws.com/0/43480/c856dfd5-ed7f-92c9-71e1-eea1b475eba9.png)

## 補足
今のところ、 npm で公開したりする予定はないので適当にGitHubに突っ込んでおきます。
https://github.com/tbpgr/hubot_scripts