AWS Lambdaで作るGitHub bot/GitHub bot made with AWS Lambda

AWS Lambdaで作るGitHub bot/GitHub bot made with AWS Lambda

575b9ff9b04d8598ab4f5774bb431b02?s=128

Kenichi Sasaki

March 06, 2018
Tweet

Transcript

  1. 7.
  2. 10.

    開発環境 • デプロイツール • Apex (http://apex.run) • GitHub統合方法 • Personal

    access tokens • (bot専用のGitHubアカウントがよさそう)
  3. 13.

    Apex project init $ apex init _ ____ _______ __

    / \ | _ \| ____\ \/ / / _ \ | |_) | _| \ / / ___ \| __/| |___ / \ /_/ \_\_| |_____/_/\_\ Enter the name of your project. It should be machine-friendly, as this is used to prefix your functions in Lambda. Project name: SRE-SET-Automation-Night-Vol2 Enter an optional description of your project. Project description: A sample apex project for SRE-SET automation night vol.2 [+] creating IAM SRE-SET-Automation-Night-Vol2_lambda_function role [+] creating IAM SRE-SET-Automation-Night-Vol2_lambda_logs policy [+] attaching policy to lambda_function role. [+] creating ./project.json [+] creating ./functions Setup complete, deploy those functions! $ apex deploy
  4. 15.

    How to get personal token Settings > Developer settings >

    Personal access tokens > Generate new token Scopeの選択は repo
  5. 17.

    Encrypt personal token $ aws kms encrypt --key-id \ arn:aws:kms:us-east-1:xxxxxxxxxxxx:alias/AutomationNight

    \ --plaintext $TOKEN \ --query CiphertextBlob \ --output text (base64化されたTOKENが出力される) $ vim project.json "role": "arn:aws:iam::... "environment": { "GITHUB_TOKEN": (base64化されたTOKEN) }, "kms_arn": 作成したKMS Keyのarn
  6. 18.

    Decrypt the token const AWS = require("aws-sdk"); const decrypt =

    (encrypted) => { const kms = new AWS.KMS(); return kms.decrypt({ CiphertextBlob: new Buffer(encrypted, 'base64') }).promise() .then((data) => { return data.Plaintext.toString('ascii'); }); };
  7. 19.

    Decryptされたtokenで認証 exports.handle = (event, context, callback) => { let message

    = JSON.parse(event.Records[0].Sns.Message); let encryptedGithubToken = process.env['GITHUB_TOKEN']; decrypt(encryptedGithubToken) .then((token) => { // Decrypt enveironment variable GITHUB_TOKEN octokit.authenticate({ type: "oauth", token: token }); // 以降実行したいコード }
  8. 23.

    AWS SNS - Lambda連携 exports.handle = (event, context, callback) =>

    { let msg = JSON.parse(event.Records[0].Sns.Message); { Event: { Records[{ Sns: { Message: “エスケープされたJSON形式でのGitHubイベント” } }] } }
  9. 24.

    PullRequestにコメント const octokit = require("@octokit/rest")(); // This condition prevents infinity

    loop if (mag.comment.body.match(/^repeat/i)) { // Call GitHub api which creates issue comment. return octokit.issues.createComment({ owner: msg.repository.owner.login, repo: msg.repository.name, number: msg.issue.number, body: `@${msg.sender.login}, You said "${msg.comment.body}"` }); } else { return Promise.resolve("The comment was ignored."); }
  10. 28.

    GitHub発行イベント変更 $ export TOKEN=xxxx $ export REPO_NAME=SreSetAutomationNightVol2 $ export GITHUB_ID=siroken3

    $ export HOOK_ID=$(curl -X GET -s -H "Authorization: token $TOKEN" https://api.github.com/repos/$GITHUB_ID/$REPO_NAME/hooks | jq '.[] | select( .name == "amazonsns") | .id') $ curl -X PATCH -s -H "Authorization: token xxxxxx" https://api.github.com/repos/$GITHUB_ID/$REPO_NAME/hooks/$HO OK_ID -d '{"add_events": ["issue_comment"], "remove_events": ["push"]}'
  11. 34.