125 lines
4.2 KiB
JavaScript
125 lines
4.2 KiB
JavaScript
import fs from "fs-extra";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import fetch from "node-fetch";
|
|
import dotenv from "dotenv";
|
|
|
|
dotenv.config({ path: "./.env" });
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const version = process.argv[2];
|
|
if (!version) {
|
|
console.error("Version not passed to create-gitea-release.js");
|
|
process.exit(1);
|
|
}
|
|
|
|
const { GITEA_URL, GITEA_USERNAME, GITEA_REPO, GITEA_TOKEN } = process.env;
|
|
if (!GITEA_URL || !GITEA_USERNAME || !GITEA_REPO || !GITEA_TOKEN) {
|
|
console.error("Missing required environment variables");
|
|
process.exit(1);
|
|
}
|
|
|
|
const getChangelogContent = async () => {
|
|
try {
|
|
const changelogPath = path.resolve(__dirname, "../CHANGELOG.md");
|
|
const content = await fs.readFile(changelogPath, "utf8");
|
|
|
|
// Extract the section for the current version
|
|
const versionHeading = `## [${version}]`;
|
|
const sections = content.split(/(?=^## \[)/m); // Split at version headings
|
|
|
|
if (sections.length < 2) {
|
|
console.warn("Couldn't find version section in changelog");
|
|
return "No changelog content available.";
|
|
}
|
|
|
|
// The first section is the latest version
|
|
const latestSection = sections[1];
|
|
return latestSection.trim();
|
|
} catch (err) {
|
|
console.error("Error reading changelog:", err);
|
|
return "No changelog content available.";
|
|
}
|
|
};
|
|
|
|
const createOrUpdateRelease = async (releaseNotes) => {
|
|
const tagName = `v${version}`;
|
|
const apiBase = `https://${GITEA_URL}/api/v1/repos/${GITEA_USERNAME}/${GITEA_REPO}`;
|
|
|
|
try {
|
|
const existing = await fetch(`${apiBase}/releases/tags/${tagName}`, {
|
|
headers: { Authorization: `token ${GITEA_TOKEN}` },
|
|
});
|
|
|
|
if (existing.ok) {
|
|
// Update existing release
|
|
const existingRelease = await existing.json();
|
|
console.log(`Updating existing release ${tagName}`);
|
|
|
|
const response = await fetch(
|
|
`${apiBase}/releases/${existingRelease.id}`,
|
|
{
|
|
method: "PATCH",
|
|
headers: {
|
|
Authorization: `token ${GITEA_TOKEN}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
name: tagName,
|
|
body: releaseNotes,
|
|
draft: false,
|
|
prerelease: false, // Change to true if you want prereleases
|
|
}),
|
|
}
|
|
);
|
|
|
|
if (!response.ok)
|
|
throw new Error(`Failed to update release: ${response.status}`);
|
|
const release = await response.json();
|
|
console.log("Release updated:", release.html_url);
|
|
return release;
|
|
} else if (existing.status === 404) {
|
|
// Create new release
|
|
console.log(`Creating new release ${tagName}`);
|
|
|
|
const response = await fetch(`${apiBase}/releases`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `token ${GITEA_TOKEN}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
tag_name: tagName,
|
|
name: `Release ${version}`,
|
|
body: releaseNotes,
|
|
draft: false,
|
|
prerelease: false, // Change to true if you want prereleases
|
|
}),
|
|
});
|
|
|
|
if (!response.ok)
|
|
throw new Error(`Failed to create release: ${response.status}`);
|
|
const release = await response.json();
|
|
console.log("Release created:", release.html_url);
|
|
return release;
|
|
} else {
|
|
throw new Error(`Failed to check release: ${existing.status}`);
|
|
}
|
|
} catch (error) {
|
|
console.error("Error in createOrUpdateRelease:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
(async () => {
|
|
try {
|
|
const releaseNotes = await getChangelogContent();
|
|
await createOrUpdateRelease(releaseNotes);
|
|
} catch (error) {
|
|
console.error("Release failed:", error);
|
|
process.exit(1);
|
|
}
|
|
})();
|