From 4a3eec7371662c0fd482000d4c43192a51fb5720 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 31 May 2022 16:39:12 +0300 Subject: [PATCH] refactor(changelog): Relocate in-range release fetching (#15798) * refactor(changelog): Move in-range release fetching * Update lib/workers/repository/update/pr/changelog/index.ts --- .../repository/update/pr/changelog/index.ts | 12 ++++---- .../update/pr/changelog/source-github.ts | 28 +++++++++++-------- .../update/pr/changelog/source-gitlab.ts | 24 +++++++++------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/workers/repository/update/pr/changelog/index.ts b/lib/workers/repository/update/pr/changelog/index.ts index 72eff8a217fd5d..fcca0fc7f56028 100644 --- a/lib/workers/repository/update/pr/changelog/index.ts +++ b/lib/workers/repository/update/pr/changelog/index.ts @@ -2,7 +2,6 @@ import { logger } from '../../../../../logger'; import { detectPlatform } from '../../../../../modules/platform/util'; import * as allVersioning from '../../../../../modules/versioning'; import type { BranchUpgradeConfig } from '../../../../types'; -import { getInRangeReleases } from './releases'; import * as sourceGithub from './source-github'; import * as sourceGitlab from './source-gitlab'; import type { ChangeLogResult } from './types'; @@ -10,9 +9,9 @@ import type { ChangeLogResult } from './types'; export * from './types'; export async function getChangeLogJSON( - args: BranchUpgradeConfig + config: BranchUpgradeConfig ): Promise { - const { sourceUrl, versioning, currentVersion, newVersion } = args; + const { sourceUrl, versioning, currentVersion, newVersion } = config; try { if (!(sourceUrl && currentVersion && newVersion)) { return null; @@ -24,7 +23,6 @@ export async function getChangeLogJSON( logger.debug( `Fetching changelog: ${sourceUrl} (${currentVersion} -> ${newVersion})` ); - const releases = args.releases || (await getInRangeReleases(args)); let res: ChangeLogResult | null = null; @@ -32,10 +30,10 @@ export async function getChangeLogJSON( switch (platform) { case 'gitlab': - res = await sourceGitlab.getChangeLogJSON({ ...args, releases }); + res = await sourceGitlab.getChangeLogJSON(config); break; case 'github': - res = await sourceGithub.getChangeLogJSON({ ...args, releases }); + res = await sourceGithub.getChangeLogJSON(config); break; default: @@ -48,7 +46,7 @@ export async function getChangeLogJSON( return res; } catch (err) /* istanbul ignore next */ { - logger.error({ config: args, err }, 'getChangeLogJSON error'); + logger.error({ config, err }, 'getChangeLogJSON error'); return null; } } diff --git a/lib/workers/repository/update/pr/changelog/source-github.ts b/lib/workers/repository/update/pr/changelog/source-github.ts index 34729fa91467fa..8d62b41dce635a 100644 --- a/lib/workers/repository/update/pr/changelog/source-github.ts +++ b/lib/workers/repository/update/pr/changelog/source-github.ts @@ -11,6 +11,7 @@ import { regEx } from '../../../../../util/regex'; import type { BranchUpgradeConfig } from '../../../../types'; import { getTags } from './github'; import { addReleaseNotes } from './release-notes'; +import { getInRangeReleases } from './releases'; import { ChangeLogError, ChangeLogRelease, ChangeLogResult } from './types'; function getCachedTags( @@ -28,16 +29,18 @@ function getCachedTags( return promisedRes; } -export async function getChangeLogJSON({ - versioning, - currentVersion, - newVersion, - sourceUrl, - sourceDirectory, - releases, - depName, - manager, -}: BranchUpgradeConfig): Promise { +export async function getChangeLogJSON( + config: BranchUpgradeConfig +): Promise { + const { + versioning, + currentVersion, + newVersion, + sourceUrl, + sourceDirectory, + depName, + manager, + } = config; if (sourceUrl === 'https://github.com/DefinitelyTyped/DefinitelyTyped') { logger.trace('No release notes for @types'); return null; @@ -48,12 +51,12 @@ export async function getChangeLogJSON({ const url = sourceUrl.startsWith('https://github.com/') ? 'https://api.github.com/' : sourceUrl; - const config = hostRules.find({ + const { token } = hostRules.find({ hostType: PlatformId.Github, url, }); // istanbul ignore if - if (!config.token) { + if (!token) { if (host.endsWith('github.com')) { if (!GlobalConfig.get().githubTokenWarn) { logger.debug( @@ -85,6 +88,7 @@ export async function getChangeLogJSON({ logger.debug({ sourceUrl }, 'Invalid github URL found'); return null; } + const releases = config.releases || (await getInRangeReleases(config)); if (!releases?.length) { logger.debug('No releases'); return null; diff --git a/lib/workers/repository/update/pr/changelog/source-gitlab.ts b/lib/workers/repository/update/pr/changelog/source-gitlab.ts index 64c3f04edc2f31..04815f531e264f 100644 --- a/lib/workers/repository/update/pr/changelog/source-gitlab.ts +++ b/lib/workers/repository/update/pr/changelog/source-gitlab.ts @@ -8,6 +8,7 @@ import { regEx } from '../../../../../util/regex'; import type { BranchUpgradeConfig } from '../../../../types'; import { getTags } from './gitlab'; import { addReleaseNotes } from './release-notes'; +import { getInRangeReleases } from './releases'; import type { ChangeLogRelease, ChangeLogResult } from './types'; const cacheNamespace = 'changelog-gitlab-release'; @@ -28,16 +29,18 @@ function getCachedTags( return promisedRes; } -export async function getChangeLogJSON({ - versioning, - currentVersion, - newVersion, - sourceUrl, - releases, - depName, - manager, - sourceDirectory, -}: BranchUpgradeConfig): Promise { +export async function getChangeLogJSON( + config: BranchUpgradeConfig +): Promise { + const { + versioning, + currentVersion, + newVersion, + sourceUrl, + depName, + manager, + sourceDirectory, + } = config; logger.trace('getChangeLogJSON for gitlab'); const version = allVersioning.get(versioning); const { protocol, host, pathname } = URL.parse(sourceUrl); @@ -52,6 +55,7 @@ export async function getChangeLogJSON({ logger.info({ sourceUrl }, 'Invalid gitlab URL found'); return null; } + const releases = config.releases || (await getInRangeReleases(config)); if (!releases?.length) { logger.debug('No releases'); return null;