Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions .github/scripts/publish-local-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ set -euo pipefail
: "${RELEASE_VERSION:?RELEASE_VERSION is required.}"
: "${RELEASE_TAG:?RELEASE_TAG is required.}"
: "${NPM_DIST_TAG:?NPM_DIST_TAG is required.}"
: "${RELEASE_TARBALL:?RELEASE_TARBALL is required.}"

if [ ! -s "${RELEASE_TARBALL}" ]; then
echo "::error::RELEASE_TARBALL does not exist or is empty: ${RELEASE_TARBALL}"
exit 2
fi

npm_visibility_attempts="${NPM_VISIBILITY_ATTEMPTS:-10}"
npm_ambiguous_visibility_attempts="${NPM_AMBIGUOUS_VISIBILITY_ATTEMPTS:-3}"
Expand Down Expand Up @@ -112,7 +118,59 @@ remote_tag_exists() {
done
}

verify_published_package() {
local verify_directory="${RUNNER_TEMP}/memos-local-plugin-registry-verification"
local verify_json="${verify_directory}/npm-pack.json"
local verify_filename
local verify_tarball
local package_version
local manifest_version

mkdir -p "${verify_directory}"
bash "${script_directory}/retry.sh" --label "download published npm package" -- \
bash -euo pipefail -c 'npm pack "$1" --json --silent --pack-destination "$2" > "$3"' \
_ "${PACKAGE_NAME}@${RELEASE_VERSION}" "${verify_directory}" "${verify_json}"
verify_filename="$(
node -e '
const fs = require("node:fs");
const raw = fs.readFileSync(process.argv[1], "utf8");
const jsonStart = raw.match(/^\[/m);
if (!jsonStart || jsonStart.index === undefined) {
throw new Error("npm pack output did not contain a JSON report");
}
const report = JSON.parse(raw.slice(jsonStart.index));
if (!Array.isArray(report) || report.length !== 1 || !report[0].filename) {
throw new Error("npm pack did not report exactly one registry tarball");
}
fs.writeFileSync(process.argv[1], `${JSON.stringify(report, null, 2)}\n`);
process.stdout.write(report[0].filename);
' "${verify_json}"
)"
verify_tarball="${verify_directory}/${verify_filename}"
package_version="$(
tar -xOf "${verify_tarball}" package/package.json \
| node -e '
const fs = require("node:fs");
process.stdout.write(JSON.parse(fs.readFileSync(0, "utf8")).version);
'
)"
manifest_version="$(
tar -xOf "${verify_tarball}" package/adapters/hermes/plugin.yaml \
| awk '$1 == "version:" { print $2; exit }'
)"
if [ "${package_version}" != "${RELEASE_VERSION}" ]; then
echo "::error::Published package.json version ${package_version} does not match ${RELEASE_VERSION}."
exit 1
fi
if [ "${manifest_version}" != "${RELEASE_VERSION}" ]; then
echo "::error::Published Hermes manifest version ${manifest_version} does not match ${RELEASE_VERSION}."
exit 1
fi
}

published_version_visible=false
if npm_version_exists; then
published_version_visible=true
if remote_tag_exists "${RELEASE_TAG}"; then
echo "${PACKAGE_NAME}@${RELEASE_VERSION} and ${RELEASE_TAG} already exist; treating this as an idempotent rerun."
elif [ "${RECOVER_EXISTING_NPM_RELEASE:-false}" = "true" ]; then
Expand All @@ -128,7 +186,7 @@ else

for attempt in 1 2 3; do
set +e
npm publish --access public --tag "${NPM_DIST_TAG}" >"${attempt_directory}/${attempt}.log" 2>&1
npm publish "${RELEASE_TARBALL}" --access public --tag "${NPM_DIST_TAG}" >"${attempt_directory}/${attempt}.log" 2>&1
publish_status=$?
set -e
sed -n '1,160p' "${attempt_directory}/${attempt}.log"
Expand Down Expand Up @@ -159,7 +217,9 @@ else
fi
done

if ! wait_for_npm_version "${npm_visibility_attempts}"; then
if wait_for_npm_version "${npm_visibility_attempts}"; then
published_version_visible=true
else
if [ "${publish_accepted}" = "true" ]; then
echo "::warning::npm publish succeeded, but ${PACKAGE_NAME}@${RELEASE_VERSION} is not visible after propagation retries; continuing with tag, Release, and PR creation."
else
Expand All @@ -168,3 +228,9 @@ else
fi
fi
fi

if [ "${published_version_visible}" = "true" ]; then
verify_published_package
else
echo "::warning::Skipping registry tarball verification until ${PACKAGE_NAME}@${RELEASE_VERSION} becomes visible."
fi
58 changes: 58 additions & 0 deletions .github/scripts/publish-local-plugin.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,43 @@ case "\${1:-}" in
;;
publish)
increment_counter publish >/dev/null
printf '%s' "\${2:-}" > "\${NPM_MOCK_STATE_DIR}/published-argument"
if [ "\${NPM_MOCK_SCENARIO}" = "publish-fails" ]; then
echo "npm error code E500" >&2
exit 1
fi
echo "+ \${PACKAGE_NAME}@\${RELEASE_VERSION}"
exit 0
;;
pack)
increment_counter pack >/dev/null
destination=""
while [ "$#" -gt 0 ]; do
if [ "$1" = "--pack-destination" ]; then
destination="$2"
shift 2
continue
fi
shift
done
if [ -z "\${destination}" ]; then
echo "Missing --pack-destination" >&2
exit 2
fi
pack_root="\${NPM_MOCK_STATE_DIR}/pack-root"
filename="memtensor-memos-local-plugin-\${RELEASE_VERSION}.tgz"
rm -rf "\${pack_root}"
mkdir -p "\${pack_root}/package/adapters/hermes" "\${destination}"
printf '{"name":"%s","version":"%s"}\\n' \
"\${PACKAGE_NAME}" "\${RELEASE_VERSION}" \
> "\${pack_root}/package/package.json"
printf 'version: %s\\n' \
"\${NPM_MOCK_MANIFEST_VERSION:-\${RELEASE_VERSION}}" \
> "\${pack_root}/package/adapters/hermes/plugin.yaml"
tar -czf "\${destination}/\${filename}" -C "\${pack_root}" package
printf '[{"filename":"%s"}]\\n' "\${filename}"
exit 0
;;
*)
echo "Unexpected npm command: $*" >&2
exit 2
Expand All @@ -67,8 +97,10 @@ function runScenario(scenario, overrides = {}) {
mkdirSync(stateDirectory);

const npmPath = join(binDirectory, "npm");
const releaseTarball = join(fixtureDirectory, "release.tgz");
writeFileSync(npmPath, mockNpm, "utf8");
chmodSync(npmPath, 0o755);
writeFileSync(releaseTarball, "release fixture", "utf8");

const result = spawnSync("bash", [publishScript], {
cwd: fixtureDirectory,
Expand All @@ -81,6 +113,7 @@ function runScenario(scenario, overrides = {}) {
RELEASE_VERSION: "2.0.12",
RELEASE_TAG: "memos-local-plugin-v2.0.12",
NPM_DIST_TAG: "latest",
RELEASE_TARBALL: releaseTarball,
RECOVER_EXISTING_NPM_RELEASE: "false",
DOC_AGENT_RELEASE_FAILURE_URL: "",
DOC_AGENT_RELEASE_NOTES_DRAFT_TOKEN: "",
Expand All @@ -97,6 +130,14 @@ function runScenario(scenario, overrides = {}) {
...result,
viewCount: readCounter(stateDirectory, "view"),
publishCount: readCounter(stateDirectory, "publish"),
packCount: readCounter(stateDirectory, "pack"),
publishedArgument: (() => {
try {
return readFileSync(join(stateDirectory, "published-argument"), "utf8");
} catch {
return "";
}
})(),
};
rmSync(fixtureDirectory, { recursive: true, force: true });
return outcome;
Expand All @@ -108,6 +149,8 @@ test("waits through two post-publish 404 responses before the version becomes vi
assert.equal(result.status, 0, result.stderr);
assert.equal(result.publishCount, 1);
assert.equal(result.viewCount, 4);
assert.equal(result.packCount, 1);
assert.match(result.publishedArgument, /release\.tgz$/);
assert.match(result.stdout, /became visible on attempt 3/);
});

Expand All @@ -117,7 +160,9 @@ test("continues release metadata creation when publish succeeds but visibility r
assert.equal(result.status, 0, result.stderr);
assert.equal(result.publishCount, 1);
assert.equal(result.viewCount, 4);
assert.equal(result.packCount, 0);
assert.match(result.stdout, /npm publish succeeded.*continuing with tag, Release, and PR creation/s);
assert.match(result.stdout, /Skipping registry tarball verification/);
});

test("fails when publish fails and the requested version remains absent", () => {
Expand All @@ -127,3 +172,16 @@ test("fails when publish fails and the requested version remains absent", () =>
assert.equal(result.publishCount, 3);
assert.match(result.stdout + result.stderr, /npm publish failed after three attempts/);
});

test("fails when the published Hermes manifest version differs", () => {
const result = runScenario("eventually-visible", {
NPM_MOCK_MANIFEST_VERSION: "2.0.11",
});

assert.notEqual(result.status, 0);
assert.equal(result.packCount, 1);
assert.match(
result.stdout + result.stderr,
/Published Hermes manifest version 2\.0\.11 does not match 2\.0\.12/,
);
});
36 changes: 35 additions & 1 deletion .github/workflows/memos-local-plugin-post-merge-dry-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,51 @@ on:
- ".github/scripts/publish-local-plugin.sh"
- ".github/scripts/publish-local-plugin.test.mjs"
- ".github/scripts/retry.sh"
- "apps/memos-local-plugin/package.json"
- "apps/memos-local-plugin/package-lock.json"
- "apps/memos-local-plugin/adapters/hermes/plugin.yaml"
- "apps/memos-local-plugin/scripts/sync-hermes-version.cjs"
- "apps/memos-local-plugin/tests/unit/install/hermes-version-sync.test.ts"

permissions:
contents: write
pull-requests: write

jobs:
prepare-version:
if: ${{ github.repository == 'MemTensor/MemOS' }}
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4

- name: Generate dry-run prerelease version
id: version
working-directory: apps/memos-local-plugin
env:
RUN_NUMBER: ${{ github.run_number }}
run: |
version="$(
node -e '
const current = require("./package.json").version;
const match = current.match(/^(\d+)\.(\d+)\.(\d+)/);
if (!match) {
throw new Error(`Unsupported package version: ${current}`);
}
process.stdout.write(
`${match[1]}.${match[2]}.${Number(match[3]) + 1}-ci.${process.env.RUN_NUMBER}`,
);
'
)"
echo "version=${version}" >> "${GITHUB_OUTPUT}"

dry-run:
if: ${{ github.repository == 'MemTensor/MemOS' }}
needs: prepare-version
uses: ./.github/workflows/memos-local-plugin-publish.yml
with:
version: "2.0.10"
version: ${{ needs.prepare-version.outputs.version }}
tag: "latest"
git_ref: ""
release_notes: ""
Expand Down
66 changes: 63 additions & 3 deletions .github/workflows/memos-local-plugin-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,67 @@ jobs:
exit 1
fi
bash ../../.github/scripts/retry.sh --label "bump package version" -- npm version "${RELEASE_VERSION}" --no-git-tag-version --allow-same-version
npm run sync:hermes-version
npm run check:hermes-version

- name: Validate and build package
env:
RELEASE_VERSION: ${{ inputs.version }}
run: |
set -euo pipefail
bash ../../.github/scripts/retry.sh --attempts 2 --label "npm run lint" -- npm run lint
bash ../../.github/scripts/retry.sh --attempts 2 --label "npm test" -- npm test
bash ../../.github/scripts/retry.sh --label "npm pack dry-run" -- bash -euo pipefail -c 'npm pack --dry-run --json > "${RUNNER_TEMP}/memos-local-plugin-pack.json"'
release_dir="${RUNNER_TEMP}/memos-local-plugin-release"
pack_json="${RUNNER_TEMP}/memos-local-plugin-pack.json"
mkdir -p "${release_dir}"
bash ../../.github/scripts/retry.sh --label "npm pack" -- \
bash -euo pipefail -c 'npm pack --json --silent --pack-destination "$1" > "$2"' \
_ "${release_dir}" "${pack_json}"

pack_filename="$(
node -e '
const fs = require("node:fs");
const raw = fs.readFileSync(process.argv[1], "utf8");
const jsonStart = raw.match(/^\[/m);
if (!jsonStart || jsonStart.index === undefined) {
throw new Error("npm pack output did not contain a JSON report");
}
const report = JSON.parse(raw.slice(jsonStart.index));
if (!Array.isArray(report) || report.length !== 1 || !report[0].filename) {
throw new Error("npm pack did not report exactly one tarball");
}
fs.writeFileSync(process.argv[1], `${JSON.stringify(report, null, 2)}\n`);
process.stdout.write(report[0].filename);
' "${pack_json}"
)"
release_tarball="${release_dir}/${pack_filename}"
test -s "${release_tarball}"

package_version="$(
tar -xOf "${release_tarball}" package/package.json \
| node -e '
const fs = require("node:fs");
process.stdout.write(JSON.parse(fs.readFileSync(0, "utf8")).version);
'
)"
manifest_version="$(
tar -xOf "${release_tarball}" package/adapters/hermes/plugin.yaml \
| awk '$1 == "version:" { print $2; exit }'
)"
if [ "${package_version}" != "${RELEASE_VERSION}" ]; then
echo "::error::Tarball package.json version ${package_version} does not match ${RELEASE_VERSION}."
exit 1
fi
if [ "${manifest_version}" != "${RELEASE_VERSION}" ]; then
echo "::error::Tarball Hermes manifest version ${manifest_version} does not match ${RELEASE_VERSION}."
exit 1
fi

sha256sum "${release_tarball}" > "${release_tarball}.sha256"
{
echo "RELEASE_TARBALL=${release_tarball}"
echo "RELEASE_TARBALL_SHA256=${release_tarball}.sha256"
} >> "${GITHUB_ENV}"

- name: Draft GitHub Release notes
id: release_notes
Expand Down Expand Up @@ -232,6 +287,8 @@ jobs:

cp "${RELEASE_NOTES_FILE}" "${inspection_dir}/release-notes.md"
cp "${RUNNER_TEMP}/memos-local-plugin-pack.json" "${inspection_dir}/npm-pack.json"
cp "${RELEASE_TARBALL}" "${inspection_dir}/"
cp "${RELEASE_TARBALL_SHA256}" "${inspection_dir}/"
if [ -n "${EVIDENCE_FILE}" ] && [ -s "${EVIDENCE_FILE}" ]; then
cp "${EVIDENCE_FILE}" "${inspection_dir}/evidence.json"
fi
Expand Down Expand Up @@ -420,7 +477,7 @@ jobs:
--base "${DEFAULT_BRANCH}" \
--head "${release_branch}" \
--title "${release_title}" \
--body "Bumps apps/memos-local-plugin/package.json for the published npm release ${RELEASE_VERSION}."
--body "Synchronizes package and Hermes metadata for the published npm release ${RELEASE_VERSION}."
status=$?
set -e
if [ "${status}" = 0 ]; then
Expand All @@ -438,7 +495,10 @@ jobs:
done
}

git add apps/memos-local-plugin/package.json apps/memos-local-plugin/package-lock.json
git add \
apps/memos-local-plugin/package.json \
apps/memos-local-plugin/package-lock.json \
apps/memos-local-plugin/adapters/hermes/plugin.yaml
created_release_commit=false
if ! git diff --staged --quiet; then
git commit -m "${release_title}"
Expand Down
Loading
Loading