Skip to content

Commit 0ced8b9

Browse files
meorphismeorphis
andauthored
get gem name from gemspec file 🥲 (#214)
Co-authored-by: meorphis <[email protected]>
1 parent 8ce53ee commit 0ced8b9

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

‎src/strategies/ruby.ts‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,48 @@ export class Ruby extends BaseStrategy {
8080
createIfMissing: false,
8181
updater: new GemfileLock({
8282
version,
83-
gemName:
84-
this.component ||
85-
// grab the gem name from the version file path if it's not provided via the component
86-
this.versionFile.match(/lib\/(.*)\/version.rb/)?.[1] ||
87-
'',
83+
gemName: await this.determineGemName(),
8884
}),
8985
});
9086

9187
return updates;
9288
}
9389

90+
private async determineGemName(): Promise<string> {
91+
// attempt to grab the gem name by regex matching the gemspec file; it is arbitrary ruby code so
92+
// this could fail but only if the user has implemented some weird custom code
93+
const gemspec = await this.github.findFilesByGlobAndRef(
94+
'*.gemspec',
95+
this.changesBranch
96+
);
97+
98+
if (gemspec.length === 1) {
99+
const gemspecContent = (
100+
await this.github.getFileContentsOnBranch(
101+
gemspec[0],
102+
this.changesBranch
103+
)
104+
).parsedContent;
105+
106+
const gemNameMatch = gemspecContent.match(
107+
/\.name\s*=\s*['"](.*)['"]/
108+
)?.[1];
109+
110+
if (gemNameMatch) {
111+
return gemNameMatch;
112+
} else {
113+
this.logger.error('could not determine gem name from gemspec file');
114+
}
115+
}
116+
117+
return (
118+
this.component ||
119+
// grab the gem name from the version file path if it's not provided via the component
120+
this.versionFile.match(/lib\/(.*)\/version.rb/)?.[1] ||
121+
''
122+
);
123+
}
124+
94125
protected async postProcessCommits(
95126
commits: ConventionalCommit[]
96127
): Promise<ConventionalCommit[]> {

‎test/strategies/ruby.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {expect} from 'chai';
1717
import {GitHub} from '../../src/github';
1818
import {Ruby} from '../../src/strategies/ruby';
1919
import * as sinon from 'sinon';
20-
import {assertHasUpdate} from '../helpers';
20+
import {assertHasUpdate, buildGitHubFileRaw} from '../helpers';
2121
import {buildMockConventionalCommit} from '../helpers';
2222
import {TagName} from '../../src/util/tag-name';
2323
import {Version} from '../../src/version';
@@ -26,6 +26,14 @@ import {VersionRB} from '../../src/updaters/ruby/version-rb';
2626
import {GemfileLock} from '../../src/updaters/ruby/gemfile-lock';
2727
import {PullRequestBody} from '../../src/util/pull-request-body';
2828

29+
const GEM_NAME = 'google-cloud-automl-gem';
30+
31+
const GEMFILE_CONTENTS = `
32+
Gem::Specification.new do |gem|
33+
gem.name = "${GEM_NAME}"
34+
end
35+
`;
36+
2937
const sandbox = sinon.createSandbox();
3038

3139
const COMMITS = [
@@ -46,7 +54,18 @@ describe('Ruby', () => {
4654
repo: 'ruby-test-repo',
4755
defaultBranch: 'main',
4856
});
57+
58+
const fileFilesStub = sandbox.stub(github, 'findFilesByGlobAndRef');
59+
fileFilesStub
60+
.withArgs('*.gemspec', 'main')
61+
.resolves(['google-cloud-automl.gemspec']);
62+
63+
const getFileContentsStub = sandbox.stub(github, 'getFileContentsOnBranch');
64+
getFileContentsStub
65+
.withArgs('google-cloud-automl.gemspec', 'main')
66+
.resolves(buildGitHubFileRaw(GEMFILE_CONTENTS));
4967
});
68+
5069
afterEach(() => {
5170
sandbox.restore();
5271
});
@@ -91,7 +110,11 @@ describe('Ruby', () => {
91110
github,
92111
component: 'google-cloud-automl',
93112
});
94-
const latestRelease = undefined;
113+
const latestRelease = {
114+
tag: new TagName(Version.parse('0.123.4'), 'google-cloud-automl'),
115+
sha: 'abc123',
116+
notes: 'some notes',
117+
};
95118
const release = await strategy.buildReleasePullRequest({
96119
commits: COMMITS,
97120
latestRelease,
@@ -110,7 +133,20 @@ describe('Ruby', () => {
110133
'rbi/lib/google/cloud/automl/version.rbi',
111134
VersionRB
112135
);
113-
assertHasUpdate(updates, 'Gemfile.lock', GemfileLock);
136+
137+
const gemfileLockUpdate = assertHasUpdate(
138+
updates,
139+
'Gemfile.lock',
140+
GemfileLock
141+
);
142+
143+
// gemfile lock updater should be able to update content that includes the gem name from
144+
// the gemspec file
145+
expect(
146+
gemfileLockUpdate.updater.updateContent(
147+
`${GEM_NAME} (${latestRelease.tag.version})`
148+
)
149+
).to.eql(`${GEM_NAME} (${release!.version})`);
114150
});
115151
it('allows overriding version file', async () => {
116152
const strategy = new Ruby({

0 commit comments

Comments
 (0)