Skip to content

Commit

Permalink
refactor(gitlab): Extract getSourceUrl function (#12511)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Nov 5, 2021
1 parent 3cd70f8 commit fa71763
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/datasource/gitlab-tags/index.ts
@@ -1,15 +1,15 @@
import * as packageCache from '../../util/cache/package';
import { GitlabHttp } from '../../util/http/gitlab';
import { regEx } from '../../util/regex';
import { joinUrlParts } from '../../util/url';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import type { GitlabTag } from './types';
import { defaultRegistryUrl, getDepHost, getSourceUrl } from './util';

export const id = 'gitlab-tags';
const gitlabApi = new GitlabHttp(id);

export const customRegistrySupport = true;
export const defaultRegistryUrls = ['https://gitlab.com'];
export const defaultRegistryUrls = [defaultRegistryUrl];
export const registryStrategy = 'first';

const cacheNamespace = 'datasource-gitlab';
Expand All @@ -23,7 +23,7 @@ export async function getReleases({
registryUrl,
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const depHost = registryUrl.replace(regEx(/\/api\/v4$/), '');
const depHost = getDepHost(registryUrl);

const cachedResult = await packageCache.get<ReleaseResult>(
cacheNamespace,
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function getReleases({
).body;

const dependency: ReleaseResult = {
sourceUrl: joinUrlParts(depHost, repo),
sourceUrl: getSourceUrl(repo, registryUrl),
releases: null,
};
dependency.releases = gitlabTags.map(({ name, commit }) => ({
Expand Down
13 changes: 13 additions & 0 deletions lib/datasource/gitlab-tags/util.ts
@@ -0,0 +1,13 @@
import { regEx } from '../../util/regex';
import { joinUrlParts } from '../../util/url';

export const defaultRegistryUrl = 'https://gitlab.com';

export function getDepHost(registryUrl: string = defaultRegistryUrl): string {
return registryUrl.replace(regEx(/\/api\/v4$/), '');
}

export function getSourceUrl(lookupName: string, registryUrl?: string): string {
const depHost = getDepHost(registryUrl);
return joinUrlParts(depHost, lookupName);
}

0 comments on commit fa71763

Please sign in to comment.