generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvSetup.js
68 lines (58 loc) · 1.84 KB
/
envSetup.js
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
const core = require('@actions/core');
const github = require('@actions/github');
function requireNonNull(toBeNotNull, message) {
if (!toBeNotNull) throw Error(message);
return toBeNotNull;
}
function getVersionAndPublishChannel(githubRef) {
const [, , channel, version] = githubRef.split('/');
if (!(channel && version)) {
throw Error(`Expected the branch name ${githubRef} to match pattern refs/head/<releaseChannel>/<versionNumber>`)
}
return {
channel,
version: `${version}.beta.${new Date().valueOf().toString(32)}`
};
}
function getReleaseNotes() {
if (github.context.eventName === 'push') {
const pushPayload = github.context.payload
const commits = pushPayload.commits || []
return commits
.map(commit => `- ${commit.message}`)
.join('\n');
}
return `- No release notes`
}
async function setUpNonProd() {
const githubRef = requireNonNull(
core.getInput('branch-override') || process.env.GITHUB_REF,
'Expected environment GITHUB_REF to be defined!'
);
const {
version,
channel
} = getVersionAndPublishChannel(githubRef)
core.info(`Releasing with version '${version}'`)
core.info(`And on channel '${channel}'`)
const releaseNotes = getReleaseNotes()
core.info(`With release notes
${releaseNotes}`)
core.exportVariable('VERSION', version);
core.exportVariable('PUBLISH_CHANNEL', channel);
core.exportVariable('RELEASE_NOTES', releaseNotes.split('\n').join("<br/>"))
}
function setUpProd() {
throw Error('Production environment setup not available yet!');
}
const envSetup = async environmentToSetUp => {
switch (environmentToSetUp) {
case 'non-prod':
return setUpNonProd();
case 'prod':
return setUpProd();
default:
throw Error(`Unknown environment ${environmentToSetUp}, expecting 'non-prod' or 'prod'`)
}
};
module.exports = envSetup;