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