From b9c8c44fdae8e23aa0cf649b480c5c22f7885a51 Mon Sep 17 00:00:00 2001 From: Adam Setch <adam.setch@outlook.com> Date: Mon, 24 Jul 2023 16:01:39 -0400 Subject: [PATCH] refactor(util/hash): replace sha256 hasha use cases (#23547) --- lib/modules/datasource/crate/index.ts | 6 ++---- lib/modules/datasource/docker/common.ts | 4 ++-- .../datasource/github-release-attachments/digest.spec.ts | 8 +++----- lib/modules/datasource/rubygems/metadata-cache.ts | 4 ++-- lib/modules/platform/pr-body.spec.ts | 4 ++-- lib/modules/platform/pr-body.ts | 4 ++-- lib/util/hasha.ts | 5 ----- lib/workers/repository/onboarding/pr/index.ts | 2 +- 8 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 lib/util/hasha.ts diff --git a/lib/modules/datasource/crate/index.ts b/lib/modules/datasource/crate/index.ts index 2853588477..7fdcf58781 100644 --- a/lib/modules/datasource/crate/index.ts +++ b/lib/modules/datasource/crate/index.ts @@ -1,4 +1,3 @@ -import hasha from 'hasha'; import Git from 'simple-git'; import upath from 'upath'; import { GlobalConfig } from '../../../config/global'; @@ -7,6 +6,7 @@ import * as memCache from '../../../util/cache/memory'; import { cache } from '../../../util/cache/package/decorator'; import { privateCacheDir, readCacheFile } from '../../../util/fs'; import { simpleGitConfig } from '../../../util/git/config'; +import { toSha256 } from '../../../util/hash'; import { newlineRegex, regEx } from '../../../util/regex'; import { joinUrlParts, parseUrl } from '../../../util/url'; import * as cargoVersioning from '../../versioning/cargo'; @@ -214,9 +214,7 @@ export class CrateDatasource extends Datasource { private static cacheDirFromUrl(url: URL): string { const proto = url.protocol.replace(regEx(/:$/), ''); const host = url.hostname; - const hash = hasha(url.pathname, { - algorithm: 'sha256', - }).substring(0, 7); + const hash = toSha256(url.pathname).substring(0, 7); return `crate-registry-${proto}-${host}-${hash}`; } diff --git a/lib/modules/datasource/docker/common.ts b/lib/modules/datasource/docker/common.ts index 5dc02fb406..57b359f08c 100644 --- a/lib/modules/datasource/docker/common.ts +++ b/lib/modules/datasource/docker/common.ts @@ -1,6 +1,5 @@ import is from '@sindresorhus/is'; import { parse } from 'auth-header'; -import hasha from 'hasha'; import { HOST_DISABLED, PAGE_NOT_FOUND_ERROR, @@ -8,6 +7,7 @@ import { import { logger } from '../../../logger'; import type { HostRule } from '../../../types'; import { ExternalHostError } from '../../../types/errors/external-host-error'; +import { toSha256 } from '../../../util/hash'; import * as hostRules from '../../../util/host-rules'; import type { Http } from '../../../util/http'; import type { @@ -285,7 +285,7 @@ export function getRegistryRepository( export function extractDigestFromResponseBody( manifestResponse: HttpResponse ): string { - return 'sha256:' + hasha(manifestResponse.body, { algorithm: 'sha256' }); + return 'sha256:' + toSha256(manifestResponse.body); } export function findLatestStable(tags: string[]): string | null { diff --git a/lib/modules/datasource/github-release-attachments/digest.spec.ts b/lib/modules/datasource/github-release-attachments/digest.spec.ts index 19264bc096..40fedd2b14 100644 --- a/lib/modules/datasource/github-release-attachments/digest.spec.ts +++ b/lib/modules/datasource/github-release-attachments/digest.spec.ts @@ -1,6 +1,6 @@ -import hasha from 'hasha'; import * as httpMock from '../../../../test/http-mock'; import type { GithubDigestFile } from '../../../util/github/types'; +import { toSha256 } from '../../../util/hash'; import { GitHubReleaseAttachmentMocker } from './test'; import { GithubReleaseAttachmentsDatasource } from '.'; @@ -55,7 +55,7 @@ describe('modules/datasource/github-release-attachments/digest', () => { 'asset.zip': content, 'smallest.zip': '1'.repeat(8 * 1024), }); - const contentDigest = await hasha.async(content, { algorithm: 'sha256' }); + const contentDigest = toSha256(content); const digestAsset = await githubReleaseAttachments.findDigestAsset( release, @@ -147,9 +147,7 @@ describe('modules/datasource/github-release-attachments/digest', () => { const release = releaseMock.withAssets('v1.0.1', { 'asset.zip': updatedContent, }); - const contentDigest = await hasha.async(updatedContent, { - algorithm: 'sha256', - }); + const contentDigest = toSha256(updatedContent); const digest = await githubReleaseAttachments.mapDigestAssetToRelease( digestAsset, diff --git a/lib/modules/datasource/rubygems/metadata-cache.ts b/lib/modules/datasource/rubygems/metadata-cache.ts index 225522b9b0..9bae7437cf 100644 --- a/lib/modules/datasource/rubygems/metadata-cache.ts +++ b/lib/modules/datasource/rubygems/metadata-cache.ts @@ -1,5 +1,5 @@ -import hasha from 'hasha'; import * as packageCache from '../../../util/cache/package'; +import { toSha256 } from '../../../util/hash'; import type { Http } from '../../../util/http'; import { AsyncResult, Result } from '../../../util/result'; import { parseUrl } from '../../../util/url'; @@ -21,7 +21,7 @@ export class MetadataCache { ): Promise<ReleaseResult> { const cacheNs = `datasource-rubygems`; const cacheKey = `metadata-cache:${registryUrl}:${packageName}`; - const hash = hasha(versions, { algorithm: 'sha256' }); + const hash = toSha256(versions.join('')); const loadCache = (): AsyncResult<ReleaseResult, unknown> => Result.wrapNullable( diff --git a/lib/modules/platform/pr-body.spec.ts b/lib/modules/platform/pr-body.spec.ts index 89967f8a31..f25cc491c9 100644 --- a/lib/modules/platform/pr-body.spec.ts +++ b/lib/modules/platform/pr-body.spec.ts @@ -1,4 +1,4 @@ -import hasha from 'hasha'; +import { toSha256 } from '../../util/hash'; import { getPrBodyStruct, hashBody } from './pr-body'; describe('modules/platform/pr-body', () => { @@ -83,7 +83,7 @@ describe('modules/platform/pr-body', () => { it('returns raw config hash', () => { const config = '{}'; - const rawConfigHash = hasha(config, { algorithm: 'sha256' }); + const rawConfigHash = toSha256(config); const input = `<!--renovate-config-hash:${rawConfigHash}-->`; const hash = hashBody(input); expect(getPrBodyStruct(input)).toEqual({ diff --git a/lib/modules/platform/pr-body.ts b/lib/modules/platform/pr-body.ts index 88f28826bc..97dc74b028 100644 --- a/lib/modules/platform/pr-body.ts +++ b/lib/modules/platform/pr-body.ts @@ -1,7 +1,7 @@ import is from '@sindresorhus/is'; -import hasha from 'hasha'; import { logger } from '../../logger'; import { stripEmojis } from '../../util/emoji'; +import { toSha256 } from '../../util/hash'; import { regEx } from '../../util/regex'; import { fromBase64 } from '../../util/string'; import type { PrBodyStruct } from './types'; @@ -31,7 +31,7 @@ export function hashBody(body: string | undefined): string { } result = stripEmojis(result); result = noWhitespaceOrHeadings(result); - result = hasha(result, { algorithm: 'sha256' }); + result = toSha256(result); return result; } diff --git a/lib/util/hasha.ts b/lib/util/hasha.ts deleted file mode 100644 index a9d9f22b3e..0000000000 --- a/lib/util/hasha.ts +++ /dev/null @@ -1,5 +0,0 @@ -import hasha from 'hasha'; - -export function toSha256(input: string): string { - return hasha(input, { algorithm: 'sha256' }); -} diff --git a/lib/workers/repository/onboarding/pr/index.ts b/lib/workers/repository/onboarding/pr/index.ts index 08b58ea760..f18aa839b2 100644 --- a/lib/workers/repository/onboarding/pr/index.ts +++ b/lib/workers/repository/onboarding/pr/index.ts @@ -9,7 +9,7 @@ import { hashBody } from '../../../../modules/platform/pr-body'; import { scm } from '../../../../modules/platform/scm'; import { emojify } from '../../../../util/emoji'; import { getFile } from '../../../../util/git'; -import { toSha256 } from '../../../../util/hasha'; +import { toSha256 } from '../../../../util/hash'; import * as template from '../../../../util/template'; import type { BranchConfig } from '../../../types'; import { -- GitLab