Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a9f0cf2
Move codeSpan to lib, where plx release uses it beside ship
davidjalbers Dec 6, 2026
182fc32
Test: plx promote against local bare repositories
davidjalbers Dec 6, 2026
226e51d
plx promote: fast-forward env/<name> on the remote to the head of its…
davidjalbers Dec 6, 2026
720b057
Test: plx release against local bare repositories and a stand-in for gh
davidjalbers Dec 6, 2026
e5caf21
plx release: tag origin/main with the next version and create its rel…
davidjalbers Dec 6, 2026
227af7b
Test: a repo renders no promote or release task
davidjalbers Dec 6, 2026
8dc43c7
The repo-turbo template renders no promote task: plx promote and plx …
davidjalbers Dec 6, 2026
e29973d
The narration of a promotion names where the branch moves from and to…
davidjalbers Dec 6, 2026
a0880f1
The comparison with the earlier render of an app repo's mise.toml lea…
davidjalbers Dec 6, 2026
cc0877e
Test: a long release, the first release's notes, remote tags only, gh…
davidjalbers Dec 6, 2026
205284f
plx promote and plx release: explicit refspecs, remote tags only, not…
davidjalbers Dec 6, 2026
aeb4e9b
Test: release in a clone that fetches another branch than main
davidjalbers Dec 6, 2026
cd7db4b
Test: the narration of a long release shows some subjects and counts …
davidjalbers Dec 6, 2026
438e55e
plx release: the narration lists twenty subjects and counts the rest
davidjalbers Dec 6, 2026
4b9d825
Test: a process that exits without reading its input fails by its exi…
davidjalbers Dec 6, 2026
2bae087
exec: a process that exits without reading its input fails by its exi…
davidjalbers Dec 6, 2026
228cdd4
Test: a run of plx release finishes a release whose tag is pushed, wi…
davidjalbers Dec 6, 2026
6187225
plx release finishes a release whose tag is pushed and has none, and …
davidjalbers Dec 6, 2026
0105cc2
Test: a finishing run says so, a failing gh release view stops it
davidjalbers Dec 6, 2026
4104399
plx release: a run that only finishes a release says so, and only gh'…
davidjalbers Dec 6, 2026
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
4 changes: 4 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { register as deploy } from './deploy.ts';
import { register as doctor } from './doctor.ts';
import { register as newSite } from './new.ts';
import { register as overview } from './overview.ts';
import { register as promote } from './promote.ts';
import { register as release } from './release.ts';
import { register as requirements } from './requirements.ts';
import { register as umbrella } from './umbrella.ts';
import { register as update } from './update.ts';
Expand All @@ -28,4 +30,6 @@ export const commands: ((program: Command) => void)[] = [
requirements,
changeset,
deploy,
promote,
release,
];
275 changes: 275 additions & 0 deletions packages/cli/src/commands/promote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import assert from 'node:assert/strict';
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { test } from 'node:test';
import * as lib from '@plexus-ms/lib';
import { Context, realIo } from '@plexus-ms/lib';
import { commit, dir, fixture, git, plx, refs, snapshot } from '../testing/forge.ts';

const ref = (origin: string, branch: string) => git(origin, 'rev-parse', '-q', '--verify', `refs/heads/${branch}`);
const promote = (repo: string, ...args: string[]) => plx('-y', '--json', 'promote', ...args, '-C', repo);

test('promote creates env/<name> on the remote at the head of main when it does not exist', async () => {
const { repo, origin } = fixture('creates');
const head = git(origin, 'rev-parse', 'main');
const { code, stdout } = await promote(repo, 'staging');
assert.equal(code, 0);
assert.equal(ref(origin, 'env/staging'), head);
assert.deepEqual(JSON.parse(stdout), {
environment: 'staging',
branch: 'env/staging',
source: 'main',
from: null,
to: head,
commits: 1,
outcome: 'created',
});
});

test('promote fast-forwards env/<name> to the head of its source, and the commits it moves over are counted', async () => {
const { repo, origin } = fixture('forwards');
await promote(repo, 'staging');
const before = ref(origin, 'env/staging');
commit(repo, 'second');
const head = commit(repo, 'third');
git(repo, 'push', '-q', 'origin', 'main');
const { stdout } = await promote(repo, 'staging');
assert.equal(ref(origin, 'env/staging'), head);
assert.deepEqual(JSON.parse(stdout), {
environment: 'staging',
branch: 'env/staging',
source: 'main',
from: before,
to: head,
commits: 2,
outcome: 'fast-forwarded',
});
});

test('promote takes the source from the remote: a main that is not pushed is not promoted', async () => {
const { repo, origin } = fixture('unpushed');
commit(repo, 'local only');
await promote(repo, 'staging');
assert.equal(ref(origin, 'env/staging'), git(origin, 'rev-parse', 'main'));
assert.notEqual(ref(origin, 'env/staging'), git(repo, 'rev-parse', 'HEAD'));
});

test('promote of an environment whose source is another one moves it to where that one is on the remote', async () => {
const { repo, origin } = fixture('chain');
await promote(repo, 'staging');
commit(repo, 'not yet in staging');
git(repo, 'push', '-q', 'origin', 'main');
const { stdout } = await promote(repo, 'production');
assert.equal(ref(origin, 'env/production'), ref(origin, 'env/staging'));
assert.equal(JSON.parse(stdout).source, 'env/staging');
assert.notEqual(ref(origin, 'env/production'), git(origin, 'rev-parse', 'main'));
});

test('promote refuses an environment whose source branch is not on the remote yet, and says to promote that one first', async () => {
const { repo, origin } = fixture('source-missing');
const before = refs(origin);
await assert.rejects(promote(repo, 'production'), /env\/staging.*not on the remote.*promote staging first/s);
assert.deepEqual(refs(origin), before);
});

test('promote refuses a move that is no fast-forward, and leaves the branch where it is', async () => {
const { repo, origin } = fixture('diverged');
await promote(repo, 'staging');
// Someone put a commit of their own on the environment's branch.
const other = join(dir, 'diverged-other');
git(dir, 'clone', '-q', origin, other);
git(other, 'checkout', '-q', 'env/staging');
const theirs = commit(other, 'a hotfix on the branch');
git(other, 'push', '-q', 'origin', 'env/staging');
commit(repo, 'next');
git(repo, 'push', '-q', 'origin', 'main');
const before = refs(origin);
await assert.rejects(promote(repo, 'staging'), /env\/staging is not an ancestor of main.*fast-forward/s);
assert.deepEqual(refs(origin), before);
assert.equal(ref(origin, 'env/staging'), theirs);
});

test('promote of a branch already at its source pushes nothing and says so', async () => {
const { repo, origin } = fixture('unchanged');
await promote(repo, 'staging');
const before = refs(origin);
const { code, stdout } = await plx('-y', 'promote', 'staging', '-C', repo);
assert.equal(code, 0);
assert.deepEqual(refs(origin), before);
assert.match(stdout, /^✓ env\/staging is at main already/);
assert.equal(JSON.parse((await promote(repo, 'staging')).stdout).outcome, 'unchanged');
});

test('promote refuses an undeclared name, naming the declared ones', async () => {
const { repo, origin } = fixture('undeclared');
const before = refs(origin);
await assert.rejects(promote(repo, 'qa'), /qa is no declared environment.*staging, production/s);
await assert.rejects(promote(repo, 'main'), /main is no declared environment/);
assert.deepEqual(refs(origin), before);
});

test('promote refuses an invalid declaration, and a repo that declares nothing', async () => {
const invalid = fixture('invalid', '---\nenvironments:\n staging:\n promotes_from: develop\n---\n');
await assert.rejects(promote(invalid.repo, 'staging'), /invalid.*develop/s);
const none = fixture('none', '---\n---\n');
await assert.rejects(promote(none.repo, 'staging'), /declares no environment/);
assert.deepEqual(refs(invalid.origin), [`refs/heads/main ${git(invalid.origin, 'rev-parse', 'main')}`]);
});

test('promote takes the one name and no other argument', async () => {
const { repo, origin } = fixture('arguments');
const before = refs(origin);
await assert.rejects(promote(repo, 'staging', 'production'), /too many arguments/);
await assert.rejects(promote(repo, 'staging', '--force'), /unknown option '--force'/);
await assert.rejects(plx('-y', 'promote', '-C', repo), /missing required argument 'name'/);
assert.deepEqual(refs(origin), before);
});

test('promote changes nothing of the checkout: not its branch, head, branches or files, wherever it stands and however dirty', async () => {
const { repo, origin } = fixture('checkout');
git(repo, 'checkout', '-q', '-b', 'topic');
commit(repo, 'topic work');
writeFileSync(join(repo, 'notes.txt'), 'edited, not committed\n');
writeFileSync(join(repo, 'untracked.txt'), 'x\n');
const before = snapshot(repo);
await promote(repo, 'staging');
assert.deepEqual(snapshot(repo), before);
assert.equal(ref(origin, 'env/staging'), git(origin, 'rev-parse', 'main'));
});

test('promote asks before it pushes, and off a terminal without --yes it is an error that pushes nothing', async () => {
const { repo, origin } = fixture('asks');
const before = refs(origin);
await assert.rejects(plx('promote', 'staging', '-C', repo), /cannot ask .* without a terminal; pass --yes/);
assert.deepEqual(refs(origin), before);
});

test('promote --dry-run prints what it would push and pushes nothing', async () => {
const { repo, origin } = fixture('dry');
const before = refs(origin);
const { code, stderr } = await plx('--dry-run', 'promote', 'staging', '-C', repo);
assert.equal(code, 0);
assert.match(stderr, /\[dry-run\] \$ \(cd .*git push --no-verify origin [0-9a-f]{40}:refs\/heads\/env\/staging/);
assert.deepEqual(refs(origin), before);
});

test('promote --quiet narrates nothing and --json prints the result alone', async () => {
const { repo } = fixture('quiet');
const { stdout, stderr } = await plx('-y', '-q', '--json', 'promote', 'staging', '-C', repo);
assert.equal(stderr, '');
assert.equal(JSON.parse(stdout).outcome, 'created');
});

test('promote pushes nothing when the answer to the question is no, and asks it once, naming the branch', async () => {
const { repo, origin } = fixture('declined');
const asked: string[] = [];
const ctx = new Context(
{ quiet: true },
{
...realIo(),
prompt: async (question) => {
asked.push(question);
return false;
},
},
);
const before = refs(origin);
await assert.rejects(lib.promote(ctx, repo, 'staging'), /aborted; nothing was pushed/);
assert.deepEqual(asked, ['push env/staging?']);
assert.deepEqual(refs(origin), before);
});

/** A clone of the fixture's origin as a workflow's checkout makes it: only `main`, and perhaps only the last commits. */
function narrow(name: string, origin: string, ...options: string[]): string {
const clone = join(dir, name);
git(dir, 'clone', '-q', '--single-branch', '--branch', 'main', ...options, `file://${origin}`, clone);
return clone;
}

/** A remote whose env/staging is `behind` commits behind main, and whose main holds `behind + 1` commits. */
function behind(name: string, ahead: number) {
const f = fixture(name);
git(f.repo, 'push', '-q', 'origin', 'main:refs/heads/env/staging');
for (let i = 0; i < ahead; i++) commit(f.repo, `next ${i}`);
git(f.repo, 'push', '-q', 'origin', 'main');
return f;
}

test('promote in a clone that fetched only main gives the answers of a full clone: it sees the branch that exists', async () => {
const { origin } = behind('single', 2);
const clone = narrow('single-clone', origin);
const staging = ref(origin, 'env/staging');
const forwarded = JSON.parse((await promote(clone, 'staging')).stdout);
assert.equal(forwarded.outcome, 'fast-forwarded');
assert.equal(forwarded.from, staging);
assert.equal(forwarded.commits, 2);
assert.equal(ref(origin, 'env/staging'), git(origin, 'rev-parse', 'main'));
const production = JSON.parse((await promote(clone, 'production')).stdout);
assert.equal(production.outcome, 'created', 'its source, env/staging, is on the remote');
assert.equal(ref(origin, 'env/production'), ref(origin, 'env/staging'));
assert.equal(JSON.parse((await promote(clone, 'staging')).stdout).outcome, 'unchanged');
});

test('promote in a clone that fetched only main refuses a move that is no fast-forward, as a full clone does', async () => {
const { origin } = behind('single-diverged', 1);
const other = join(dir, 'single-diverged-other');
git(dir, 'clone', '-q', origin, other);
git(other, 'checkout', '-q', 'env/staging');
commit(other, 'a hotfix on the branch');
git(other, 'push', '-q', 'origin', 'env/staging');
const before = refs(origin);
await assert.rejects(promote(narrow('single-diverged-clone', origin), 'staging'), /not an ancestor of main/);
assert.deepEqual(refs(origin), before);
});

test('promote in a shallow clone gives the answers of a full clone where the history it holds decides', async () => {
const { origin } = behind('shallow', 2);
const clone = narrow('shallow-clone', origin, '--depth', '5');
const forwarded = JSON.parse((await promote(clone, 'staging')).stdout);
assert.equal(forwarded.outcome, 'fast-forwarded');
assert.equal(forwarded.commits, 2);
assert.equal(ref(origin, 'env/staging'), git(origin, 'rev-parse', 'main'));
assert.equal(JSON.parse((await promote(clone, 'production')).stdout).outcome, 'created');
});

test('promote in a shallow clone that cannot tell whether the move is a fast-forward says so and names the remedy', async () => {
const { origin } = behind('shallow-cut', 3);
const clone = narrow('shallow-cut-clone', origin, '--depth', '1');
const before = refs(origin);
await assert.rejects(promote(clone, 'staging'), /shallow clone.*cannot tell.*git fetch --unshallow/s);
assert.deepEqual(refs(origin), before);
});

test('promote names what the move is in its narration: where the branch is, and where it goes', async () => {
const { repo, origin } = behind('narration', 1);
const from = ref(origin, 'env/staging').slice(0, 7);
const to = git(origin, 'rev-parse', 'main').slice(0, 7);
const moved = await plx('-y', 'promote', 'staging', '-C', repo);
assert.match(
moved.stderr,
new RegExp(`move env/staging on origin from ${from} to ${to}, the head of main: 1 commit`),
);
const created = await plx('-y', 'promote', 'production', '-C', repo);
assert.match(created.stderr, new RegExp(`create env/production on origin at ${to}, the head of env/staging`));
});

test('promote in a subdirectory of the repo reads the repo’s root marker, and outside a repo refuses', async () => {
const { repo, origin } = fixture('subdirectory');
mkdirSync(join(repo, 'packages', 'web'), { recursive: true });
writeFileSync(join(repo, 'packages', 'web', 'PLEXUS.md'), '---\n---\n');
const { code } = await promote(join(repo, 'packages', 'web'), 'staging');
assert.equal(code, 0);
assert.equal(ref(origin, 'env/staging'), git(origin, 'rev-parse', 'main'));
const outside = join(dir, 'outside');
mkdirSync(outside);
await assert.rejects(promote(outside, 'staging'), /is not inside a git repository/);
});

test('promote where the remote cannot be read shows git’s own message and pushes nothing', async () => {
const { repo } = fixture('unreadable');
git(repo, 'remote', 'set-url', 'origin', join(dir, 'there-is-no-such-remote.git'));
await assert.rejects(
promote(repo, 'staging'),
/there-is-no-such-remote\.git.*(does not appear to be a git repository|No such file)/s,
);
});
30 changes: 30 additions & 0 deletions packages/cli/src/commands/promote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// plx promote <name>: fast-forward `env/<name>` on the remote to the head of
// its declared source, `main` or another environment's branch, and create the
// branch when it does not exist (§ 6.8 Decisions). lib's `promote` does the
// work, and its header says how, in order; this file maps the arguments and
// prints the result. It takes no other argument and has no override. Of the
// checkout it reads the root marker of the repo the directory is in; it
// changes no file, branch or checked-out branch, only the remote-tracking refs
// it fetches and their reflogs.
//
// plx promote staging
// plx -y promote production # without a terminal
// plx -n promote staging -C ../shop # print what would be pushed
import { resolve } from 'node:path';
import { promote, renderPromoted } from '@plexus-ms/lib';
import type { Command } from 'commander';
import { contextOf } from '../program.ts';

export function register(program: Command): void {
program
.command('promote')
.description(
'fast-forward env/<name> on the remote to where its declared source is, creating the branch when it does not exist',
)
.argument('<name>', 'a declared environment')
.option('-C, --dir <dir>', 'the checkout to read the declaration from', '.')
.action(async (name: string, opts: { dir: string }, command: Command) => {
const ctx = contextOf(command);
ctx.result(await promote(ctx, resolve(opts.dir), name), renderPromoted);
});
}
Loading