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」を追加してみます。

  1. {
  2. "name": "amazonsns",
  3. "events": [
  4. "push"
  5. ],
  6. "supported_events": [
  7. "commit_comment",
  8. "create",
  9. "delete",
  10. "deployment",
  11. "deployment_status",
  12. "download",
  13. "follow",
  14. "fork",
  15. "fork_apply",
  16. "gist",
  17. "gollum",
  18. "issue_comment",
  19. "issues",
  20. "member",
  21. "public",
  22. "pull_request",
  23. "pull_request_review_comment",
  24. "push",
  25. "release",
  26. "status",
  27. "team_add",
  28. "watch"
  29. ],
  30. "title": "AmazonSNS",
  31. "schema": [
  32. [
  33. "string",
  34. "aws_key"
  35. ],
  36. [
  37. "string",
  38. "sns_topic"
  39. ],
  40. [
  41. "string",
  42. "sns_region"
  43. ],
  44. [
  45. "password",
  46. "aws_secret"
  47. ]
  48. ]
  49. }

 まずは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です。

  1. [
  2. {
  3. "type": "Repository",
  4. "id": ********,
  5. "name": "amazonsns",
  6. "active": true,
  7. "events": [
  8. "push"
  9. ],
  10. "config": {
  11. "aws_key": "********",
  12. "aws_secret": "********",
  13. "sns_region": "ap-northeast-1",
  14. "sns_topic": "arn:aws:sns:ap-northeast-1:************:github"
  15. },
  16. "updated_at": "2017-08-19T06:53:53Z",
  17. "created_at": "2017-08-19T06:53:53Z",
  18. "url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID",
  19. "test_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/test",
  20. "ping_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/pings",
  21. "last_response": {
  22. "code": 0,
  23. "status": "unused",
  24. "message": "Unused"
  25. }
  26. }
  27. ]

 最後に、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」が追加されていたら完了です。

  1. {
  2. "type": "Repository",
  3. "id": ********,
  4. "name": "amazonsns",
  5. "active": true,
  6. "events": [
  7. "issue_comment",
  8. "push"
  9. ],
  10. "config": {
  11. "aws_key": "AKIAIXD6KKDO5LYIT2GA",
  12. "aws_secret": "********",
  13. "sns_region": "ap-northeast-1",
  14. "sns_topic": "arn:aws:sns:ap-northeast-1:************:github"
  15. },
  16. "updated_at": "2017-08-19T08:28:06Z",
  17. "created_at": "2017-08-19T06:53:53Z",
  18. "url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID",
  19. "test_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/test",
  20. "ping_url": "https://api.github.com/repos/OWNER/REPOSITORY_NAME/hooks/HOOK_ID/pings",
  21. "last_response": {
  22. "code": 0,
  23. "status": "unused",
  24. "message": "Unused"
  25. }
  26. }

Copyright © ITmedia, Inc. All Rights Reserved.

スポンサーからのお知らせPR

Coding Edge 髫ェ蛟�スコ荵斟帷ケ晢スウ郢ァ�ュ郢晢スウ郢ァ�ー

隴幢スャ隴鯉ス・隴帷」ッ菫」

注目のテーマ

4AI by @IT - AIを作り、動かし、守り、生かす
Microsoft & Windows最前線2025
AI for エンジニアリング
ローコード/ノーコード セントラル by @IT - ITエンジニアがビジネスの中心で活躍する組織へ
Cloud Native Central by @IT - スケーラブルな能力を組織に
システム開発ノウハウ 【発注ナビ】PR
あなたにおすすめの記事PR

RSSについて

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

メールマガジン登録

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