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

feat(cache): Converter from class name to namespace #31060

Closed
Closed
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
87 changes: 86 additions & 1 deletion lib/util/cache/package/key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getCombinedKey } from './key';
import {
classNameToCacheNamespace,
getCombinedKey,
splitIdentifier,
} from './key';

describe('util/cache/package/key', () => {
describe('getCombinedKey', () => {
Expand All @@ -8,4 +12,85 @@ describe('util/cache/package/key', () => {
);
});
});

describe('splitIdentifierName', () => {
test.each`
identifier | components
${''} | ${[]}
${'Foo'} | ${['Foo']}
${'FooBar'} | ${['Foo', 'Bar']}
${'FooBarBaz'} | ${['Foo', 'Bar', 'Baz']}
${'FooBAR'} | ${['Foo', 'BAR']}
${'FooBARBaz'} | ${['Foo', 'BAR', 'Baz']}
${'foo'} | ${['foo']}
${'fooBar'} | ${['foo', 'Bar']}
${'fooBarBaz'} | ${['foo', 'Bar', 'Baz']}
${'fooBAR'} | ${['foo', 'BAR']}
${'fooBARBaz'} | ${['foo', 'BAR', 'Baz']}
`('$identifier -> $components', ({ identifier, components }) => {
expect(splitIdentifier(identifier)).toEqual(components);
});
});

describe('classNameToCacheNamespace', () => {
test.each`
className | namespace
${'ArtifactoryDatasource'} | ${'datasource-artifactory'}
${'AwsMachineImageDatasource'} | ${'datasource-aws-machine-image'}
${'AwsRdsDatasource'} | ${'datasource-aws-rds'}
${'AzureBicepResourceDatasource'} | ${'datasource-azure-bicep-resource'}
${'AzurePipelinesTasksDatasource'} | ${'datasource-azure-pipelines-tasks'}
${'BazelDatasource'} | ${'datasource-bazel'}
${'BitbucketTagsDatasource'} | ${'datasource-bitbucket-tags'}
${'BitriseDatasource'} | ${'datasource-bitrise'}
${'CdnjsDatasource'} | ${'datasource-cdnjs'}
${'ConanDatasource'} | ${'datasource-conan'}
${'CondaDatasource'} | ${'datasource-conda'}
${'CpanDatasource'} | ${'datasource-cpan'}
${'CrateDatasource'} | ${'datasource-crate'}
${'DenoDatasource'} | ${'datasource-deno'}
${'DockerDatasource'} | ${'datasource-docker'}
${'DotnetVersionDatasource'} | ${'datasource-dotnet-version'}
${'EndoflifeDateDatasource'} | ${'datasource-endoflife-date'}
${'GalaxyDatasource'} | ${'datasource-galaxy'}
${'GalaxyCollectionDatasource'} | ${'datasource-galaxy-collection'}
${'GitRefsDatasource'} | ${'datasource-git-refs'}
${'GitTagsDatasource'} | ${'datasource-git-tags'}
${'GiteaReleasesDatasource'} | ${'datasource-gitea-releases'}
${'GiteaTagsDatasource'} | ${'datasource-gitea-tags'}
${'GithubReleaseAttachmentsDatasource'} | ${'datasource-github-release-attachments'}
${'GitlabPackagesDatasource'} | ${'datasource-gitlab-packages'}
${'GitlabReleasesDatasource'} | ${'datasource-gitlab-releases'}
${'GitlabTagsDatasource'} | ${'datasource-gitlab-tags'}
${'GlasskubePackagesDatasource'} | ${'datasource-glasskube-packages'}
${'GoDatasource'} | ${'datasource-go'}
${'GoDirectDatasource'} | ${'datasource-go-direct'}
${'GoProxyDatasource'} | ${'datasource-go-proxy'}
${'GolangVersionDatasource'} | ${'datasource-golang-version'}
${'GradleVersionDatasource'} | ${'datasource-gradle-version'}
${'HelmDatasource'} | ${'datasource-helm'}
${'HermitDatasource'} | ${'datasource-hermit'}
${'HexDatasource'} | ${'datasource-hex'}
${'HexpmBobDatasource'} | ${'datasource-hexpm-bob'}
${'JavaVersionDatasource'} | ${'datasource-java-version'}
${'JenkinsPluginsDatasource'} | ${'datasource-jenkins-plugins'}
${'NodeVersionDatasource'} | ${'datasource-node-version'}
${'NugetV3Api'} | ${'nuget-v3-api'}
${'OrbDatasource'} | ${'datasource-orb'}
${'PackagistDatasource'} | ${'datasource-packagist'}
${'PodDatasource'} | ${'datasource-pod'}
${'PythonVersionDatasource'} | ${'datasource-python-version'}
${'RepologyDatasource'} | ${'datasource-repology'}
${'RubyVersionDatasource'} | ${'datasource-ruby-version'}
${'RubygemsDatasource'} | ${'datasource-rubygems'}
${'TerraformDatasource'} | ${'datasource-terraform'}
${'TerraformModuleDatasource'} | ${'datasource-terraform-module'}
${'TerraformProviderDatasource'} | ${'datasource-terraform-provider'}
${'TerraformProviderHash'} | ${'terraform-provider-hash'}
${'Unity3dDatasource'} | ${'datasource-unity3d'}
${'GitDatasource'} | ${'datasource-git'}
`('$className -> $namespace', ({ className, namespace }) => {
expect(classNameToCacheNamespace(className)).toBe(namespace);
});
});
});
43 changes: 43 additions & 0 deletions lib/util/cache/package/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import type { CombinedKey, PackageCacheNamespace } from './types';

export function splitIdentifier(identifier: string): string[] {
const result: string[] = [];

let isPrevUpper = false;

let wordStart = 0;
for (let i = 0; i < identifier.length; i += 1) {
const char = identifier[i];
const charLower = char.toLocaleLowerCase();
const isUpper = char !== charLower;

if (isUpper === false && isPrevUpper && i - wordStart > 1) {
const prevIdx = i - 1;
result.push(identifier.slice(wordStart, prevIdx));
wordStart = prevIdx;
}

if (isUpper && !isPrevUpper && wordStart !== i) {
result.push(identifier.slice(wordStart, i));
wordStart = i;
}

isPrevUpper = isUpper;
}

if (wordStart < identifier.length) {
result.push(identifier.slice(wordStart));
}

return result;
}

export function classNameToCacheNamespace(className: string): string {
const parts = splitIdentifier(className).map((id) => id.toLocaleLowerCase());
const lastPart = parts.pop();

if (lastPart === 'datasource') {
return [lastPart, ...parts].join('-');
}

return [...parts, lastPart].join('-');
}

/**
* Returns the key used by underlying storage implementations
*/
Expand Down