{"id":310,"date":"2010-06-19T01:58:59","date_gmt":"2010-06-18T23:58:59","guid":{"rendered":"http:\/\/grummfy.be\/blog\/?p=310"},"modified":"2010-06-19T02:18:05","modified_gmt":"2010-06-19T00:18:05","slug":"webhook-google-code-recevoir-un-mail-a-chaque-commit","status":"publish","type":"post","link":"https:\/\/grummfy.be\/blog\/310","title":{"rendered":"WebHook Google Code &#8211; recevoir un mail \u00e0 chaque commit"},"content":{"rendered":"<p>Dans Google code il y a la possibilit\u00e9 d&rsquo;utiliser un gestionnaire de version tel que subversion (svn) ou mercurial (hg). C&rsquo;est bien pratique, mais malheureusement, de base, rien n&rsquo;est pr\u00e9vu pour pr\u00e9venir (except\u00e9 par flux RSS) les gens de ces mises \u00e0 jour. Cependant, Google code permet d&rsquo;utiliser un webhook en post commit.<\/p>\n<h2>Qu&rsquo;est-ce qu&rsquo;un webhook?<\/h2>\n<p>Un webhook c&rsquo;est un \u00ab\u00a0crochet web\u00a0\u00bb, c&rsquo;est-\u00e0-dire une URL a appel\u00e9e apr\u00e8s (avant ou pendant) une action X. Dans notre cas, apr\u00e8s chaque commit une URL est appel\u00e9e.<\/p>\n<h2>Utilisation<\/h2>\n<p>Voici un exemple de code que j&rsquo;utilise pour plusieurs de mes projets :<\/p>\n<pre lang=\"PHP\"><?php\r\n\/\/ project name\r\n$projects = array('mon-super-projet');\r\n\r\n\/\/ google code webhook key\r\n$keys = array(\r\n\t'b-box'\t=> 'Top-Secret_key_fourni_par_google-dans-l-adminsitration'\r\n);\r\n\r\n\/\/user agent from google code\r\n$useragent = 'Google Code Project Hosting (+http:\/\/code.google.com\/p\/support\/wiki\/PostCommitWebHooks)';\r\n\r\n\/\/email of all owner (eg. project chief)\r\n$owners = array('vous@example.com');\r\n\r\n\/\/email of all team members except owners\r\n$users = array('toi@example.com');\r\n\r\n\/\/sender of email\r\n$sender = 'WebHook mailer<webmaster@exemple.com>';\r\n\r\n\/\/----------------------------------------------------------------------\r\n$project = (isset($_GET['p']))?$_GET['p']:'';\r\n$revision = (isset($_GET['r']))?intval($_GET['r']):-99;\r\n$data = file_get_contents('php:\/\/input');\r\n$digest = (isset($_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC']))?$_SERVER['HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC']:'';\r\n\r\n\/\/----------------------------------------------------------------------\r\n\/**\r\n * Send a mail\r\n * @param string $from email of the sender : sample@example.com or \"name\"<sample@example.com>;\r\n * @param array $to [a] => list of email [cc], [bcc] (hidden), ...\r\n * @param string $subject\r\n * @param string $body\r\n * @return bool true if success\r\n *\/\r\nfunction mailer($from, array $to, $subject, $body)\r\n{\r\n\tif (empty($to))\r\n\t{\r\n\t\treturn false;\r\n\t}\r\n\r\n\t$headers = 'From: ' . $from . \"\\n\";\r\n\r\n\t$a = '';\r\n\r\n\tif (isset($to['a']) &amp;&amp; !empty($to['a']))\r\n\t{\r\n\t\t$a = implode(',', $to['a']);\r\n\t}\r\n\r\n\tif (isset($to['bcc']) &amp;&amp; !empty($to['bcc']))\r\n\t{\r\n\t\t$headers .= 'Bcc: ' . implode(',', $to['bcc']) . \"\\n\";\r\n\t}\r\n\r\n\tif (isset($to['cc']) &amp;&amp; !empty($to['cc']))\r\n\t{\r\n\t\t$headers .= 'Cc: ' . implode(',', $to['cc']) . \"\\n\";\r\n\t}\r\n\r\n\t$headers .= 'MIME-Version: 1.0' . \"\\n\";\r\n\t$headers .= 'Content-Type: text\/plain; charset=\"UTF-8\"' . \"\\n\";\r\n\t$headers .= 'Content-Transfer-Encoding: 8bit' . \"\\n\";\r\n\t$headers .= 'X-Mailer: PHP\/' . phpversion();\r\n\r\n\treturn mail($a, '[webhook]' . $subject, $body, $headers);\r\n}\r\n\r\nfunction failed($test_id, $msg)\r\n{\r\n\tglobal $sender, $owners;\r\n\r\n\t$msg .= \"\\n--\\nWebHook mail from the Google code project\";\r\n\r\n\tmailer($sender, array('bcc' => $owners), 'failed test #' . $test_id, $msg);\r\n\r\n\tdie('KO');\r\n}\r\n\r\nfunction get_ip()\r\n{ \r\n\treturn (isset($_SERVER['HTTP_X_FORWARDED_FOR']))?$_SERVER['HTTP_X_FORWARDED_FOR']:(isset($_SERVER['HTTP_CLIENT_IP']))?$_SERVER['HTTP_CLIENT_IP']:$_SERVER['REMOTE_ADDR'];\r\n}\r\n\r\n\/\/----------------------------------------------------------------------\r\nif ($useragent != $_SERVER['HTTP_USER_AGENT'])\r\n{\r\n\t\/\/ failed 1\r\n\tfailed(1, 'User agent is bad : ' . htmlspecialchars($_SERVER['HTTP_USER_AGENT']) . \"\\n\\nFrom : \" . get_ip());\r\n}\r\nelseif (empty($project) || !in_array($project, $projects))\r\n{\r\n\t\/\/ failed 2\r\n\tfailed(2, 'No project set : ' . htmlspecialchars($project) . \"\\n\\nFrom : \" . get_ip());\r\n}\r\nelse\r\n{\r\n\t$hmac = hash_hmac('md5', $data, $keys[ $project ]);\r\n\t$data = json_decode($data, true);\r\n\r\n\tif (empty($digest) || $digest != $hmac)\r\n\t{\r\n\t\t\/\/ failed 3\r\n\t\tfailed(3, 'Bad digest : ' . $digest . ' vs ' . $hmac . \"\\n\\nFrom : \" . get_ip());\r\n\t}\r\n\telseif (intval($data['revision_count']) != count($data['revisions']))\r\n\t{\r\n\t\t\/\/ failed 4\r\n\t\tfailed(4, 'Bad count : ' . count($data['revisions']) . ' vs ' . intval($data['revision_count']) . \"\\n\\nFrom : \" . get_ip());\r\n\t}\r\n\telse\r\n\t{\r\n\t\t$mail_body = '';\r\n\t\tforeach($data['revisions'] as $_revision)\r\n\t\t{\r\n\t\t\t$mail_body .= 'Revision : ' . \"\\t\" . htmlentities($_revision['revision']) . ' from ' . htmlentities($_revision['author']) . ' at ' . date('Y-m-d H:i', intval($_revision['timestamp'])) . \"\\n\";\r\n\t\t\t$mail_body .= 'Added : ' . \"\\t\" . implode(\"\\n\\t\\t\", htmlentities($_revision['added'])) . \"\\n\";\r\n\t\t\t$mail_body .= 'Modified : ' . \"\\t\" . implode(\"\\n\\t\\t\\t\", htmlentities($_revision['modified'])) . \"\\n\";\r\n\t\t\t$mail_body .= 'Removed : ' . \"\\t\" . implode(\"\\n\\t\\t\\t\", htmlentities($_revision['removed'])) . \"\\n\\n\";\r\n\/\/\t\t\t$mail_body .= 'URL : ' . \"\\t\\t\" . htmlentities($_revision['url']) . \"\\n\\n\";\r\n\t\t\t$mail_body .= 'Message : ' . \"\\t\" . htmlentities($_revision['message']) . \"\\n\\n\\n\";\r\n\/\/\t\t\t$_revision['path_count'];\r\n\t\t}\r\n\t\t$mail_body .= \"\\n--\\nWebHook mail from the Google code project : \" . $project . \"\\nhttp:\/\/code.google.com\/p\/\" . $project . \"\/\\n\";\r\n\r\n\t\tmailer($sender, array('bcc' => $owners + $users), '[' . $project . ']New revision #' . $revision, $mail_body);\r\n\t}\r\n}\r\n\r\nexit('OK');\r\n\r\n# EOF<\/pre>\n<\/p>\n<p>Plus d&rsquo;info : <a href=\"http:\/\/code.google.com\/p\/support\/wiki\/PostCommitWebHooks\">PostCommitWebHooks<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google code et webhook : envoyer un email a chaque commit<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[9],"tags":[38,110,24,42,28,37],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-dev","tag-decouverte","tag-php","tag-programmation","tag-script","tag-trucs-et-astuces","tag-web"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":2,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":317,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions\/317"}],"wp:attachment":[{"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grummfy.be\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}