|
| 1 | +#!/usr/bin/env node |
| 2 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 3 | +import { pathToFileURL } from "node:url"; |
| 4 | + |
| 5 | +export const INTENT_SCHEMA = "memos.local-plugin.release-intent.v2"; |
| 6 | +export const INTENT_MARKER = "doc-agent-local-plugin-release-intent"; |
| 7 | +const MEMOS_TAG_RE = /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/; |
| 8 | +const INTENT_RE = new RegExp( |
| 9 | + `<!--\\s*${INTENT_MARKER}\\s*\\n(\\{[^<]*\\})\\s*\\n-->`, |
| 10 | + "g", |
| 11 | +); |
| 12 | + |
| 13 | +function fail(message) { |
| 14 | + throw new Error(String(message)); |
| 15 | +} |
| 16 | + |
| 17 | +function stableVersion(raw) { |
| 18 | + const value = String(raw || "").trim().replace(/^v/, ""); |
| 19 | + if (!/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(value)) { |
| 20 | + fail(`local plugin release intent requires a stable SemVer; received ${raw || "<empty>"}`); |
| 21 | + } |
| 22 | + return value; |
| 23 | +} |
| 24 | + |
| 25 | +export function buildLocalPluginReleaseIntent({ |
| 26 | + enabled, |
| 27 | + version = "", |
| 28 | + tag = "", |
| 29 | + sourceSha = "", |
| 30 | + evidenceDigest = "", |
| 31 | + memosReleaseTag = "", |
| 32 | + pluginReleaseUrl = "", |
| 33 | +} = {}) { |
| 34 | + const active = enabled === true || String(enabled) === "true"; |
| 35 | + const normalizedMemOSTag = String(memosReleaseTag || "").trim(); |
| 36 | + if (!MEMOS_TAG_RE.test(normalizedMemOSTag)) { |
| 37 | + fail(`local plugin release intent requires a stable MemOS release tag; received ${memosReleaseTag || "<empty>"}`); |
| 38 | + } |
| 39 | + if (!/^[0-9a-f]{64}$/.test(String(evidenceDigest || ""))) { |
| 40 | + fail("local plugin release intent requires a SHA-256 evidence_digest"); |
| 41 | + } |
| 42 | + if (!active) { |
| 43 | + return { |
| 44 | + schema: INTENT_SCHEMA, |
| 45 | + enabled: false, |
| 46 | + memos_release_tag: normalizedMemOSTag, |
| 47 | + paired_release: false, |
| 48 | + version: "", |
| 49 | + tag: "", |
| 50 | + source_sha: "", |
| 51 | + evidence_digest: evidenceDigest, |
| 52 | + plugin_release_url: "", |
| 53 | + docs_trigger: "none", |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + const normalizedVersion = stableVersion(version); |
| 58 | + const expectedTag = `memos-local-plugin-v${normalizedVersion}`; |
| 59 | + if (String(tag || "").trim() !== expectedTag) { |
| 60 | + fail(`local plugin release intent tag must equal ${expectedTag}`); |
| 61 | + } |
| 62 | + if (!/^[0-9a-f]{40}$/.test(String(sourceSha || "").trim())) { |
| 63 | + fail("enabled local plugin release intent requires the 40-character published tag commit SHA"); |
| 64 | + } |
| 65 | + const expectedUrl = `https://github.com/MemTensor/MemOS/releases/tag/${expectedTag}`; |
| 66 | + if (String(pluginReleaseUrl || "").trim() !== expectedUrl) { |
| 67 | + fail(`enabled local plugin release intent plugin_release_url must equal ${expectedUrl}`); |
| 68 | + } |
| 69 | + return { |
| 70 | + schema: INTENT_SCHEMA, |
| 71 | + enabled: true, |
| 72 | + memos_release_tag: normalizedMemOSTag, |
| 73 | + paired_release: true, |
| 74 | + version: `v${normalizedVersion}`, |
| 75 | + tag: expectedTag, |
| 76 | + source_sha: String(sourceSha).trim(), |
| 77 | + evidence_digest: evidenceDigest, |
| 78 | + plugin_release_url: expectedUrl, |
| 79 | + docs_trigger: "local_plugin_release_published", |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +export function parseLocalPluginReleaseIntent(body) { |
| 84 | + const matches = [...String(body || "").matchAll(INTENT_RE)]; |
| 85 | + if (matches.length !== 1) { |
| 86 | + fail(`MemOS Release must contain exactly one local-plugin intent marker; found ${matches.length}`); |
| 87 | + } |
| 88 | + let payload; |
| 89 | + try { |
| 90 | + payload = JSON.parse(matches[0][1]); |
| 91 | + } catch { |
| 92 | + fail("MemOS local-plugin release intent is not valid JSON"); |
| 93 | + } |
| 94 | + if (!payload || typeof payload !== "object" || Array.isArray(payload)) { |
| 95 | + fail("MemOS local-plugin release intent must be an object"); |
| 96 | + } |
| 97 | + if (payload.schema !== INTENT_SCHEMA) { |
| 98 | + fail(`unsupported MemOS local-plugin release intent schema ${payload.schema || "<empty>"}`); |
| 99 | + } |
| 100 | + const rebuilt = buildLocalPluginReleaseIntent({ |
| 101 | + enabled: payload.enabled, |
| 102 | + version: payload.version, |
| 103 | + tag: payload.tag, |
| 104 | + sourceSha: payload.source_sha, |
| 105 | + evidenceDigest: payload.evidence_digest, |
| 106 | + memosReleaseTag: payload.memos_release_tag, |
| 107 | + pluginReleaseUrl: payload.plugin_release_url, |
| 108 | + }); |
| 109 | + if (JSON.stringify(payload) !== JSON.stringify(rebuilt)) { |
| 110 | + fail("MemOS local-plugin release intent contains unexpected or inconsistent fields"); |
| 111 | + } |
| 112 | + return payload; |
| 113 | +} |
| 114 | + |
| 115 | +export function appendIntentToReleaseNotes(notes, intent) { |
| 116 | + const source = String(notes || "").trimEnd(); |
| 117 | + if (!source) fail("MemOS release notes are empty"); |
| 118 | + if (source.includes(`<!-- ${INTENT_MARKER}`)) { |
| 119 | + fail("MemOS release notes already contain a local plugin release intent marker"); |
| 120 | + } |
| 121 | + return `${source}\n\n<!-- ${INTENT_MARKER}\n${JSON.stringify(intent)}\n-->\n`; |
| 122 | +} |
| 123 | + |
| 124 | +export function main() { |
| 125 | + const notesFile = String(process.env.RELEASE_NOTES_FILE || "").trim(); |
| 126 | + const outputFile = String(process.env.OUTPUT_RELEASE_NOTES_FILE || notesFile).trim(); |
| 127 | + if (!notesFile || !outputFile) fail("RELEASE_NOTES_FILE and OUTPUT_RELEASE_NOTES_FILE are required"); |
| 128 | + const intent = buildLocalPluginReleaseIntent({ |
| 129 | + enabled: process.env.LOCAL_PLUGIN_RELEASE_ENABLED, |
| 130 | + version: process.env.LOCAL_PLUGIN_VERSION, |
| 131 | + tag: process.env.LOCAL_PLUGIN_TAG, |
| 132 | + sourceSha: process.env.LOCAL_PLUGIN_TAG_SHA, |
| 133 | + evidenceDigest: process.env.LOCAL_PLUGIN_EVIDENCE_DIGEST, |
| 134 | + memosReleaseTag: process.env.MEMOS_RELEASE_TAG, |
| 135 | + pluginReleaseUrl: process.env.LOCAL_PLUGIN_RELEASE_URL, |
| 136 | + }); |
| 137 | + writeFileSync(outputFile, appendIntentToReleaseNotes(readFileSync(notesFile, "utf8"), intent), "utf8"); |
| 138 | + console.log(`Appended ${INTENT_SCHEMA} marker (enabled=${intent.enabled}).`); |
| 139 | +} |
| 140 | + |
| 141 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 142 | + try { |
| 143 | + main(); |
| 144 | + } catch (error) { |
| 145 | + console.error(`::error::${error.message}`); |
| 146 | + process.exitCode = 1; |
| 147 | + } |
| 148 | +} |
0 commit comments