Skip to content

Commit

Permalink
fix(changelog/gitlab): custom endpoint and repository length validati…
Browse files Browse the repository at this point in the history
…on (#23165)
  • Loading branch information
setchy authored Jul 5, 2023
1 parent b17ab8a commit dbe5f09
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
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;
}
}

0 comments on commit dbe5f09

Please sign in to comment.