浏览代码

Merge pull request #1497 from ckeditor/slack

Internal: Replaced an integration with Slack provided by Travis with a custom script that sends a notification for specified branches. Closes #1493.
Piotrek Koszuliński 6 年之前
父节点
当前提交
f81acb739e
共有 2 个文件被更改,包括 139 次插入7 次删除
  1. 8 7
      .travis.yml
  2. 131 0
      scripts/notify-travis-status.js

+ 8 - 7
.travis.yml

@@ -14,6 +14,7 @@ node_js:
 cache:
 - node_modules
 before_install:
+- export START_TIME=$( date +%s )
 - export DISPLAY=:99.0
 - sh -e /etc/init.d/xvfb start
 - npm i -g yarn
@@ -29,10 +30,10 @@ script:
 - 'if [ $TRAVIS_TEST_RESULT -eq 0 ]; then
     travis_wait yarn run docs:build-and-publish-nightly;
   fi'
-notifications:
-  slack:
-    rooms:
-    - secure: NKUpSnmkmlRRua0URoQJjEqNfIHx6tuqJhVCbQ6vU5rdUdw6A43OfG7Qym3MRQlXymFuIF0MUsOrS6t0GeXE9hSpC+5ynF02E5wdTD1/R7Tjq0XEdx0mXJTYjuOQA16fvwMxm5p3Ub5uWpSH+gBtyG86UPCbVSxDvvmxHqResAw=
-    on_success: change
-    on_failure: always
-    on_pull_requests: false
+after_script:
+- export END_TIME=$( date +%s )
+- yarn add slack-notify --ignore-workspace-root-check
+- node ./scripts/notify-travis-status.js
+env:
+  global:
+  - secure: RO140EQDHmEOPJPikk8eCY5IdHpnEKGm41p5U1ewAbeZv1DpCG+rSumR2JdYl75kFAaZvCSm1NuVMM+kmYd+/z+LQbKj7QH5G/UHNho3H89blIU6WlJhT0YR5vclm9rvnEvOtxnfODca1Qrw+CaCoJks2o4VYbJB7mOBVNsh7Bc=

+ 131 - 0
scripts/notify-travis-status.js

@@ -0,0 +1,131 @@
+#!/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)
+
+It also has some dependencies:
+- "slack-notify"
+
+ */
+
+const buildBranch = process.env.TRAVIS_BRANCH;
+
+// Send a notification only for main branches...
+if ( buildBranch !== 'master' && buildBranch !== 'master-revisions' ) {
+	process.exit();
+}
+
+// ...and "push" builds...
+if ( process.env.TRAVIS_EVENT_TYPE !== 'push' ) {
+	process.exit();
+}
+
+// ...and for builds that failed.
+if ( process.env.TRAVIS_TEST_RESULT == 0 ) {
+	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 ) );
+
+const messageOptions = {
+	icon_url: 'https://a.slack-edge.com/66f9/img/services/travis_36.png',
+	unfurl_links: 1,
+	username: 'Travis CI',
+	attachments: [
+		{
+			color: 'danger',
+			fields: [
+				{
+					title: 'Branch',
+					value: `<https://github.com/${ owner }/${ repo }/tree/${ buildBranch }|#${ buildBranch }>`,
+					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.onError = err => {
+	console.log( 'API error occurred:', err );
+};
+
+slack.send( messageOptions );
+
+/**
+ * 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 }>`;
+		} );
+}