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

fix: get latest maintenance major tag from github releases #2076

Merged
merged 11 commits into from Oct 26, 2021
4 changes: 3 additions & 1 deletion packages/core/src/auto.ts
Expand Up @@ -1684,7 +1684,9 @@ export default class Auto {
private async oldRelease(
options: IShipItOptions
): Promise<ShipitInfo | undefined> {
const latestTag = await this.git?.getLatestTagInBranch();
const latestTag = await this.git?.getLatestTagInBranch(
getCurrentBranch() || ""
);
const result = await this.publishFullRelease({
...options,
from: latestTag,
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/utils/get-current-branch.ts
Expand Up @@ -3,16 +3,21 @@ import { execSync } from "child_process";

const env = envCi();

/**
* Validates that the given branch name should be returned by environment context
*/
const isValidBranch = (branch: string | undefined) => typeof branch === "string" && branch !== "undefined"

/** Get the current branch the git repo is set to */
export function getCurrentBranch() {
const isPR = "isPr" in env && env.isPr;
let branch: string | undefined;
// env-ci sets branch to target branch (ex: main) in some CI services.
// so we should make sure we aren't in a PR just to be safe

if (isPR && "prBranch" in env) {
if (isPR && "prBranch" in env && isValidBranch(env.prBranch)) {
branch = env.prBranch;
} else {
} else if(isValidBranch(env.branch)) {
branch = env.branch;
}

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/utils/load-plugins.ts
Expand Up @@ -166,18 +166,18 @@ export function findPlugin(
return userPlugin;
}

const officialPlugin = path.join("@auto-it", pluginPath);
const canaryPlugin = path.join("@auto-canary", pluginPath);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This prioritizes Canaries so I could test the NPM plugin 😄


// Try importing official plugin
if (exists(officialPlugin)) {
return officialPlugin;
if (exists(canaryPlugin)) {
return canaryPlugin;
}

const canaryPlugin = path.join("@auto-canary", pluginPath);
const officialPlugin = path.join("@auto-it", pluginPath);

// Try importing official plugin
if (exists(canaryPlugin)) {
return canaryPlugin;
if (exists(officialPlugin)) {
return officialPlugin;
}

// Try requiring a package
Expand Down
43 changes: 30 additions & 13 deletions plugins/npm/src/index.ts
Expand Up @@ -264,9 +264,15 @@ 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) {
async function getPreviousVersion(auto: Auto, prereleaseBranch: string, currentBranch: string | undefined) {
let previousVersion = "";

let isMaintenanceBranch = false;

if(auto.config?.versionBranches && currentBranch) {
isMaintenanceBranch = currentBranch.includes(typeof auto.config.versionBranches === "boolean" ? "version-" : auto.config.versionBranches)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Open to thoughts of how to source the default here

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this check could go here as versionBranch instead (to align with the naming style for how preleaseBranch), then you could just pass versionBranch as an argument to getPreviousVersion directly.

}

if (isMonorepo()) {
auto.logger.veryVerbose.info(
"Using monorepo to calculate previous release"
Expand All @@ -281,7 +287,7 @@ async function getPreviousVersion(auto: Auto, prereleaseBranch: string) {
} else {
const releasedPackage = getMonorepoPackage();

if (!releasedPackage.name && !releasedPackage.version) {
if (isMaintenanceBranch || (!releasedPackage.name && !releasedPackage.version)) {
previousVersion = auto.prefixRelease(monorepoVersion);
} else {
previousVersion = await greaterRelease(
Expand All @@ -297,15 +303,18 @@ async function getPreviousVersion(auto: Auto, prereleaseBranch: string) {
"Using package.json to calculate previous version"
);
const { version, name } = await loadPackageJson();

previousVersion = version
? await greaterRelease(
auto.prefixRelease,
name,
auto.prefixRelease(version),
prereleaseBranch
)
: "0.0.0";
if(isMaintenanceBranch && version) {
previousVersion = version
} else {
previousVersion = version
? await greaterRelease(
auto.prefixRelease,
name,
auto.prefixRelease(version),
prereleaseBranch
)
: "0.0.0";
}
}

auto.logger.verbose.info(
Expand Down Expand Up @@ -736,7 +745,7 @@ export default class NPMPlugin implements IPlugin {
});

auto.hooks.getPreviousVersion.tapPromise(this.name, () =>
getPreviousVersion(auto, prereleaseBranch)
getPreviousVersion(auto, prereleaseBranch, branch)
);

auto.hooks.getRepository.tapPromise(this.name, async () => {
Expand Down Expand Up @@ -771,6 +780,14 @@ export default class NPMPlugin implements IPlugin {
return line;
}

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

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

spentacular marked this conversation as resolved.
Show resolved Hide resolved
const lernaPackages = await this.getLernaPackages();
const changedPackages = await getChangedPackages({
sha: commit.hash,
Expand Down Expand Up @@ -1199,7 +1216,7 @@ export default class NPMPlugin implements IPlugin {
const lastRelease = await auto.git!.getLatestRelease();
const latestTag =
(await auto.git?.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await getPreviousVersion(auto, prereleaseBranch));
(await getPreviousVersion(auto, prereleaseBranch, branch));

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