diff --git a/src/strategies/go-yoshi.ts b/src/strategies/go-yoshi.ts index 50be5a32d..347d0142b 100644 --- a/src/strategies/go-yoshi.ts +++ b/src/strategies/go-yoshi.ts @@ -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'}, @@ -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 diff --git a/src/strategies/go.ts b/src/strategies/go.ts index 5a3fc0ea2..d23a52463 100644 --- a/src/strategies/go.ts +++ b/src/strategies/go.ts @@ -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( @@ -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 diff --git a/src/updaters/go/github-imports-go.ts b/src/updaters/go/github-imports-go.ts index 8b66cd0f5..445feb9fa 100644 --- a/src/updaters/go/github-imports-go.ts +++ b/src/updaters/go/github-imports-go.ts @@ -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() diff --git a/src/updaters/go/go-mod.ts b/src/updaters/go/go-mod.ts new file mode 100644 index 000000000..7b43cd8f0 --- /dev/null +++ b/src/updaters/go/go-mod.ts @@ -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() + }` + ); + } +} diff --git a/test/strategies/go-yoshi.ts b/test/strategies/go-yoshi.ts index f5564135e..61b46a786 100644 --- a/test/strategies/go-yoshi.ts +++ b/test/strategies/go-yoshi.ts @@ -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(); @@ -116,7 +117,7 @@ describe('GoYoshi', () => { .stub(github, 'getFileContentsOnBranch') .resolves( buildGitHubFileContent( - './test/updaters/fixtures', + './test/updaters/fixtures/go', 'file-with-imports-v2.go' ) ); @@ -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 () => { diff --git a/test/strategies/go.ts b/test/strategies/go.ts index 4c8ca0a8b..9632e7700 100644 --- a/test/strategies/go.ts +++ b/test/strategies/go.ts @@ -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(); @@ -115,7 +116,7 @@ describe('Go', () => { .stub(github, 'getFileContentsOnBranch') .resolves( buildGitHubFileContent( - './test/updaters/fixtures', + './test/updaters/fixtures/go', 'file-with-imports-v2.go' ) ); @@ -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); + }); }); }); diff --git a/test/updaters/fixtures/file-with-go-snippet-v1.md b/test/updaters/fixtures/go/file-with-go-snippet-v1.md similarity index 100% rename from test/updaters/fixtures/file-with-go-snippet-v1.md rename to test/updaters/fixtures/go/file-with-go-snippet-v1.md diff --git a/test/updaters/fixtures/file-with-go-snippet-v2.md b/test/updaters/fixtures/go/file-with-go-snippet-v2.md similarity index 100% rename from test/updaters/fixtures/file-with-go-snippet-v2.md rename to test/updaters/fixtures/go/file-with-go-snippet-v2.md diff --git a/test/updaters/fixtures/file-with-go-snippet-v3.md b/test/updaters/fixtures/go/file-with-go-snippet-v3.md similarity index 100% rename from test/updaters/fixtures/file-with-go-snippet-v3.md rename to test/updaters/fixtures/go/file-with-go-snippet-v3.md diff --git a/test/updaters/fixtures/file-with-imports-v1.go b/test/updaters/fixtures/go/file-with-imports-v1.go similarity index 100% rename from test/updaters/fixtures/file-with-imports-v1.go rename to test/updaters/fixtures/go/file-with-imports-v1.go diff --git a/test/updaters/fixtures/file-with-imports-v2.go b/test/updaters/fixtures/go/file-with-imports-v2.go similarity index 100% rename from test/updaters/fixtures/file-with-imports-v2.go rename to test/updaters/fixtures/go/file-with-imports-v2.go diff --git a/test/updaters/fixtures/file-with-imports-v3.go b/test/updaters/fixtures/go/file-with-imports-v3.go similarity index 100% rename from test/updaters/fixtures/file-with-imports-v3.go rename to test/updaters/fixtures/go/file-with-imports-v3.go diff --git a/test/updaters/fixtures/go/go-v1.mod b/test/updaters/fixtures/go/go-v1.mod new file mode 100644 index 000000000..4a574279e --- /dev/null +++ b/test/updaters/fixtures/go/go-v1.mod @@ -0,0 +1,3 @@ +module github.com/stainless-sdks/meorphis-test-40-go + +go 1.21 \ No newline at end of file diff --git a/test/updaters/fixtures/go/go-v2.mod b/test/updaters/fixtures/go/go-v2.mod new file mode 100644 index 000000000..95f6a5eb7 --- /dev/null +++ b/test/updaters/fixtures/go/go-v2.mod @@ -0,0 +1,3 @@ +module github.com/stainless-sdks/meorphis-test-40-go/v2 + +go 1.21 \ No newline at end of file diff --git a/test/updaters/fixtures/go/go-v3.mod b/test/updaters/fixtures/go/go-v3.mod new file mode 100644 index 000000000..06bca25ba --- /dev/null +++ b/test/updaters/fixtures/go/go-v3.mod @@ -0,0 +1,3 @@ +module github.com/stainless-sdks/meorphis-test-40-go/v3 + +go 1.21 \ No newline at end of file diff --git a/test/updaters/go-imports.ts b/test/updaters/go-imports.ts index 7cc88ecf7..f5a845bb7 100644 --- a/test/updaters/go-imports.ts +++ b/test/updaters/go-imports.ts @@ -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', () => { diff --git a/test/updaters/go-mod.ts b/test/updaters/go-mod.ts new file mode 100644 index 000000000..3e06fad80 --- /dev/null +++ b/test/updaters/go-mod.ts @@ -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); + }); + }); +});