Slack Subversion Intergration 한글 코멘트 사용하기

slack Jan 17, 2017

Slack을 내가 투입되는 팀마다 사용한지가 꽤 되었다.

특히나 CI서버의 Job 상태나 형상관리 서버의 commit notification을 slack app기능으로 받는건 꽤나 유용하다.(빌드 깨진거 벌금 매길때 참 좋다 ㅎㅎ)

요샌 형상관리서버로 Git을 많이 사용한다지만 아직 SI쪽에서는 SVN이 주를 이루고 있다.

찾아보니 많은 예제들이 github, gitlab이나 bitbucket 설정하는 건 자세하게 도움말이 많은데 subversion 설정 예제는 엄청 간단하긴 하지만 한글 코멘트가 잘나오게 설정하려면 아주 약간의 수정이 필요하다.

Slack Subversion Intergration 설정

Slack에서의 설정은 매우 간단하다.

App Directory 메뉴에서 Subversion 을 선택하면 끝;;

Hook에 사용될 Token 값과 SVN post-commit handler 샘플만 가져다 사용하면 된다.

alt

Subversion hook 설정

Subversion Hook은 만들어둔 레파지토리에 이벤트가 발생 했을 경우 선/후처리를 위해 제공된다.

comment를 입력하지 않거나 이슈트래커 번호 없으면 commit이 불가능하게 하거나 하는 등의 작업을 추가하는데 많이들 사용하고, Slack Notification도 이와 같은 hook을 사용해서 slack에 notification이 될 수 있게 작업할 수 있다.

Hook 스크립트는 서버에서 실행이 될 수 있는 스크립트는 거의 다 지원된다. (php, perl, shell, 기타등등)

그리고 맨 위 그림에도 보이지만 감사하게도 샘플로 펄스크립트 예제를 같이 제공해 주고 있다.

다만 아쉬웠던건 여전히 영어권 이외의 국가들은 별 신경을 안써준다는거.. 한글이 들어간 comment들은 죄다 유니코드로 나온다.

alt

펄스크립트는 만들어 본적이 없지만 간단하게 서칭해보니 조금만 손보면 한글도 잘 나온다 :)

이제 세팅을 시작해보면~

서버에서 레파지토리를 생성하면 root directoryhook이라는 디렉토리가 존재한다.

기본적으로 아래와 같이 몇가지 템플릿도 제공한다.

$ ls -al /repository_path/hooks
post-commit.tmpl
post-lock.tmpl
post-revprop-change.tmpl
post-unlock.tmpl
pre-commit.tmpl
pre-lock.tmpl
pre-revprop-change.tmpl
pre-unlock.tmpl
start-commit.tmpl
pre-commit <-- 신규로 추가

commit 이 되면 slack에 notification이 가면 되므로 해당 hooks 디렉토리에 post-commit 파일을 펄스크립트 예제 파일로 만들면 된다. (확장자는 없이 만들면된다.)

#!/usr/bin/perl


# Copyright 2013 Tiny Speck, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#
# An SVN post-commit handler for posting to Slack. Setup the channel and get the token
# from your team's services page. Change the options below to reflect your team's settings.
#
# Requires these perl modules:
# HTTP::Request
# LWP::UserAgent
# JSON


# Submits the following post to the slack servers

# POST https: //foo.slack.com/services/hooks/subversion?token=xxxxxx
# Content-Type: application/x-www-form-urlencoded
# Host: foo.slack.com
# Content-Length: 101
#
# payload=%7B%22revision%22%3A1%2C%22url%22%3A%22http%3A%2F%2Fsvnserver%22%2C%22author%22%3A%22digiguru%22%2C%22log%22%3A%22Log%20info%22%7D

#
# I am not a perl programmer. Beware.
#

use warnings;
use strict;

use HTTP::Request::Common qw(POST);
use HTTP::Status qw(is_client_error);
use LWP::UserAgent;
use JSON;
# 한글 사용을 위해 추가
use Encode qw(decode_utf8);


#
# Customizable vars. Set these to the information for your team
#

my $opt_domain = "team.slack.com"; # Your team's domain
my $opt_token = "토큰값"; # The token from your SVN services page


#
# this script gets called by the SVN post-commit handler
# with these args:
#
# [0] path to repo
# [1] revision committed
#
# we need to find out what happened in that revision and then act on it
#

# 한글 사용을 위해 변경
my $log = qx|export LC_ALL="ko_KR.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;
$log = decode_utf8($log);
#my $log = `/usr/bin/svnlook log -r $ARGV[1] $ARGV[0]`;
my $files = `/usr/bin/svnlook changed -r $ARGV[1] $ARGV[0]`;
my $who = `/usr/bin/svnlook author -r $ARGV[1] $ARGV[0]`;
my $url = "http://svn.dev/repos/kcx/?op=revision&rev=$ARGV[1]"; # optionally set this to the url of your internal commit browser. Ex: http://svnserver/wsvn/main/?op=revision&rev=$ARGV[1]
chomp $who;

my $payload = {
	'revision'	=> $files.'['.$ARGV[1].']',
	'url'		=> $url,
	'author'	=> $who,
	'log'		=> $log,
};

my $ua = LWP::UserAgent->new;
$ua->timeout(15);

my $req = POST( "https://${opt_domain}/services/hooks/subversion?token=${opt_token}", ['payload' => encode_json($payload)] );
my $s = $req->as_string;
print STDERR "Request:\n$s\n";

my $resp = $ua->request($req);
$s = $resp->as_string;
print STDERR "Response:\n$s\n";

perl 모듈이 기본적으로 설치가 안되있다면 간단하게 설치 가능하다. (ubuntu 기준)

$ sudo apt-get install libjson-perl

자 이제 세팅은 완료 됐으니 간단하게 commit 을 날려보면

alt

이렇게 subversion 채널에 commit 되면 한글 comment도 제대로 notification을 받을 수 있다.

해당 예제는 unix 서버 기반의 svn server만 적용 가능하고

visualsvn을 사용한다면 svn-slack-notifier를 비슷한 방법으로 사용하면 된다.

Tags

권혁

백발까지 코딩하고 싶은 상부상조 프로그래머

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.