-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
136 lines (117 loc) · 4.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as core from '@actions/core';
import * as github from '@actions/github';
import {HerokuPlatformApiApp, HerokuPlatformApiConfigVars} from '@heroku-cli/typescript-api-schema';
import * as fs from 'fs';
import {each, mapValues} from 'lodash-es';
import {AppJson, AppJsonAddon, AppJsonEnv} from './lib/heroku';
import Heroku = require('heroku-client');
async function createAddons(heroku: Heroku, targetApp: string, addons: Array<AppJsonAddon | string>) {
return await Promise.all(addons.map(async(addon: AppJsonAddon | string) => {
const newAddon = typeof addon === 'string'
? {plan: addon}
: {
plan: addon.plan,
config: addon.options,
attachment: addon.as && {
name: addon.as,
},
};
console.log(`Creating addon ${newAddon.plan}...`);
const response = await heroku.post(`/apps/${targetApp}/addons`, {body: newAddon});
console.log(response);
}));
}
async function addEnvs(
heroku: Heroku,
sourceApp: string,
targetApp: string,
envs: Record<string, AppJsonEnv>,
additional?: HerokuPlatformApiConfigVars,
) {
const sourceEnvs = await heroku.get<HerokuPlatformApiConfigVars>(`/apps/${sourceApp}/config-vars`);
const targetEnvs = mapValues(envs, (envDef, key) => {
if (envDef.generator) {
throw 'ENVs with generator not supported atm.';
}
const value = envDef.value || sourceEnvs[key];
if (envDef.required && !value) {
throw `Missing required env ${key}`;
}
return value;
});
if (additional) {
each(additional, (val, key) => {
if (val) targetEnvs[key] = val;
});
}
console.log(`Adding ENVs to ${targetApp}...`);
return await heroku.patch(`/apps/${targetApp}/config-vars`, {body: targetEnvs});
}
async function createApp(
heroku: Heroku,
appName: string,
sourceApp: HerokuPlatformApiApp,
pipelineId: string,
pipelineStage: string,
appJson: AppJson,
additionalEnvs: HerokuPlatformApiConfigVars,
) {
const newAppRequest = {
name: appName,
region: sourceApp.region?.name,
stack: sourceApp.stack?.name,
team: sourceApp.team?.name,
};
console.log(`Creating new app ${appName}...`);
const createPath = newAppRequest.team ? '/teams/apps' : '/apps';
const newApp = await heroku.post<HerokuPlatformApiApp>(createPath, {body: newAppRequest});
console.log('Created App.', newApp.id);
try {
const pipelineCoupling = {
app: appName,
pipeline: pipelineId,
stage: pipelineStage,
};
console.log(`Putting ${appName} in pipeline ${pipelineId}, stage ${pipelineStage}...`);
await heroku.post('/pipeline-couplings', {body: pipelineCoupling});
await createAddons(heroku, appName, appJson.addons);
await addEnvs(heroku, sourceApp.name!, appName, appJson.env, additionalEnvs);
} catch (e) {
await heroku.delete(`/apps/${appName}`);
throw e;
}
return newApp;
}
async function run() {
const appName = core.getInput('app');
const envFrom = core.getInput('env_from');
const pipelineId = core.getInput('pipeline');
const herokuApiToken = core.getInput('heroku_api_token');
const appJsonPath = core.getInput('app_json');
const pipelineStage = core.getInput('stage');
const prNumber = core.getInput('pr_number');
const herokuBranch = github.context.ref;
const appJson: AppJson = JSON.parse(fs.readFileSync(appJsonPath).toString());
const heroku = new Heroku({token: herokuApiToken});
const apps = await heroku.get<HerokuPlatformApiApp[]>('/apps');
const sourceApp = apps.find(({name}) => name === envFrom);
const existingApp = apps.find(({name}) => name === appName);
if (existingApp) {
console.log(`App exists: ${existingApp.id}`);
} else if (sourceApp) {
const newApp = await createApp(heroku, appName, sourceApp, pipelineId, pipelineStage, appJson, {
HEROKU_PR_NUMBER: prNumber,
HEROKU_APP_NAME: appName,
HEROKU_BRANCH: herokuBranch,
});
}
}
try {
run().catch((e) => {
console.error(e);
core.setFailed(e.message);
});
} catch (e) {
console.error(e);
core.setFailed(e.message);
}