Skip to content

Commit

Permalink
Moved script for sending Slack notifications from the main repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Mar 29, 2019
1 parent 6b754f6 commit dafd880
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 7 deletions.
159 changes: 159 additions & 0 deletions packages/ckeditor5-dev-tests/bin/notify-travis-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env node

/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* eslint-env node */

/*
This script assumes that is being executed on Travis CI. It requires three environment variables:
- SLACK_WEBHOOK_URL - a URL where the notification should be sent
- START_TIME - POSIX time (when the script has begun the job)
- END_TIME - POSIX time (when the script has finished the job)
In order to enable the debug mode, set the `DEBUG=true` as the environment variable.
*/

const buildBranch = process.env.TRAVIS_BRANCH;

const acceptedBranches = [
't/1655',
't/ckeditor5/1655',
'master',
'master-revisions'
];

const acceptedEvents = [
'push',
'cron'
];

printDebugLog( 'Starting script: ' + __filename );

// Send a notification only for main branches...
if ( !acceptedBranches.includes( buildBranch ) ) {
printDebugLog( `Aborting due to an invalid branch (${ buildBranch }).` );

process.exit();
}

// ...and an event that triggered the build is correct...
if ( !acceptedEvents.includes( process.env.TRAVIS_EVENT_TYPE ) ) {
printDebugLog( `Aborting due to an invalid event type (${ process.env.TRAVIS_EVENT_TYPE }).` );

process.exit();
}

// ...and for builds that failed.
if ( process.env.TRAVIS_TEST_RESULT == 0 ) {
printDebugLog( 'The build did not fail. The notification will not be sent.' );

process.exit();
}

const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;

const slack = require( 'slack-notify' )( SLACK_WEBHOOK_URL );

const buildId = process.env.TRAVIS_JOB_NUMBER.split( '.' )[ 0 ];
const buildUrl = process.env.TRAVIS_JOB_WEB_URL;
const buildCommit = process.env.TRAVIS_COMMIT;
const [ owner, repo ] = process.env.TRAVIS_REPO_SLUG.split( '/' );
const commitUrl = `https://github.com/${ owner }/${ repo }/commit/${ buildCommit }`;
const shortCommit = buildCommit.substring( 0, 7 );
const execTime = getExecutionTime( parseInt( process.env.END_TIME ), parseInt( process.env.START_TIME ) );

slack.onError = err => {
console.log( 'API error occurred:', err );
};

const data = {
icon_url: 'https://a.slack-edge.com/66f9/img/services/travis_36.png',
unfurl_links: 1,
username: 'Travis CI',
attachments: [
{
color: 'danger',
fields: [
{
title: 'Repository (branch)',
value: [
`<https://github.com/${ owner }/${ repo }|${ repo }>`,
`(<https://github.com/${ owner }/${ repo }/tree/${ buildBranch }|${ buildBranch }>)`,
].join( ' ' ),
short: true
},
{
title: 'Commit',
value: `<${ commitUrl }|${ shortCommit }>`,
short: true
},
{
title: 'Build',
value: `<${ buildUrl }|#${ buildId }>`,
short: true
},
{
title: 'Testing time',
value: `${ execTime.mins } min ${ execTime.secs } sec`,
short: true
},
{
title: 'Commit message',
value: getFormattedMessage( process.env.TRAVIS_COMMIT_MESSAGE, owner, repo ),
short: false
},
]
},
]
};

slack.send( data );

/**
* Returns an object that compares two dates.
*
* @param {Number} endTime
* @param {Number} startTime
* @returns {Object}
*/
function getExecutionTime( endTime, startTime ) {
const execTime = {
ms: endTime - startTime
};

execTime.days = Math.floor( execTime.ms / 86400 );
execTime.hours = Math.floor( ( execTime.ms - 86400 * execTime.days ) / 3600 );
execTime.mins = Math.floor( ( ( execTime.ms - 86400 * execTime.days ) - 3600 * execTime.hours ) / 60 );
execTime.secs = ( ( execTime.ms - 86400 * execTime.days ) - 3600 * execTime.hours ) - 60 * execTime.mins;

return execTime;
}

/**
* Replaces `#Id` and `Repo/Owner#Id` with URls to Github Issues.
*
* @param {String} message
* @param {String} repoOwner
* @param {String} repoName
* @returns {string}
*/
function getFormattedMessage( message, repoOwner, repoName ) {
return message
.replace( / #(\d+)/g, ( _, issueId ) => {
return ` <https://github.com/${ repoOwner }/${ repoName }/issues/${ issueId }|#${ issueId }>`;
} )
.replace( /([\w-]+\/[\w-]+)#(\d+)/g, ( _, repoSlug, issueId ) => {
return `<https://github.com/${ repoSlug }/issues/${ issueId }|${ repoSlug }#${ issueId }>`;
} );
}

function printDebugLog( message ) {
if ( process.env.DEBUG == 'true' ) {
console.log( '[Slack Notification]', message );
}
}
2 changes: 2 additions & 0 deletions packages/ckeditor5-dev-tests/bin/test-travis.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -e

# Current work directory.
PACKAGE_ROOT=$(pwd)

Expand Down
8 changes: 5 additions & 3 deletions packages/ckeditor5-dev-tests/bin/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ if ( options.files.length === 0 ) {
options.themePath = path.resolve( cwd, 'packages', 'ckeditor5-theme-lark', 'theme', 'theme.css' );

tests.runAutomatedTests( options )
.then( () => {
process.exit( 0 );
} )
.catch( error => {
// Mark result of this task as invalid.
process.exitCode = 1;

console.log( chalk.red( error ) );

process.exit( 1 );
} );
4 changes: 1 addition & 3 deletions packages/ckeditor5-dev-tests/lib/tasks/runautomatedtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ function runKarma( options ) {
if ( exitCode === 0 ) {
resolve();
} else {
reject();

process.exit( exitCode );
reject( new Error( `Karma exited with ${ exitCode }.` ) );
}
} );

Expand Down
4 changes: 3 additions & 1 deletion packages/ckeditor5-dev-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"postcss-loader": "^3.0.0",
"raw-loader": "^1.0.0",
"sinon": "^6.3.4",
"slack-notify": "^0.1.7",
"style-loader": "^0.23.0",
"table": "^5.2.3",
"webpack": "^4.15.0"
Expand All @@ -68,7 +69,8 @@
"ckeditor5-dev-tests-prepare-package-json": "./bin/prepare-package-json.js",
"ckeditor5-dev-tests-install-dependencies": "./bin/install-dependencies.sh",
"ckeditor5-dev-tests-save-revision": "./bin/save-revision.js",
"ckeditor5-dev-tests-check-dependencies": "./bin/check-dependencies.js"
"ckeditor5-dev-tests-check-dependencies": "./bin/check-dependencies.js",
"ckeditor5-dev-tests-notify-travis-status": "./bin/notify-travis-status.js"
},
"author": "CKSource (http://cksource.com/)",
"license": "GPL-2.0-or-later",
Expand Down

0 comments on commit dafd880

Please sign in to comment.