Skip to content

Commit

Permalink
add go.mod updater (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
meorphis authored Oct 9, 2024
1 parent 1d81b76 commit 80e4b84
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/strategies/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {Release} from '../release';
import {VersionGo} from '../updaters/go/version-go';
import {dirname} from 'path';
import {GithubImportsGo} from '../updaters/go/github-imports-go';
import {GoModUpdater} from '../updaters/go/go-mod';

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
Expand Down Expand Up @@ -71,6 +72,14 @@ export class GoYoshi extends BaseStrategy {
}),
});

updates.push({
path: this.addPath('go.mod'),
createIfMissing: false,
updater: new GoModUpdater({
version,
}),
});

const goFiles = await this.github.findFilesByGlobAndRef(
'**/*.go',
this.changesBranch
Expand Down
9 changes: 9 additions & 0 deletions src/strategies/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Changelog} from '../updaters/changelog';
import {BaseStrategy, BuildUpdatesOptions} from './base';
import {Update} from '../update';
import {GithubImportsGo} from '../updaters/go/github-imports-go';
import {GoModUpdater} from '../updaters/go/go-mod';

export class Go extends BaseStrategy {
protected async buildUpdates(
Expand All @@ -34,6 +35,14 @@ export class Go extends BaseStrategy {
}),
});

updates.push({
path: this.addPath('go.mod'),
createIfMissing: false,
updater: new GoModUpdater({
version,
}),
});

const goFiles = await this.github.findFilesByGlobAndRef(
'**/*.go',
this.changesBranch
Expand Down
2 changes: 1 addition & 1 deletion src/updaters/go/github-imports-go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class GithubImportsGo extends DefaultUpdater {
}

return content.replace(
/"(https:\/\/pkg.go.dev\/)?github\.com\/([^/"\n]+)\/([^/"\n]+)(\/v([1-9]\d*))?(\/[^"\n]+)?"/g,
/"(https:\/\/pkg.go.dev\/)?github\.com\/([^/"\r?\n]+)\/([^/"\r?\n]+)(\/v([1-9]\d*))?(\/[^"\r?\n]+)?"/g,
(_, prefix, user, repo, ___, ____, path) =>
`"${prefix ?? ''}github.com/${user}/${repo}${
this.version.major < 2 ? '' : '/v' + this.version.major.toString()
Expand Down
17 changes: 17 additions & 0 deletions src/updaters/go/go-mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {DefaultUpdater} from '../default';

export class GoModUpdater extends DefaultUpdater {
updateContent(content: string): string {
if (this.version.major < 2) {
return content;
}

return content.replace(
/module github\.com\/([^/"\r?\n]+)\/([^/"\r?\n]+)(\/v([1-9]\d*))?/g,
(_, user, repo) =>
`module github.com/${user}/${repo}${
this.version.major < 2 ? '' : '/v' + this.version.major.toString()
}`
);
}
}
27 changes: 26 additions & 1 deletion test/strategies/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {Changelog} from '../../src/updaters/changelog';
import snapshot = require('snap-shot-it');
import {VersionGo} from '../../src/updaters/go/version-go';
import {GithubImportsGo} from '../../src/updaters/go/github-imports-go';
import {GoModUpdater} from '../../src/updaters/go/go-mod';

const sandbox = sinon.createSandbox();

Expand Down Expand Up @@ -116,7 +117,7 @@ describe('GoYoshi', () => {
.stub(github, 'getFileContentsOnBranch')
.resolves(
buildGitHubFileContent(
'./test/updaters/fixtures',
'./test/updaters/fixtures/go',
'file-with-imports-v2.go'
)
);
Expand All @@ -129,6 +130,30 @@ describe('GoYoshi', () => {
const updates = release!.updates;
assertHasUpdate(updates, 'file-with-imports-v2.go', GithubImportsGo);
});

it('finds and updates a go.mod file', async () => {
const strategy = new GoYoshi({
targetBranch: 'main',
github,
component: 'iam',
});
sandbox
.stub(github, 'getFileContentsOnBranch')
.resolves(
buildGitHubFileContent(
'./test/updaters/fixtures/go',
'file-with-imports-v2.go'
)
);
sandbox.stub(github, 'findFilesByFilenameAndRef').resolves([]);
const latestRelease = undefined;
const release = await strategy.buildReleasePullRequest({
commits: COMMITS,
latestRelease,
});
const updates = release!.updates;
assertHasUpdate(updates, 'go.mod', GoModUpdater);
});
});
describe('buildReleasePullRequest', () => {
it('filters out submodule commits', async () => {
Expand Down
26 changes: 25 additions & 1 deletion test/strategies/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {TagName} from '../../src/util/tag-name';
import {Version} from '../../src/version';
import {Changelog} from '../../src/updaters/changelog';
import {GithubImportsGo} from '../../src/updaters/go/github-imports-go';
import {GoModUpdater} from '../../src/updaters/go/go-mod';

const sandbox = sinon.createSandbox();

Expand Down Expand Up @@ -115,7 +116,7 @@ describe('Go', () => {
.stub(github, 'getFileContentsOnBranch')
.resolves(
buildGitHubFileContent(
'./test/updaters/fixtures',
'./test/updaters/fixtures/go',
'file-with-imports-v2.go'
)
);
Expand All @@ -128,5 +129,28 @@ describe('Go', () => {
const updates = release!.updates;
assertHasUpdate(updates, 'file-with-imports-v2.go', GithubImportsGo);
});
it('finds and updates a go.mod file', async () => {
const strategy = new Go({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
});
sandbox
.stub(github, 'getFileContentsOnBranch')
.resolves(
buildGitHubFileContent(
'./test/updaters/fixtures/go',
'file-with-imports-v2.go'
)
);
sandbox.stub(github, 'findFilesByFilenameAndRef').resolves([]);
const latestRelease = undefined;
const release = await strategy.buildReleasePullRequest({
commits: COMMITS,
latestRelease,
});
const updates = release!.updates;
assertHasUpdate(updates, 'go.mod', GoModUpdater);
});
});
});
3 changes: 3 additions & 0 deletions test/updaters/fixtures/go/go-v1.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/stainless-sdks/meorphis-test-40-go

go 1.21
3 changes: 3 additions & 0 deletions test/updaters/fixtures/go/go-v2.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/stainless-sdks/meorphis-test-40-go/v2

go 1.21
3 changes: 3 additions & 0 deletions test/updaters/fixtures/go/go-v3.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/stainless-sdks/meorphis-test-40-go/v3

go 1.21
2 changes: 1 addition & 1 deletion test/updaters/go-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {expect} from 'chai';
import {Version} from '../../src/version';
import {GithubImportsGo} from '../../src/updaters/go/github-imports-go';

const fixturesPath = './test/updaters/fixtures';
const fixturesPath = './test/updaters/fixtures/go';

describe('GithubImportsGo', () => {
describe('.go files', () => {
Expand Down
44 changes: 44 additions & 0 deletions test/updaters/go-mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {readFileSync} from 'fs';
import {resolve} from 'path';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {Version} from '../../src/version';
import {GoModUpdater} from '../../src/updaters/go/go-mod';

const fixturesPath = './test/updaters/fixtures/go';

describe('GoModUpdater', () => {
describe('go.mod files', () => {
const v1File = readFileSync(resolve(fixturesPath, 'go-v1.mod'), 'utf8');
const v2File = readFileSync(resolve(fixturesPath, 'go-v2.mod'), 'utf8');
const v3File = readFileSync(resolve(fixturesPath, 'go-v3.mod'), 'utf8');

it('makes no changes if the old version has a major version of 1 and the new version also has a major version of 1', async () => {
const importsUpdater = new GoModUpdater({
version: Version.parse('1.0.0'),
});
expect(importsUpdater.updateContent(v1File)).to.equal(v1File);
});

it('updates the version in the imports if the old version has a major version of 1 and the new version has a major version of 2', async () => {
const importsUpdater = new GoModUpdater({
version: Version.parse('2.0.0'),
});
expect(importsUpdater.updateContent(v1File)).to.equal(v2File);
});

it('makes no changes if the old version has a major version of 2 and the new version also has a major version of 2', async () => {
const importsUpdater = new GoModUpdater({
version: Version.parse('2.0.0'),
});
expect(importsUpdater.updateContent(v2File)).to.equal(v2File);
});

it('updates the version in the imports if the old version has a major version of 2 and the new version has a major version of 3', async () => {
const importsUpdater = new GoModUpdater({
version: Version.parse('3.0.0'),
});
expect(importsUpdater.updateContent(v2File)).to.equal(v3File);
});
});
});

0 comments on commit 80e4b84

Please sign in to comment.