Support shared organization secrets and variables

This commit is contained in:
Josh Gross
2023-01-30 16:37:08 -05:00
parent 7e4b75f6cd
commit e6caab1eb9
2 changed files with 61 additions and 2 deletions
@@ -42,6 +42,15 @@ export async function getVariables(
}
>();
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,
@@ -77,6 +86,7 @@ export async function getRemoteVariables(
): Promise<{
repoVariables: Pair[];
environmentVariables: Pair[];
organizationVariables: Pair[];
}> {
// Repo variables
return {
@@ -88,7 +98,10 @@ export async function getRemoteVariables(
(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)
)
};
}
@@ -137,3 +150,23 @@ async function fetchEnvironmentVariables(
return [];
}
async function fetchOrganizationVariables(octokit: Octokit, repo: RepositoryContext): Promise<Pair[]> {
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);
}
return [];
}