Skip to content

Commit

Permalink
Get correct next version when using onlyGraduateWithReleaseLabel ship…
Browse files Browse the repository at this point in the history
…it option
  • Loading branch information
adierkens committed Jul 15, 2022
1 parent a15f77b commit 93bce2e
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 118 deletions.
251 changes: 144 additions & 107 deletions plugins/version-file/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Auto, IPlugin, execPromise, validatePluginConfiguration, getCurrentBranch, determineNextVersion, DEFAULT_PRERELEASE_BRANCHES } from '@auto-it/core';
import {
Auto,
IPlugin,
execPromise,
validatePluginConfiguration,
getCurrentBranch,
determineNextVersion,
DEFAULT_PRERELEASE_BRANCHES,
} from "@auto-it/core";
import { promisify } from "util";
import * as t from "io-ts";
import * as fs from "fs";
Expand All @@ -7,13 +15,12 @@ import { inc, ReleaseType } from "semver";
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);


const pluginOptions = t.partial({
/** Path to file (from where auto is executed) where the version is stored */
versionFile: t.string,

/** Optional script that executes release pipeline stages */
publishScript: t.string
publishScript: t.string,
});

export type IVersionFilePluginOptions = t.TypeOf<typeof pluginOptions>;
Expand All @@ -22,49 +29,50 @@ export type IVersionFilePluginOptions = t.TypeOf<typeof pluginOptions>;
* Reads version file from location specified in config
*/
async function getPreviousVersion(auto: Auto, versionFile: string) {
auto.logger.veryVerbose.info(`Reading version from file `, versionFile)
return readFile(versionFile, "utf-8")
auto.logger.veryVerbose.info(`Reading version from file `, versionFile);
return readFile(versionFile, "utf-8");
}

/** Writes new version to version file at specified location */
async function writeNewVersion(auto: Auto, version: string, versionFile: string) {
auto.logger.veryVerbose.info(`Writing version to file `, versionFile)
return writeFile(versionFile, version)
async function writeNewVersion(
auto: Auto,
version: string,
versionFile: string
) {
auto.logger.veryVerbose.info(`Writing version to file `, versionFile);
return writeFile(versionFile, version);
}

/** Reset the scope changes of all the packages */
async function gitReset(auto: Auto) {
auto.logger.veryVerbose.info("Hard resetting local changes")
auto.logger.veryVerbose.info("Hard resetting local changes");
await execPromise("git", ["reset", "--hard", "HEAD"]);
}

/** Generates canary release notes */
function makeCanaryNotes(canaryVersion: string){
return `Try this version out locally by upgrading relevant packages to ${canaryVersion}`
function makeCanaryNotes(canaryVersion: string) {
return `Try this version out locally by upgrading relevant packages to ${canaryVersion}`;
}


/** Plugin to orchestrate releases in a repo where version is maintained in a flat file */
export default class VersionFilePlugin implements IPlugin {
/** The name of the plugin */
name = 'version-file';
name = "version-file";

/** Version file location */
readonly versionFile: string;

/** Release script location */
readonly publishScript: string | undefined
readonly publishScript: string | undefined;

/** Initialize the plugin with it's options */
constructor(options: IVersionFilePluginOptions) {
this.versionFile = options.versionFile ?? "VERSION";
this.publishScript = options.publishScript
this.publishScript = options.publishScript;
}


/** Tap into auto plugin points. */
apply(auto: Auto) {

const prereleaseBranches =
auto.config?.prereleaseBranches || DEFAULT_PRERELEASE_BRANCHES;

Expand All @@ -83,117 +91,146 @@ export default class VersionFilePlugin implements IPlugin {
}
});

auto.hooks.getPreviousVersion.tapPromise(this.name, () =>{
return getPreviousVersion(auto, this.versionFile)
auto.hooks.getPreviousVersion.tapPromise(this.name, () => {
return getPreviousVersion(auto, this.versionFile);
});

auto.hooks.version.tapPromise( this.name, async ({ bump }) => {
const lastVersion = await getPreviousVersion(auto, this.versionFile)
auto.hooks.version.tapPromise(this.name, async ({ bump }) => {
const lastVersion = await getPreviousVersion(auto, this.versionFile);
const newVersion = inc(lastVersion, bump as ReleaseType);

auto.logger.log.info(`Calculated new version as: ${newVersion}`)
auto.logger.log.info(`Calculated new version as: ${newVersion}`);

if (newVersion){
if (newVersion) {
// Seal versions via commit and tag
await writeNewVersion(auto, newVersion, this.versionFile)
await execPromise("git", ["commit", "-am", `"Bump version to: v${newVersion} [skip ci]"`]);
await writeNewVersion(auto, newVersion, this.versionFile);
await execPromise("git", [
"tag",
`v${newVersion}`
"commit",
"-am",
`"Bump version to: v${newVersion} [skip ci]"`,
]);
await execPromise("git", ["tag", `v${newVersion}`]);
auto.logger.verbose.info("Successfully versioned repo");
} else {
auto.logger.log.error(`Error: Unable to calculate new version based off of ${lastVersion} being bumped with a ${bump} release`)
throw new Error ("Version bump failed")
auto.logger.log.error(
`Error: Unable to calculate new version based off of ${lastVersion} being bumped with a ${bump} release`
);
throw new Error("Version bump failed");
}
});

auto.hooks.publish.tapPromise(this.name, async () => {

// Call release script if provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["release"])
if (this.publishScript) {
auto.logger.log.info(
`Calling release script in repo at ${this.publishScript}`
);
await execPromise(this.publishScript, ["release"]);
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
auto.logger.log.info(
"Skipping calling release script in repo since none was provided"
);
}

// push tag and version change commit up
await execPromise("git", ["push", auto.remote, branch || auto.baseBranch, "--tags"]);
});

auto.hooks.canary.tapPromise(this.name, async ({ bump, canaryIdentifier}) => {

// Figure out canary version
const lastRelease =
(await auto.git!.getLatestRelease()) ||
(await auto.git?.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await getPreviousVersion(auto, this.versionFile));
const current = await auto.getCurrentVersion(lastRelease);
const nextVersion = inc(current, bump as ReleaseType);
const canaryVersion = `${nextVersion}-${canaryIdentifier}`;

auto.logger.log.info(`Marking version as ${canaryVersion}`);

// Write Canary version
await writeNewVersion(auto, canaryVersion, this.versionFile)

// Ship canary release if release script is provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["snapshot"]);
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}


// Reset temporary canary versioning
await gitReset(auto);

return {
newVersion: canaryVersion,
details: makeCanaryNotes(canaryVersion),
};
});

auto.hooks.next.tapPromise(this.name, async (preReleaseVersions, { bump }) => {

// Figure out next version
const lastRelease = await auto.git!.getLatestRelease();
const latestTag =
(await auto.git?.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await getPreviousVersion(auto, this.versionFile));
const nextVersion = determineNextVersion(
lastRelease,
latestTag,
bump,
prereleaseBranch
);
const prefixedVersion = auto.prefixRelease(nextVersion);
preReleaseVersions.push(prefixedVersion);

auto.logger.log.info(`Marking version as ${prefixedVersion}`);

// Write version to file
await writeNewVersion(auto, prefixedVersion, this.versionFile)

// ship next release if release script is provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["snapshot"]);
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}

// Push next tag
// push tag and version change commit up
await execPromise("git", [
"tag",
prefixedVersion
"push",
auto.remote,
branch || auto.baseBranch,
"--tags",
]);
await execPromise("git", ["push", auto.remote, branch, "--tags"]);

return preReleaseVersions
});

auto.hooks.canary.tapPromise(
this.name,
async ({ bump, canaryIdentifier }) => {
// Figure out canary version
const lastRelease =
(await auto.git!.getLatestRelease()) ||
(await auto.git?.getLastTagNotInBaseBranch(prereleaseBranch)) ||
(await getPreviousVersion(auto, this.versionFile));

const current = await auto.getCurrentVersion(lastRelease);

const nextVersion = inc(current, bump as ReleaseType);

const canaryVersion = `${nextVersion}-${canaryIdentifier}`;

auto.logger.log.info(`Marking version as ${canaryVersion}`);

// Write Canary version
await writeNewVersion(auto, canaryVersion, this.versionFile);

// Ship canary release if release script is provided
if (this.publishScript) {
auto.logger.log.info(
`Calling release script in repo at ${this.publishScript}`
);
await execPromise(this.publishScript, ["snapshot"]);
} else {
auto.logger.log.info(
"Skipping calling release script in repo since none was provided"
);
}

// Reset temporary canary versioning
await gitReset(auto);

return {
newVersion: canaryVersion,
details: makeCanaryNotes(canaryVersion),
};
}
);

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, { bump }) => {
// Figure out next version
const lastRelease = await auto.git!.getLatestRelease();
const currentBranch = await getCurrentBranch();

const latestTagInBranch = await (currentBranch === auto.baseBranch
? auto.git?.getLatestTagInBranch()
: auto.git?.getLastTagNotInBaseBranch(prereleaseBranch));

const latestTag =
latestTagInBranch ||
(await getPreviousVersion(auto, this.versionFile));

const nextVersion = determineNextVersion(
lastRelease,
latestTag,
bump,
prereleaseBranch
);

const prefixedVersion = auto.prefixRelease(nextVersion);
preReleaseVersions.push(prefixedVersion);

auto.logger.log.info(`Marking version as ${prefixedVersion}`);

// Write version to file
await writeNewVersion(auto, prefixedVersion, this.versionFile);

// ship next release if release script is provided
if (this.publishScript) {
auto.logger.log.info(
`Calling release script in repo at ${this.publishScript}`
);
await execPromise(this.publishScript, ["snapshot"]);
} else {
auto.logger.log.info(
"Skipping calling release script in repo since none was provided"
);
}

// Push next tag
await execPromise("git", ["tag", prefixedVersion]);
await execPromise("git", ["push", auto.remote, branch, "--tags"]);

return preReleaseVersions;
}
);
}
}
26 changes: 15 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
integrity sha512-K1kQv1BZVtMXQqdpNZt9Pgh85KwamsWX9gYyq1xG4cpyb+EacfMiNfumrju16piFXanCUrCR0P1DowPjV2qV/A==

"@auto-it/bot-list@link:packages/bot-list":
version "10.36.2"
version "10.37.1"

"@auto-it/core@link:packages/core":
version "10.36.2"
version "10.37.1"
dependencies:
"@auto-it/bot-list" "link:packages/bot-list"
"@endemolshinegroup/cosmiconfig-typescript-loader" "^3.0.2"
Expand Down Expand Up @@ -59,7 +59,7 @@
url-join "^4.0.0"

"@auto-it/npm@link:plugins/npm":
version "10.36.2"
version "10.37.1"
dependencies:
"@auto-it/core" "link:packages/core"
"@auto-it/package-json-utils" "link:packages/package-json-utils"
Expand All @@ -77,13 +77,13 @@
user-home "^2.0.0"

"@auto-it/package-json-utils@link:packages/package-json-utils":
version "10.36.2"
version "10.37.1"
dependencies:
parse-author "^2.0.0"
parse-github-url "1.0.2"

"@auto-it/released@link:plugins/released":
version "10.36.2"
version "10.37.1"
dependencies:
"@auto-it/bot-list" "link:packages/bot-list"
"@auto-it/core" "link:packages/core"
Expand All @@ -92,6 +92,15 @@
io-ts "^2.1.2"
tslib "2.1.0"

"@auto-it/version-file@link:plugins/version-file":
version "10.37.1"
dependencies:
"@auto-it/core" "link:packages/core"
fp-ts "^2.5.3"
io-ts "^2.1.2"
semver "^7.0.0"
tslib "1.10.0"

"@babel/code-frame@7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"
Expand Down Expand Up @@ -11531,12 +11540,7 @@ path-key@^3.0.0, path-key@^3.1.0:
resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
integrity sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==

path-parse@^1.0.5, path-parse@^1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==

path-parse@^1.0.7:
path-parse@^1.0.5, path-parse@^1.0.6, path-parse@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
Expand Down

0 comments on commit 93bce2e

Please sign in to comment.