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

Next/Canary Support for Gradle #1842

Merged
merged 11 commits into from Mar 18, 2021
73 changes: 73 additions & 0 deletions plugins/gradle/__tests__/gradle.test.ts
Expand Up @@ -141,6 +141,79 @@ describe("Gradle Plugin", () => {
]);
});

test("should not increment version - canary", async () => {
const properties = "version: 1.0.0";
exec.mockReturnValueOnce(properties);
await hooks.beforeRun.promise({} as any);

const spy = jest.fn();
exec.mockReturnValueOnce(properties).mockImplementation(spy);

const canaryVersion = await hooks.canary.promise({ bump: Auto.SEMVER.patch, canaryIdentifier: "canary123" });

expect(canaryVersion).toBe("1.0.0-SNAPSHOT")
});

test("should not increment version - canary w/ default snapshot", async () => {
const properties = "version: 1.0.0-SNAPSHOT";
exec.mockReturnValueOnce(properties);
await hooks.beforeRun.promise({} as any);

const spy = jest.fn();
exec.mockReturnValueOnce(properties).mockImplementation(spy);

const canaryVersion = await hooks.canary.promise({ bump: Auto.SEMVER.patch, canaryIdentifier: "canary123" });

expect(canaryVersion).toBe("1.0.0-SNAPSHOT")
});

test("should not increment version - next", async () => {
const properties = "version: 1.0.0";
exec.mockReturnValueOnce(properties);
await hooks.beforeRun.promise({} as any);

const spy = jest.fn();

exec.mockReturnValueOnce(properties).mockImplementation(spy);

const nextVersion = await hooks.next.promise([], { bump: Auto.SEMVER.patch, commits: [], fullReleaseNotes: "", releaseNotes: "" });

expect(nextVersion[0]).toBe("1.0.0-SNAPSHOT")
});

test("should not increment version - next w/ default snapshot", async () => {
const properties = "version: 1.0.0-SNAPSHOT";
exec.mockReturnValueOnce(properties);
await hooks.beforeRun.promise({} as any);

const spy = jest.fn();

exec.mockReturnValueOnce(properties).mockImplementation(spy);

const nextVersion = await hooks.next.promise([], { bump: Auto.SEMVER.patch, commits: [], fullReleaseNotes: "", releaseNotes: "" });

expect(nextVersion[0]).toBe("1.0.0-SNAPSHOT")
});

test("should tag release - next w/ default snapshot", async () => {
const properties = "version: 1.0.0-SNAPSHOT";
exec.mockReturnValueOnce(properties);
await hooks.beforeRun.promise({} as any);

const spy = jest.fn();

exec.mockReturnValueOnce(properties).mockImplementation(spy);

await hooks.next.promise([], { bump: Auto.SEMVER.patch, commits: [], fullReleaseNotes: "", releaseNotes: "" });

expect(spy).toHaveBeenCalledWith(expect.stringMatching("git"), [
"tag",
"1.0.0-SNAPSHOT",
"-m",
"\"Tag pre-release: 1.0.0-SNAPSHOT\"",
]);
});

test("should version release - patch w/ custom snapshot", async () => {
const properties = `
version: 1.0.0.SNAP
Expand Down
87 changes: 86 additions & 1 deletion plugins/gradle/src/index.ts
Expand Up @@ -3,6 +3,7 @@ import {
IPlugin,
execPromise,
validatePluginConfiguration,
getCurrentBranch
} from "@auto-it/core";
import { IExtendedCommit } from "@auto-it/core/dist/log-parse";

Expand Down Expand Up @@ -89,6 +90,9 @@ export default class GradleReleasePluginPlugin implements IPlugin {
/** should this release be a snapshot release */
private snapshotRelease = false;

/** should this release be a canary release */
private canaryRelease = false;

/** Initialize the plugin with it's options */
constructor(options: IGradleReleasePluginPluginOptions = {}) {
this.options = {
Expand Down Expand Up @@ -238,8 +242,89 @@ export default class GradleReleasePluginPlugin implements IPlugin {
]);
});

auto.hooks.canary.tapPromise(
this.name,
async ({ dryRun, quiet }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This hook receives canaryIdentifier that should be used to create the canary version

this.canaryRelease = true;

const releaseVersion = await getVersion(
this.options.gradleCommand,
this.options.gradleOptions
);

const canaryVersion = `${releaseVersion}${defaultSnapshotSuffix}`;

if (dryRun) {
if (quiet) {
console.log(canaryVersion);
} else {
auto.logger.log.info(`Would have published: ${releaseVersion}`);
}

return canaryVersion;
}

const { publish } = this.properties;

if (publish) {
await execPromise(this.options.gradleCommand, [
"publish",
...this.options.gradleOptions,
]);
}
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved

return canaryVersion;
}
);

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, { dryRun }) => {
const branch = getCurrentBranch() || "";

const version = await getVersion(
this.options.gradleCommand,
this.options.gradleOptions
);

const nextVersion = `${version}${defaultSnapshotSuffix}`;

// Don't need to increment version since version should have already
// been incremented by canary during PR, so take the current version
// and publish it
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved
if (nextVersion) {
preReleaseVersions.push(nextVersion);
}

if (dryRun) {
auto.logger.log.info(`Would have published: ${nextVersion}`);
return preReleaseVersions;
}

await execPromise("git", [
"tag",
nextVersion ?? "",
"-m",
`"Tag pre-release: ${nextVersion}"`,
]);

await execPromise("git", ["push", auto.remote, branch, "--tags"]);

const { publish } = this.properties;

if (publish) {
await execPromise(this.options.gradleCommand, [
"publish",
...this.options.gradleOptions,
]);
}

return preReleaseVersions;
}
);

auto.hooks.afterShipIt.tapPromise(this.name, async ({ dryRun }) => {
if (!this.snapshotRelease || dryRun) {
if (!this.snapshotRelease || dryRun || this.canaryRelease) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This hook receives a context that will be canary if it's a canary release. you should be able to remove the class field and just do the check with context

return;
}

Expand Down