Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add useVersion support to npm changelog #2347

Merged
merged 1 commit into from Apr 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/auto-args.ts
Expand Up @@ -83,6 +83,8 @@ export type IChangelogOptions = BaseBranch &
to?: string;
/** Do not make any changes to changelog file */
noChanges?: boolean;
/** Override the version to release */
useVersion?: string;
};

export type IReleaseOptions = BaseBranch &
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/auto.ts
Expand Up @@ -96,6 +96,8 @@ interface ChangelogLifecycle {
currentVersion: string;
/** The last version of the project */
lastRelease: string;
/** Override the version to release */
useVersion?: string;
}

interface TestingToken {
Expand Down Expand Up @@ -1933,12 +1935,13 @@ export default class Auto {
this.logger.log.info("New Release Notes\n", releaseNotes);

const currentVersion = await this.getCurrentVersion(lastRelease);
const context = {
const context: ChangelogLifecycle = {
bump,
commits: await this.release.getCommits(lastRelease, to || undefined),
releaseNotes,
lastRelease,
currentVersion,
useVersion: options.useVersion,
};

if (!noChanges) {
Expand Down
66 changes: 66 additions & 0 deletions plugins/npm/__tests__/monorepo-log.test.ts
Expand Up @@ -370,3 +370,69 @@ test("should create extra change logs for sub-packages", async () => {
"packages/@foobar/release/CHANGELOG.md"
);
});


test("should respect the useVersion config option", async () => {
readFileSync.mockReturnValue('{ "version": "independent" }');

getLernaPackages.mockImplementation(async () =>
Promise.resolve([
{
path: "packages/@foobar/release",
name: "@foobar/release",
version: "1.0.0",
},
{
path: "packages/@foobar/party",
name: "@foobar/party",
version: "1.0.0",
},
])
);

exec.mockImplementation(
async () =>
"packages/@foobar/release/README.md\npackages/@foobar/party/package.json"
);

execPromise.mockResolvedValueOnce("@foobar/release");

const plugin = new NpmPlugin();
const hooks = makeHooks();
const update = jest.fn();

plugin.apply({
config: { prereleaseBranches: ["next"] },
hooks,
logger: dummyLog(),
release: {
updateChangelogFile: update,
makeChangelog: () => {
const t = new Changelog(dummyLog(), {
owner: "andrew",
repo: "test",
baseUrl: "https://github.custom.com/",
labels: defaultLabels,
baseBranch: "main",
prereleaseBranches: ["next"],
});
t.hooks.renderChangelogTitle.tap("test", (label) => label);
return t;
},
} as any,
} as Auto.Auto);
await hooks.beforeCommitChangelog.promise({
bump: Auto.SEMVER.patch,
commits: await commitsPromise,
currentVersion: "1.0.0",
useVersion: "2.0.0-alpha.1",
lastRelease: "0.1.0",
releaseNotes: "",
});

expect(update).toHaveBeenCalledWith(
"v2.0.0-alpha.1",
"major\n- [PLAYA-5052] - Some Feature [#12345](https://github.custom.com/pull/12345)",
"packages/@foobar/release/CHANGELOG.md"
);
});
68 changes: 45 additions & 23 deletions plugins/npm/src/index.ts
Expand Up @@ -152,7 +152,6 @@ export async function getChangedPackages({
return [...changed];
}


/** Get the package with the greatest version in a monorepo */
export function getMonorepoPackage() {
const packages = getPackages(process.cwd());
Expand All @@ -162,7 +161,9 @@ export function getMonorepoPackage() {
}

// Remove pre-releases so that released package versions take precedence
let releasedPackages = packages.filter(subPackage => prerelease(subPackage.package?.version || '') === null);
let releasedPackages = packages.filter(
(subPackage) => prerelease(subPackage.package?.version || "") === null
);
// If doing this would remove all packages, this means were not any @latest releases yet
// In that case, restore the original list of packages.
if (releasedPackages.length === 0) {
Expand Down Expand Up @@ -219,10 +220,7 @@ interface GetArgsOptions {
}

/** Get the args to use legacy auth */
function getLegacyAuthArgs(
useLegacy: boolean,
options: GetArgsOptions = {}
) {
function getLegacyAuthArgs(useLegacy: boolean, options: GetArgsOptions = {}) {
if (!useLegacy) {
return [];
}
Expand All @@ -241,9 +239,7 @@ function getPublishFolderArgs(
return [];
}

return options.isMonorepo
? ["--contents", publishFolder]
: [publishFolder];
return options.isMonorepo ? ["--contents", publishFolder] : [publishFolder];
}

/** Get the args to set the registry. Only used with lerna */
Expand Down Expand Up @@ -290,7 +286,11 @@ const markdownList = (lines: string[]) =>
lines.map((line) => `- \`${line}\``).join("\n");

/** Get the previous version. Typically from a package distribution description file. */
async function getPreviousVersion(auto: Auto, prereleaseBranch: string, isMaintenanceBranch: boolean) {
async function getPreviousVersion(
auto: Auto,
prereleaseBranch: string,
isMaintenanceBranch: boolean
) {
let previousVersion = "";

if (isMonorepo()) {
Expand All @@ -307,7 +307,10 @@ async function getPreviousVersion(auto: Auto, prereleaseBranch: string, isMainte
} else {
const releasedPackage = getMonorepoPackage();

if (isMaintenanceBranch || (!releasedPackage.name && !releasedPackage.version)) {
if (
isMaintenanceBranch ||
(!releasedPackage.name && !releasedPackage.version)
) {
previousVersion = auto.prefixRelease(monorepoVersion);
} else {
previousVersion = await greaterRelease(
Expand All @@ -323,8 +326,8 @@ async function getPreviousVersion(auto: Auto, prereleaseBranch: string, isMainte
"Using package.json to calculate previous version"
);
const { version, name } = await loadPackageJson();
if(isMaintenanceBranch && version) {
previousVersion = version
if (isMaintenanceBranch && version) {
previousVersion = version;
} else {
previousVersion = version
? await greaterRelease(
Expand Down Expand Up @@ -702,8 +705,12 @@ export default class NPMPlugin implements IPlugin {

let isMaintenanceBranch = false;

if(auto.config?.versionBranches && branch) {
isMaintenanceBranch = branch.includes(typeof auto.config.versionBranches === "boolean" ? "version-" : auto.config.versionBranches)
if (auto.config?.versionBranches && branch) {
isMaintenanceBranch = branch.includes(
typeof auto.config.versionBranches === "boolean"
? "version-"
: auto.config.versionBranches
);
}

auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => {
Expand Down Expand Up @@ -810,10 +817,13 @@ export default class NPMPlugin implements IPlugin {
}

// Allows us to see the commit being assessed
auto.logger.veryVerbose.info(`Rendering changelog line for commit:`, commit)
auto.logger.veryVerbose.info(
`Rendering changelog line for commit:`,
commit
);

// adds commits to changelog only if hash is resolvable
if(!commit || !commit.hash) {
if (!commit || !commit.hash) {
return line;
}

Expand Down Expand Up @@ -885,7 +895,7 @@ export default class NPMPlugin implements IPlugin {

auto.hooks.beforeCommitChangelog.tapPromise(
this.name,
async ({ commits, bump }) => {
async ({ commits, bump, useVersion }) => {
if (!isMonorepo() || !auto.release || !this.subPackageChangelogs) {
return;
}
Expand Down Expand Up @@ -924,7 +934,9 @@ export default class NPMPlugin implements IPlugin {
const includedCommits = commits.filter((commit) =>
commit.files.some((file) => inFolder(lernaPackage.path, file))
);
const title = `v${inc(lernaPackage.version, bump as ReleaseType)}`;
const title = `v${
useVersion || inc(lernaPackage.version, bump as ReleaseType)
}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only semantic change in this file, everything else was prettier

const releaseNotes = await changelog.generateReleaseNotes(
includedCommits
);
Expand Down Expand Up @@ -985,7 +997,9 @@ export default class NPMPlugin implements IPlugin {

logVersion(canaryPackageList.join("\n"));
} else {
logVersion(useVersion || inc(monorepoVersion, bump as ReleaseType) || bump);
logVersion(
useVersion || inc(monorepoVersion, bump as ReleaseType) || bump
);
}

return;
Expand All @@ -994,7 +1008,9 @@ export default class NPMPlugin implements IPlugin {
const monorepoBump =
isIndependent || !isBaseBranch
? useVersion || bump
: useVersion || (await bumpLatest(getMonorepoPackage(), bump)) || bump;
: useVersion ||
(await bumpLatest(getMonorepoPackage(), bump)) ||
bump;

let commitMessage = isIndependent
? "Bump independent versions [skip ci]"
Expand Down Expand Up @@ -1028,7 +1044,9 @@ export default class NPMPlugin implements IPlugin {
}

const latestBump = isBaseBranch
? useVersion || (await bumpLatest(await loadPackageJson(), bump)) || bump
? useVersion ||
(await bumpLatest(await loadPackageJson(), bump)) ||
bump
: useVersion || bump;

if (dryRun) {
Expand Down Expand Up @@ -1260,7 +1278,11 @@ export default class NPMPlugin implements IPlugin {
const lastRelease = await auto.git!.getLatestRelease();
const latestTag =
(await auto.git?.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await getPreviousVersion(auto, prereleaseBranch, isMaintenanceBranch));
(await getPreviousVersion(
auto,
prereleaseBranch,
isMaintenanceBranch
));

if (isMonorepo()) {
auto.logger.verbose.info("Detected monorepo, using lerna");
Expand Down