notify-travis-status.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. /**
  3. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  4. * For licensing, see LICENSE.md.
  5. */
  6. /* eslint-env node */
  7. /*
  8. This script assumes that is being executed on Travis CI. It requires three environment variables:
  9. - SLACK_WEBHOOK_URL - a URL where the notification should be sent
  10. - START_TIME - POSIX time (when the script has begun the job)
  11. - END_TIME - POSIX time (when the script has finished the job)
  12. It also has some dependencies:
  13. - "slack-notify"
  14. - "@octokit/rest@^16.13.4"
  15. */
  16. const buildBranch = process.env.TRAVIS_BRANCH;
  17. // Send a notification only for main branches...
  18. if ( buildBranch !== 'master' && buildBranch !== 'master-revisions' ) {
  19. process.exit();
  20. }
  21. // ...and "push" builds.
  22. if ( process.env.TRAVIS_EVENT_TYPE !== 'push' ) {
  23. process.exit();
  24. }
  25. const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
  26. const slack = require( 'slack-notify' )( SLACK_WEBHOOK_URL );
  27. const GitHubApi = require( '@octokit/rest' );
  28. const github = new GitHubApi( {
  29. version: '3.0.0'
  30. } );
  31. const buildResult = process.env.TRAVIS_TEST_RESULT == 0;
  32. const messageColor = buildResult ? 'good' : 'danger';
  33. const buildId = process.env.TRAVIS_JOB_NUMBER.split( '.' )[ 0 ];
  34. const buildUrl = process.env.TRAVIS_JOB_WEB_URL;
  35. const buildCommit = process.env.TRAVIS_COMMIT;
  36. const [ owner, repo ] = process.env.TRAVIS_REPO_SLUG.split( '/' );
  37. const commitUrl = `https://github.com/${ owner }/${ repo }/commit/${ buildCommit }`;
  38. const shortCommit = buildCommit.substring( 0, 7 );
  39. const execTime = {
  40. ms: parseInt( process.env.END_TIME ) - parseInt( process.env.START_TIME )
  41. };
  42. execTime.days = Math.floor( execTime.ms / 86400 );
  43. execTime.hours = Math.floor( ( execTime.ms - 86400 * execTime.days ) / 3600 );
  44. execTime.mins = Math.floor( ( ( execTime.ms - 86400 * execTime.days ) - 3600 * execTime.hours ) / 60 );
  45. execTime.secs = ( ( execTime.ms - 86400 * execTime.days ) - 3600 * execTime.hours ) - 60 * execTime.mins;
  46. const message = `Build <${ buildUrl }|#${ buildId }> (<${ commitUrl }|${ shortCommit }>) of \
  47. ${ owner }/${ repo }@${ buildBranch } by [Author] ${ buildResult ? 'passed' : 'errored' } \
  48. in ${ execTime.mins } min ${ execTime.secs } sec`;
  49. const messageOptions = {
  50. icon_url: 'https://a.slack-edge.com/66f9/img/services/travis_36.png',
  51. unfurl_links: 1,
  52. username: 'Travis CI',
  53. attachments: [
  54. {
  55. color: messageColor,
  56. }
  57. ]
  58. };
  59. slack.onError = err => {
  60. console.log( 'API error occurred:', err );
  61. };
  62. github.repos.getCommit( { owner, repo, sha: buildCommit } )
  63. .then( response => {
  64. messageOptions.attachments[ 0 ].text = message.replace( '[Author]', response.data.commit.author.name );
  65. } )
  66. .catch( () => {
  67. messageOptions.attachments[ 0 ].text = message.replace( 'by [Author] ', '' );
  68. messageOptions.attachments[ 0 ].pretext = '_Could not fetch an author of the commit._';
  69. } )
  70. .then( () => {
  71. slack.send( messageOptions );
  72. } );