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(sbt-package): Fallback to Maven datasource for GitLab packages #16817

Merged
merged 9 commits into from
Aug 1, 2022
3 changes: 2 additions & 1 deletion lib/modules/datasource/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
DatasourceApi,
DigestConfig,
GetReleasesConfig,
RegistryStrategy,
ReleaseResult,
} from './types';

Expand All @@ -23,7 +24,7 @@ export abstract class Datasource implements DatasourceApi {

defaultVersioning: string | undefined;

registryStrategy: 'first' | 'hunt' | 'merge' | undefined = 'first';
registryStrategy: RegistryStrategy | undefined = 'first';

protected http: Http;

Expand Down
11 changes: 8 additions & 3 deletions lib/modules/datasource/maven/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import mavenVersion from '../../versioning/maven';
import * as mavenVersioning from '../../versioning/maven';
import { compare } from '../../versioning/maven/compare';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import type {
GetReleasesConfig,
RegistryStrategy,
Release,
ReleaseResult,
} from '../types';
import { MAVEN_REPO } from './common';
import type { MavenDependency, ReleaseMap } from './types';
import {
Expand Down Expand Up @@ -57,9 +62,9 @@ export class MavenDatasource extends Datasource {

override readonly defaultRegistryUrls = defaultRegistryUrls;

override readonly defaultVersioning = mavenVersioning.id;
override readonly defaultVersioning: string = mavenVersioning.id;

override readonly registryStrategy = 'merge';
override readonly registryStrategy: RegistryStrategy = 'merge';

constructor(id = MavenDatasource.id) {
super(id);
Expand Down
38 changes: 38 additions & 0 deletions lib/modules/datasource/sbt-package/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,43 @@ describe('modules/datasource/sbt-package/index', () => {
releases: [{ version: '1.2.3' }],
});
});

it('falls back to Maven for GitLab-hosted packages', async () => {
httpMock
.scope('https://gitlab.com/api/v4/projects/123/packages/maven/')
.get('/org/example/example/maven-metadata.xml')
.reply(
200,
`
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.example</groupId>
<artifactId>package</artifactId>
<versioning>
<latest>1.2.3</latest>
<release>1.2.3</release>
<versions>
<version>1.2.3</version>
</versions>
</versioning>
</metadata>
`
)
.head('/org/example/example/1.2.3/example-1.2.3.pom')
.reply(200)
.get('/org/example/example/1.2.3/example-1.2.3.pom')
.reply(200);

const res = await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPackageDatasource.id,
depName: 'org.example:example',
registryUrls: [
'https://gitlab.com/api/v4/projects/123/packages/maven/',
],
});

expect(res).toMatchObject({});
});
});
});
31 changes: 23 additions & 8 deletions lib/modules/datasource/sbt-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { XmlDocument } from 'xmldoc';
import { logger } from '../../../logger';
import { Http } from '../../../util/http';
import { regEx } from '../../../util/regex';
import { ensureTrailingSlash } from '../../../util/url';
import { ensureTrailingSlash, parseUrl } from '../../../util/url';
import * as ivyVersioning from '../../versioning/ivy';
import { compare } from '../../versioning/maven/compare';
import { Datasource } from '../datasource';
import { MavenDatasource } from '../maven';
import { MAVEN_REPO } from '../maven/common';
import { downloadHttpProtocol } from '../maven/util';
import type { GetReleasesConfig, ReleaseResult } from '../types';
Expand All @@ -15,8 +15,8 @@ import {
parseIndexDir,
} from './util';

export class SbtPackageDatasource extends Datasource {
static id = 'sbt-package';
export class SbtPackageDatasource extends MavenDatasource {
viceice marked this conversation as resolved.
Show resolved Hide resolved
static override id = 'sbt-package';
zharinov marked this conversation as resolved.
Show resolved Hide resolved

override readonly defaultRegistryUrls = [MAVEN_REPO];

Expand Down Expand Up @@ -143,15 +143,30 @@ export class SbtPackageDatasource extends Datasource {
return result;
}

async getReleases({
packageName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
private isGitlab(url: string): boolean {
let result = false;
const parsedUrl = parseUrl(url);
if (parsedUrl) {
const { hostname } = parsedUrl;
result = hostname.endsWith('.gitlab.com') || hostname === 'gitlab.com';
zharinov marked this conversation as resolved.
Show resolved Hide resolved
}
return result;
}

override async getReleases(
config: GetReleasesConfig
): Promise<ReleaseResult | null> {
const { packageName, registryUrl } = config;
// istanbul ignore if
if (!registryUrl) {
return null;
}

if (this.isGitlab(registryUrl)) {
const mavenReleases = await super.getReleases(config);
return mavenReleases;
}

const [groupId, artifactId] = packageName.split(':');
const groupIdSplit = groupId.split('.');
const artifactIdSplit = artifactId.split('_');
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/datasource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export interface ReleaseResult {
replacementVersion?: string;
}

export type RegistryStrategy = 'first' | 'hunt' | 'merge';

export interface DatasourceApi extends ModuleApi {
id: string;
getDigest?(config: DigestConfig, newValue?: string): Promise<string | null>;
Expand All @@ -86,7 +88,7 @@ export interface DatasourceApi extends ModuleApi {
* hunt: registryUrls will be tried in order until one returns a result
* merge: all registryUrls will be tried and the results merged if more than one returns a result
*/
registryStrategy?: 'first' | 'hunt' | 'merge';
registryStrategy?: RegistryStrategy;

/**
* Whether custom registryUrls are allowed.
Expand Down