Skip to content

Commit

Permalink
Update for new PATs
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyboy-roks committed Aug 8, 2023
1 parent c206516 commit 9e01ce9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
54 changes: 32 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {
Expand All @@ -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.
Expand All @@ -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());
}
Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
//
Expand All @@ -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.
Expand All @@ -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());
}
Expand Down

0 comments on commit 9e01ce9

Please sign in to comment.