/
check-circle-ci-status.js
50 lines (40 loc) · 1.38 KB
/
check-circle-ci-status.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const http = require('request-promise-json');
const {execRead, logPromise} = require('../utils');
// https://circleci.com/docs/api/v1-reference/#projects
const CIRCLE_CI_BASE_URL =
'https://circleci.com/api/v1.1/project/github/facebook/react/tree/master';
const check = async ({cwd}) => {
const token = process.env.CIRCLE_CI_API_TOKEN;
const uri = `${CIRCLE_CI_BASE_URL}?circle-token=${token}&limit=1`;
const response = await http.get(uri, true);
const {outcome, status, vcs_revision: ciRevision} = response[0];
const gitRevision = await execRead('git rev-parse HEAD', {cwd});
if (gitRevision !== ciRevision) {
throw Error(
chalk`
CircleCI is stale
{white The latest Git revision is {yellow.bold ${gitRevision}}}
{white The most recent CircleCI revision is {yellow.bold ${ciRevision}}}
{white Please wait for CircleCI to catch up.}
`
);
} else if (outcome !== 'success') {
throw Error(
chalk`
CircleCI failed
{white The most recent CircleCI build has a status of {red.bold ${outcome ||
status}}}
{white Please retry this build in CircleCI if you believe this is an error.}
`
);
}
};
module.exports = async params => {
if (params.local) {
return;
}
return logPromise(check(params), 'Checking CircleCI status');
};