Merge pull request #73 from github/cschleiden/support-env-secrets
Support environment secrets
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {data} from "@github/actions-expressions";
|
||||
import {ContextProviderConfig} from "@github/actions-languageservice";
|
||||
import {WorkflowContext} from "@github/actions-languageservice/context/workflow-context";
|
||||
import {isMapping, isString} from "@github/actions-workflow-parser";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {getSecrets} from "./context-providers/secrets";
|
||||
import {getStepsContext} from "./context-providers/steps";
|
||||
@@ -27,7 +28,23 @@ export function contextProviders(
|
||||
) => {
|
||||
switch (name) {
|
||||
case "secrets": {
|
||||
const secrets = await getSecrets(octokit, cache, repo.owner, repo.name);
|
||||
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 secrets = await getSecrets(octokit, cache, repo, environmentName);
|
||||
|
||||
defaultContext = defaultContext || new data.Dictionary();
|
||||
secrets.forEach(secret => defaultContext!.add(secret.value, new data.StringData("***")));
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
import {StringData} from "@github/actions-expressions/data/string";
|
||||
import {Octokit} from "@octokit/rest";
|
||||
import {RepositoryContext} from "../initializationOptions";
|
||||
import {TTLCache} from "../utils/cache";
|
||||
|
||||
export async function getSecrets(
|
||||
octokit: Octokit,
|
||||
cache: TTLCache,
|
||||
owner: string,
|
||||
name: string
|
||||
repo: RepositoryContext,
|
||||
environmentName?: string
|
||||
): Promise<StringData[]> {
|
||||
const repoSecrets = await cache.get(`${owner}/${name}/secrets`, undefined, () => fetchSecrets(octokit, owner, name));
|
||||
const secrets: StringData[] = [];
|
||||
|
||||
return repoSecrets;
|
||||
// Repo secrets
|
||||
const repoSecrets = await cache.get(`${repo.owner}/${repo.name}/secrets`, undefined, () =>
|
||||
fetchSecrets(octokit, repo.owner, repo.name)
|
||||
);
|
||||
|
||||
secrets.push(...repoSecrets);
|
||||
|
||||
// Environment secrets
|
||||
if (environmentName) {
|
||||
const envSecrets = await cache.get(
|
||||
`${repo.owner}/${repo.name}/secrets/environment/${environmentName}`,
|
||||
undefined,
|
||||
() => fetchEnvironmentSecrets(octokit, repo.id, environmentName)
|
||||
);
|
||||
|
||||
secrets.push(...envSecrets);
|
||||
}
|
||||
|
||||
return secrets.sort();
|
||||
}
|
||||
|
||||
async function fetchSecrets(octokit: Octokit, owner: string, name: string): Promise<StringData[]> {
|
||||
@@ -27,3 +46,22 @@ async function fetchSecrets(octokit: Octokit, owner: string, name: string): Prom
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
async function fetchEnvironmentSecrets(
|
||||
octokit: Octokit,
|
||||
repositoryId: number,
|
||||
environmentName: string
|
||||
): Promise<StringData[]> {
|
||||
try {
|
||||
const response = await octokit.actions.listEnvironmentSecrets({
|
||||
repository_id: repositoryId,
|
||||
environment_name: environmentName
|
||||
});
|
||||
|
||||
return response.data.secrets.map(secret => new StringData(secret.name));
|
||||
} catch (e) {
|
||||
console.log("Failure to retrieve environment secrets: ", e);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -47,12 +47,18 @@ export async function getContext(
|
||||
|
||||
function getDefaultContext(name: string, workflowContext: WorkflowContext, mode: Mode): ContextValue | undefined {
|
||||
switch (name) {
|
||||
case "env":
|
||||
return getEnvContext(workflowContext);
|
||||
|
||||
case "github":
|
||||
return getGithubContext(workflowContext);
|
||||
|
||||
case "inputs":
|
||||
return getInputsContext(workflowContext);
|
||||
|
||||
case "job":
|
||||
return getJobContext(workflowContext);
|
||||
|
||||
case "matrix":
|
||||
return getMatrixContext(workflowContext, mode);
|
||||
|
||||
@@ -76,12 +82,6 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode:
|
||||
|
||||
case "strategy":
|
||||
return getStrategyContext(workflowContext);
|
||||
|
||||
case "job":
|
||||
return getJobContext(workflowContext);
|
||||
|
||||
case "env":
|
||||
return getEnvContext(workflowContext);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
Generated
+6
-6
@@ -702,9 +702,9 @@
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@github/actions-workflow-parser": {
|
||||
"version": "0.0.41",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.41/234e6042ee06199b4314589ad77dfd7fe0debe6d",
|
||||
"integrity": "sha512-s27gUhNH80YhFmkcQ5GE5BA5D15xbQ48YjwKCwYKN+ndldaOhpqa+v5Q+NfZlNwBmJ/8lJZob09/IiGRgFq7ag==",
|
||||
"version": "0.0.42",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.42/e511c44db93c2c8b522e48ca54fe733063902dd3",
|
||||
"integrity": "sha512-Qd4vmYjNdFxHBer0P3RympLnBqDCigRqrxvMFRFEfLhUQ1oeBHmmvF6sCEgQ+JtKFlwVPvOoJLmfN/r8o5NEBQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@github/actions-expressions": "*",
|
||||
@@ -13565,9 +13565,9 @@
|
||||
}
|
||||
},
|
||||
"@github/actions-workflow-parser": {
|
||||
"version": "0.0.41",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.41/234e6042ee06199b4314589ad77dfd7fe0debe6d",
|
||||
"integrity": "sha512-s27gUhNH80YhFmkcQ5GE5BA5D15xbQ48YjwKCwYKN+ndldaOhpqa+v5Q+NfZlNwBmJ/8lJZob09/IiGRgFq7ag==",
|
||||
"version": "0.0.42",
|
||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.42/e511c44db93c2c8b522e48ca54fe733063902dd3",
|
||||
"integrity": "sha512-Qd4vmYjNdFxHBer0P3RympLnBqDCigRqrxvMFRFEfLhUQ1oeBHmmvF6sCEgQ+JtKFlwVPvOoJLmfN/r8o5NEBQ==",
|
||||
"requires": {
|
||||
"@github/actions-expressions": "*",
|
||||
"yaml": "^2.0.0-8"
|
||||
|
||||
Reference in New Issue
Block a user