From 956e223680918561b9869b9c4a0244f1b2201698 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Sun, 24 Apr 2022 21:00:06 +0200 Subject: [PATCH] feat(app/action): add `quota_required_*` options (#1014) --- source/app/action/index.mjs | 20 +++++++++++------- source/plugins/core/metadata.yml | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/source/app/action/index.mjs b/source/app/action/index.mjs index 8951d7c9..e5716c41 100644 --- a/source/app/action/index.mjs +++ b/source/app/action/index.mjs @@ -140,6 +140,9 @@ function quit(reason) { "output.action": _action, "output.condition": _output_condition, delay, + "quota.required.rest":_quota_required_rest, + "quota.required.graphql":_quota_required_graphql, + "quota.required.search":_quota_required_search, "notice.release": _notice_releases, ...config } = metadata.plugins.core.inputs.action({core, preset}) @@ -184,19 +187,22 @@ function quit(reason) { //Test token validity and requests count else if (!/^NOT_NEEDED$/.test(token)) { //Check rate limit + let ratelimit = false const {data} = await api.rest.rateLimit.get().catch(() => ({data: {resources: {}}})) Object.assign(resources, data.resources) - info("API requests (REST)", resources.core ? `${resources.core.remaining}/${resources.core.limit}` : "(unknown)") - info("API requests (GraphQL)", resources.graphql ? `${resources.graphql.remaining}/${resources.graphql.limit}` : "(unknown)") - info("API requests (search)", resources.search ? `${resources.search.remaining}/${resources.search.limit}` : "(unknown)") - if ((!resources.core.remaining) || (!resources.graphql.remaining)) { - console.warn("::warning::It seems you have reached your API requests limit. Please retry later.") + for (const type of ["core", "graphql", "search"]) { + const name = {core: "REST", graphql: "GraphQL", search: "Search"}[type] + const quota = {core:_quota_required_rest, graphql:_quota_required_graphql, search:_quota_required_search}[type] ?? 1 + info(`API requests (${name})`, resources[type] ? `${resources[type].remaining}/${resources[type].limit}${quota ? ` (${quota}+ required)` : ""}` : "(unknown)") + if ((resources[type]) && (resources[type].remaining < quota)) + ratelimit = true + } + if (ratelimit) { + console.warn("::warning::It seems you have reached your API requests limit or configured quota. Please retry later.") info.break() console.log("Nothing can be done currently, thanks for using metrics!") quit("skipped") } - if (!resources.search.remaining) - console.warn("::warning::It seems you have reached your Search API requests limit. Some plugins may return less accurate results.") //Check scopes try { const {headers} = await api.rest.request("HEAD /") diff --git a/source/plugins/core/metadata.yml b/source/plugins/core/metadata.yml index 57041fed..980d81fe 100644 --- a/source/plugins/core/metadata.yml +++ b/source/plugins/core/metadata.yml @@ -379,6 +379,42 @@ inputs: min: 0 max: 3600 + quota_required_rest: + description: | + Minimum GitHub REST API requests quota required to run + + Action will cancel itself without any errors if requirements are not met + + This option has no effect when `token` is set to `NOT_NEEDED` + type: number + default: 200 + min: 0 + max: 5000 + + quota_required_graphql: + description: | + Minimum GitHub GraphQL API requests quota required to run + + Action will cancel itself without any errors if requirements are not met + + This option has no effect when `token` is set to `NOT_NEEDED` + type: number + default: 200 + min: 0 + max: 5000 + + quota_required_search: + description: | + Minimum GitHub REST API requests quota required to run + + Action will cancel itself without any errors if requirements are not met + + This option has no effect when `token` is set to `NOT_NEEDED` + type: number + default: 0 + min: 0 + max: 30 + notice_releases: description: Notice about new releases of metrics type: boolean