fix(app/docker): correctly parse input variables (#852)

This commit is contained in:
Simon Lecoq
2022-02-09 01:24:54 +01:00
committed by GitHub
parent 4983bad99d
commit 58ff5eee45
3 changed files with 35 additions and 15 deletions

View File

@@ -2,7 +2,7 @@
## 0 Prepare your machine ## 0 Prepare your machine
A server with a recent version of [docker](https://www.docker.com/) is required. A machine with a recent version of [docker](https://www.docker.com/) is required.
## 1 Run docker image ## 1 Run docker image
@@ -12,3 +12,7 @@ docker run --env INPUT_TOKEN=**** --env INPUT_USER=user --volume=/tmp:/renders g
``` ```
To pass parameters, pass environment variable with the same name as the corresponding action option but in uppercase and prefixed with `INPUT_`. To pass parameters, pass environment variable with the same name as the corresponding action option but in uppercase and prefixed with `INPUT_`.
Generated files will be created in the mounted `/renders` directory.
> 💡 When running *metrics* with docker, [`output_action`](/source/plugins/core/README.md#-configuring-output-action) will automatically default to `none` instead. To use a different output action, both `GITHUB_REPOSITORY` (notice the absence of `INPUT_` prefix) and `INPUT_COMMITTER_TOKEN` (with sufficient permissions) environment variables must be set.

View File

@@ -94,6 +94,14 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
info("Setup", "complete") info("Setup", "complete")
info("Version", conf.package.version) info("Version", conf.package.version)
//Docker run environment default values
if (!metadata.env.ghactions) {
info("Docker environment", "(enabled)")
process.env.INPUT_OUTPUT_ACTION = process.env.INPUT_OUTPUT_ACTION ?? "none"
process.env.INPUT_COMMITTER_TOKEN = process.env.INPUT_COMMITTER_TOKEN ?? process.env.INPUT_TOKEN
process.env.GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY ?? "octocat/hello-world"
}
//Core inputs //Core inputs
Object.assign(preset, await presets(core.getInput("config_presets"), {log:false, core})) Object.assign(preset, await presets(core.getInput("config_presets"), {log:false, core}))
const { const {
@@ -150,7 +158,7 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
//See https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats //See https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats
info("GitHub token format", /^gh[pousr]_/.test(token) ? "correct" : "(old or invalid)") info("GitHub token format", /^gh[pousr]_/.test(token) ? "correct" : "(old or invalid)")
if (!token) if (!token)
throw new Error("You must provide a valid GitHub personal token to gather your metrics (see https://github.com/lowlighter/metrics/blob/master/.github/readme/partials/setup/action/setup.md for more informations)") throw new Error("You must provide a valid GitHub personal token to gather your metrics (see https://github.com/lowlighter/metrics/blob/master/.github/readme/partials/documentation/setup/action.md for more informations)")
conf.settings.token = token conf.settings.token = token
const api = {} const api = {}
api.graphql = octokit.graphql.defaults({headers:{authorization:`token ${token}`}}) api.graphql = octokit.graphql.defaults({headers:{authorization:`token ${token}`}})
@@ -167,7 +175,7 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
const {headers} = await api.rest.request("HEAD /") const {headers} = await api.rest.request("HEAD /")
if (!("x-oauth-scopes" in headers)) { if (!("x-oauth-scopes" in headers)) {
throw new Error( throw new Error(
'GitHub API did not send any "x-oauth-scopes" header back from provided "token". It means that your token may not be valid or you\'re using GITHUB_TOKEN which cannot be used since metrics will fetch data outside of this repository scope. Use a personal access token instead (see https://github.com/lowlighter/metrics/blob/master/.github/readme/partials/setup/action/setup.md for more informations).', 'GitHub API did not send any "x-oauth-scopes" header back from provided "token". It means that your token may not be valid or you\'re using GITHUB_TOKEN which cannot be used since metrics will fetch data outside of this repository scope. Use a personal access token instead (see https://github.com/lowlighter/metrics/blob/master/.github/readme/partials/documentation/setup/action.md for more informations).',
) )
} }
info("Token validity", "seems ok") info("Token validity", "seems ok")
@@ -190,11 +198,12 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
info("GitHub repository", `${user}/${q.repo}`) info("GitHub repository", `${user}/${q.repo}`)
//Current repository //Current repository
if (metadata.env.ghactions)
info("Current repository", `${github.context.repo.owner}/${github.context.repo.repo}`) info("Current repository", `${github.context.repo.owner}/${github.context.repo.repo}`)
//Committer //Committer
const committer = {} const committer = {}
if (!dryrun) { if ((!dryrun)&&(_action !== "none")) {
//Compute committer informations //Compute committer informations
committer.token = _token || token committer.token = _token || token
committer.gist = _action === "gist" ? _gist : null committer.gist = _action === "gist" ? _gist : null
@@ -236,7 +245,6 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
} }
else else
throw error throw error
} }
//Retrieve previous render SHA to be able to update file content through API //Retrieve previous render SHA to be able to update file content through API
committer.sha = null committer.sha = null
@@ -258,7 +266,7 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
} }
info("Previous render sha", committer.sha ?? "(none)") info("Previous render sha", committer.sha ?? "(none)")
} }
else else if (dryrun)
info("Dry-run", true) info("Dry-run", true)

View File

@@ -12,6 +12,9 @@ const categories = ["core", "github", "social", "community"]
//Previous descriptors //Previous descriptors
let previous = null let previous = null
//Environment
const env = {ghactions:`${process.env.GITHUB_ACTIONS}` === "true"}
/**Metadata descriptor parser */ /**Metadata descriptor parser */
export default async function metadata({log = true, diff = false} = {}) { export default async function metadata({log = true, diff = false} = {}) {
//Paths //Paths
@@ -81,7 +84,7 @@ export default async function metadata({log = true, diff = false} = {}) {
const descriptor = yaml.load(`${await fs.promises.readFile(__descriptor, "utf-8")}`) const descriptor = yaml.load(`${await fs.promises.readFile(__descriptor, "utf-8")}`)
//Metadata //Metadata
return {plugins:Plugins, templates:Templates, packaged, descriptor} return {plugins:Plugins, templates:Templates, packaged, descriptor, env}
} }
/**Metadata extractor for inputs */ /**Metadata extractor for inputs */
@@ -244,7 +247,9 @@ metadata.plugin = async function({__plugins, __templates, name, logger}) {
const q = {} const q = {}
for (const key of Object.keys(inputs)) { for (const key of Object.keys(inputs)) {
//Parse input //Parse input
let value = `${core.getInput(key)}`.trim() let value
if (env.ghactions) {
value = `${core.getInput(key)}`.trim()
try { try {
value = decodeURIComponent(value) value = decodeURIComponent(value)
} }
@@ -252,6 +257,9 @@ metadata.plugin = async function({__plugins, __templates, name, logger}) {
logger(`metrics/inputs > failed to decode uri for ${key}: ${value}`) logger(`metrics/inputs > failed to decode uri for ${key}: ${value}`)
value = "<default-value>" value = "<default-value>"
} }
}
else
value = process.env[`INPUT_${key.toUpperCase()}`]?.trim() ?? "<default-value>"
const unspecified = value === "<default-value>" const unspecified = value === "<default-value>"
//From presets //From presets
if ((key in preset) && (unspecified)) { if ((key in preset) && (unspecified)) {