Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/readme/partials/documentation/setup/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 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

Expand All @@ -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_`.

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.
20 changes: 14 additions & 6 deletions source/app/action/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
info("Setup", "complete")
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
Object.assign(preset, await presets(core.getInput("config_presets"), {log:false, core}))
const {
Expand Down Expand Up @@ -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
info("GitHub token format", /^gh[pousr]_/.test(token) ? "correct" : "(old or invalid)")
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
const api = {}
api.graphql = octokit.graphql.defaults({headers:{authorization:`token ${token}`}})
Expand All @@ -167,7 +175,7 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
const {headers} = await api.rest.request("HEAD /")
if (!("x-oauth-scopes" in headers)) {
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")
Expand All @@ -190,11 +198,12 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
info("GitHub repository", `${user}/${q.repo}`)

//Current repository
info("Current repository", `${github.context.repo.owner}/${github.context.repo.repo}`)
if (metadata.env.ghactions)
info("Current repository", `${github.context.repo.owner}/${github.context.repo.repo}`)

//Committer
const committer = {}
if (!dryrun) {
if ((!dryrun)&&(_action !== "none")) {
//Compute committer informations
committer.token = _token || token
committer.gist = _action === "gist" ? _gist : null
Expand Down Expand Up @@ -236,7 +245,6 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
}
else
throw error

}
//Retrieve previous render SHA to be able to update file content through API
committer.sha = null
Expand All @@ -258,7 +266,7 @@ async function retry(func, {retries = 1, delay = 0} = {}) {
}
info("Previous render sha", committer.sha ?? "(none)")
}
else
else if (dryrun)
info("Dry-run", true)


Expand Down
24 changes: 16 additions & 8 deletions source/app/metrics/metadata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const categories = ["core", "github", "social", "community"]
//Previous descriptors
let previous = null

//Environment
const env = {ghactions:`${process.env.GITHUB_ACTIONS}` === "true"}

/**Metadata descriptor parser */
export default async function metadata({log = true, diff = false} = {}) {
//Paths
Expand Down Expand Up @@ -81,7 +84,7 @@ export default async function metadata({log = true, diff = false} = {}) {
const descriptor = yaml.load(`${await fs.promises.readFile(__descriptor, "utf-8")}`)

//Metadata
return {plugins:Plugins, templates:Templates, packaged, descriptor}
return {plugins:Plugins, templates:Templates, packaged, descriptor, env}
}

/**Metadata extractor for inputs */
Expand Down Expand Up @@ -244,14 +247,19 @@ metadata.plugin = async function({__plugins, __templates, name, logger}) {
const q = {}
for (const key of Object.keys(inputs)) {
//Parse input
let value = `${core.getInput(key)}`.trim()
try {
value = decodeURIComponent(value)
}
catch {
logger(`metrics/inputs > failed to decode uri for ${key}: ${value}`)
value = "<default-value>"
let value
if (env.ghactions) {
value = `${core.getInput(key)}`.trim()
try {
value = decodeURIComponent(value)
}
catch {
logger(`metrics/inputs > failed to decode uri for ${key}: ${value}`)
value = "<default-value>"
}
}
else
value = process.env[`INPUT_${key.toUpperCase()}`]?.trim() ?? "<default-value>"
const unspecified = value === "<default-value>"
//From presets
if ((key in preset) && (unspecified)) {
Expand Down