ããã«ã¡ã¯ãæè¡é¨ã®æ¾å°¾(@Kazu_cocoa)ã§ãã
主ã«ã¢ãã¤ã«ã¢ããªéçºã«ããã¦ãæ°ã¶æåããGitHubã®Webhooksã使ã£ãã¨ããåãçµã¿ãå§ãã¾ãããHipChatãSlackãªã©ãã¯ããã¨ããæ§ã ãªãµã¼ãã¹ã¨ã®é£æºãµã¼ãã¹ãæä¾ãã¦ããGitHubã§ãããWebhooksã®æ©è½ã使ããã¨ã§ãããèªåãã¡ã®éçºãæ¯æ´ããæªæ¥ãåµé ã§ããã°ã¨æãã¾ãã以éã§ã¯ãå®éã®ä½¿ç¨ä¾ããã®å®è£ ãããªè©±ãã¸ã¨è©±ãããããã¾ãã
ã¯ãã¯ãããã«ãããWebhooksã®ä½¿ç¨ä¾
ãã§ãã¯ãªã¹ãã«ããã»ã«ããã§ãã¯ãPRæã«å®æ½ãã
ã¢ãã¤ã«ã¢ããªéçºã¯Webã¢ããªéçºã¨ç°ãªãç¹ãå¤ã ããã¾ããä¾ãã°ãéçºå¯¾è±¡ã®é¢ã§ã¯ç«¯æ«ã®å¤æ§æ§ã端æ«ã·ã¹ãã å´è¨å®ã»éä¿¡ç¶æ ã®å¤æ§ããªã©ããªãªã¼ã¹ã®é¢ã§ã¯ãããã¤ã«å¯¾ããå¶éãæ´æ°ãã¦ã¼ã¶ä¾åã§ãããã¨ãªã©ãã§ãããã®ãããå½ããåå質ã®åºä¸ãã®ããã«ä¸å ·åã®ä½ãè¾¼ã¿ãæ¸ããä»çµã¿ãå¿ è¦ã§ããã
å½ããåå質ã®åºä¸ãã®ããã«ãåºæ¬çãªæ¯ãèãã®èæ ®æ¼ãã«ããä¸å ·åã®ä½ãè¾¼ã¿ããã¯ã©ãã·ã¥ã«ç¹ããä¸å ·åã®ä½ãè¾¼ã¿ãé²ããã¨ã«ç¦ç¹ãçµã対çãèãã¾ããã対çã®1ã¤ã¨ãã¦ã確èªãã観ç¹ãçµã£ããã§ãã¯ãªã¹ããä½æããå¤æ´ãmasterã«ãã¼ã¸ããåã®Pull Requestã®æ®µéã§éçºè ããã§ãã¯ã§ããããã«ãã¾ããããã®ãã§ãã¯ãªã¹ãã«ããã»ã«ããã§ãã¯ã¨ãä»éçºè ã®ã¬ãã¥ã¼ã«ããå½ããåå質ã®åºä¸ããå³ã£ã¦ãã¾ãã
Pull Requestã¸ã®ãã§ãã¯ãªã¹ãå¼µãã¤ãã¯ãå½åã¯å©ç¨è ã®åå¿ãå¾ã¦æ¹åããããã«äººæã§è¡ã£ã¦ãã¾ãããä¸æ¹ãéçºãã¹ã±ã¼ã«ããã¨äººæã«ãã対å¿ã追ãã¤ããªããªããã¨ã¯æããã§ããã
Webhooksã使ã£ã¦æ©æ¢°çã«ãã§ãã¯ãªã¹ããå¿çãã
éçºã®ã¹ã±ã¼ã«ã«å¯¾å¿ãããããGitHubã®Webhooksã使ããã¨ã§ãã®ãã§ãã¯ãªã¹ãã®å¿çãæ©æ¢°ã«ä»»ãããã¨ã«ãã¾ãããå ·ä½çã«ã¯ã以ä¸ã®é åºã§å¦çãè¡ããã¾ãã
- éçºä¸ã®masterãã©ã³ãã«å¯¾ãã¦éçºè ãPull Requestãåºã
- Pull Requestãå¥æ©ã¨ããGitHubã®Webhooksãåä¿¡ãããµã¼ããããã®Pull Requestã¸ã®ã³ã¡ã³ãã¨ãã¦ãã§ãã¯ãªã¹ããè¿ã
- ä»éçºè ã®ã¬ãã¥ã¼ã¨ããã§ãã¯ãªã¹ãã«ãããã§ãã¯ãçµããPull Requestãéçºè ãèªåã§masterã«ãã¼ã¸ãã
ãã§ãã¯ãªã¹ãããã³ãã¬åã§ããç²åº¦ã¾ã§è½ã¨ãè¾¼ã¿ã¾ããããç¾æ®µéã®ã¯ã©ã¤ã¢ã³ãã¢ããªã®ä¸å ·åç¶æ ãè¦ãéãã§ã¯ããç¨åº¦å¹æãã ãã¦ããããã§ããè足ã§ããããã®ãã§ãã¯ãªã¹ãã«ã¯ããä¸ã¤çããããã¾ããããã¯ãªãªã¼ã¹åæ¤è¨¼ã®æ®µéã§ããã¹ãã¨ã³ã¸ãã¢ãæ¢ç´¢çã«é害度ã®é«ãä¸å ·åãè¦ã¤ãã¦ããçºã®ãã£ãããä½ããã¨ã§ãããªãªã¼ã¹ã¾ã§ã«å¤ãã®æéãæ¤è¨¼ã«å²ãã訳ã§ã¯ãªãã®ã§ãæ¤è¨¼ããå´ã¨ãã¦ããã¦ãã¤ãã¦æ¢ç´¢çã«æ¤è¨¼ãé²ãããã¨ã¯éè¦ãªè¦ç´ ã§ããã
GitHubã«ãããã§ãã¯ãªã¹ãã®å¿çä¾
ãã§ãã¯ãçµãããã¼ã¸ãç¡äºè¡ã£ããããã«åããã¦ã¤ã¤ãããã¦ããã¾ãã
GitHub Webhooksã®ä½¿ãæ¹
GitHub Webhooksã¨ã¯
Webhooksãè¨å®ãããªãã¸ããªã«å¯¾ããæä½ãå¥æ©ã«ãè¨å®ããURLã«å¯¾ãã¦HTTPãªã¯ã¨ã¹ããéä¿¡ããGitHubã®ãµã¼ãã¹ã§ããå³1ãå³2ã®ããã«GUIããWebhooksãæå¹ã«ãããã¨ã§å©ç¨å¯è½ã«ãªãã¾ããWebhooksã«é¢ãã詳細ãªèª¬æã¯å ¬å¼ããã¥ã¡ã³ããåç §ãã ãããWebhooksã¯GitHub/GitHub Enteroriseã®ãããã«ããã¦ãå©ç¨å¯è½ã§ãããã ããå¤ããã¼ã¸ã§ã³ã®GitHub Enterpriseã§ã¯ãGUIã§ã¯ãªãWeb APIçµç±ã«ããWebhooksã®è¨å®ãå¿ è¦ã«ãªãããããã¾ããã
å³1:Webhooksã®è¨å®ç»é¢
å³2:Webhooksã®æå¹å
Pull Requestãå¥æ©ã«ã³ã¡ã³ããè¿ã
å ã»ã©ã®Webhooksã®ä½¿ç¨ä¾ã®ããã«ããããªãã¸ããªã«å¯¾ããPull Requestãå¥æ©ã¨ããWebhooksã«å¯¾ãã¦ããã®ãªãã¸ããªã¸ä½ããã®ã³ã¡ã³ããè¿ãã¨ããæµãã以ä¸ã«è¨è¿°ãã¦ã¿ã¾ãã
ã¾ãã¯GitHubå´è¨å®ã®Webhooksã®è¨å®ã«ããã¦ãHTTPãªã¯ã¨ã¹ãéä¿¡å ãµã¼ãã®URLãæå®ãpull_requestã«ãã§ãã¯ãããWebhooksãActivateãã¾ããå³1,2ããã®è¨å®ç»é¢ã§ããè¨å®ãçµãããããã®Webhooksãåãåããµã¼ãã«ä»¥ä¸ã®ãããªå®è£ ã®ãµã³ãã«ãç¨æãã¾ãã
require 'sinatra' require 'octokit' # https://github.com/octokit/octokit.rb require 'hashie' post '/hook_sample' do # Webhooksã®ãããã«å«ã¾ããè¦ç´ X-Github-Eventãåå¾ãã # 'pull_request'ã®ãããªæååãå«ã¾ãã¦ãã github_event = request.env['HTTP_X_GITHUB_EVENT'] # Webhooksã®JSONå½¢å¼ã®ãã¤ãã¼ã(https://developer.github.com/v3/)ãåå¾ãã req_body = Hashie::Mash.new(JSON.parse(request.body.read)) repository = req_body.repository.full_name # GitHub Web APIã¯ã©ã¤ã¢ã³ãã®Octokitã®ã¤ã³ã¹ã¿ã³ã¹ä½æ # ACCESS_TOKEN: Web APIã使ãããã«å¿ è¦ã¨ãªãOAuthèªè¨¼ã«ä½¿ãæåå client = Octokit::Client.new access_token: ACCESS_TOKEN case github_event when 'pull_request' # GitHub Web API çµç±ã§ã³ã¡ã³ããéã client.add_comment(repository, req_body.pull_request.number, 'ã³ã¡ã³ã') else # pull_request以å¤ã®å¦ç end end
GitHub Enterpriseã§Octokitã使ãæã«æ³¨æãã¹ããã¨
ã¯ãã¯ãããã§ã¯ãGitHub Enterpriseã使ã£ã¦ãã¾ããGutHub Enterpriseã®ããã«æä½ãããGitHubã®APIãç¬èªãã¹ãä¸ã«ããå ´åã以ä¸ã®ããã«newããåã«ãã¹ãåãæå®ããå¿ è¦ãããã¾ãã
require 'octokit' Octokit.api_endpoint = 'http://api.github.dev' Octokit.web_endpoint = 'http://github.dev' client = Octokit::Client.new access_token: ACCESS_TOKEN
ãã®ããã«ãããã¨ã§ã以ä¸ã®ããã«GitHub Enterpriseã¸ã¢ã¯ã»ã¹ããããã«ãªãã¾ãã
puts client.api_endpoint # => 'http://api.github.dev/' puts client.web_endpoint # => 'http://github.dev/'
Pull Requestã¸ã®å¿ç以å¤ã«ã
Webhooksã調ã¹ãã¨ãissueãä½æããããissueã«å¯¾ãã¦ã³ã¡ã³ããã¤ãããããã¨ãå¥æ©ã«ããã®issueã«è¨è¼ãããæååãèªãã§æå®ã®æè¨ãã³ã¡ã³ãããã¨ããæµããå®ç¾ãããã¨ãã§ãã¾ããããã«ãããä¾ãã°Webhooksãè¨å®ãã¦ãããªãã¸ããªã®issueã«ããã¦ãplease comment me
ã¨ããã¨ããã®issueã«å¯¾ãã¦æ©æ¢°çã«ã³ã¡ã³ããè¿ããã¨ãã§ããããã«ãªãã¾ãã
GitHubã«Botãçæ¯ããéçºãè£å©ãã
Hubotã®ãããªBotããã£ãããã¼ã«ä¸ã§ææãæã¤ãã®ããã«éçºããã»ã¹ãè£å©ããChatOpsã¨ããè¨èãåå¨ããããã«ãGitHubä¸ã«ããã¦ãBotãéçºãè£å©ãã¦ãããããªå½¢ã«ã¿ãã¦ãã¾ãããã©ãã¾ã§ä»»ããããªã©ã®èæ ®ãå ¥ãã¨æãã¾ãããããããBotã«ä½æ¥ã®è£å©ãä»»ãããããªæªæ¥ãå®ç¾ã§ãããã§ããã
æå¾ã«
GitHubã®Webhooksã®ããã«ãæ§ã ãªãµã¼ãã¹ãã¤ãªãåãããåã ã®ç®çãä½å¶ã«è¦åã£ãããã»ã¹ã®æ§ç¯ãæ¯æ´ããæ段ã¯å··ã«ããµãã¦ãã¾ããä»åã®å®ä¾ã¯ãå質ã®åºä¸ããç®çã¨ãããä¸å ·åä½ãè¾¼ã¿ãé¿ããããã®æ°ã¥ããéçºã®ä¸é£ã®ããã»ã¹ã®ä¸ã«çµã¿è¾¼ãã§ããä¸ä¾ã§ãããä¸æ¹ã§ãæ§ã ãªç¨éã¸ã®æªæ¥ã¸é²ããã¨ãã§ããããªé£æºã ã£ãã®ã§ã¯ãªãã§ããããã
ã¨ããã§ããã®ãããªåãçµã¿ã¯ãã¹ãã¨ã³ã¸ãã¢ã¨ãã¦ã®æ¹åæ´»åã®ã²ã¨ã¤ã®å´é¢ã§ãããçããã®è¿ãã«ãã¾ããã¹ãã¨ã³ã¸ãã¢ã¯ãå質ãããåä¸ãããããã«ã©ã®ãããªåµé çãªåãçµã¿ããã¦ãã¾ããï¼è²ã ãªåãçµã¿ãèãã¦ã¿ãããã®ã§ããã