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(internal): datasource registryStrategy #6549

Merged
merged 5 commits into from Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/datasource/__snapshots__/index.spec.ts.snap
Expand Up @@ -22,3 +22,18 @@ Object {
"sourceUrl": "https://github.com/nodejs/node",
}
`;

exports[`datasource/index merges registries and returns success 1`] = `
Object {
"releases": Array [
Object {
"version": "1.0.0",
},
Object {
"version": "1.1.0",
},
],
}
`;

exports[`datasource/index warns if multiple registryUrls for registryStrategy=first 1`] = `null`;
7 changes: 6 additions & 1 deletion lib/datasource/common.ts
Expand Up @@ -7,7 +7,9 @@ export interface Config {
registryUrls?: string[];
}

export type DigestConfig = Config;
export interface DigestConfig extends Config {
registryUrl?: string;
viceice marked this conversation as resolved.
Show resolved Hide resolved
}

interface ReleasesConfigBase {
compatibility?: Record<string, string>;
Expand All @@ -17,6 +19,7 @@ interface ReleasesConfigBase {

export interface GetReleasesConfig extends ReleasesConfigBase {
lookupName: string;
registryUrl?: string;
}

export interface GetPkgReleasesConfig extends ReleasesConfigBase {
Expand Down Expand Up @@ -65,6 +68,7 @@ export interface ReleaseResult {
sourceUrl?: string;
tags?: Record<string, string>;
versions?: any;
registryUrl?: string;
}

export interface Datasource {
Expand All @@ -74,6 +78,7 @@ export interface Datasource {
defaultRegistryUrls?: string[];
appendRegistryUrls?: string[];
defaultConfig?: object;
registryStrategy?: 'first' | 'hunt' | 'merge';
}

export class DatasourceError extends Error {
Expand Down
7 changes: 5 additions & 2 deletions lib/datasource/docker/index.spec.ts
Expand Up @@ -31,13 +31,16 @@ describe('api/docker', () => {

describe('getRegistryRepository', () => {
it('handles local registries', () => {
const res = docker.getRegistryRepository('registry:5000/org/package', []);
const res = docker.getRegistryRepository(
'registry:5000/org/package',
'https://index.docker.io'
);
expect(res).toMatchSnapshot();
});
it('supports registryUrls', () => {
const res = docker.getRegistryRepository(
'my.local.registry/prefix/image',
['https://my.local.registry/prefix']
'https://my.local.registry/prefix'
);
expect(res).toMatchSnapshot();
});
Expand Down
23 changes: 12 additions & 11 deletions lib/datasource/docker/index.ts
@@ -1,6 +1,5 @@
import { OutgoingHttpHeaders } from 'http';
import URL from 'url';
import is from '@sindresorhus/is';
import AWS from 'aws-sdk';
import hasha from 'hasha';
import parseLinkHeader from 'parse-link-header';
Expand All @@ -16,6 +15,8 @@ import { DatasourceError, GetReleasesConfig, ReleaseResult } from '../common';
// TODO: replace www-authenticate with https://www.npmjs.com/package/auth-header ?

export const id = 'docker';
export const defaultRegistryUrls = ['https://index.docker.io'];
export const registryStrategy = 'first';

export const defaultConfig = {
managerBranchPrefix: 'docker-',
Expand Down Expand Up @@ -57,10 +58,10 @@ export interface RegistryRepository {

export function getRegistryRepository(
lookupName: string,
registryUrls: string[]
registryUrl: string
): RegistryRepository {
if (is.nonEmptyArray(registryUrls)) {
const dockerRegistry = registryUrls[0]
if (registryUrl !== defaultRegistryUrls[0]) {
const dockerRegistry = registryUrl
.replace('https://', '')
.replace(/\/?$/, '/');
if (lookupName.startsWith(dockerRegistry)) {
Expand All @@ -77,10 +78,10 @@ export function getRegistryRepository(
split.shift();
}
let repository = split.join('/');
if (!registry && is.nonEmptyArray(registryUrls)) {
[registry] = registryUrls;
if (!registry) {
registry = registryUrl;
}
if (!registry || registry === 'docker.io') {
if (registry === 'docker.io') {
registry = 'index.docker.io';
}
if (!/^https?:\/\//.exec(registry)) {
Expand Down Expand Up @@ -327,12 +328,12 @@ async function getManifestResponse(
* - Return the digest as a string
*/
export async function getDigest(
{ registryUrls, lookupName }: GetReleasesConfig,
{ registryUrl, lookupName }: GetReleasesConfig,
newValue?: string
): Promise<string | null> {
const { registry, repository } = getRegistryRepository(
lookupName,
registryUrls
registryUrl
);
logger.debug(`getDigest(${registry}, ${repository}, ${newValue})`);
const newTag = newValue || 'latest';
Expand Down Expand Up @@ -608,11 +609,11 @@ async function getLabels(
*/
export async function getReleases({
lookupName,
registryUrls,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const { registry, repository } = getRegistryRepository(
lookupName,
registryUrls
registryUrl
);
const tags = await getTags(registry, repository);
if (!tags) {
Expand Down
11 changes: 4 additions & 7 deletions lib/datasource/gitlab-tags/index.ts
@@ -1,5 +1,4 @@
import URL from 'url';
import is from '@sindresorhus/is';
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { GitlabHttp } from '../../util/http/gitlab';
Expand All @@ -8,6 +7,8 @@ import { GetReleasesConfig, ReleaseResult } from '../common';
const gitlabApi = new GitlabHttp();

export const id = 'gitlab-tags';
export const defaultRegistryUrls = ['https://gitlab.com'];
export const registryStrategy = 'first';

const cacheNamespace = 'datasource-gitlab';
function getCacheKey(depHost: string, repo: string): string {
Expand All @@ -23,13 +24,9 @@ type GitlabTag = {
};

export async function getReleases({
registryUrls,
registryUrl: depHost,
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
// Use registryUrls if present, otherwise default to publid gitlab.com
const depHost = is.nonEmptyArray(registryUrls)
? registryUrls[0].replace(/\/$/, '')
: 'https://gitlab.com';
let gitlabTags: GitlabTag[];
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
Expand Down Expand Up @@ -65,7 +62,7 @@ export async function getReleases({
}

const dependency: ReleaseResult = {
sourceUrl: `${depHost}/${repo}`,
sourceUrl: URL.resolve(depHost, repo),
rarkins marked this conversation as resolved.
Show resolved Hide resolved
releases: null,
};
dependency.releases = gitlabTags.map(({ name, commit }) => ({
Expand Down