-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvariables.ts
196 lines (181 loc) · 6.17 KB
/
variables.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
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
import {data, DescriptionDictionary} from "@actions/expressions";
import {Pair} from "@actions/expressions/data/expressiondata";
import {StringData} from "@actions/expressions/data/index";
import {WorkflowContext} from "@actions/languageservice/context/workflow-context";
import {log, warn} from "@actions/languageservice/log";
import {isMapping, isString} from "@actions/workflow-parser";
import {Octokit} from "@octokit/rest";
import {RequestError} from "@octokit/request-error";
import {RepositoryContext} from "../initializationOptions";
import {TTLCache} from "../utils/cache";
import {errorStatus} from "../utils/error";
import {getRepoPermission} from "../utils/repo-permission";
export async function getVariables(
workflowContext: WorkflowContext,
octokit: Octokit,
cache: TTLCache,
repo: RepositoryContext,
defaultContext: DescriptionDictionary | undefined
): Promise<DescriptionDictionary | undefined> {
const permission = await getRepoPermission(octokit, cache, repo);
if (permission === "none") {
const secretsContext = defaultContext || new DescriptionDictionary();
secretsContext.complete = false;
return secretsContext;
}
let environmentName: string | undefined;
if (workflowContext?.job?.environment) {
if (isString(workflowContext.job.environment)) {
environmentName = workflowContext.job.environment.value;
} else if (isMapping(workflowContext.job.environment)) {
for (const x of workflowContext.job.environment) {
if (isString(x.key) && x.key.value === "name") {
if (isString(x.value)) {
environmentName = x.value.value;
}
break;
}
}
}
}
const variablesContext = defaultContext || new DescriptionDictionary();
try {
const variables = await getRemoteVariables(octokit, cache, repo, environmentName);
// Build combined map of variables
const variablesMap = new Map<
string,
{
key: string;
value: data.StringData;
description?: string;
}
>();
variables.organizationVariables.forEach(variable =>
variablesMap.set(variable.key.toLowerCase(), {
key: variable.key,
value: new data.StringData(variable.value.coerceString()),
description: `${variable.value.coerceString()} - Organization variable`
})
);
// Override org variables with repo variables
variables.repoVariables.forEach(variable =>
variablesMap.set(variable.key.toLowerCase(), {
key: variable.key,
value: new data.StringData(variable.value.coerceString()),
description: `${variable.value.coerceString()} - Repository variable`
})
);
// Override repo variables with environment veriables (if defined)
variables.environmentVariables.forEach(variable =>
variablesMap.set(variable.key.toLowerCase(), {
key: variable.key,
value: new data.StringData(variable.value.coerceString()),
description: `${variable.value.coerceString()} - Variable for environment \`${environmentName || ""}\``
})
);
// Sort variables by key and add to context
Array.from(variablesMap.values())
.sort((a, b) => a.key.localeCompare(b.key))
.forEach(variable => variablesContext?.add(variable.key, variable.value, variable.description));
return variablesContext;
} catch (e) {
if (!(e instanceof RequestError)) throw e;
if (e.name == "HttpError" && e.status == 404) {
log("Failure to request variables. Ignore if you're using GitHub Enterprise Server below version 3.8");
return variablesContext;
} else throw e;
}
}
export async function getRemoteVariables(
octokit: Octokit,
cache: TTLCache,
repo: RepositoryContext,
environmentName?: string
): Promise<{
repoVariables: Pair[];
environmentVariables: Pair[];
organizationVariables: Pair[];
}> {
// Repo variables
return {
repoVariables: await cache.get(`${repo.owner}/${repo.name}/vars`, undefined, () =>
fetchVariables(octokit, repo.owner, repo.name)
),
environmentVariables:
(environmentName &&
(await cache.get(`${repo.owner}/${repo.name}/vars/environment/${environmentName}`, undefined, () =>
fetchEnvironmentVariables(octokit, repo.id, environmentName)
))) ||
[],
organizationVariables: await cache.get(`${repo.owner}/vars`, undefined, () =>
fetchOrganizationVariables(octokit, repo)
)
};
}
async function fetchVariables(octokit: Octokit, owner: string, name: string): Promise<Pair[]> {
try {
return await octokit.paginate(
octokit.actions.listRepoVariables,
{
owner: owner,
repo: name,
per_page: 100
},
response =>
response.data.map(variable => {
return {key: variable.name, value: new StringData(variable.value)};
})
);
} catch (e) {
console.log("Failure to retrieve variables: ", e);
throw e;
}
}
async function fetchEnvironmentVariables(
octokit: Octokit,
repositoryId: number,
environmentName: string
): Promise<Pair[]> {
try {
return await octokit.paginate(
octokit.actions.listEnvironmentVariables,
{
repository_id: repositoryId,
environment_name: environmentName,
per_page: 100
},
response =>
response.data.map(variable => {
return {key: variable.name, value: new StringData(variable.value)};
})
);
} catch (e) {
if (errorStatus(e) === 404) {
warn(`Environment ${environmentName} not found`);
return [];
}
console.log("Failure to retrieve environment variables: ", e);
throw e;
}
}
async function fetchOrganizationVariables(octokit: Octokit, repo: RepositoryContext): Promise<Pair[]> {
if (!repo.organizationOwned) {
return [];
}
try {
const variables: {name: string; value: string}[] = await octokit.paginate(
"GET /repos/{owner}/{repo}/actions/organization-variables",
{
owner: repo.owner,
repo: repo.name,
per_page: 100
}
);
return variables.map(variable => {
return {key: variable.name, value: new StringData(variable.value)};
});
} catch (e) {
console.log("Failure to retrieve organization variables: ", e);
throw e;
}
}