1 | const keyBy = require('./key-by')
|
2 |
|
3 | const V3_HEADER = 'application/vnd.heroku+json; version=3'
|
4 | const FILTERS_HEADER = V3_HEADER + '.filters'
|
5 |
|
6 | function getCoupling (heroku, app) {
|
7 | return heroku.get(`/apps/${app}/pipeline-couplings`)
|
8 | }
|
9 |
|
10 | function postCoupling (heroku, pipeline, app, stage) {
|
11 | return heroku.post('/pipeline-couplings', {
|
12 | body: { app, pipeline, stage }
|
13 | })
|
14 | }
|
15 |
|
16 | function patchCoupling (heroku, id, stage) {
|
17 | return heroku.patch(`/pipeline-couplings/${id}`, { body: { stage } })
|
18 | }
|
19 |
|
20 | function deleteCoupling (heroku, id) {
|
21 | return heroku.delete(`/pipeline-couplings/${id}`)
|
22 | }
|
23 |
|
24 | function createCoupling (heroku, pipeline, app, stage) {
|
25 | return postCoupling(heroku, pipeline.id, app, stage)
|
26 | }
|
27 |
|
28 | function updateCoupling (heroku, app, stage) {
|
29 | return getCoupling(heroku, app)
|
30 | .then(coupling => patchCoupling(heroku, coupling.id, stage))
|
31 | }
|
32 |
|
33 | function removeCoupling (heroku, app) {
|
34 | return getCoupling(heroku, app)
|
35 | .then(coupling => deleteCoupling(heroku, coupling.id))
|
36 | }
|
37 |
|
38 | function listCouplings (heroku, pipelineId) {
|
39 | return heroku.get(`/pipelines/${pipelineId}/pipeline-couplings`)
|
40 | }
|
41 |
|
42 | function getPipeline (heroku, id) {
|
43 | return heroku.get(`/pipelines/${id}`)
|
44 | }
|
45 |
|
46 | function findPipelineByName (heroku, idOrnName) {
|
47 | return heroku.get(`/pipelines?eq[name]=${idOrnName}`)
|
48 | }
|
49 |
|
50 | function createPipeline (heroku, name) {
|
51 | return heroku.post('/pipelines', { body: { name } })
|
52 | }
|
53 |
|
54 | function createAppSetup (heroku, body) {
|
55 | return heroku.post('/app-setups', { body })
|
56 | }
|
57 |
|
58 | function getAppFilter (heroku, appIds) {
|
59 | return heroku.request({
|
60 | method: 'POST',
|
61 | path: `/filters/apps`,
|
62 | headers: { 'Accept': FILTERS_HEADER },
|
63 | body: { in: { id: appIds } }
|
64 | })
|
65 | }
|
66 |
|
67 | function listPipelineApps (heroku, pipelineId) {
|
68 | return listCouplings(heroku, pipelineId).then((couplings) => {
|
69 | const appIds = couplings.map((coupling) => coupling.app.id)
|
70 |
|
71 | return getAppFilter(heroku, appIds).then((apps) => {
|
72 | const couplingsByAppId = keyBy(couplings, (coupling) => coupling.app.id)
|
73 | apps.forEach((app) => { app.coupling = couplingsByAppId[app.id] })
|
74 |
|
75 | return apps
|
76 | })
|
77 | })
|
78 | }
|
79 |
|
80 | function getAccountFeature (heroku, feature) {
|
81 | return heroku.get(`/account/features/${feature}`)
|
82 | }
|
83 |
|
84 | function getAppSetup (heroku, buildId) {
|
85 | return heroku.get(`/app-setups/${buildId}`)
|
86 | }
|
87 |
|
88 | module.exports = {
|
89 | createAppSetup,
|
90 | createCoupling,
|
91 | createPipeline,
|
92 | deleteCoupling,
|
93 | getAccountFeature,
|
94 | getAppFilter,
|
95 | getAppSetup,
|
96 | getCoupling,
|
97 | getPipeline,
|
98 | findPipelineByName,
|
99 | listCouplings,
|
100 | listPipelineApps,
|
101 | patchCoupling,
|
102 | postCoupling,
|
103 | removeCoupling,
|
104 | updateCoupling,
|
105 | V3_HEADER
|
106 | }
|