Skip to content
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
83 changes: 83 additions & 0 deletions lib/modules/datasource/bazel/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { getPkgReleases } from '..';
import { GlobalConfig } from '../../../config/global';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import { BazelDatasource } from '.';
import { Fixtures } from '~test/fixtures';
import * as httpMock from '~test/http-mock';
import { fs } from '~test/util';

vi.mock('../../../util/fs');

const datasource = BazelDatasource.id;
const defaultRegistryUrl = BazelDatasource.bazelCentralRepoUrl;
const packageName = 'rules_foo';
const path = BazelDatasource.packageMetadataPath(packageName);

const mockMetadata = {
versions: ['0.14.8', '0.14.9', '0.15.0', '0.16.0'],
yanked_versions: {
'0.15.0': 'yanked for security reasons',
},
homepage: 'https://github.com/foo/bar',
};

describe('modules/datasource/bazel/index', () => {
describe('getReleases', () => {
it('throws for error', async () => {
Expand Down Expand Up @@ -80,4 +92,75 @@ describe('modules/datasource/bazel/index', () => {
});
});
});

describe('local file handling', () => {
const mockFilePath = `/tmp/mock-registry/modules/${packageName}/metadata.json`;
const localRegistryUrl = `file:///tmp/mock-registry`;

beforeEach(() => {
GlobalConfig.set({
localDir: '/tmp/mock-registry',
});
});

it('should handle local file correctly', async () => {
vi.mocked(fs.readLocalFile).mockImplementation((file: string) => {
if (file === mockFilePath) {
return Promise.resolve(JSON.stringify(mockMetadata));
}
return Promise.resolve(null);
});

vi.mocked(fs.isValidLocalPath).mockImplementation((file: string) => {
return file === mockFilePath;
});

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: [localRegistryUrl],
});
expect(res).toEqual({
registryUrl: localRegistryUrl,
releases: [
{ version: '0.14.8' },
{ version: '0.14.9' },
{ version: '0.15.0', isDeprecated: true },
{ version: '0.16.0' },
],
sourceUrl: 'https://github.com/foo/bar',
});
});

it('should return null for invalid file path', async () => {
vi.mocked(fs.isValidLocalPath).mockImplementation(() => false);

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: [localRegistryUrl],
});
expect(res).toBeNull();
});

it('should return null for empty file content', async () => {
vi.mocked(fs.readLocalFile).mockImplementation((file: string) => {
if (file === mockFilePath) {
return Promise.resolve(null);
}
return Promise.resolve(null);
});

vi.mocked(fs.isValidLocalPath).mockImplementation((file: string) => {
return file === mockFilePath;
});

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: [localRegistryUrl],
});
expect(res).toBeNull();
});
});
});
23 changes: 18 additions & 5 deletions lib/modules/datasource/bazel/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import is from '@sindresorhus/is';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { cache } from '../../../util/cache/package/decorator';
import { isValidLocalPath, readLocalFile } from '../../../util/fs';
import { HttpError } from '../../../util/http';
import { joinUrlParts } from '../../../util/url';
import { id as bazelVersioningId } from '../../versioning/bazel-module';
Expand Down Expand Up @@ -40,13 +41,25 @@ export class BazelDatasource extends Datasource {
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const path = BazelDatasource.packageMetadataPath(packageName);
const url = joinUrlParts(registryUrl!, path);

const result: ReleaseResult = { releases: [] };
try {
const { body: metadata } = await this.http.getJson(
url,
BazelModuleMetadata,
);
let metadata: BazelModuleMetadata;
const FILE_PREFIX = 'file://';
if (url.startsWith(FILE_PREFIX)) {
const filePath = url.slice(FILE_PREFIX.length);
if (!isValidLocalPath(filePath)) {
return null;
}
const fileContent = await readLocalFile(filePath, 'utf8');
if (!fileContent) {
return null;
}
metadata = BazelModuleMetadata.parse(JSON.parse(fileContent));
} else {
const response = await this.http.getJson(url, BazelModuleMetadata);
metadata = response.body;
}

result.releases = metadata.versions
.map((v) => new BzlmodVersion(v))
.sort(BzlmodVersion.defaultCompare)
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/bazel/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The `bazel` datasource is designed to query one or more [Bazel registries](https://bazel.build/external/registry) using the first successful result.
The `bazel` datasource is designed to query one or more [Bazel registries](https://bazel.build/external/registry) or file based BCR using the first successful result.
2 changes: 2 additions & 0 deletions lib/modules/datasource/bazel/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const BazelModuleMetadata = z.object({
versions: z.array(z.string()),
yanked_versions: z.record(z.string(), z.string()).optional(),
});

export type BazelModuleMetadata = z.infer<typeof BazelModuleMetadata>;
Loading