Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export npm_config_git_tag_version=false

NPM_VERSION=$(npm version $NEW_VERSION)
VERSION=${NPM_VERSION:1}
echo "Previous version: $OLD_VERSION"
echo "New version: $VERSION"
12 changes: 7 additions & 5 deletions src/commands/__tests__/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { runPreReleaseCommand } from '../prepare';
jest.mock('../../utils/system');

describe('runPreReleaseCommand', () => {
const oldVersion = '2.3.3';
const newVersion = '2.3.4';
const mockedSpawnProcess = spawnProcess as jest.Mock;

Expand All @@ -15,16 +16,16 @@ describe('runPreReleaseCommand', () => {
test('runs with default command', async () => {
expect.assertions(1);

await runPreReleaseCommand(newVersion);
await runPreReleaseCommand(oldVersion, newVersion);

expect(mockedSpawnProcess).toBeCalledWith(
'/bin/bash',
[pathJoin('scripts', 'bump-version.sh'), '', newVersion],
[pathJoin('scripts', 'bump-version.sh'), oldVersion, newVersion],
{
env: {
...process.env,
CRAFT_NEW_VERSION: newVersion,
CRAFT_OLD_VERSION: '',
CRAFT_OLD_VERSION: oldVersion,
},
}
);
Expand All @@ -34,18 +35,19 @@ describe('runPreReleaseCommand', () => {
expect.assertions(1);

await runPreReleaseCommand(
oldVersion,
newVersion,
'python ./increase_version.py "argument 1"'
);

expect(mockedSpawnProcess).toBeCalledWith(
'python',
['./increase_version.py', 'argument 1', '', newVersion],
['./increase_version.py', 'argument 1', oldVersion, newVersion],
{
env: {
...process.env,
CRAFT_NEW_VERSION: newVersion,
CRAFT_OLD_VERSION: '',
CRAFT_OLD_VERSION: oldVersion,
},
}
);
Expand Down
20 changes: 14 additions & 6 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
handleGlobalError,
reportError,
} from '../utils/errors';
import { getGitClient, getDefaultBranch } from '../utils/git';
import { getGitClient, getDefaultBranch, getLatestTag } from '../utils/git';
import { isDryRun, promptConfirmation } from '../utils/helpers';
import { formatJson } from '../utils/strings';
import { spawnProcess } from '../utils/system';
Expand Down Expand Up @@ -141,7 +141,7 @@ async function createReleaseBranch(
const branchPrefix = releaseBranchPrefix || DEFAULT_RELEASE_BRANCH_NAME;
const branchName = `${branchPrefix}/${newVersion}`;

const branchHead = await git.raw(['show-ref', '--heads', branchName]);
const branchHead = await git.raw('show-ref', '--heads', branchName);

// in case `show-ref` can't find a branch it returns `null`
if (branchHead) {
Expand Down Expand Up @@ -227,6 +227,7 @@ async function commitNewVersion(
* @param preReleaseCommand Custom pre-release command
*/
export async function runPreReleaseCommand(
oldVersion: string,
newVersion: string,
preReleaseCommand?: string
): Promise<boolean> {
Expand All @@ -242,11 +243,11 @@ export async function runPreReleaseCommand(
sysCommand = '/bin/bash';
args = [DEFAULT_BUMP_VERSION_PATH];
}
args = [...args, '', newVersion];
args = [...args, oldVersion, newVersion];
logger.info(`Running the pre-release command...`);
const additionalEnv = {
CRAFT_NEW_VERSION: newVersion,
CRAFT_OLD_VERSION: '',
CRAFT_OLD_VERSION: oldVersion,
};
await spawnProcess(sysCommand, args, {
env: { ...process.env, ...additionalEnv },
Expand Down Expand Up @@ -472,8 +473,7 @@ export async function prepareMain(argv: PrepareOptions): Promise<any> {

logger.info(`Preparing to release the version: ${newVersion}`);

// Create a new release branch and check it out. Throw an error if it already
// exists.
// Create a new release branch and check it out. Fail if it already exists.
const branchName = await createReleaseBranch(
git,
rev,
Expand All @@ -482,6 +482,13 @@ export async function prepareMain(argv: PrepareOptions): Promise<any> {
config.releaseBranchPrefix
);

// Do this once we are on the release branch as we might be releasing from
// a custom revision and it is harder to tell git to give us the tag right
// before a specific revision.
// TL;DR - WARNING:
// The order matters here, do not move this command above craeteReleaseBranch!
const oldVersion = await getLatestTag(git);

// Check & update the changelog
await prepareChangelog(
newVersion,
Expand All @@ -491,6 +498,7 @@ export async function prepareMain(argv: PrepareOptions): Promise<any> {

// Run a pre-release script (e.g. for version bumping)
const preReleaseCommandRan = await runPreReleaseCommand(
oldVersion,
newVersion,
config.preReleaseCommand
);
Expand Down
5 changes: 5 additions & 0 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export async function getDefaultBranch(
);
}

export async function getLatestTag(git: SimpleGit): Promise<string> {
// This part is courtesy of https://stackoverflow.com/a/7261049/90297
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
}

export function stripRemoteName(
branch: string | undefined,
remoteName: string
Expand Down