From b34393cfcd42d37768cdde2198cae6d05eeea7c8 Mon Sep 17 00:00:00 2001
From: Kir_Antipov <kp.antipov@gmail.com>
Date: Tue, 2 Jan 2024 13:46:03 +0000
Subject: [PATCH] Fixed version resolution in releaseless context

Fixes #78
---
 src/platforms/github/github-context.ts | 21 ++++++++++++++++++++-
 src/program.ts                         |  2 +-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/platforms/github/github-context.ts b/src/platforms/github/github-context.ts
index 43e28b2..9e83841 100644
--- a/src/platforms/github/github-context.ts
+++ b/src/platforms/github/github-context.ts
@@ -63,8 +63,27 @@ export class GitHubContext {
      * Gets the tag associated with the context, if available.
      */
     get tag(): string | undefined {
+        const payload = this.payload;
+        if (payload.release?.tag_name) {
+            return payload.release.tag_name;
+        }
+
         const ref = this.ref;
-        return ref?.startsWith(GITHUB_REF_TAG_PREFIX) && ref.substring(GITHUB_REF_TAG_PREFIX.length);
+        if (ref?.startsWith(GITHUB_REF_TAG_PREFIX)) {
+            return ref.substring(GITHUB_REF_TAG_PREFIX.length);
+        }
+
+        return undefined;
+    }
+
+    /**
+     * Gets the version associated with the context, if available.
+     */
+    get version(): string | undefined {
+        const tag = this.tag;
+
+        // Remove the `v` prefix, popularized by GitHub.
+        return /v\d/.test(tag) ? tag.substring(1) : tag;
     }
 
     /**
diff --git a/src/program.ts b/src/program.ts
index ea6be83..4dcc1b6 100644
--- a/src/program.ts
+++ b/src/program.ts
@@ -111,7 +111,7 @@ async function fillInDefaultValues<T extends McPublishInput[P], P extends Platfo
     const unwrappedGameVersions = gameVersions ? GameVersionFilter.filter(gameVersions, options.gameVersionFilter).map(x => x.id) : wrappedGameVersions;
 
     (options as UnionToIntersection<McPublishInput[PlatformType]>).id ||= metadata?.getProjectId(platform) || "";
-    options.version ||= githubContext.payload.release?.tag_name || metadata?.version;
+    options.version ||= githubContext.version || metadata?.version;
     options.versionType ||= VersionType.parseFromFileName(metadata?.version || primaryFile.name);
     options.name ??= githubContext.payload.release?.name || options.version;
     options.changelog ??= githubContext.payload.release?.body || "";