Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Extract heroku api function into its own file 🐿 v2.10.3
Browse files Browse the repository at this point in the history
  • Loading branch information
taktran committed Oct 19, 2018
1 parent 9b909ea commit 15341f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
24 changes: 24 additions & 0 deletions lib/heroku-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function herokuApi ({ endpoint, authToken }) {
return fetch(
`https://api.heroku.com${endpoint}`,
{
headers: {
Accept: 'Accept: application/vnd.heroku+json; version=3',
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`
}
}
).then(response => {
const { ok, status, statusText } = response;
if (!ok) {
let err = new Error(`BadResponse: ${status} ${statusText}`);
err.name = 'BAD_RESPONSE';
err.status = status;
throw err;
}

return response.json();
});
}

module.exports = herokuApi;
47 changes: 19 additions & 28 deletions lib/pipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,41 @@
const herokuAuthToken = require('./heroku-auth-token');
const co = require('co');
const shellpromise = require('shellpromise');
const api = require('./heroku-api');
const spawn = require('child_process').spawn;

function api (url, token){
return fetch(
'https://api.heroku.com' + url,
{
headers: {
'Accept': 'Accept: application/vnd.heroku+json; version=3',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
}
).then(response => {
if(!response.ok){
let err = new Error(`BadResponse: ${response.status} ${response.statusText}`);
err.name = 'BAD_RESPONSE';
err.status = response.status;
throw err;
}

return response.json();
});
}

async function info (pipelineName){
const token = await herokuAuthToken();
const pipeline = await api(`/pipelines/${pipelineName}`, token);
const authToken = await herokuAuthToken();
const pipeline = await api({
endpoint: `/pipelines/${pipelineName}`,
authToken
});

return pipeline;
}

function getApps (pipelineName){
return co(function* (){
let token = yield herokuAuthToken();
let pipelines = yield api('/pipelines', token);
let authToken = yield herokuAuthToken();
let pipelines = yield api({
endpoint: '/pipelines',
authToken
});
let pipeline = pipelines.find(p => p.name === pipelineName);
if(!pipeline){
throw new Error('Could not find pipeline ' + pipeline);
}

let couplings = yield api('/pipelines/' + pipeline.id + '/pipeline-couplings', token);
let couplings = yield api({
endpoint: `/pipelines/${pipeline.id}/pipeline-couplings`,
authToken
});
let result = { staging:null, production:{ us:null,eu:null }, all:[] };
for(let coupling of couplings){
let app = yield api('/apps/' + coupling.app.id, token);
let app = yield api({
endpoint: `/apps/${coupling.app.id}`,
authToken
});
result.all.push(app.name);
if(coupling.stage === 'staging'){
result.staging = app.name;
Expand Down

0 comments on commit 15341f4

Please sign in to comment.