LoginSignup
3
12

More than 3 years have passed since last update.

codecommitとredmineを連携させる

Last updated at Posted at 2020-01-20

概要

世の中的にgit→redmineはやり方整ってるけど、codecommit→redmineは無いのでごにょごにょした記録。
手順にはなってませんが、ヒントにはなるかな、と。

※こっちはPullRequestの連携

やりたい構成図

ざっくりこんなかんじ
image.png

やったこと概要

redmine側の設定

  • redmineにgit連携のプラグインgithub_hookを入れる
  • redmineのサーバにcodecommit上のリポジトリのbareリポジトリを作る

参考にしたサイトはこちら

AWS側の設定

  • codecommitにトリガーを設定し、codecommit上で何かイベントがあったときにLambdaが起動するようにする
  • Lambdaの設定をする
    • イベント拾ってpostするpythonを書く
    • 宛先がオンプレサーバのためVPC経由で通信できるようにする必要があり、LambdaがVPC起動できるようにする

苦労した点

  • bareリポジトリをID/Pass入力無しでfetchできるようにする
    • ここを参考に
    • redmineがコンテナなので、コンテナの中からできるようにするために exec -itで入ってやって終わってcommitがめんどい
  • github_hookに何をPOSTすればいいのかわからん
    • 自前のgitlabからテスト送信してみて、何送ってるかログから確認した
  • VPCでLambdaを起動させる
    • IAMポリシー不足でエラー出てるのに気づかなかった(上のほうにデカデカとでてた)

Lambdaのコード

import json
import boto3
import urllib
import urllib.request

codecommit = boto3.client('codecommit')

def lambda_handler(event, context):

    references = { reference['ref'] for reference in event['Records'][0]['codecommit']['references'] }
    #print("-------------------")
    #print("event: " +str(event)) 
    #print("context: " +str(context)) 

    # event(Lambdaに渡される情報)からrepository名とcommitNoを拾ってくる
    repository = event['Records'][0]['eventSourceARN'].split(':')[5]
    commitNo = event['Records'][0]['codecommit']['references'][0]['commit']

    try:
        # codecommitからcomittした情報をもろもろ取ってくる
        response = codecommit.get_commit(repositoryName=repository,commitId=commitNo)

        # redmine連携に必要な情報をセット
        id = response['commit']['commitId']
        message = response['commit']['message']
        timestamp = response['commit']['author']['date']
        authorname= response['commit']['author']['name']
        authoremail= response['commit']['author']['email']

        # codecommitからcodecommitのURL情報を取ってくる(いらないかも)
        response = codecommit.get_repository(repositoryName=repository)
        codecommiturl=response['repositoryMetadata']['cloneUrlHttp'] +"/" +id


        # redmineにPOSTする
        # <redmine-url><projectname><repositryname>は環境に合わせて
        redmine = 'http://<redmine-url>/github_hook?project_id=<projectname>&repository_id=<repositryname>'
        str_data = {
            'id': id,
            'message': message,
            'timestamp': timestamp,
            'url': codecommiturl,
            'author': {
                'name': authorname,
                'email': authoremail
            }
        }
        headers = {
            'Content-Type': 'application/json'
        }


        json_data = json.dumps(str_data).encode("utf-8")

        # requestを作成
        req = urllib.request.Request(url=redmine,data=json_data,headers=headers)

        # requestを投げる
        with urllib.request.urlopen(req) as res:
            body = res.read().decode('utf-8')
            print(body)
        try:
            with urllib.request.urlopen(req) as res:
                body = res.read()
                print(res)
                print(body)
        except urllib.error.HTTPError as err:
            print(err.code)
        except urllib.error.URLError as err:
            print(err.reason)

        return response['repositoryMetadata']['cloneUrlHttp']

    except Exception as e:
        print(e)
        print('Error getting repository {}. Make sure it exists and that your repository is in the same region as this function.'.format(repository))
        raise e
3
12
1

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
12