From bc94980bc053c37d1f5c74499f3400b52b13657b Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Wed, 27 Oct 2021 13:22:09 +0300 Subject: [PATCH] refactor(go): Extract digest functionality to separate file (#12351) --- lib/datasource/go/digest.ts | 39 +++++++++++++++++++++++++++++++++ lib/datasource/go/index.spec.ts | 3 ++- lib/datasource/go/index.ts | 38 +------------------------------- 3 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 lib/datasource/go/digest.ts diff --git a/lib/datasource/go/digest.ts b/lib/datasource/go/digest.ts new file mode 100644 index 0000000000..0dd01efcc9 --- /dev/null +++ b/lib/datasource/go/digest.ts @@ -0,0 +1,39 @@ +import * as github from '../github-tags'; +import type { DigestConfig } from '../types'; +import { bitbucket, getDatasource } from './util'; + +/** + * go.getDigest + * + * This datasource resolves a go module URL into its source repository + * and then fetches the digest it if it is on GitHub. + * + * This function will: + * - Determine the source URL for the module + * - Call the respective getDigest in github to retrieve the commit hash + */ +export async function getDigest( + { lookupName }: Partial<DigestConfig>, + value?: string +): Promise<string | null> { + const source = await getDatasource(lookupName); + if (!source) { + return null; + } + + // ignore v0.0.0- pseudo versions that are used Go Modules - look up default branch instead + const tag = value && !value.startsWith('v0.0.0-2') ? value : undefined; + + switch (source.datasource) { + case github.id: { + return github.getDigest(source, tag); + } + case bitbucket.id: { + return bitbucket.getDigest(source, tag); + } + /* istanbul ignore next: can never happen, makes lint happy */ + default: { + return null; + } + } +} diff --git a/lib/datasource/go/index.spec.ts b/lib/datasource/go/index.spec.ts index 68be8a79a4..f667468293 100644 --- a/lib/datasource/go/index.spec.ts +++ b/lib/datasource/go/index.spec.ts @@ -2,7 +2,8 @@ import { getPkgReleases } from '..'; import * as httpMock from '../../../test/http-mock'; import { logger, mocked } from '../../../test/util'; import * as _hostRules from '../../util/host-rules'; -import { id as datasource, getDigest } from '.'; +import { getDigest } from './digest'; +import { id as datasource } from '.'; jest.mock('../../util/host-rules'); diff --git a/lib/datasource/go/index.ts b/lib/datasource/go/index.ts index e43df23258..99c554cbba 100644 --- a/lib/datasource/go/index.ts +++ b/lib/datasource/go/index.ts @@ -2,7 +2,7 @@ import { logger } from '../../logger'; import { regEx } from '../../util/regex'; import * as github from '../github-tags'; import * as gitlab from '../gitlab-tags'; -import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types'; +import type { GetReleasesConfig, ReleaseResult } from '../types'; import * as goproxy from './goproxy'; import { bitbucket, getDatasource } from './util'; @@ -115,39 +115,3 @@ export async function getReleases( return res; } - -/** - * go.getDigest - * - * This datasource resolves a go module URL into its source repository - * and then fetches the digest it if it is on GitHub. - * - * This function will: - * - Determine the source URL for the module - * - Call the respective getDigest in github to retrieve the commit hash - */ -export async function getDigest( - { lookupName }: Partial<DigestConfig>, - value?: string -): Promise<string | null> { - const source = await getDatasource(lookupName); - if (!source) { - return null; - } - - // ignore v0.0.0- pseudo versions that are used Go Modules - look up default branch instead - const tag = value && !value.startsWith('v0.0.0-2') ? value : undefined; - - switch (source.datasource) { - case github.id: { - return github.getDigest(source, tag); - } - case bitbucket.id: { - return bitbucket.getDigest(source, tag); - } - /* istanbul ignore next: can never happen, makes lint happy */ - default: { - return null; - } - } -} -- GitLab