Skip to content

Commit

Permalink
refactor(changelog): Relocate in-range release fetching (#15798)
Browse files Browse the repository at this point in the history
* refactor(changelog): Move in-range release fetching

* Update lib/workers/repository/update/pr/changelog/index.ts
  • Loading branch information
zharinov committed May 31, 2022
1 parent c92f052 commit 4a3eec7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
12 changes: 5 additions & 7 deletions lib/workers/repository/update/pr/changelog/index.ts
Expand Up @@ -2,17 +2,16 @@ 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';

export * from './types';

export async function getChangeLogJSON(
args: BranchUpgradeConfig
config: BranchUpgradeConfig
): Promise<ChangeLogResult | null> {
const { sourceUrl, versioning, currentVersion, newVersion } = args;
const { sourceUrl, versioning, currentVersion, newVersion } = config;
try {
if (!(sourceUrl && currentVersion && newVersion)) {
return null;
Expand All @@ -24,18 +23,17 @@ export async function getChangeLogJSON(
logger.debug(
`Fetching changelog: ${sourceUrl} (${currentVersion} -> ${newVersion})`
);
const releases = args.releases || (await getInRangeReleases(args));

let res: ChangeLogResult | null = null;

const platform = detectPlatform(sourceUrl);

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:
Expand All @@ -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;
}
}
28 changes: 16 additions & 12 deletions lib/workers/repository/update/pr/changelog/source-github.ts
Expand Up @@ -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(
Expand All @@ -28,16 +29,18 @@ function getCachedTags(
return promisedRes;
}

export async function getChangeLogJSON({
versioning,
currentVersion,
newVersion,
sourceUrl,
sourceDirectory,
releases,
depName,
manager,
}: BranchUpgradeConfig): Promise<ChangeLogResult | null> {
export async function getChangeLogJSON(
config: BranchUpgradeConfig
): Promise<ChangeLogResult | null> {
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;
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 14 additions & 10 deletions lib/workers/repository/update/pr/changelog/source-gitlab.ts
Expand Up @@ -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';
Expand All @@ -28,16 +29,18 @@ function getCachedTags(
return promisedRes;
}

export async function getChangeLogJSON({
versioning,
currentVersion,
newVersion,
sourceUrl,
releases,
depName,
manager,
sourceDirectory,
}: BranchUpgradeConfig): Promise<ChangeLogResult | null> {
export async function getChangeLogJSON(
config: BranchUpgradeConfig
): Promise<ChangeLogResult | null> {
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);
Expand All @@ -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;
Expand Down

0 comments on commit 4a3eec7

Please sign in to comment.