Skip to content

Commit

Permalink
Expose release plan markdown summary function (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Sep 20, 2024
1 parent dad039b commit 7470505
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: breaking, feature, fix, internal
changeKind: feature
packages:
- "@chronus/chronus"
---

[API] Expose function to get markdown summary of release plan
2 changes: 1 addition & 1 deletion .github/workflows/prepare-release-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
name: Build

- name: Create release branch
run: node ./eng/prepare-release-branch.js
run: pnpm tsx ./eng/prepare-release-pr.ts
env:
GITHUB_TOKEN: ${{secrets.CUSTOM_GITHUB_TOKEN}}
11 changes: 8 additions & 3 deletions eng/prepare-release-branch.js → eng/prepare-release-pr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/* eslint-disable no-console */
/* eslint-disable no-undef */
// @ts-check
import { context, getOctokit } from "@actions/github";
import { execSync } from "child_process";
import { showStatusAsMarkdown } from "../packages/chronus/dist/cli/commands/show-status.js";
import {
NodeChronusHost,
renderReleasePlanAsMarkdown,
resolveCurrentReleasePlan,
} from "../packages/chronus/src/index.js";
import {} from "../packages/chronus/src/release-plan/current.js";
const branchName = "publish/auto-release";

const changeStatus = await showStatusAsMarkdown(process.cwd());
const plan = await resolveCurrentReleasePlan(NodeChronusHost, process.cwd());
const changeStatus = await renderReleasePlanAsMarkdown(plan);
execSync(`pnpm change version`, { stdio: "inherit" });
const stdout = execSync(`git status --porcelain`).toString();

Expand Down
7 changes: 7 additions & 0 deletions eng/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*.ts", "../packages/chronus/src/index.ts"]
}
41 changes: 3 additions & 38 deletions packages/chronus/src/cli/commands/show-status.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pc from "picocolors";
import type { ReleaseAction, ReleasePlan } from "../../release-plan/types.js";
import { resolveCurrentReleasePlan } from "../../release-plan/current.js";
import type { ReleaseAction } from "../../release-plan/types.js";
import type { VersionType } from "../../types.js";
import { NodeChronusHost } from "../../utils/node-host.js";
import { loadChronusWorkspace } from "../../workspace/load.js";
import { resolveReleasePlan, type BumpVersionOptions } from "./bump-versions.js";

export interface ShowStatusOptions {
readonly ignorePolicies?: boolean;
Expand All @@ -15,7 +14,7 @@ function log(...args: any[]) {
console.log(...args);
}
export async function showStatus(cwd: string, options: ShowStatusOptions): Promise<void> {
const releasePlan = await resolveCurrentReleasePlan(cwd, options);
const releasePlan = await resolveCurrentReleasePlan(NodeChronusHost, cwd, options);

let max = 50;
for (const action of releasePlan.actions) {
Expand Down Expand Up @@ -49,37 +48,3 @@ function logType(actions: ReleaseAction[], type: VersionType, pad: number, color
}
}
}

async function resolveCurrentReleasePlan(cwd: string, options?: BumpVersionOptions): Promise<ReleasePlan> {
const host = NodeChronusHost;
const workspace = await loadChronusWorkspace(host, cwd);
return await resolveReleasePlan(host, workspace, options);
}

export async function showStatusAsMarkdown(cwd: string): Promise<string> {
const releasePlan = await resolveCurrentReleasePlan(cwd);
return [
"",
"## Change summary:",
"",
...typeAsMarkdown(releasePlan.actions, "major"),
"",
...typeAsMarkdown(releasePlan.actions, "minor"),
"",
...typeAsMarkdown(releasePlan.actions, "patch"),
"",
].join("\n");
}

function typeAsMarkdown(actions: ReleaseAction[], type: VersionType): string[] {
const bold = (x: string) => `**${x}**`;
const filteredActions = actions.filter((x) => x.type === type);
if (filteredActions.length === 0) {
return [`### ${"No"} packages to be bumped at ${bold(type)}`];
} else {
return [
`### ${filteredActions.length} packages to be bumped at ${bold(type)}:`,
...filteredActions.map((action) => `- ${action.packageName} \`${action.oldVersion}\` → \`${action.newVersion}\``),
];
}
}
1 change: 1 addition & 0 deletions packages/chronus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type { AreaStatus, ChangeArea, ChangeStatus, PackageStatus } from "./change/find.js";
export { getWorkspaceStatus } from "./change/get-workspace-status.js";
export { renderReleasePlanAsMarkdown, resolveCurrentReleasePlan } from "./release-plan/index.js";
export { NodeChronusHost } from "./utils/node-host.js";
export { loadChronusWorkspace } from "./workspace/index.js";
export type { ChronusWorkspace } from "./workspace/types.js";
13 changes: 13 additions & 0 deletions packages/chronus/src/release-plan/current.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type BumpVersionOptions, resolveReleasePlan } from "../cli/commands/bump-versions.js";
import type { ChronusHost } from "../utils/host.js";
import { loadChronusWorkspace } from "../workspace/load.js";
import type { ReleasePlan } from "./types.js";

export async function resolveCurrentReleasePlan(
host: ChronusHost,
cwd: string,
options?: BumpVersionOptions,
): Promise<ReleasePlan> {
const workspace = await loadChronusWorkspace(host, cwd);
return await resolveReleasePlan(host, workspace, options);
}
2 changes: 2 additions & 0 deletions packages/chronus/src/release-plan/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { assembleReleasePlan } from "./assemble-release-plan.js";
export { resolveCurrentReleasePlan } from "./current.js";
export { renderReleasePlanAsMarkdown } from "./markdown.js";
export type * from "./types.js";
32 changes: 32 additions & 0 deletions packages/chronus/src/release-plan/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { VersionType } from "../types.js";
import type { ReleaseAction, ReleasePlan } from "./types.js";

/**
* Render a markdown summary of the given release plan
*/
export async function renderReleasePlanAsMarkdown(releasePlan: ReleasePlan): Promise<string> {
return [
"",
"## Change summary:",
"",
...typeAsMarkdown(releasePlan.actions, "major"),
"",
...typeAsMarkdown(releasePlan.actions, "minor"),
"",
...typeAsMarkdown(releasePlan.actions, "patch"),
"",
].join("\n");
}

function typeAsMarkdown(actions: ReleaseAction[], type: VersionType): string[] {
const bold = (x: string) => `**${x}**`;
const filteredActions = actions.filter((x) => x.type === type);
if (filteredActions.length === 0) {
return [`### ${"No"} packages to be bumped at ${bold(type)}`];
} else {
return [
`### ${filteredActions.length} packages to be bumped at ${bold(type)}:`,
...filteredActions.map((action) => `- ${action.packageName} \`${action.oldVersion}\` → \`${action.newVersion}\``),
];
}
}

0 comments on commit 7470505

Please sign in to comment.