LoginSignup
6
8

More than 5 years have passed since last update.

gitのリモートリポジトリにpushされたら通知する

Last updated at Posted at 2015-09-02

きっかけ

開発環境が

  • GitHub、Slack、Chatworkなどの外部サービス使えない
  • リポジトリの流れはローカル(個人毎)→リモート(個人毎)→共有

なところでリモート(個人毎)へのpushを簡単にチェックしたかった。

やったこと

githooks

["日時" "ブランチ名" "リポジトリ名"]の形式で出力させる
例)2015/09/02 12:34 feature/XXX hoge.git
下のスクリプトへのリンクを.git/hooksに配置

post-update
#!/bin/bash

# push されたブランチ名が BRANCH に入る
BRANCH=$(git rev-parse --symbolic --abbrev-ref $1)

# 誰のリポジトリにpushされたか
DIR=$(basename `pwd`)

# push時の日付
NTIME=$(date "+%Y/%m/%d %H:%M")
case "$BRANCH" in
  "develop" )
      #develop用の処理
      echo "dev"
      ;;
  release* | hotfix*)
      #release, hotfix用の処理
      echo "release"
      ;;
  "master")
      #master用の処理
      echo "master"
      ;;
  feature* )
      #feature用の処理
      echo "feature"
      echo $NTIME $BRANCH $DIR  >> "log_dir"/push_history
      # 最新30件分取得
      # 出力先は後述のブラウザで表示のためhttpサーバの公開用ディレクトリに出力
      tail "log_dir"/push_history -n 30  > "log_dir"/push_history.log
      ;;
esac

ブラウザ上に表示

<div
Related Push <span id="ptime"></span><br>
<div id="pushlog" ></div>
</div>
var afterPush;
var beforePush;
setInterval("updatePushLog()", "適当な間隔で");
$(function() {
    checkPermission();
    displayPushLog();
});

# push履歴の表示
function displayPushLog() {
    var now = new Date();
    var str = now.getHours()+"";
    str+= now.getMinutes()+"";
    str+= now.getSeconds()+"";
    $('#ptime').text(str);
    $.get("data/push_history.log", function(data) {
      $('#pushlog').html(data.split('\n').reverse().join('<br>'));
    });
    afterPush = data;
}

# 前回のログと比較して変更がアレば通知
function updatePushLog() {
    displayPushLog();
    if (beforePush === undefined) {
        beforePush = afterPush;
    }
    if (afterPush !== beforePush) {
        var lastCount = afterPush.match(/\n/g);
        notify.createNotification("Pushされました", {
            body: (afterPush.split('\n').reverse())[1],
            icon: "alert.ico"
        });
        beforePush = afterPush;
    }
}

# 通知設定確認
function checkPermission() {
    if (!notify.isSupported) {} else {
        switch (notify.permissionLevel()) {
            case notify.PERMISSION_GRANTED:
                break;
            case notify.PERMISSION_DEFAULT:
                notify.requestPermission();
                break;
            case notify.PERMISSION_DENIED:
                alert("デスクトップ通知が許可されていません。ブラウザの設定を確認して下さい。");
                break;
        }
    }
}

まとめ

ほんとこじんまりでならありな手段かも知れない。
でもGitBucket等のクローン系を入れてしまった方が結果として楽だと思う。
参考:コミットしたらGitBucketとJenkinsとHipChatに任せて他の事やってましょう(1)

まあ今回はgithooksのお勉強ということで。

6
8
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
6
8