From dfce7932e48a403f0209f9f6ced4af19318a8965 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 26 Jul 2023 13:59:04 -0400 Subject: [PATCH 1/5] slight refactor Signed-off-by: IThundxr --- src/webserver/github.ts | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/webserver/github.ts b/src/webserver/github.ts index fab1fba..d3e14b9 100644 --- a/src/webserver/github.ts +++ b/src/webserver/github.ts @@ -25,9 +25,9 @@ export const handleWebhook = (client: Client, req: Request, res: Response) => { actionPush(req); } - if (req.body.action === 'in_progress') { + if (req.body.action === 'requested') { // this is run on the workflow_run webhook event - // when the workflow is started + // when the workflow is requested if (isBuildRun(req)) { actionStart(client, req); } @@ -59,8 +59,7 @@ const actionStart = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const run_number = await getRunNumber(workflow_run.url); - const version = await getVersion(workflow_run.url); + const version = await getVersion(workflow_run.url, workflow_run.run_number); const commitString = generateCommitsString(workflow_run.head_sha); @@ -72,7 +71,7 @@ const actionStart = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: Build is running for **#${run_number}** ${process.env.LOADING_EMOJI} + Status: Build is running for **#${workflow_run.run_number}** ${process.env.LOADING_EMOJI} Version: ${version} ${commitString} ` @@ -100,8 +99,7 @@ const actionCompleted = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const run_number = await getRunNumber(workflow_run.url); - const version = await getVersion(workflow_run.url); + const version = await getVersion(workflow_run.url, workflow_run.run_number); const status = workflow_run.conclusion === 'success' @@ -134,7 +132,7 @@ const actionCompleted = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: **${status} #${run_number}** in ${timeTaken} + Status: **${status} #${workflow_run.run_number}** in ${timeTaken} Version: ${version} ${commitString} ` @@ -197,13 +195,7 @@ const getTimeTaken = (time: number) => { return timeTaken; }; -const getRunNumber = async (url: URL) => { - const request = await fetch(url); - const data = await request.json(); - return data.run_number; -}; - -const getVersion = async (apiurl: URL) => { +const getVersion = async (apiurl: URL, run_number: string) => { const runDataRequest = await fetch(apiurl); const runData = await runDataRequest.json(); @@ -220,9 +212,7 @@ const getVersion = async (apiurl: URL) => { if (modVersionLine && minecraftVersionLine) { const modVersion = modVersionLine.split('=')[1].trim(); const minecraftVersion = minecraftVersionLine.split('=')[1].trim(); - return `${modVersion}-mc${minecraftVersion}.${await getRunNumber( - apiurl - )}`; + return `${modVersion}-mc${minecraftVersion}.${run_number}`; } else { return "Couldn't find version"; } From 66ca4bc15a39e4ae17615240922013e584f8c07e Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 26 Jul 2023 14:01:20 -0400 Subject: [PATCH 2/5] nvm have to use the other way Signed-off-by: IThundxr --- src/webserver/github.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/webserver/github.ts b/src/webserver/github.ts index d3e14b9..fab1fba 100644 --- a/src/webserver/github.ts +++ b/src/webserver/github.ts @@ -25,9 +25,9 @@ export const handleWebhook = (client: Client, req: Request, res: Response) => { actionPush(req); } - if (req.body.action === 'requested') { + if (req.body.action === 'in_progress') { // this is run on the workflow_run webhook event - // when the workflow is requested + // when the workflow is started if (isBuildRun(req)) { actionStart(client, req); } @@ -59,7 +59,8 @@ const actionStart = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const version = await getVersion(workflow_run.url, workflow_run.run_number); + const run_number = await getRunNumber(workflow_run.url); + const version = await getVersion(workflow_run.url); const commitString = generateCommitsString(workflow_run.head_sha); @@ -71,7 +72,7 @@ const actionStart = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: Build is running for **#${workflow_run.run_number}** ${process.env.LOADING_EMOJI} + Status: Build is running for **#${run_number}** ${process.env.LOADING_EMOJI} Version: ${version} ${commitString} ` @@ -99,7 +100,8 @@ const actionCompleted = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const version = await getVersion(workflow_run.url, workflow_run.run_number); + const run_number = await getRunNumber(workflow_run.url); + const version = await getVersion(workflow_run.url); const status = workflow_run.conclusion === 'success' @@ -132,7 +134,7 @@ const actionCompleted = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: **${status} #${workflow_run.run_number}** in ${timeTaken} + Status: **${status} #${run_number}** in ${timeTaken} Version: ${version} ${commitString} ` @@ -195,7 +197,13 @@ const getTimeTaken = (time: number) => { return timeTaken; }; -const getVersion = async (apiurl: URL, run_number: string) => { +const getRunNumber = async (url: URL) => { + const request = await fetch(url); + const data = await request.json(); + return data.run_number; +}; + +const getVersion = async (apiurl: URL) => { const runDataRequest = await fetch(apiurl); const runData = await runDataRequest.json(); @@ -212,7 +220,9 @@ const getVersion = async (apiurl: URL, run_number: string) => { if (modVersionLine && minecraftVersionLine) { const modVersion = modVersionLine.split('=')[1].trim(); const minecraftVersion = minecraftVersionLine.split('=')[1].trim(); - return `${modVersion}-mc${minecraftVersion}.${run_number}`; + return `${modVersion}-mc${minecraftVersion}.${await getRunNumber( + apiurl + )}`; } else { return "Couldn't find version"; } From 83dcf945c6eefed443ee8f3aed4fff58a77670eb Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 26 Jul 2023 14:28:02 -0400 Subject: [PATCH 3/5] finally finish this Signed-off-by: IThundxr --- src/index.ts | 1 - src/webserver/github.ts | 61 ++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4eb5d6c..ac3317c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import 'dotenv/config'; import commandHandler from './handlers/command.handler'; import { logHandler } from './handlers/log.handler'; import './webserver'; -import { green } from 'colorette'; export const client = new Client({ intents: [ diff --git a/src/webserver/github.ts b/src/webserver/github.ts index fab1fba..6026224 100644 --- a/src/webserver/github.ts +++ b/src/webserver/github.ts @@ -59,8 +59,7 @@ const actionStart = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const run_number = await getRunNumber(workflow_run.url); - const version = await getVersion(workflow_run.url); + const version = await getVersion(workflow_run.url, workflow_run.run_number); const commitString = generateCommitsString(workflow_run.head_sha); @@ -72,7 +71,7 @@ const actionStart = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: Build is running for **#${run_number}** ${process.env.LOADING_EMOJI} + Status: Build is running for **#${workflow_run.run_number}** ${process.env.LOADING_EMOJI} Version: ${version} ${commitString} ` @@ -100,8 +99,7 @@ const actionCompleted = async (client: Client, req: Request) => { const unix_started_at = Math.floor( Date.parse(workflow_run.run_started_at) / 1000 ); - const run_number = await getRunNumber(workflow_run.url); - const version = await getVersion(workflow_run.url); + const version = await getVersion(workflow_run.url, workflow_run.run_number); const status = workflow_run.conclusion === 'success' @@ -134,7 +132,7 @@ const actionCompleted = async (client: Client, req: Request) => { }) .setDescription( `## Build - Status: **${status} #${run_number}** in ${timeTaken} + Status: **${status} #${workflow_run.run_number}** in ${timeTaken} Version: ${version} ${commitString} ` @@ -146,14 +144,25 @@ const actionCompleted = async (client: Client, req: Request) => { .setColor(statusColor); if (workflow_run.conclusion == 'success') { + const mod_version = await getRawVersion(workflow_run.url, 'mod'); + const minecraft_version = await getRawVersion( + workflow_run.url, + 'minecraft' + ); + + const versionFabric = `${mod_version}+fabric-mc${minecraft_version}-build.${workflow_run.run_number}`; + const versionForge = `${mod_version}+forge-mc${minecraft_version}-build.${workflow_run.run_number}`; + const fabricJar = `${process.env.MAVEN_REPO}${versionFabric}/Steam_Rails-${versionFabric}.jar`; + const forgeJar = `${process.env.MAVEN_REPO}${versionForge}/Steam_Rails-${versionForge}.jar`; + const fabricButton = new ButtonBuilder() .setStyle(ButtonStyle.Link) .setLabel('Fabric') - .setURL(`${process.env.MAVEN_REPO}`); + .setURL(fabricJar); const forgeButton = new ButtonBuilder() .setStyle(ButtonStyle.Link) .setLabel('Forge') - .setURL(`${process.env.MAVEN_REPO}`); + .setURL(forgeJar); const actionRow = new ActionRowBuilder().addComponents( fabricButton, @@ -197,13 +206,30 @@ const getTimeTaken = (time: number) => { return timeTaken; }; -const getRunNumber = async (url: URL) => { - const request = await fetch(url); - const data = await request.json(); - return data.run_number; +const getVersion = async (apiurl: URL, run_number: string) => { + const runDataRequest = await fetch(apiurl); + const runData = await runDataRequest.json(); + + const request = await fetch( + `https://raw.githubusercontent.com/${runData.repository.full_name}/${runData.head_commit.id}/gradle.properties` + ); + const data = await request.text(); + + const lines = data.split('\n'); + const modVersionLine = lines.find((line) => line.startsWith('mod_version')); + const minecraftVersionLine = lines.find((line) => + line.startsWith('minecraft_version') + ); + if (modVersionLine && minecraftVersionLine) { + const modVersion = modVersionLine.split('=')[1].trim(); + const minecraftVersion = minecraftVersionLine.split('=')[1].trim(); + return `${modVersion}-mc${minecraftVersion}.${run_number}`; + } else { + return "Couldn't find version"; + } }; -const getVersion = async (apiurl: URL) => { +const getRawVersion = async (apiurl: URL, type: string) => { const runDataRequest = await fetch(apiurl); const runData = await runDataRequest.json(); @@ -220,9 +246,12 @@ const getVersion = async (apiurl: URL) => { if (modVersionLine && minecraftVersionLine) { const modVersion = modVersionLine.split('=')[1].trim(); const minecraftVersion = minecraftVersionLine.split('=')[1].trim(); - return `${modVersion}-mc${minecraftVersion}.${await getRunNumber( - apiurl - )}`; + if (type == 'mod') { + return modVersion; + } else if (type == 'minecraft') { + return minecraftVersion; + } + return "Couldn't find version"; } else { return "Couldn't find version"; } From 2bcfda527ea001d3cb6be949e0390de86e968ec6 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 26 Jul 2023 14:34:44 -0400 Subject: [PATCH 4/5] undefined protection? i think this should work Signed-off-by: IThundxr --- src/webserver/github.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/webserver/github.ts b/src/webserver/github.ts index 6026224..282256e 100644 --- a/src/webserver/github.ts +++ b/src/webserver/github.ts @@ -180,6 +180,7 @@ const actionCompleted = async (client: Client, req: Request) => { const generateCommitsString = (head_sha: string) => { const commitsArray = commitMap.get(head_sha); if (!commitsArray) return; + if (commitsArray.commits.length == 0) return; const commitString = commitsArray.commits .map((commit) => { const committer = commit.committer; From fd973dedefb5b9d4fdab47b118757baddb8d3d1d Mon Sep 17 00:00:00 2001 From: IThundxr Date: Wed, 26 Jul 2023 14:42:14 -0400 Subject: [PATCH 5/5] nvm that didnt work this should fix it Signed-off-by: IThundxr --- src/webserver/github.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/webserver/github.ts b/src/webserver/github.ts index 282256e..67c7553 100644 --- a/src/webserver/github.ts +++ b/src/webserver/github.ts @@ -178,9 +178,11 @@ const actionCompleted = async (client: Client, req: Request) => { }; const generateCommitsString = (head_sha: string) => { + if (!commitMap.get(head_sha)) { + return 'No commits found'; + } const commitsArray = commitMap.get(head_sha); if (!commitsArray) return; - if (commitsArray.commits.length == 0) return; const commitString = commitsArray.commits .map((commit) => { const committer = commit.committer;