Skip to content

Commit

Permalink
tie go version updates to specific repo (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
meorphis authored Oct 9, 2024
1 parent 80e4b84 commit 7217264
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/strategies/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class GoYoshi extends BaseStrategy {
createIfMissing: false,
updater: new GithubImportsGo({
version,
repository: this.repository,
}),
});
}
Expand Down
1 change: 1 addition & 0 deletions src/strategies/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class Go extends BaseStrategy {
createIfMissing: true,
updater: new GithubImportsGo({
version,
repository: this.repository,
}),
});
}
Expand Down
24 changes: 19 additions & 5 deletions src/updaters/go/github-imports-go.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import {Repository} from '@google-automations/git-file-utils';
import {DefaultUpdater} from '../default';
import {Version} from '../../version';

export class GithubImportsGo extends DefaultUpdater {
private repository: Repository;

constructor(options: {repository: Repository; version: Version}) {
super(options);
this.repository = options.repository;
}

updateContent(content: string): string {
if (this.version.major < 2) {
return content;
}

return content.replace(
/"(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()
}${path ?? ''}"`
new RegExp(
`"(https://pkg.go.dev/)?github.com/${this.repository.owner}/${this.repository.repo}(/v([1-9]\\d*))?(/[^"\\r?\\n]+)?"`,
'g'
),
(_, prefix, __, ___, path) =>
`"${prefix ?? ''}github.com/${this.repository.owner}/${
this.repository.repo
}${this.version.major < 2 ? '' : '/v' + this.version.major.toString()}${
path ?? ''
}"`
);
}
}
32 changes: 32 additions & 0 deletions test/updaters/go-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,43 @@ describe('GithubImportsGo', () => {

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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
version: Version.parse('3.0.0'),
});
expect(importsUpdater.updateContent(v2File)).to.equal(v3File);
Expand All @@ -71,27 +87,43 @@ describe('GithubImportsGo', () => {

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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
version: Version.parse('1.0.0'),
});
expect(importsUpdater.updateContent(v1MdFile)).to.equal(v1MdFile);
});

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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
version: Version.parse('2.0.0'),
});
expect(importsUpdater.updateContent(v1MdFile)).to.equal(v2MdFile);
});

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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
version: Version.parse('2.0.0'),
});
expect(importsUpdater.updateContent(v2MdFile)).to.equal(v2MdFile);
});

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 GithubImportsGo({
repository: {
owner: 'cloudflare',
repo: 'cloudflare-go',
},
version: Version.parse('3.0.0'),
});
expect(importsUpdater.updateContent(v2MdFile)).to.equal(v3MdFile);
Expand Down

0 comments on commit 7217264

Please sign in to comment.