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

feat(bundler): add git refs support #32362

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 43 additions & 11 deletions lib/modules/manager/bundler/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,14 @@ exports[`modules/manager/bundler/extract extractPackageFile() parse mastodon Gem
},
},
{
"datasource": "rubygems",
"currentDigest": "0b799ead604f900ed50685e9b2d469cd2befba5b",
"datasource": "git-refs",
"depName": "health_check",
"managerData": {
"lineNumber": 53,
},
"packageName": "https://github.com/ianheggie/health_check",
"sourceUrl": "https://github.com/ianheggie/health_check",
},
{
"currentValue": "'~> 4.3'",
Expand Down Expand Up @@ -591,11 +594,14 @@ exports[`modules/manager/bundler/extract extractPackageFile() parse mastodon Gem
},
},
{
"datasource": "rubygems",
"currentDigest": "fd184883048b922b176939f851338d0a4971a532",
"datasource": "git-refs",
"depName": "nilsimsa",
"managerData": {
"lineNumber": 63,
},
"packageName": "https://github.com/witgo/nilsimsa",
"sourceUrl": "https://github.com/witgo/nilsimsa",
},
{
"currentValue": "'~> 1.10'",
Expand Down Expand Up @@ -660,11 +666,14 @@ exports[`modules/manager/bundler/extract extractPackageFile() parse mastodon Gem
},
},
{
"datasource": "rubygems",
"currentDigest": "58465d2e213991f8afb13b984854a49fcdcc980c",
"datasource": "git-refs",
"depName": "posix-spawn",
"managerData": {
"lineNumber": 71,
},
"packageName": "https://github.com/rtomayko/posix-spawn",
"sourceUrl": "https://github.com/rtomayko/posix-spawn",
},
{
"currentValue": "'~> 2.1'",
Expand Down Expand Up @@ -899,11 +908,14 @@ exports[`modules/manager/bundler/extract extractPackageFile() parse mastodon Gem
},
},
{
"datasource": "rubygems",
"currentDigest": "e742697a0906e74e8bb777ef98137bc3955d981d",
"datasource": "git-refs",
"depName": "json-ld",
"managerData": {
"lineNumber": 99,
},
"packageName": "https://github.com/ruby-rdf/json-ld.git",
"sourceUrl": "https://github.com/ruby-rdf/json-ld",
},
{
"currentValue": "'~> 3.0'",
Expand Down Expand Up @@ -1494,11 +1506,13 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
},
},
{
"datasource": "rubygems",
"datasource": "git-refs",
"depName": "webpacker",
"managerData": {
"lineNumber": 16,
},
"packageName": "https://github.com/rails/webpacker",
"sourceUrl": "https://github.com/rails/webpacker",
},
{
"currentValue": ""~> 3.1.11"",
Expand Down Expand Up @@ -1681,14 +1695,17 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
},
},
{
"datasource": "rubygems",
"currentValue": "update-pg",
"datasource": "git-refs",
"depName": "queue_classic",
"depTypes": [
"job",
],
"managerData": {
"lineNumber": 54,
},
"packageName": "https://github.com/rafaelfranca/queue_classic",
"sourceUrl": "https://github.com/rafaelfranca/queue_classic",
},
{
"datasource": "rubygems",
Expand Down Expand Up @@ -1791,14 +1808,17 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
},
},
{
"datasource": "rubygems",
"currentValue": "close-race",
"datasource": "git-refs",
"depName": "websocket-client-simple",
"depTypes": [
"cable",
],
"managerData": {
"lineNumber": 71,
},
"packageName": "https://github.com/matthewd/websocket-client-simple",
"sourceUrl": "https://github.com/matthewd/websocket-client-simple",
},
{
"datasource": "rubygems",
Expand Down Expand Up @@ -2024,15 +2044,19 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
},
},
{
"datasource": "rubygems",
"currentValue": "master",
"datasource": "git-refs",
"depName": "activerecord-jdbcsqlite3-adapter",
"lockedVersion": "52.1-java",
"managerData": {
"lineNumber": 129,
},
"packageName": "https://github.com/jruby/activerecord-jdbc-adapter",
"sourceUrl": "https://github.com/jruby/activerecord-jdbc-adapter",
},
{
"datasource": "rubygems",
"currentValue": "master",
"datasource": "git-refs",
"depName": "activerecord-jdbcmysql-adapter",
"depTypes": [
"db",
Expand All @@ -2041,9 +2065,12 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
"managerData": {
"lineNumber": 131,
},
"packageName": "https://github.com/jruby/activerecord-jdbc-adapter",
"sourceUrl": "https://github.com/jruby/activerecord-jdbc-adapter",
},
{
"datasource": "rubygems",
"currentValue": "master",
"datasource": "git-refs",
"depName": "activerecord-jdbcpostgresql-adapter",
"depTypes": [
"db",
Expand All @@ -2052,6 +2079,8 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
"managerData": {
"lineNumber": 132,
},
"packageName": "https://github.com/jruby/activerecord-jdbc-adapter",
"sourceUrl": "https://github.com/jruby/activerecord-jdbc-adapter",
},
{
"currentValue": "">= 1.3.0"",
Expand Down Expand Up @@ -2104,11 +2133,14 @@ exports[`modules/manager/bundler/extract extractPackageFile() parses rails Gemfi
},
},
{
"datasource": "rubygems",
"currentValue": "master",
"datasource": "git-refs",
"depName": "activerecord-oracle_enhanced-adapter",
"managerData": {
"lineNumber": 154,
},
"packageName": "https://github.com/rsim/oracle-enhanced",
"sourceUrl": "https://github.com/rsim/oracle-enhanced",
},
{
"datasource": "rubygems",
Expand Down
36 changes: 36 additions & 0 deletions lib/modules/manager/bundler/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,40 @@ describe('modules/manager/bundler/extract', () => {
],
});
});

it('parses git refs in Gemfile', async () => {
const gitRefGemfile = codeBlock`
gem 'foo', git: 'https://github.com/foo/foo', ref: 'fd184883048b922b176939f851338d0a4971a532'
gem 'bar', git: 'https://github.com/bar/bar', tag: 'v1.0.0'
gem 'baz', github: 'baz/baz', branch: 'master'
`;

fs.readLocalFile.mockResolvedValueOnce(gitRefGemfile);
const res = await extractPackageFile(gitRefGemfile, 'Gemfile');
expect(res).toMatchObject({
deps: [
{
depName: 'foo',
packageName: 'https://github.com/foo/foo',
sourceUrl: 'https://github.com/foo/foo',
currentDigest: 'fd184883048b922b176939f851338d0a4971a532',
datasource: 'git-refs',
},
{
depName: 'bar',
packageName: 'https://github.com/bar/bar',
sourceUrl: 'https://github.com/bar/bar',
currentValue: 'v1.0.0',
datasource: 'git-refs',
},
{
depName: 'baz',
packageName: 'https://github.com/baz/baz',
sourceUrl: 'https://github.com/baz/baz',
currentValue: 'master',
datasource: 'git-refs',
},
],
});
});
});
65 changes: 47 additions & 18 deletions lib/modules/manager/bundler/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { readLocalFile } from '../../../util/fs';
import { newlineRegex, regEx } from '../../../util/regex';
import { GitRefsDatasource } from '../../datasource/git-refs';
import { RubyVersionDatasource } from '../../datasource/ruby-version';
import { RubygemsDatasource } from '../../datasource/rubygems';
import type { PackageDependency, PackageFileContent } from '../types';
Expand All @@ -12,6 +13,16 @@ function formatContent(input: string): string {
return input.replace(regEx(/^ {2}/), '') + '\n'; //remove leading whitespace and add a new line at the end
}

const variableMatchRegex = regEx(
`^(?<key>\\w+)\\s*=\\s*['"](?<value>[^'"]+)['"]`,
);
const gemGitRefsMatchRegex = regEx(
`^\\s*gem\\s+(['"])(?<depName>[^'"]+)['"]((\\s*,\\s*git:\\s*['"](?<gitUrl>[^'"]+)['"])|(\\s*,\\s*github:\\s*['"](?<repoName>[^'"]+)['"]))(\\s*,\\s*branch:\\s*['"](?<branchName>[^'"]+)['"])?(\\s*,\\s*ref:\\s*['"](?<refName>[^'"]+)['"])?(\\s*,\\s*tag:\\s*['"](?<tagName>[^'"]+)['"])?`,
);
const gemMatchRegex = regEx(
`^\\s*gem\\s+(['"])(?<depName>[^'"]+)(['"])(\\s*,\\s*(?<currentValue>(['"])[^'"]+['"](\\s*,\\s*['"][^'"]+['"])?))?(\\s*,\\s*source:\\s*(['"](?<registryUrl>[^'"]+)['"]|(?<sourceName>[^'"]+)))?`,
);

export async function extractPackageFile(
content: string,
packageFile?: string,
Expand Down Expand Up @@ -114,36 +125,54 @@ export async function extractPackageFile(
});
}

const variableMatchRegex = regEx(
`^(?<key>\\w+)\\s*=\\s*['"](?<value>[^'"]+)['"]`,
);
const variableMatch = variableMatchRegex.exec(line);
if (variableMatch) {
if (variableMatch.groups?.key) {
variables[variableMatch.groups?.key] = variableMatch.groups?.value;
}
}

const gemMatchRegex = regEx(
`^\\s*gem\\s+(['"])(?<depName>[^'"]+)(['"])(\\s*,\\s*(?<currentValue>(['"])[^'"]+['"](\\s*,\\s*['"][^'"]+['"])?))?(\\s*,\\s*source:\\s*(['"](?<registryUrl>[^'"]+)['"]|(?<sourceName>[^'"]+)))?`,
);
const gemMatch = gemMatchRegex.exec(line);
if (gemMatch) {
const gemGitRefsMatch = gemGitRefsMatchRegex.exec(line)?.groups;
const gemMatch = gemMatchRegex.exec(line)?.groups;

if (gemGitRefsMatch) {
const dep: PackageDependency = {
depName: gemMatch.groups?.depName,
depName: gemGitRefsMatch.depName,
managerData: { lineNumber },
};
if (gemMatch.groups?.currentValue) {
const currentValue = gemMatch.groups.currentValue;
dep.currentValue = currentValue;
if (gemGitRefsMatch.gitUrl) {
const gitUrl = gemGitRefsMatch.gitUrl;
dep.packageName = gitUrl;

if (gitUrl.startsWith('https://')) {
dep.sourceUrl = gitUrl.replace(/\.git$/, '');
}
Djiit marked this conversation as resolved.
Show resolved Hide resolved
} else if (gemGitRefsMatch.repoName) {
dep.packageName = `https://github.com/${gemGitRefsMatch.repoName}`;
dep.sourceUrl = dep.packageName;
}
if (gemGitRefsMatch.refName) {
dep.currentDigest = gemGitRefsMatch.refName;
} else if (gemGitRefsMatch.branchName) {
dep.currentValue = gemGitRefsMatch.branchName;
} else if (gemGitRefsMatch.tagName) {
dep.currentValue = gemGitRefsMatch.tagName;
}
if (gemMatch.groups?.registryUrl) {
const registryUrl = gemMatch.groups.registryUrl;
dep.registryUrls = [registryUrl];
dep.datasource = GitRefsDatasource.id;
res.deps.push(dep);
} else if (gemMatch) {
const dep: PackageDependency = {
depName: gemMatch.depName,
managerData: { lineNumber },
};
if (gemMatch.currentValue) {
const currentValue = gemMatch.currentValue;
dep.currentValue = currentValue;
}
if (gemMatch.groups?.sourceName) {
const registryUrl = variables[gemMatch.groups.sourceName];
dep.registryUrls = [registryUrl];
if (gemMatch.registryUrl) {
dep.registryUrls = [gemMatch.registryUrl];
} else if (gemMatch.sourceName) {
dep.registryUrls = [variables[gemMatch.sourceName]];
}
dep.datasource = RubygemsDatasource.id;
res.deps.push(dep);
Expand Down
Loading