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
65 changes: 65 additions & 0 deletions packages/core/src/__tests__/get-current-branch.test.ts
@@ -0,0 +1,65 @@
import { execSync } from "child_process";
import { CiEnv } from "env-ci";
import { getCurrentBranch } from "../auto";

jest.mock('child_process')

describe('getCurrentBranch', () => {

beforeEach(() => {
jest.clearAllMocks()
})

describe('when isPr', () => {
it('returns pr branch from env ci when valid', () => {
const env: Partial<CiEnv> = {
isPr: true,
prBranch: 'my-pr-branch'
}
expect(getCurrentBranch(env)).toBe('my-pr-branch')
});

it('tries git command when PR is invalid', () => {
const env: Partial<CiEnv> = {
isPr: true,
prBranch: 'undefined'
}

getCurrentBranch(env);

expect(execSync).toHaveBeenCalledWith("git symbolic-ref --short HEAD", {
encoding: "utf8",
stdio: "ignore",
})
});
})

describe('when not isPr', () => {

it('returns pr branch from env ci when valid', () => {
const env: Partial<CiEnv> = {
isPr: false,
prBranch: 'my-pr-branch',
branch: 'my-release-branch'
}

expect(getCurrentBranch(env)).toBe('my-release-branch');

expect(execSync).not.toHaveBeenCalled()
});

it('tries git command when branch name is invalid', () => {
const env: Partial<CiEnv> = {
isPr: false,
prBranch: 'my-pr-branch',
branch: undefined
}
getCurrentBranch(env);

expect(execSync).toHaveBeenCalledWith("git symbolic-ref --short HEAD", {
encoding: "utf8",
stdio: "ignore",
})
});
})
})
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
15 changes: 10 additions & 5 deletions packages/core/src/utils/get-current-branch.ts
@@ -1,18 +1,23 @@
import envCi from "env-ci";
import envCi, { CiEnv } from "env-ci";
import { execSync } from "child_process";

const env = envCi();
const defaultCiEnvironment = 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() {
export function getCurrentBranch(env: Partial<CiEnv> = defaultCiEnvironment) {
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
36 changes: 36 additions & 0 deletions plugins/npm/__tests__/npm.test.ts
Expand Up @@ -398,6 +398,42 @@ describe("getPreviousVersion", () => {

expect(await hooks.getPreviousVersion.promise()).toBe("0.1.2");
});

test("should ignore greatest published monorepo package in maintenance mode", async () => {
execPromise.mockClear()
mockFs({
"lerna.json": `
{
"name": "test",
"version": "1.5.0"
}
`,
...monorepoPackagesOnFs,
});
const plugin = new NPMPlugin();
const hooks = makeHooks();

// isMonorepo
monorepoPackages.mockReturnValueOnce(monorepoPackagesResult);
// published version of test package
execPromise.mockReturnValueOnce("2.1.0");

jest.spyOn(Auto, 'getCurrentBranch').mockReturnValueOnce('major-2')


plugin.apply({
config: { prereleaseBranches: ["next"], versionBranches: 'major-' },
hooks,
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease: (str) => str,
} as Auto.Auto);


expect(await hooks.getPreviousVersion.promise()).toBe("1.5.0");
expect(execPromise).not.toHaveBeenCalled()
});
});

test("should use string semver if no published package", async () => {
Expand Down
43 changes: 30 additions & 13 deletions plugins/npm/src/index.ts
Expand Up @@ -264,7 +264,7 @@ 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, isMaintenanceBranch: boolean) {
let previousVersion = "";

if (isMonorepo()) {
Expand All @@ -281,7 +281,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 +297,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 @@ -668,6 +671,12 @@ export default class NPMPlugin implements IPlugin {
? branch
: prereleaseBranches[0];

let isMaintenanceBranch = false;

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) => {
if (name === this.name || name === `@auto-it/${this.name}`) {
return validatePluginConfiguration(this.name, pluginOptions, options);
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, isMaintenanceBranch)
);

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, isMaintenanceBranch));

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