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(go): Release is not idempotent to existing tags #72

Merged
merged 3 commits into from
Apr 13, 2021
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
13 changes: 11 additions & 2 deletions src/help/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ export function init() {
* Cerate a tag.
*
* @param name tag name.
* @returns true if the tag was created, false if it already exists.
*/
export function tag(name: string) {
shell.run(`git tag -a ${name} -m ${name}`);
export function tag(name: string): boolean {
try {
shell.run(`git tag -a ${name} -m ${name}`, { capture: true });
return true;
} catch (e) {
if (e.message.includes('already exists')) {
return false;
}
throw e;
}
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/help/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@ export interface RunOptions {
*/
export function run(command: string, options: RunOptions = {}): string {
const shsplit = shlex.split(command);
const pipe = options.capture ?? false;
const pipeOrInherit = (options.capture ?? false) ? 'pipe': 'inherit';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We now need stderr piping as well.

const result = child.spawnSync(shsplit[0], shsplit.slice(1), {
stdio: ['ignore', pipe ? 'pipe' : 'inherit', process.stdout],
stdio: ['ignore', pipeOrInherit, pipeOrInherit],
cwd: options.cwd,
shell: options.shell,
});
if (result.error) {
throw result.error;
}
// we always redirect stderr to stdout so its ok to take stdout.
// but when we don't pipe, we don't have the output.
const message = result.stdout?.toString() ?? `Command failed: ${command}`;

const stdout = result.stdout?.toString();
const stderr = result.stderr?.toString();

if (result.status !== 0) {
const message = `
Command failed: ${command}.
Output: ${stdout}
Error:${stderr}`;
throw new Error(message);
}
return message;
return stdout;
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/targets/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,25 @@ export class GoReleaser {
const commitMessage = this.gitCommitMessage ?? this.buildReleaseMessage(modules);
git.commit(commitMessage);

const tags = modules.map(m => this.buildTagName(m));
const refs = [...tags, this.gitBranch];
const tags = [];
for (const module of modules) {
const name = this.buildTagName(module);
const created = git.tag(name);
if (created) { tags.push(name); }
}

if (tags.length === 0) {
console.log('All tags already exist. Skipping release');
return {};
}

tags.forEach(t => git.tag(t));
const refs = [...tags, this.gitBranch];

if (this.dryRun) {
console.log('===========================================');
console.log(' 🏜️ DRY-RUN MODE 🏜️');
console.log('===========================================');
refs.forEach(t => console.log(`Remote ref will update: ${t}`));
refs.forEach(t => console.log(`Remote ref will be updated: ${t}`));
} else {
refs.forEach(t => git.push(t));
}
Expand Down
42 changes: 39 additions & 3 deletions test/targets/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import * as git from '../../src/help/git';
import * as os from '../../src/help/os';
import * as shell from '../../src/help/shell';

function initRepo(repoDir: string) {
interface Initializers {
postInit?: (repoDir: string) => void;
}

function initRepo(repoDir: string, postInit?: (repoDir: string) => void) {
const cwd = process.cwd();
try {
process.chdir(repoDir);
git.init();
git.add('.');
git.identify('jsii-release-test', '<>');
git.commit('Initial Commit');
if (postInit) { postInit(repoDir); };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A way for tests to influence the target repository.

} finally {
process.chdir(cwd);
}
}

function createReleaser(fixture: string, props: Omit<GoReleaserProps, 'dir' | 'dryRun'> = {}) {
function createReleaser(fixture: string, props: Omit<GoReleaserProps, 'dir' | 'dryRun'> = {}, initializers: Initializers = {}) {

const fixturePath = path.join(__dirname, '..', '__fixtures__', fixture);
const sourceDir = path.join(os.mkdtempSync(), fixture);
Expand All @@ -35,7 +40,7 @@ function createReleaser(fixture: string, props: Omit<GoReleaserProps, 'dir' | 'd
(git as any).clone = function(_: string, targetDir: string) {
// the cloned repo is always the original fixture.
fs.copySync(fixturePath, targetDir, { recursive: true });
initRepo(targetDir);
initRepo(targetDir, initializers.postInit);
};

return { releaser, sourceDir };
Expand Down Expand Up @@ -230,3 +235,34 @@ test('throws when no major version suffix', () => {
expect(() => releaser.release()).toThrow(/expected to end with '\/v3'/);

});

test('skips when all tags already exist', () => {

const { releaser, sourceDir } = createReleaser('sub-modules', {}, {
postInit: (_: string) => {
git.tag('module1/v1.1.0');
git.tag('module2/v1.1.0');
},
});

fs.writeFileSync(path.join(sourceDir, 'file'), 'test');

const release = releaser.release();
expect(release).toStrictEqual({});

});

test('creates missing tags only', () => {

const { releaser, sourceDir } = createReleaser('sub-modules', {}, {
postInit: (_: string) => {
git.tag('module2/v1.1.0');
},
});

fs.writeFileSync(path.join(sourceDir, 'file'), 'test');

const release = releaser.release();
expect(release.tags).toEqual(['module1/v1.1.0']);

});