Skip to content

Instantly share code, notes, and snippets.

@khsk
Last active October 8, 2015 04:37
Show Gist options
  • Save khsk/2bad1eef92a2705c57f1 to your computer and use it in GitHub Desktop.
Save khsk/2bad1eef92a2705c57f1 to your computer and use it in GitHub Desktop.
Subversionのログをcronでポーリングしゆる~くコミット検知 ref: http://qiita.com/khsk/items/f559c798a8511cf118f4
root@a01f3dcd16fe:/# which sh
/bin/sh
root@a01f3dcd16fe:/# ll /bin/sh
lrwxrwxrwx 1 root root 4 Feb 19 2014 /bin/sh -> dash*
root@a01f3dcd16fe:/# service cron start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service cron start
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start cron
root@a01f3dcd16fe:/# which start
/sbin/start
root@a01f3dcd16fe:/# ll /sbin/start
lrwxrwxrwx 1 root root 7 Jul 18 2014 /sbin/start -> initctl*
root@a01f3dcd16fe:/# cat /sbin/initctl
#!/bin/sh
exit 0
#!/bin/bash
# 変数の置換が#!/bin/sh ではできないのでbashで実行
# 命名的代入 リポジトリはURLを チャンネル名には#をつけない
repository=$1
# 引数の場合はエスケープが必要になるのでここでつける
channel="#"$2
# logの件数はデフォルト10の第3引数で変動
limit=${3:-10}
# logファイルが重複しないようにリポジトリ名のハッシュ値をsuffixに
suffix=`echo -n "$repository" | md5sum`
# そのままのハッシュでは余計なものがついてエラーになるので、英数字のみにするため末尾を取り除く
suffix=${suffix%' -'}
echo $suffix
svn log "$repository" --limit $limit > /tmp/shared/new-"$suffix".log
if [ ! -f /tmp/shared/old-"$suffix".log ]; then
cat /tmp/shared/new-"$suffix".log > /tmp/shared/old-"$suffix".log
echo '比較ファイル無し'
exit
fi
#diff=`diff -u /tmp/shared/old-${suffix}.log /tmp/shared/new-${suffix}.log`
diff=`diff --new-line-format='%L' --old-line-format='' --unchanged-line-format='' /tmp/shared/old-${suffix}.log /tmp/shared/new-${suffix}.log`
if [ -z "$diff" ]; then
echo '変更無し'
# 変更無しならログも更新しない
exit
fi
# 改行とタブがあるとJSONエラー ただし改行は\nを入れるとslack側で行ってくれる
# 変数の置換はshでは動作しない http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1227556757
# bashで改行などの特殊記号を使う場合は$''でくくらなければならない http://oshiete.goo.ne.jp/qa/6733547.html
# 文字列 \n は''で囲まないとnにされる
diff="${diff//$'\n'/'\n'}"
diff="${diff//$'\t'/ }"
# どこのリポジトリのログかを表示する
diff="$repository"'\n'"$diff"
# slackに通知
curl -X POST --data-urlencode 'payload={"channel": "'$channel'", "username": "subversion", "text": "'"$diff"'", "icon_emoji": ":octocat:"}' https://hooks.slack.com/services/「slackトークン」
# 旧ログを更新
cat /tmp/shared/new-"$suffix".log > /tmp/shared/old-"$suffix".log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment