From 15341f48eaeecbd3cc12a52d4cde43a2e701cb13 Mon Sep 17 00:00:00 2001 From: Tak Tran Date: Fri, 19 Oct 2018 11:17:16 +0100 Subject: [PATCH] =?UTF-8?q?Extract=20heroku=20api=20function=20into=20its?= =?UTF-8?q?=20own=20file=20=20=F0=9F=90=BF=20v2.10.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/heroku-api.js | 24 ++++++++++++++++++++++++ lib/pipelines.js | 47 +++++++++++++++++++---------------------------- 2 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 lib/heroku-api.js diff --git a/lib/heroku-api.js b/lib/heroku-api.js new file mode 100644 index 00000000..56432a91 --- /dev/null +++ b/lib/heroku-api.js @@ -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; diff --git a/lib/pipelines.js b/lib/pipelines.js index 0c1712ca..214a8e6a 100644 --- a/lib/pipelines.js +++ b/lib/pipelines.js @@ -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;