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 {data} from "@github/actions-expressions";
|
||||||
import {ContextProviderConfig} from "@github/actions-languageservice";
|
import {ContextProviderConfig} from "@github/actions-languageservice";
|
||||||
import {WorkflowContext} from "@github/actions-languageservice/context/workflow-context";
|
import {WorkflowContext} from "@github/actions-languageservice/context/workflow-context";
|
||||||
|
import {isMapping, isString} from "@github/actions-workflow-parser";
|
||||||
import {Octokit} from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
import {getSecrets} from "./context-providers/secrets";
|
import {getSecrets} from "./context-providers/secrets";
|
||||||
import {getStepsContext} from "./context-providers/steps";
|
import {getStepsContext} from "./context-providers/steps";
|
||||||
@@ -27,7 +28,23 @@ export function contextProviders(
|
|||||||
) => {
|
) => {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "secrets": {
|
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();
|
defaultContext = defaultContext || new data.Dictionary();
|
||||||
secrets.forEach(secret => defaultContext!.add(secret.value, new data.StringData("***")));
|
secrets.forEach(secret => defaultContext!.add(secret.value, new data.StringData("***")));
|
||||||
|
|||||||
@@ -1,16 +1,35 @@
|
|||||||
import {StringData} from "@github/actions-expressions/data/string";
|
import {StringData} from "@github/actions-expressions/data/string";
|
||||||
import {Octokit} from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
|
import {RepositoryContext} from "../initializationOptions";
|
||||||
import {TTLCache} from "../utils/cache";
|
import {TTLCache} from "../utils/cache";
|
||||||
|
|
||||||
export async function getSecrets(
|
export async function getSecrets(
|
||||||
octokit: Octokit,
|
octokit: Octokit,
|
||||||
cache: TTLCache,
|
cache: TTLCache,
|
||||||
owner: string,
|
repo: RepositoryContext,
|
||||||
name: string
|
environmentName?: string
|
||||||
): Promise<StringData[]> {
|
): 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[]> {
|
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 [];
|
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 {
|
function getDefaultContext(name: string, workflowContext: WorkflowContext, mode: Mode): ContextValue | undefined {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
|
case "env":
|
||||||
|
return getEnvContext(workflowContext);
|
||||||
|
|
||||||
case "github":
|
case "github":
|
||||||
return getGithubContext(workflowContext);
|
return getGithubContext(workflowContext);
|
||||||
|
|
||||||
case "inputs":
|
case "inputs":
|
||||||
return getInputsContext(workflowContext);
|
return getInputsContext(workflowContext);
|
||||||
|
|
||||||
|
case "job":
|
||||||
|
return getJobContext(workflowContext);
|
||||||
|
|
||||||
case "matrix":
|
case "matrix":
|
||||||
return getMatrixContext(workflowContext, mode);
|
return getMatrixContext(workflowContext, mode);
|
||||||
|
|
||||||
@@ -76,12 +82,6 @@ function getDefaultContext(name: string, workflowContext: WorkflowContext, mode:
|
|||||||
|
|
||||||
case "strategy":
|
case "strategy":
|
||||||
return getStrategyContext(workflowContext);
|
return getStrategyContext(workflowContext);
|
||||||
|
|
||||||
case "job":
|
|
||||||
return getJobContext(workflowContext);
|
|
||||||
|
|
||||||
case "env":
|
|
||||||
return getEnvContext(workflowContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Generated
+6
-6
@@ -702,9 +702,9 @@
|
|||||||
"link": true
|
"link": true
|
||||||
},
|
},
|
||||||
"node_modules/@github/actions-workflow-parser": {
|
"node_modules/@github/actions-workflow-parser": {
|
||||||
"version": "0.0.41",
|
"version": "0.0.42",
|
||||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.41/234e6042ee06199b4314589ad77dfd7fe0debe6d",
|
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.42/e511c44db93c2c8b522e48ca54fe733063902dd3",
|
||||||
"integrity": "sha512-s27gUhNH80YhFmkcQ5GE5BA5D15xbQ48YjwKCwYKN+ndldaOhpqa+v5Q+NfZlNwBmJ/8lJZob09/IiGRgFq7ag==",
|
"integrity": "sha512-Qd4vmYjNdFxHBer0P3RympLnBqDCigRqrxvMFRFEfLhUQ1oeBHmmvF6sCEgQ+JtKFlwVPvOoJLmfN/r8o5NEBQ==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@github/actions-expressions": "*",
|
"@github/actions-expressions": "*",
|
||||||
@@ -13565,9 +13565,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@github/actions-workflow-parser": {
|
"@github/actions-workflow-parser": {
|
||||||
"version": "0.0.41",
|
"version": "0.0.42",
|
||||||
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.41/234e6042ee06199b4314589ad77dfd7fe0debe6d",
|
"resolved": "https://npm.pkg.github.com/download/@github/actions-workflow-parser/0.0.42/e511c44db93c2c8b522e48ca54fe733063902dd3",
|
||||||
"integrity": "sha512-s27gUhNH80YhFmkcQ5GE5BA5D15xbQ48YjwKCwYKN+ndldaOhpqa+v5Q+NfZlNwBmJ/8lJZob09/IiGRgFq7ag==",
|
"integrity": "sha512-Qd4vmYjNdFxHBer0P3RympLnBqDCigRqrxvMFRFEfLhUQ1oeBHmmvF6sCEgQ+JtKFlwVPvOoJLmfN/r8o5NEBQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@github/actions-expressions": "*",
|
"@github/actions-expressions": "*",
|
||||||
"yaml": "^2.0.0-8"
|
"yaml": "^2.0.0-8"
|
||||||
|
|||||||
Reference in New Issue
Block a user