Skip to content

Instantly share code, notes, and snippets.

@Sauloxd
Created July 22, 2020 13:13
Show Gist options
  • Save Sauloxd/212d9ff3d50a345c17f94d0936483057 to your computer and use it in GitHub Desktop.
Save Sauloxd/212d9ff3d50a345c17f94d0936483057 to your computer and use it in GitHub Desktop.
CircleCI API
const util = require('util')
const https = require('https');
const CIRCLE_CI_TOKEN = 'Add you user token here'; //https://app.circleci.com/settings/user/tokens
const log = obj => console.log(util.inspect(obj, {showHidden: false, depth: null}));
const circleCiApi = makeCircleCiApi();
'github/QultureRocks/QultureApp'
const versionControlSystem = 'github';
const orgName = 'OrganizationName'; // If it's not in an org, fix projectSlug
cosnt repoName = 'RepoName'
circleCiApi.insights().then(log);
function makeCircleCiApi() {
const options = {
hostname: 'circleci.com',
port: 443,
method: 'GET',
headers: {
'Circle-Token': CIRCLE_CI_TOKEN
}
}
const projectSlug = `${versionControlSystem}/${orgName}/${repoName}`;
const circleCiApi = {
insights: () => request({
...options,
path: '/api/v2/insights/' + projectSlug + '/workflows'
}),
}
return circleCiApi;
}
function request(options) {
return new Promise((resolve, rej) => {
const req = https.request(options, (response) => {
if(response.statusCode === 401) process.exit(1)
response.setEncoding("utf8");
let body = "";
response.on('data', (data) => {
body += data;
})
response.on('end', () => {
body = JSON.parse(body);
resolve(body)
})
})
req.on('error', (error) => {
rej(error)
})
req.end();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment