Skip to content

Commit

Permalink
Merge pull request #693 from ckeditor/i/internal/651
Browse files Browse the repository at this point in the history
Feature (tests): The `notify-travis-status` script will mention an author of a commit that caused to fail the CI.
  • Loading branch information
pomek authored Mar 8, 2021
2 parents 22e4fb1 + 769a01b commit d0dcf25
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
21 changes: 21 additions & 0 deletions packages/ckeditor5-dev-tests/bin/members.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"AnnaTomanek": "U044RC5NQ",
"Comandeer": "U0BK6HSJJ",
"FilipTokarski": "URSK5KV9V",
"godai78": "U017DJLU7NX",
"jacekbogdanski": "U8LAY97DE",
"kwach2000": "U01BRKQL8P4",
"LukaszGudel": "U01FRHAT8T1",
"ma2ciek": "U2JGX5TC7",
"magda-chrzescian": "U01FL082L22",
"maxbarnas": "U1EN0S6NT",
"mlewand": "U03UQQ1N3",
"Mgsy": "U5A38S2PN",
"niegowski": "U01017YC7C7",
"oleq": "U03UU2MNN",
"pomek": "U1D6HMH5M",
"psmyrek": "U01BJRWJCSJ",
"Reinmar": "U03UQRP0C",
"scofalik": "U05611ZMM",
"wwalc": "U03UQQ8PY"
}
83 changes: 73 additions & 10 deletions packages/ckeditor5-dev-tests/bin/notify-travis-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@

/* 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.
*/
// This script assumes that is being executed on Travis CI. It requires following 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)
// - GITHUB_TOKEN - token to a Github account with the scope: "repos". It is requires for obtaining an author of
// the commit if the build failed. The repository can be private and we can't use the public API.
//
// If the `SLACK_NOTIFY_COMMIT_URL` environment variable is defined, the script use the URL as the commit URL.
// Otherwise, a marge of variables `TRAVIS_REPO_SLUG` and `TRAVIS_COMMIT` will be used.
//
// In order to enable the debug mode, set the `DEBUG=true` as the environment variable.

const childProcess = require( 'child_process' );

// A map that translates Github accounts to Slack ids.
const members = require( './members.json' );

const buildBranch = process.env.TRAVIS_BRANCH;

Expand Down Expand Up @@ -111,6 +117,18 @@ const data = {
]
};

const commitAuthor = getCommitAuthor();

if ( commitAuthor ) {
const slackAccount = members[ commitAuthor ];

if ( slackAccount ) {
data.text = `<@${ slackAccount }>, could you take a look?`;
} else {
data.text = '_The author of the commit could not be obtained._';
}
}

slack.send( data );

/**
Expand Down Expand Up @@ -151,6 +169,51 @@ function getFormattedMessage( message, repoOwner, repoName ) {
} );
}

/**
* Returns a URL to GitHub API which returns details of the commit that caused the CI to fail its job.
*
* @returns {String}
*/
function getGithubApiUrl() {
let commitUrl;

const { SLACK_NOTIFY_COMMIT_URL, TRAVIS_REPO_SLUG, TRAVIS_COMMIT } = process.env;

if ( SLACK_NOTIFY_COMMIT_URL ) {
commitUrl = SLACK_NOTIFY_COMMIT_URL;
} else {
commitUrl = `https://github.com/${ TRAVIS_REPO_SLUG }/commit/${ TRAVIS_COMMIT }`;
}

return commitUrl.replace( 'github.com/', 'api.github.com/repos/' ).replace( '/commit/', '/commits/' );
}

/**
* Returns a name of an account that made the commit. If couldn't be obtained, returns `null` instead.
*
* @returns {String|null}
*/
function getCommitAuthor() {
const curlArguments = [
`-H "Authorization: token ${ process.env.GITHUB_TOKEN }"`,
getGithubApiUrl()
];

const curlOutput = childProcess.spawnSync( 'curl', curlArguments, {
encoding: 'utf8',
shell: true,
stderr: 'inherit'
} );

try {
const curlDetails = JSON.parse( curlOutput.stdout );

return curlDetails.author.login;
} catch ( err ) {
return null;
}
}

function printDebugLog( message ) {
if ( process.env.DEBUG == 'true' ) {
console.log( '[Slack Notification]', message );
Expand Down

0 comments on commit d0dcf25

Please sign in to comment.