Skip to content

Commit

Permalink
feat(repo/changelogs): allow user configuration of source url (#16873)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Ladzaretti committed Aug 22, 2022
1 parent 064c1e9 commit a54cc47
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 5 deletions.
24 changes: 24 additions & 0 deletions docs/usage/configuration-options.md
Expand Up @@ -1876,6 +1876,30 @@ For example to apply a special label for Major updates:
}
```

### customChangelogUrl

Use this field to set the source URL for a package, including overriding an existing one.
Source URLs are necessary in order to look up release notes.

Using this field we can specify the exact url to fetch release notes from.

Example setting source URL for package "dummy":

```json
{
"packageRules": [
{
"matchPackageNames": ["dummy"],
"customChangelogUrl": "https://github.com/org/dummy"
}
]
}
```

<!-- prettier-ignore -->
!!! note
Renovate can fetch changelogs from GitHub and GitLab platforms only, and setting the URL to an unsupported host/platform type won't change that.

### replacementName

This config option only works with the `npm` manager.
Expand Down
10 changes: 10 additions & 0 deletions lib/config/options/index.ts
Expand Up @@ -1185,6 +1185,16 @@ const options: RenovateOptions[] = [
cli: false,
env: false,
},
{
name: 'customChangelogUrl',
description:
'If set, Renovate will use this url to fetch changelogs for a matched dependency. Valid only within a `packageRules` object.',
type: 'string',
stage: 'pr',
parent: 'packageRules',
cli: false,
env: false,
},
{
name: 'pinDigests',
description: 'Whether to add digests to Dockerfile source images.',
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.ts
Expand Up @@ -34,6 +34,7 @@ export interface RenovateSharedConfig {
commitMessage?: string;
commitMessagePrefix?: string;
confidential?: boolean;
customChangelogUrl?: string;
draftPR?: boolean;
enabled?: boolean;
enabledManagers?: string[];
Expand Down
14 changes: 14 additions & 0 deletions lib/workers/repository/update/pr/changelog/common.spec.ts
@@ -0,0 +1,14 @@
import { slugifyUrl } from './common';

describe('workers/repository/update/pr/changelog/common', () => {
it.each`
url | expected
${'https://github-enterprise.example.com/çhãlk/chálk'} | ${'https-github-enterprise-example-com-chalk-chalk'}
${'https://github.com/chalk/chalk'} | ${'https-github-com-chalk-chalk'}
${'https://github-enterprise.example.com/'} | ${'https-github-enterprise-example-com'}
${'https://github.com/sindresorhus/delay'} | ${'https-github-com-sindresorhus-delay'}
${'https://github.com/🔥/∂u/∂t/equals/α∇^2u'} | ${'https-github-com-du-dt-equals-a2u'}
`('isSingleVersion("$url") === $expected', ({ url, expected }) => {
expect(slugifyUrl(url)).toBe(expected);
});
});
7 changes: 7 additions & 0 deletions lib/workers/repository/update/pr/changelog/common.ts
@@ -0,0 +1,7 @@
import slugify from 'slugify';
import { regEx } from '../../../../../util/regex';

export function slugifyUrl(url: string): string {
const r = regEx(/(:?[:/.])+/g);
return slugify(url.replace(r, ' '));
}
59 changes: 59 additions & 0 deletions lib/workers/repository/update/pr/changelog/github.spec.ts
Expand Up @@ -265,6 +265,34 @@ describe('workers/repository/update/pr/changelog/github', () => {
});
});

it('supports overwriting sourceUrl for supports github enterprise and github.com changelog', async () => {
const sourceUrl = upgrade.sourceUrl;
const replacementSourceUrl = 'https://github.com/sindresorhus/got';
const config = {
...upgrade,
endpoint: 'https://github-enterprise.example.com/',
customChangelogUrl: replacementSourceUrl,
};
hostRules.add({
hostType: PlatformId.Github,
token: 'super_secret',
matchHost: 'https://github-enterprise.example.com/',
});
expect(await getChangeLogJSON(config)).toMatchObject({
hasReleaseNotes: true,
project: {
apiBaseUrl: 'https://api.github.com/',
baseUrl: 'https://github.com/',
depName: 'renovate',
repository: 'sindresorhus/got',
sourceDirectory: undefined,
sourceUrl: 'https://github.com/sindresorhus/got',
type: 'github',
},
});
expect(upgrade.sourceUrl).toBe(sourceUrl); // ensure unmodified function argument
});

it('supports github enterprise and github enterprise changelog', async () => {
hostRules.add({
hostType: PlatformId.Github,
Expand Down Expand Up @@ -298,6 +326,37 @@ describe('workers/repository/update/pr/changelog/github', () => {
});
});

it('supports overwriting sourceUrl for github enterprise and github enterprise changelog', async () => {
const sourceUrl = 'https://github-enterprise.example.com/chalk/chalk';
const replacementSourceUrl =
'https://github-enterprise.example.com/sindresorhus/got';
const config = {
...upgrade,
sourceUrl,
endpoint: 'https://github-enterprise.example.com/',
customChangelogUrl: replacementSourceUrl,
};
hostRules.add({
hostType: PlatformId.Github,
matchHost: 'https://github-enterprise.example.com/',
token: 'abc',
});
process.env.GITHUB_ENDPOINT = '';
expect(await getChangeLogJSON(config)).toMatchObject({
hasReleaseNotes: true,
project: {
apiBaseUrl: 'https://github-enterprise.example.com/api/v3/',
baseUrl: 'https://github-enterprise.example.com/',
depName: 'renovate',
repository: 'sindresorhus/got',
sourceDirectory: undefined,
sourceUrl: 'https://github-enterprise.example.com/sindresorhus/got',
type: 'github',
},
});
expect(config.sourceUrl).toBe(sourceUrl); // ensure unmodified function argument
});

it('works with same version releases but different prefix', async () => {
const githubTagsMock = jest.spyOn(
CacheableGithubTags.prototype,
Expand Down
33 changes: 33 additions & 0 deletions lib/workers/repository/update/pr/changelog/gitlab.spec.ts
Expand Up @@ -315,5 +315,38 @@ describe('workers/repository/update/pr/changelog/gitlab', () => {
],
});
});

it('supports overwriting sourceUrl for self-hosted gitlab changelog', async () => {
httpMock.scope('https://git.test.com').persist().get(/.*/).reply(200, []);
const sourceUrl = 'https://git.test.com/meno/dropzone/';
const replacementSourceUrl =
'https://git.test.com/replacement/sourceurl/';
const config = {
...upgrade,
platform: PlatformId.Gitlab,
endpoint: 'https://git.test.com/api/v4/',
sourceUrl,
customChangelogUrl: replacementSourceUrl,
};
hostRules.add({
hostType: PlatformId.Gitlab,
matchHost: 'https://git.test.com/',
token: 'abc',
});
process.env.GITHUB_ENDPOINT = '';
expect(await getChangeLogJSON(config)).toMatchObject({
hasReleaseNotes: false,
project: {
apiBaseUrl: 'https://git.test.com/api/v4/',
baseUrl: 'https://git.test.com/',
depName: 'renovate',
repository: 'replacement/sourceurl',
sourceDirectory: undefined,
sourceUrl: 'https://git.test.com/replacement/sourceurl/',
type: 'gitlab',
},
});
expect(config.sourceUrl).toBe(sourceUrl); // ensure unmodified function argument
});
});
});
6 changes: 4 additions & 2 deletions lib/workers/repository/update/pr/changelog/index.ts
Expand Up @@ -9,9 +9,11 @@ import type { ChangeLogResult } from './types';
export * from './types';

export async function getChangeLogJSON(
config: BranchUpgradeConfig
_config: BranchUpgradeConfig
): Promise<ChangeLogResult | null> {
const { sourceUrl, versioning, currentVersion, newVersion } = config;
const sourceUrl = _config.customChangelogUrl ?? _config.sourceUrl!;
const config: BranchUpgradeConfig = { ..._config, sourceUrl };
const { versioning, currentVersion, newVersion } = config;
try {
if (!(sourceUrl && currentVersion && newVersion)) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion lib/workers/repository/update/pr/changelog/source-github.ts
Expand Up @@ -10,6 +10,7 @@ import * as packageCache from '../../../../../util/cache/package';
import * as hostRules from '../../../../../util/host-rules';
import { regEx } from '../../../../../util/regex';
import type { BranchUpgradeConfig } from '../../../../types';
import { slugifyUrl } from './common';
import { getTags } from './github';
import { addReleaseNotes } from './release-notes';
import { getInRangeReleases } from './releases';
Expand Down Expand Up @@ -120,8 +121,9 @@ export async function getChangeLogJSON(
}

const cacheNamespace = 'changelog-github-release';

function getCacheKey(prev: string, next: string): string {
return `${manager}:${depName}:${prev}:${next}`;
return `${slugifyUrl(sourceUrl)}:${depName}:${prev}:${next}`;
}

const changelogReleases: ChangeLogRelease[] = [];
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/update/pr/changelog/source-gitlab.ts
Expand Up @@ -7,6 +7,7 @@ import * as memCache from '../../../../../util/cache/memory';
import * as packageCache from '../../../../../util/cache/package';
import { regEx } from '../../../../../util/regex';
import type { BranchUpgradeConfig } from '../../../../types';
import { slugifyUrl } from './common';
import { getTags } from './gitlab';
import { addReleaseNotes } from './release-notes';
import { getInRangeReleases } from './releases';
Expand Down Expand Up @@ -38,7 +39,6 @@ export async function getChangeLogJSON(
const newVersion = config.newVersion!;
const sourceUrl = config.sourceUrl!;
const depName = config.depName!;
const manager = config.manager;
const sourceDirectory = config.sourceDirectory!;

logger.trace('getChangeLogJSON for gitlab');
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function getChangeLogJSON(
}

function getCacheKey(prev: string, next: string): string {
return `${manager}:${depName}:${prev}:${next}`;
return `${slugifyUrl(sourceUrl)}:${depName}:${prev}:${next}`;
}

const changelogReleases: ChangeLogRelease[] = [];
Expand Down

0 comments on commit a54cc47

Please sign in to comment.