├── LICENSE ├── README.md └── travis-ci.php /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TravisCI-Webhooks 2 | 3 | This simple PHP code will allow you to send Travis-CI build notifications to your discord channel. 4 | 5 | ## Screenshot 6 | ![](/../images/discord_screenshot.png) 7 | 8 | ## Setup 9 | 1. Configure your webserver of choice and make sure it has support for PHP 10 | 2. Configure the script, making sure to add your unique Discord webhook URL. 11 | 3. Add the following to your `.travis.yml` file. More configuration options can be found [here](https://docs.travis-ci.com/user/notifications/#Configuring-webhook-notifications) 12 | ```yml 13 | notifications: 14 | webhooks: http://your-domain.com/travis-ci.php 15 | ``` 16 | -------------------------------------------------------------------------------- /travis-ci.php: -------------------------------------------------------------------------------- 1 | config->notifications->webhook->public_key; 28 | $signature = base64_decode($_SERVER['HTTP_SIGNATURE']); 29 | if (openssl_verify($fullPostData, $signature, $publicKey) != 1) { 30 | header('HTTP/1.0 401 Access Denied'); 31 | exit(); 32 | } 33 | 34 | if ($fullPostData == '') { 35 | header('HTTP/1.0 400 Bad request'); 36 | echo "No data submitted\n"; 37 | exit(); 38 | } 39 | 40 | $data = json_decode($fullPostData, true); 41 | 42 | if ($data === null) { 43 | header('HTTP/1.0 400 Bad request'); 44 | echo "JSON cannot be decoded\n"; 45 | exit(); 46 | } 47 | 48 | $result_string = 'result_message'; 49 | $time = 'finished_at'; 50 | 51 | if ($data[$result_string] === null) { 52 | $result_string = 'status_message'; 53 | } 54 | 55 | switch ($data[$result_string]) { 56 | case "Pending": 57 | $color = $COLOR_IN_PROGRESS; 58 | $time = 'started_at'; 59 | break; 60 | case "Passed": 61 | case "Fixed": 62 | $color = $COLOR_PASS; 63 | break; 64 | case "Broken": 65 | case "Failed": 66 | case "Still Failing": 67 | $color = $COLOR_FAIL; 68 | break; 69 | case "Canceled": 70 | $color = $COLOR_CANCEL; 71 | } 72 | 73 | $payload = array( 74 | 'username' => 'Travis CI', 75 | 'avatar_url' => 'http://i.imgur.com/kOfUGNS.png', 76 | 'embeds' => array( 77 | array( 78 | 'color' => $color, 79 | 'author' => array( 80 | 'name' => 'Build #' . $data['number'] . ' ' . $data[$result_string] . ' - ' . $data['author_name'], 81 | 'url' => $data['build_url'] 82 | ) , 83 | 'title' => '[' . $data['repository']['name'] . ':' . $data['branch'] . ']', 84 | 'url' => getRepoURL($data) , 85 | 'description' => '[`' . substr($data['commit'], 0, 7) . '`](' . getCommitURL($data) . ') ' . $data['message'], 86 | 'timestamp' => $data[$time] 87 | ) 88 | ) 89 | ); 90 | 91 | $context = stream_context_create(array( 92 | 'http' => array( 93 | 'method' => 'POST', 94 | 'header' => 'Content-Type: application/json', 95 | 'content' => json_encode($payload) 96 | ) 97 | )); 98 | 99 | $response = file_get_contents($DISCORD_WEBHOOK_URL, false, $context); 100 | 101 | function getRepoURL($data) { 102 | return 'http://github.com/' . $data['repository']['owner_name'] . '/' . $data['repository']['name']; 103 | } 104 | 105 | function getCommitURL($data) { 106 | return getRepoURL($data) . '/commit/' . $data['commit']; 107 | } 108 | 109 | ?> 110 | --------------------------------------------------------------------------------