Compare commits

..

3 commits

Author SHA1 Message Date
8d8bd0fbb8 Add "webhook" storage method
this methods allows for storing messageIDs in Discord message
2024-02-21 18:03:45 +01:00
a34b52e1d0 Add missing set check 2024-02-21 17:58:58 +01:00
b7ff0eaea5 Allow user to select paths to where to store files 2024-02-21 17:15:21 +01:00
7 changed files with 96 additions and 7052 deletions

View file

@ -8,19 +8,24 @@ inputs:
webhook: webhook:
description: "Discord webhook url to use" description: "Discord webhook url to use"
required: true required: true
outputFileLocation:
description: "Where to write a file containing current message ids for this run"
default: "./messageIDs.txt"
storageMethod: storageMethod:
description: |- description: |-
Method of storing old message ids to delete Method of storing old message ids to delete
one of: one of:
- none - doesn't store ids of sent messages - none - doesn't store ids of sent messages
- artifact - stores ids in artifact file
- git - stores ids in git repository - git - stores ids in git repository
- webhook - stores in message sent by another webhook
default: none default: none
required: true required: true
artifact_retentionDays: storegeGitFileLocation:
description: |- description: "Where in git repository file should be saved"
if artifact is selected as storageMethod, storageWebhookUrl:
number of retentionDays for artifact description: "Webhook url used to send and read message containing messageIDs"
storageWebhookMessageID:
description: "Message ID to read and edit"
runs: runs:
using: node20 using: node20

6923
dist/index.js vendored

File diff suppressed because it is too large Load diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

134
package-lock.json generated
View file

@ -9,7 +9,6 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/artifact": "<2.0.0",
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"discord.js": "^14.14.1" "discord.js": "^14.14.1"
@ -19,17 +18,6 @@
"typescript": "^5.3.3" "typescript": "^5.3.3"
} }
}, },
"node_modules/@actions/artifact": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@actions/artifact/-/artifact-1.1.2.tgz",
"integrity": "sha512-1gLONA4xw3/Q/9vGxKwkFdV9u1LE2RWGx/IpAqg28ZjprCnJFjwn4pA7LtShqg5mg5WhMek2fjpyH1leCmOlQQ==",
"dependencies": {
"@actions/core": "^1.9.1",
"@actions/http-client": "^2.0.1",
"tmp": "^0.2.1",
"tmp-promise": "^3.0.2"
}
},
"node_modules/@actions/core": { "node_modules/@actions/core": {
"version": "1.10.1", "version": "1.10.1",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.1.tgz", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.1.tgz",
@ -242,25 +230,6 @@
"npm": ">=7.0.0" "npm": ">=7.0.0"
} }
}, },
"node_modules/balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"node_modules/brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dependencies": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
}
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"node_modules/discord-api-types": { "node_modules/discord-api-types": {
"version": "0.37.61", "version": "0.37.61",
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz", "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
@ -306,44 +275,6 @@
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
}, },
"node_modules/fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw=="
},
"node_modules/glob": {
"version": "7.2.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
"dependencies": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.1.1",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
},
"engines": {
"node": "*"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==",
"dependencies": {
"once": "^1.3.0",
"wrappy": "1"
}
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"node_modules/lodash": { "node_modules/lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
@ -359,66 +290,6 @@
"resolved": "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.8.0.tgz", "resolved": "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.8.0.tgz",
"integrity": "sha512-lyWpfvNGVb5lu8YUAbER0+UMBTdR63w2mcSUlhhBTyVbxJvjgqwyAf3AZD6MprgK0uHuBoWXSDAMWLupX83o3Q==" "integrity": "sha512-lyWpfvNGVb5lu8YUAbER0+UMBTdR63w2mcSUlhhBTyVbxJvjgqwyAf3AZD6MprgK0uHuBoWXSDAMWLupX83o3Q=="
}, },
"node_modules/minimatch": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
"integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
"dependencies": {
"brace-expansion": "^1.1.7"
},
"engines": {
"node": "*"
}
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
"dependencies": {
"wrappy": "1"
}
},
"node_modules/path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/rimraf": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
"dependencies": {
"glob": "^7.1.3"
},
"bin": {
"rimraf": "bin.js"
},
"funding": {
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/tmp": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.1.tgz",
"integrity": "sha512-76SUhtfqR2Ijn+xllcI5P1oyannHNHByD80W1q447gU3mp9G9PSpGdWmjUOHRDPiHYacIk66W7ubDTuPF3BEtQ==",
"dependencies": {
"rimraf": "^3.0.0"
},
"engines": {
"node": ">=8.17.0"
}
},
"node_modules/tmp-promise": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/tmp-promise/-/tmp-promise-3.0.3.tgz",
"integrity": "sha512-RwM7MoPojPxsOBYnyd2hy0bxtIlVrihNs9pj5SUvY8Zz1sQcQG2tG1hSr8PDxfgEB8RNKDhqbIlroIarSNDNsQ==",
"dependencies": {
"tmp": "^0.2.0"
}
},
"node_modules/ts-mixer": { "node_modules/ts-mixer": {
"version": "6.0.4", "version": "6.0.4",
"resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.4.tgz", "resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.4.tgz",
@ -474,11 +345,6 @@
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"
} }
}, },
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
},
"node_modules/ws": { "node_modules/ws": {
"version": "8.14.2", "version": "8.14.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz",

View file

@ -10,7 +10,6 @@
"author": "Wroclaw", "author": "Wroclaw",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/artifact": "<2.0.0",
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"discord.js": "^14.14.1" "discord.js": "^14.14.1"

View file

@ -49,6 +49,7 @@ async function main() {
if (presentMessages.length !== messageBlocks.length) { if (presentMessages.length !== messageBlocks.length) {
core.info("number of message blocks is different, sending messages"); core.info("number of message blocks is different, sending messages");
core.debug(`read "blocks": ${messageBlocks.length}, number of read messages: ${presentMessages.length}`); core.debug(`read "blocks": ${messageBlocks.length}, number of read messages: ${presentMessages.length}`);
shouldPostMessages = true;
} }
if (!shouldPostMessages) for (let i = 0; i < messageBlocks.length; i++) { if (!shouldPostMessages) for (let i = 0; i < messageBlocks.length; i++) {
// FIXME: discord may strip special unicode characters, making strings different // FIXME: discord may strip special unicode characters, making strings different
@ -98,10 +99,10 @@ async function main() {
const messageIDsConcatenated = messageIDs.join("\n"); const messageIDsConcatenated = messageIDs.join("\n");
core.info(`Messages sent! IDs:\n${messageIDsConcatenated}`); core.info(`Messages sent! IDs:\n${messageIDsConcatenated}`);
await fs.writeFile("./messageIDs.txt", messageIDsConcatenated); await fs.writeFile(core.getInput("outputFileLocation"), messageIDsConcatenated);
await Promise.all([ await Promise.all([
storage.pushMessageIDs(), storage.pushMessageIDs(messageIDs),
presentMessageIds.map(v => client.deleteMessage(v)), presentMessageIds.map(v => client.deleteMessage(v)),
].flat() ].flat()
); );

View file

@ -1,8 +1,8 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import { create as artifact } from "@actions/artifact";
import * as core from "@actions/core"; import * as core from "@actions/core";
import { exec as _exec, type ExecOptions } from "@actions/exec"; import { exec as _exec, type ExecOptions } from "@actions/exec";
import { WebhookClient } from "discord.js";
function exec(command: string, options?: ExecOptions) { function exec(command: string, options?: ExecOptions) {
const splitted = command.trim().split(/ +/g); const splitted = command.trim().split(/ +/g);
@ -15,28 +15,20 @@ function getStorageMethod(): string {
return core.getInput("storageMethod", {required: true}) return core.getInput("storageMethod", {required: true})
} }
function getStorageWebhook(): WebhookClient {
return new WebhookClient({
url: core.getInput("storageWebhookUrl", { required: true }),
});
}
/** @returns array of messageIDs */ /** @returns array of messageIDs */
export async function getMessageIDs(): Promise<string[]> { export async function getMessageIDs(): Promise<string[]> {
const method = getStorageMethod(); const method = getStorageMethod();
switch (method) { switch (method) {
case "none": case "none":
return []; return [];
case "artifact":
return artifact()
.downloadArtifact("messageIDs", undefined, {createArtifactFolder: true})
.then(v => fs.readFile(v.downloadPath + "/messageIDs.txt"))
.then(v => v.toString()
.trim()
.split("\n")
)
.catch(e => {
core.warning("Couldn't download artifact");
core.warning(e);
core.warning("Continuing anyway");
return [];
});
case "git": case "git":
return fs.readFile("./messageIDs.txt") return fs.readFile(core.getInput("storageGitFileLocation"))
.then(v => v.toString().trim().split("\n")) .then(v => v.toString().trim().split("\n"))
.catch(e => { .catch(e => {
core.warning("Couldn't read messageIDs"); core.warning("Couldn't read messageIDs");
@ -44,30 +36,56 @@ export async function getMessageIDs(): Promise<string[]> {
core.warning("Continuing anyway"); core.warning("Continuing anyway");
return []; return [];
}) })
case "webhook":
return getStorageWebhook()
.fetchMessage(core.getInput("storageWebhookMessageID"))
.then(v => v.content.split(", "))
.catch(e => {
core.warning("Couldn't read messageIDs");
return [];
});
default: default:
throw new Error(`Storage method is unknown: ${method}`); throw new Error(`Storage method is unknown: ${method}`);
} }
} }
export async function pushMessageIDs() { export async function pushMessageIDs(messageIDs: string[]) {
const method = getStorageMethod(); const method = getStorageMethod();
switch (method) { switch (method) {
case "artifact":
const retentionDaysNumber = Number(core.getInput("artifact_retentionDays"))
const retentionDays = !isNaN(retentionDaysNumber) ? retentionDaysNumber
: retentionDaysNumber > 0 ? retentionDaysNumber
: undefined
return artifact()
.uploadArtifact("messageIDs", ["./messageIDs.txt"], "./", { retentionDays });
case "git": case "git":
const gitFileLocation = core.getInput("storageGitFileLocation", { required: true });
await fs.copyFile(
core.getInput("outputFileLocation"),
gitFileLocation
);
await exec("git config --global user.name \"Actions\"") await exec("git config --global user.name \"Actions\"")
await exec("git config --global user.email \"noreply@users.noreply.github.com\"") await exec("git config --global user.email \"noreply@users.noreply.github.com\"")
await exec("git add ./messageIDs.txt") await exec(`git add ${gitFileLocation}`);
await _exec("git", ["commit", "-m", "Update stored messageIDs"]) await _exec("git", ["commit", "-m", "Update stored messageIDs"])
await exec("git push"); await exec("git push");
return true; return true;
case "none": case "none":
return false; return false;
case "webhook":
const content = messageIDs.join(", ");
try {
const id = core.getInput("storageWebhookMessageID");
if (id === "" || isNaN(Number(id)))
throw new Error("Invalid argument provided for storageWebhookMessageID");
await getStorageWebhook().editMessage(
core.getInput("storageWebhookMessageID"),
content
)
core.info("Message for storage edited!")
}
catch (e) {
core.warning("Couldn't edit storage message");
core.warning(e as Error);
core.warning("Sending a new storage message");
const message = await getStorageWebhook().send(messageIDs.join(", "));
core.info(`Message for storage sent! ID: ${message.id}`);
}
return true;
default: default:
throw new Error(`Storage method is unknown: ${method}`); throw new Error(`Storage method is unknown: ${method}`);
} }