-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
252 lines (211 loc) · 8.1 KB
/
index.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Franz Diebold
const core = require('@actions/core');
const github = require('@actions/github');
/**
* Slugify a given string to a maximum length.
* @param {string} inputString
* @param {int} maxLength
* @return {string} The slugified string.
*/
function slugify(inputString, maxLength = 63) {
return inputString
.toLowerCase()
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
.replace(/^\s+|\s+$/g, '') // trim
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-') // collapse dashes
.slice(0, maxLength) // truncate to maximum length
.replace(/[-]+$/g, ''); // trim trailing -
}
/**
* Get the repository owner from the repository string.
* @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null;
}
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null;
}
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
// https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables
try {
// i.e. FranzDiebold/github-env-vars-action
const repository = process.env.GITHUB_REPOSITORY;
if (repository) {
core.exportVariable('CI_REPOSITORY_SLUG', slugify(repository));
core.info(`Set CI_REPOSITORY_SLUG=` +
`${process.env.CI_REPOSITORY_SLUG}`);
} else {
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "CI_REPOSITORY_SLUG".');
}
const repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('CI_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set CI_REPOSITORY_OWNER=` +
`${process.env.CI_REPOSITORY_OWNER}`);
core.exportVariable('CI_REPOSITORY_OWNER_SLUG',
slugify(repositoryOwner));
core.info(`Set CI_REPOSITORY_OWNER_SLUG=` +
`${process.env.CI_REPOSITORY_OWNER_SLUG}`);
} else {
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "CI_REPOSITORY_OWNER" and ' +
'"CI_REPOSITORY_OWNER_SLUG".');
}
const repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('CI_REPOSITORY_NAME', repositoryName);
core.info(`Set CI_REPOSITORY_NAME=` +
`${process.env.CI_REPOSITORY_NAME}`);
core.exportVariable('CI_REPOSITORY_NAME_SLUG',
slugify(repositoryName));
core.info(`Set CI_REPOSITORY_NAME_SLUG=` +
`${process.env.CI_REPOSITORY_NAME_SLUG}`);
} else {
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "CI_REPOSITORY_NAME" and ' +
'"CI_REPOSITORY_NAME_SLUG".');
}
core.exportVariable('CI_REPOSITORY', repository);
core.info(`Set CI_REPOSITORY=${process.env.CI_REPOSITORY}`);
// i.e. refs/heads/feat/feature-branch-1
const ref = process.env.GITHUB_REF;
if (ref) {
core.exportVariable('CI_REF_SLUG', slugify(ref));
core.info(`Set CI_REF_SLUG=${process.env.CI_REF_SLUG}`);
} else {
core.info('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "CI_REF_SLUG".');
}
const refName = getRefName(ref);
if (refName) {
core.exportVariable('CI_REF_NAME', refName);
core.info(`Set CI_REF_NAME=${process.env.CI_REF_NAME}`);
core.exportVariable('CI_REF_NAME_SLUG', slugify(refName));
core.info(`Set CI_REF_NAME_SLUG=${process.env.CI_REF_NAME_SLUG}`);
} else {
core.info('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "CI_REF_NAME" and ' +
'"CI_REF_NAME_SLUG".');
}
core.exportVariable('CI_REF', ref);
core.info(`Set CI_REF=${process.env.CI_REF}`);
const headRef = process.env.GITHUB_HEAD_REF;
const branchName = headRef || refName;
if (branchName) {
core.exportVariable('CI_ACTION_REF_NAME', branchName);
core.info(`Set CI_ACTION_REF_NAME=${process.env.CI_ACTION_REF_NAME}`);
core.exportVariable('CI_ACTION_REF_NAME_SLUG', slugify(branchName));
core.info('Set CI_ACTION_REF_NAME_SLUG=' +
`${process.env.CI_ACTION_REF_NAME_SLUG}`);
} else {
core.info('Environment variables "GITHUB_REF" and ' +
'"GITHUB_HEAD_REF" not set. ' +
'Cannot set "CI_ACTION_REF_NAME" and ' +
'"CI_ACTION_REF_NAME_SLUG".');
}
if (headRef) {
core.exportVariable('CI_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set CI_HEAD_REF_SLUG=${process.env.CI_HEAD_REF_SLUG}`);
} else {
core.info('Environment variable "GITHUB_HEAD_REF" not set. ' +
'Cannot set "CI_HEAD_REF_SLUG".');
}
core.exportVariable('CI_HEAD_REF', headRef);
core.info(`Set CI_HEAD_REF=${process.env.CI_HEAD_REF}`);
const baseRef = process.env.GITHUB_BASE_REF;
if (baseRef) {
core.exportVariable('CI_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set CI_BASE_REF_SLUG=${process.env.CI_BASE_REF_SLUG}`);
} else {
core.info('Environment variable "GITHUB_BASE_REF" not set. ' +
'Cannot set "CI_BASE_REF_SLUG".');
}
core.exportVariable('CI_BASE_REF', baseRef);
core.info(`Set CI_BASE_REF=${process.env.CI_BASE_REF}`);
// i.e. ffac537e6cbbf934b08745a378932722df287a53
const sha = process.env.GITHUB_SHA;
if (sha) {
core.exportVariable('CI_SHA_SHORT', getShaShort(sha));
core.info(`Set CI_SHA_SHORT=${process.env.CI_SHA_SHORT}`);
} else {
core.info('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "CI_SHA_SHORT".');
}
core.exportVariable('CI_SHA', sha);
core.info(`Set CI_SHA=${process.env.CI_SHA}`);
const pullRequest = github.context.payload &&
github.context.payload.pull_request;
if (pullRequest) {
const prSha = pullRequest.head.sha;
core.exportVariable('CI_PR_SHA_SHORT', getShaShort(prSha));
core.info(`Set CI_PR_SHA_SHORT=${process.env.CI_PR_SHA_SHORT}`);
core.exportVariable('CI_PR_SHA', prSha);
core.info(`Set CI_PR_SHA=${process.env.CI_PR_SHA}`);
const prNumber = pullRequest.number;
core.exportVariable('CI_PR_NUMBER', prNumber);
core.info(`Set CI_PR_NUMBER=${process.env.CI_PR_NUMBER}`);
core.exportVariable('CI_PR_ID', prNumber);
core.info(`Set CI_PR_ID=${process.env.CI_PR_ID}`);
const prTitle = pullRequest.title;
core.exportVariable('CI_PR_TITLE', prTitle);
core.info(`Set CI_PR_TITLE=${process.env.CI_PR_TITLE}`);
const prDescription = pullRequest.body;
core.exportVariable('CI_PR_DESCRIPTION', prDescription);
core.info(`Set CI_PR_DESCRIPTION=${process.env.CI_PR_DESCRIPTION}`);
} else {
core.info('No pull request. ' +
'Cannot set "CI_PR_SHA_SHORT", "CI_PR_SHA", "CI_PR_NUMBER", ' +
'"CI_PR_ID", "CI_PR_TITLE" and "CI_PR_DESCRIPTION".');
}
const actor = process.env.GITHUB_ACTOR;
core.exportVariable('CI_ACTOR', actor);
core.info(`Set CI_ACTOR=${process.env.CI_ACTOR}`);
const eventName = process.env.GITHUB_EVENT_NAME;
core.exportVariable('CI_EVENT_NAME', eventName);
core.info(`Set CI_EVENT_NAME=${process.env.CI_EVENT_NAME}`);
const runId = process.env.GITHUB_RUN_ID;
core.exportVariable('CI_RUN_ID', runId);
core.info(`Set CI_RUN_ID=${process.env.CI_RUN_ID}`);
const runNumber = process.env.GITHUB_RUN_NUMBER;
core.exportVariable('CI_RUN_NUMBER', runNumber);
core.info(`Set CI_RUN_NUMBER=${process.env.CI_RUN_NUMBER}`);
const workflow = process.env.GITHUB_WORKFLOW;
core.exportVariable('CI_WORKFLOW', workflow);
core.info(`Set CI_WORKFLOW=${process.env.CI_WORKFLOW}`);
const action = process.env.GITHUB_ACTION;
core.exportVariable('CI_ACTION', action);
core.info(`Set CI_ACTION=${process.env.CI_ACTION}`);
} catch (error) {
core.setFailed(error.message);
}
module.exports = {
slugify,
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};