From 9e01ce90fe54a532ca4a5505642434d5688e8cdb Mon Sep 17 00:00:00 2001 From: funnyboy-roks Date: Mon, 7 Aug 2023 22:37:05 -0500 Subject: [PATCH] Update for new PATs --- README.md | 10 ++++++++-- dist/index.js | 54 ++++++++++++++++++++++++++++++--------------------- index.js | 18 ++++++++++++----- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 6330a9b..d847e7b 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,14 @@ modrinth: The auth token to use for the Modrinth API To get this, you need to: -1. Go to https://modrinth.com/settings/account and sign in if you aren't already -1. Click "Copy token to clipboard" under Authorization Token +1. Go to https://modrinth.com/settings/pats and sign in if you aren't already +1. Click "Create a PAT" in the top-left corner +1. Name it something that describes its purpose, i.e. "GitHub actions" or "Auto Description" +1. Under scopes, find "Write Projects" and make sure it's checked +1. Give it an expiration date -- Once this expiration date is reached, you'll need to generate a new token. + 1. This is to ensure security. Anybody with this token could do harm to your project(s). +1. Press "Create PAT" +1. The PAT you just created should appear with some random numbers and letters, copy that 1. Put it in a [GitHub Secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets) 1. You're done! diff --git a/dist/index.js b/dist/index.js index 5291a30..94252e6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -14044,36 +14044,37 @@ var yamlFront = __nccwpck_require__(7774); -const main = async () => { - const auth = core.getInput('auth-token'); - const slug = core.getInput('slug'); +// See: https://docs.modrinth.com/api-spec/#tag/projects/operation/modifyProject - core.info(`Loading ${core.getInput('readme')}.`); - const readme = await promises_namespaceObject.readFile(core.getInput('readme'), 'utf-8'); +const main = async () => { + try { + const auth = core.getInput('auth-token'); + let slug = core.getInput('slug'); + // Grab the slug if it's a url + slug = slug.substring(slug.lastIndexOf('/') + 1); - let frontMatter = yamlFront.safeLoadFront(readme); + core.info(`Loading ${core.getInput('readme')}.`); + const readme = await promises_namespaceObject.readFile(core.getInput('readme'), 'utf-8'); - // use this since it removes the frontmatter - const content = frontMatter.__content.trim(); + let frontMatter = yamlFront.safeLoadFront(readme); - // Not elegant, but I'm happy with it - frontMatter.modrinth ||= {}; + // use this since it removes the frontmatter + const content = frontMatter.__content.trim(); - // We only care about stuff in the `modrinth` section - const { modrinth } = frontMatter; + // Get the `modrinth` section or empty obj if it's not set + const modrinth = frontMatter.modrinth ?? {}; - // Prevent anybody from attempting change the description body - // - // This is a little confusing, because there is a key for modrinth called `discription` and one called `body`. - // The `body` key is the one which controls the markdown description, while the `description` controls the short description shown under the name. - if (modrinth.body) { - // Give a warning, but still continue - core.warning('Ignoring `modrinth.body` in the front matter. This field should not be set.'); - } + // Prevent anybody from attempting change the description body + // + // This is a little confusing, because there is a key for modrinth called `discription` and one called `body`. + // The `body` key is the one which controls the markdown description, while the `description` controls the short description shown under the name. + if (modrinth.body) { + // Give a warning, but still continue + core.warning('Ignoring `modrinth.body` in the front matter. This field should not be set.'); + } - modrinth.body = content; + modrinth.body = content; - try { core.info('Sending request to Modrinth...'); // https://docs.modrinth.com/api-spec/#tag/projects/operation/modifyProject const req = await fetch(`https://api.modrinth.com/v2/project/${slug}`, { @@ -14084,6 +14085,13 @@ const main = async () => { 'content-type': 'Application/json', }, }); + + if (req.status == 401) { // Unauthorised -- probably not a PAT or invalid PAT + core.setFailed('Unauthorised access to API. Did you set the access token properly?'); + // Should always be JSON if we get a 401 + core.error(JSON.stringify(await req.json(), null, 4)); + return; + } core.info('Modrinth response'); const res = await req.text(); // Returns json, but not always, so text instead. @@ -14104,6 +14112,8 @@ const main = async () => { help.push(`Did you use the correct path in the config? Path specified: ${readme}`); } + help.push('If you are unable to find a solution, or you believe that this is a bug, you may file an issue at https://github.com/funnyboy-roks/modrinth-auto-desc/issues'); + help = help.map(l => '\t' + l).join('\n'); core.setFailed(`Action failed with error: ${err}.\n${help}`.trim()); } diff --git a/index.js b/index.js index 8f61530..d9f782c 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ import fs from 'fs/promises'; import actions from '@actions/core'; import fm from 'yaml-front-matter'; +// See: https://docs.modrinth.com/api-spec/#tag/projects/operation/modifyProject + const main = async () => { try { const auth = actions.getInput('auth-token'); @@ -18,11 +20,8 @@ const main = async () => { // use this since it removes the frontmatter const content = frontMatter.__content.trim(); - // Not elegant, but I'm happy with it - frontMatter.modrinth ||= {}; - - // We only care about stuff in the `modrinth` section - const { modrinth } = frontMatter; + // Get the `modrinth` section or empty obj if it's not set + const modrinth = frontMatter.modrinth ?? {}; // Prevent anybody from attempting change the description body // @@ -45,6 +44,13 @@ const main = async () => { 'content-type': 'Application/json', }, }); + + if (req.status == 401) { // Unauthorised -- probably not a PAT or invalid PAT + actions.setFailed('Unauthorised access to API. Did you set the access token properly?'); + // Should always be JSON if we get a 401 + actions.error(JSON.stringify(await req.json(), null, 4)); + return; + } actions.info('Modrinth response'); const res = await req.text(); // Returns json, but not always, so text instead. @@ -65,6 +71,8 @@ const main = async () => { help.push(`Did you use the correct path in the config? Path specified: ${readme}`); } + help.push('If you are unable to find a solution, or you believe that this is a bug, you may file an issue at https://github.com/funnyboy-roks/modrinth-auto-desc/issues'); + help = help.map(l => '\t' + l).join('\n'); actions.setFailed(`Action failed with error: ${err}.\n${help}`.trim()); }