AWS Lambdaを活用したSlackとGitHubの連携&サーバレス活用事例3選AWS Lambdaで始めるサーバレスアーキテクチャ入門(終)(2/3 ページ)

» 2017年09月05日 05時00分 公開
[先崎一樹三菱総研DCS]

GitHubとSNSの連携

 続いて、GitHubとSNSの連携を行います。

 GitHubで通知を行うリポジトリの設定画面を開き「Integrations & services」を選択します。今回は「sample」というリポジトリを用意しました。

 「Add service」から「Amazon SNS」を選択します。

 先ほど作成したSNS Topicの「Arn」、および、IAMユーザーのセキュリティ認証情報を入力します。

 これでGitHubとSNSの連携は完了です。

GitHubのIntegrations Eventの設定

 GitHub API Hooksを見れば分かりますが、GitHubからSNSへ通知可能なデフォルトイベントは「Push」のみとなっていて、必要に応じてGitHub APIをコールしイベントを追加する必要があります。今回は「Issue Comment」を追加してみます。

{
  "name": "amazonsns",
  "events": [
    "push"
  ],
  "supported_events": [
    "commit_comment",
    "create",
    "delete",
    "deployment",
    "deployment_status",
    "download",
    "follow",
    "fork",
    "fork_apply",
    "gist",
    "gollum",
    "issue_comment",
    "issues",
    "member",
    "public",
    "pull_request",
    "pull_request_review_comment",
    "push",
    "release",
    "status",
    "team_add",
    "watch"
  ],
  "title": "AmazonSNS",
  "schema": [
    [
      "string",
      "aws_key"
    ],
    [
      "string",
      "sns_topic"
    ],
    [
      "string",
      "sns_region"
    ],
    [
      "password",
      "aws_secret"
    ]
  ]
}

 まずはWebHook APIをコールするためのトークンを取得します。「Personal Access Tokens」にアクセスし「Generate new token」を選択します。

 トークンを識別する文言とトークンのスコープを選択します。今回は私個人のリポジトリが対象であるためスコープは「admin:repo_hook」としましたが、必要に応じて適切な権限を選択してください。

 トークンが表示されるので控えておきます。

 続いて、通知を行うGitHubリポジトリのHook IDを確認します。

 ターミナルなどから下記のコマンドを実行します。「YOUR_TOKEN」には1つ前の手順で確認したトークンを入力し、「OWNER」と「REPOSITORY_NAME」は適宜GitHubの情報で置き換えてください。

curl -X GET -s -H "Authorization: token YOUR_TOKEN" \
https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks

 実行結果の「id」がHook IDです。

[
  {
    "type": "Repository",
    "id": ********,
    "name": "amazonsns",
    "active": true,
    "events": [
      "push"
    ],
    "config": {
      "aws_key": "********",
      "aws_secret": "********",
      "sns_region": "ap-northeast-1",
      "sns_topic": "arn:aws:sns:ap-northeast-1:************:github"
    },
    "updated_at": "2017-08-19T06:53:53Z",
    "created_at": "2017-08-19T06:53:53Z",
    "url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID",
    "test_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/test",
    "ping_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/pings",
    "last_response": {
      "code": 0,
      "status": "unused",
      "message": "Unused"
    }
  }
]

 最後に、Integrations Eventの設定を行います。HOOK_IDは取得したものに置き換えてください。

curl -X PATCH -s -H "Authorization: token YOUR_TOKEN" \
https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID \
-d '{"active": true, "events": ["push", "issue_comment"]}'

 実行結果の「events」に「issue_comment」が追加されていたら完了です。

{
  "type": "Repository",
  "id": ********,
  "name": "amazonsns",
  "active": true,
  "events": [
    "issue_comment",
    "push"
  ],
  "config": {
    "aws_key": "AKIAIXD6KKDO5LYIT2GA",
    "aws_secret": "********",
    "sns_region": "ap-northeast-1",
    "sns_topic": "arn:aws:sns:ap-northeast-1:************:github"
  },
  "updated_at": "2017-08-19T08:28:06Z",
  "created_at": "2017-08-19T06:53:53Z",
  "url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID",
  "test_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/test",
  "ping_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/pings",
  "last_response": {
    "code": 0,
    "status": "unused",
    "message": "Unused"
  }
}

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。