Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(changelog/gitlab): custom endpoint and repository length validation #23182

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/workers/repository/update/pr/changelog/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { partial } from '../../../../../../../test/util';
import * as semverVersioning from '../../../../../../modules/versioning/semver';
import * as hostRules from '../../../../../../util/host-rules';
import type { BranchUpgradeConfig } from '../../../../../types';
import { GitLabChangeLogSource } from './source';

const upgrade = partial<BranchUpgradeConfig>({
manager: 'some-manager',
Expand All @@ -29,6 +30,8 @@ const upgrade = partial<BranchUpgradeConfig>({

const matchHost = 'https://gitlab.com/';

const changelogSource = new GitLabChangeLogSource();

describe('workers/repository/update/pr/changelog/gitlab/index', () => {
afterEach(() => {
// FIXME: add missing http mocks
Expand Down Expand Up @@ -347,4 +350,31 @@ describe('workers/repository/update/pr/changelog/gitlab/index', () => {
expect(config.sourceUrl).toBe(sourceUrl); // ensure unmodified function argument
});
});

describe('hasValidRepository', () => {
it('handles invalid repository', () => {
expect(changelogSource.hasValidRepository('foo')).toBeFalse();
});

it('handles valid repository', () => {
expect(changelogSource.hasValidRepository('some/repo')).toBeTrue();
expect(changelogSource.hasValidRepository('some/repo/name')).toBeTrue();
});
});

describe('getAllTags', () => {
it('handles endpoint', async () => {
httpMock
.scope('https://git.test.com/')
.get('/api/v4/projects/some%2Frepo/repository/tags?per_page=100')
.reply(200, [
{ name: 'v5.2.0' },
{ name: 'v5.4.0' },
{ name: 'v5.5.0' },
]);
expect(
await changelogSource.getAllTags('https://git.test.com/', 'some/repo')
).toEqual(['v5.2.0', 'v5.4.0', 'v5.5.0']);
});
});
});
4 changes: 4 additions & 0 deletions lib/workers/repository/update/pr/changelog/gitlab/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ export class GitLabChangeLogSource extends ChangeLogSource {
): string {
return `${baseUrl}${repository}/compare/${prevHead}...${nextHead}`;
}

override hasValidRepository(repository: string): boolean {
return repository.split('/').length >= 2;
}
}
11 changes: 11 additions & 0 deletions lib/workers/repository/update/pr/changelog/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ describe('workers/repository/update/pr/changelog/source', () => {
);
});
});

describe('hasValidRepository', () => {
it('handles invalid repository', () => {
expect(changelogSource.hasValidRepository('foo')).toBeFalse();
expect(changelogSource.hasValidRepository('some/repo/name')).toBeFalse();
});

it('handles valid repository', () => {
expect(changelogSource.hasValidRepository('some/repo')).toBeTrue();
});
});
});
7 changes: 6 additions & 1 deletion lib/workers/repository/update/pr/changelog/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export abstract class ChangeLogSource {
async getAllTags(endpoint: string, repository: string): Promise<string[]> {
const tags = (
await getPkgReleases({
registryUrls: [endpoint],
datasource: this.datasource,
packageName: repository,
versioning:
Expand Down Expand Up @@ -91,7 +92,7 @@ export abstract class ChangeLogSource {
return null;
}

if (repository.split('/').length !== 2) {
if (is.falsy(this.hasValidRepository(repository))) {
logger.debug(`Invalid ${this.platform} URL found: ${sourceUrl}`);
return null;
}
Expand Down Expand Up @@ -266,4 +267,8 @@ export abstract class ChangeLogSource {
protected shouldSkipPackage(config: BranchUpgradeConfig): boolean {
return false;
}

hasValidRepository(repository: string): boolean {
return repository.split('/').length === 2;
}
}