浏览代码

Added a script for sending notifications on Slack from Travis builds.

Kamil Piechaczek 6 年之前
父节点
当前提交
8cc319bb40
共有 2 个文件被更改,包括 104 次插入16 次删除
  1. 18 16
      .travis.yml
  2. 86 0
      scripts/notify-travis-status.js

+ 18 - 16
.travis.yml

@@ -14,25 +14,27 @@ 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
 install:
 - yarn add mgit2 --ignore-workspace-root-check
-- mgit sync --resolver-url-template="https://github.com/\${ path }.git"
-- git checkout package.json yarn.lock
-- rm -rf node_modules
-- yarn install
+#- mgit sync --resolver-url-template="https://github.com/\${ path }.git"
+#- git checkout package.json yarn.lock
+#- rm -rf node_modules
+#- yarn install
 script:
-- yarn run test --reporter=dots
-- yarn run docs:api --validate-only
-- '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
+- npm version
+#- yarn run test --reporter=dots
+#- yarn run docs:api --validate-only
+#- 'if [ $TRAVIS_TEST_RESULT -eq 0 ]; then
+#    travis_wait yarn run docs:build-and-publish-nightly;
+#  fi'
+after_script:
+- export END_TIME=$( date +%s )
+- yarn add slack-notify @octokit/rest@^16.13.4 --ignore-workspace-root-check
+- node ./scripts/notify-travis-status.js
+env:
+  global:
+  - secure: RO140EQDHmEOPJPikk8eCY5IdHpnEKGm41p5U1ewAbeZv1DpCG+rSumR2JdYl75kFAaZvCSm1NuVMM+kmYd+/z+LQbKj7QH5G/UHNho3H89blIU6WlJhT0YR5vclm9rvnEvOtxnfODca1Qrw+CaCoJks2o4VYbJB7mOBVNsh7Bc=

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

@@ -0,0 +1,86 @@
+#!/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"
+- "@octokit/rest@^16.13.4"
+
+ */
+
+const buildBranch = process.env.TRAVIS_BRANCH;
+
+// Send a notification only for main branches.
+// TODO: Remove Slack
+if ( buildBranch !== 'master' && buildBranch !== 'master-revisions' && buildBranch !== 'slack' ) {
+	process.exit();
+}
+
+const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
+
+const slack = require( 'slack-notify' )( SLACK_WEBHOOK_URL );
+const GitHubApi = require( '@octokit/rest' );
+const github = new GitHubApi( {
+	version: '3.0.0'
+} );
+
+const buildResult = process.env.TRAVIS_TEST_RESULT == 0;
+const messageColor = buildResult ? 'good' : 'danger';
+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 = {
+	ms: parseInt( process.env.END_TIME ) - parseInt( process.env.START_TIME )
+};
+
+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;
+
+const message = `Build <${ buildUrl }|#${ buildId }> (<${ commitUrl }|${ shortCommit }>) of \
+${ owner }/${ repo }@${ buildBranch } by [Author] ${ buildResult ? 'passed' : 'errored' } \
+in ${ execTime.mins } min ${ execTime.secs } sec`;
+
+const messageOptions = {
+	icon_url: 'https://a.slack-edge.com/66f9/img/services/travis_36.png',
+	unfurl_links: 1,
+	username: 'Travis CI',
+	attachments: [
+		{
+			color: messageColor,
+		}
+	]
+};
+
+slack.onError = err => {
+	console.log( 'API error occurred:', err );
+};
+
+github.repos.getCommit( { owner, repo, sha: buildCommit } )
+	.then( response => {
+		messageOptions.attachments[ 0 ].text = message.replace( '[Author]', response.data.commit.author.name );
+	} )
+	.catch( () => {
+		messageOptions.attachments[ 0 ].text = message.replace( 'by [Author] ', '' );
+		messageOptions.attachments[ 0 ].pretext = '_Could not fetch an author of the commit._';
+	} )
+	.then( () => {
+		slack.send( messageOptions );
+	} );