Skip to content

Commit

Permalink
refactor(changelog): Unify argument lists for the call hierarchy (#16013
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov committed Jun 11, 2022
1 parent c2fc931 commit dbf3aef
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
15 changes: 12 additions & 3 deletions lib/workers/repository/update/pr/changelog/github/index.ts
Expand Up @@ -10,7 +10,12 @@ import type {
import { GithubHttp } from '../../../../../../util/http/github';
import { fromBase64 } from '../../../../../../util/string';
import { ensureTrailingSlash } from '../../../../../../util/url';
import type { ChangeLogFile, ChangeLogNotes } from '../types';
import type {
ChangeLogFile,
ChangeLogNotes,
ChangeLogProject,
ChangeLogRelease,
} from '../types';

export const id = 'github-changelog';
const http = new GithubHttp(id);
Expand Down Expand Up @@ -107,10 +112,14 @@ export async function getReleaseNotesMd(
}

export async function getReleaseList(
apiBaseUrl: string,
repository: string
project: ChangeLogProject,
_release: ChangeLogRelease
): Promise<ChangeLogNotes[]> {
logger.trace('github.getReleaseList()');
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const apiBaseUrl = project.apiBaseUrl!;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const repository = project.repository!;
const notesSourceUrl = `${ensureTrailingSlash(
apiBaseUrl
)}repos/${repository}/releases`;
Expand Down
16 changes: 12 additions & 4 deletions lib/workers/repository/update/pr/changelog/gitlab/index.ts
Expand Up @@ -5,7 +5,12 @@ import type { GitlabTag } from '../../../../../../modules/datasource/gitlab-tags
import type { GitlabTreeNode } from '../../../../../../types/platform/gitlab';
import { GitlabHttp } from '../../../../../../util/http/gitlab';
import { ensureTrailingSlash } from '../../../../../../util/url';
import type { ChangeLogFile, ChangeLogNotes } from '../types';
import type {
ChangeLogFile,
ChangeLogNotes,
ChangeLogProject,
ChangeLogRelease,
} from '../types';

export const id = 'gitlab-changelog';
const http = new GitlabHttp(id);
Expand Down Expand Up @@ -91,11 +96,14 @@ export async function getReleaseNotesMd(
}

export async function getReleaseList(
apiBaseUrl: string,
repository: string
project: ChangeLogProject,
_release: ChangeLogRelease
): Promise<ChangeLogNotes[]> {
logger.trace('gitlab.getReleaseNotesMd()');

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const apiBaseUrl = project.apiBaseUrl!;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const repository = project.repository!;
const urlEncodedRepo = encodeURIComponent(repository);
const apiUrl = `${ensureTrailingSlash(
apiBaseUrl
Expand Down
42 changes: 27 additions & 15 deletions lib/workers/repository/update/pr/changelog/release-notes.spec.ts
Expand Up @@ -172,7 +172,10 @@ describe('workers/repository/update/pr/changelog/release-notes', () => {

describe('getReleaseList()', () => {
it('should return empty array if no apiBaseUrl', async () => {
const res = await getReleaseList({} as ChangeLogProject);
const res = await getReleaseList(
{} as ChangeLogProject,
{} as ChangeLogRelease
);
expect(res).toBeEmptyArray();
});

Expand All @@ -186,10 +189,13 @@ describe('workers/repository/update/pr/changelog/release-notes', () => {
},
] as never);

const res = await getReleaseList({
...githubProject,
repository: 'some/yet-other-repository',
});
const res = await getReleaseList(
{
...githubProject,
repository: 'some/yet-other-repository',
},
{} as ChangeLogRelease
);
expect(res).toMatchSnapshot([
{
notesSourceUrl:
Expand Down Expand Up @@ -218,10 +224,13 @@ describe('workers/repository/update/pr/changelog/release-notes', () => {
body: 'some body #123, [#124](https://gitlab.com/some/yet-other-repository/issues/124)',
},
]);
const res = await getReleaseList({
...gitlabProject,
repository: 'some/yet-other-repository',
});
const res = await getReleaseList(
{
...gitlabProject,
repository: 'some/yet-other-repository',
},
{} as ChangeLogRelease
);
expect(res).toMatchSnapshot([
{
notesSourceUrl:
Expand Down Expand Up @@ -252,12 +261,15 @@ describe('workers/repository/update/pr/changelog/release-notes', () => {
body: 'some body #123, [#124](https://my.custom.domain/some/yet-other-repository/issues/124)',
},
]);
const res = await getReleaseList({
...gitlabProject,
repository: 'some/yet-other-repository',
apiBaseUrl: 'https://my.custom.domain/api/v4/',
baseUrl: 'https://my.custom.domain/',
});
const res = await getReleaseList(
{
...gitlabProject,
repository: 'some/yet-other-repository',
apiBaseUrl: 'https://my.custom.domain/api/v4/',
baseUrl: 'https://my.custom.domain/',
},
{} as ChangeLogRelease
);
expect(res).toMatchSnapshot([
{
notesSourceUrl:
Expand Down
14 changes: 8 additions & 6 deletions lib/workers/repository/update/pr/changelog/release-notes.ts
Expand Up @@ -22,16 +22,17 @@ const markdown = new MarkdownIt('zero');
markdown.enable(['heading', 'lheading']);

export async function getReleaseList(
project: ChangeLogProject
project: ChangeLogProject,
release: ChangeLogRelease
): Promise<ChangeLogNotes[]> {
logger.trace('getReleaseList()');
const { apiBaseUrl, repository, type } = project;
try {
switch (type) {
case 'gitlab':
return await gitlab.getReleaseList(apiBaseUrl, repository);
return await gitlab.getReleaseList(project, release);
case 'github':
return await github.getReleaseList(apiBaseUrl, repository);
return await github.getReleaseList(project, release);

default:
logger.warn({ apiBaseUrl, repository, type }, 'Invalid project type');
Expand All @@ -51,15 +52,16 @@ export async function getReleaseList(
}

export function getCachedReleaseList(
project: ChangeLogProject
project: ChangeLogProject,
release: ChangeLogRelease
): Promise<ChangeLogNotes[]> {
const cacheKey = `getReleaseList-${project.apiBaseUrl}-${project.repository}`;
const cachedResult = memCache.get<Promise<ChangeLogNotes[]>>(cacheKey);
// istanbul ignore if
if (cachedResult !== undefined) {
return cachedResult;
}
const promisedRes = getReleaseList(project);
const promisedRes = getReleaseList(project, release);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}
Expand Down Expand Up @@ -103,7 +105,7 @@ export async function getReleaseNotes(
const { depName, repository } = project;
const { version, gitRef } = release;
logger.trace(`getReleaseNotes(${repository}, ${version}, ${depName})`);
const releases = await getCachedReleaseList(project);
const releases = await getCachedReleaseList(project, release);
logger.trace({ releases }, 'Release list from getReleaseList');
let releaseNotes: ChangeLogNotes | null = null;

Expand Down

0 comments on commit dbf3aef

Please sign in to comment.