Files
renovate/lib/modules/datasource/docker/dockerhub-cache.ts
T
Sebastian Poxhofer e0cec3dc95 refactor(lint): add types-location rule and enable it for lib/modules (#45497)
feat(lint): add `types-location` rule and enable it for `lib/modules`

Reviewers consistently ask for exported types to be moved into a
colocated `types.ts`. Add a `renovate/types-location` oxlint rule that
reports `export interface` / `export type` declarations outside such a
file, and turn it on across `lib/modules`.

Re-exports (`export type { X }`, `export type *`), file-local types, and
values (enums, classes, functions) are left alone. `schema.ts` files and
`schema/` directories are excluded via config, matching how
`zod-schema-location` is configured — inferred types belong next to the
schema they are derived from.

Relocating the 87 existing violations means:

- new `types.ts` files for the modules that had none
- `terragrunt/common.ts` folded into its `types.ts`, as it held nothing
  else
- `RangeBound` in `versioning/gradle/compare.ts` un-exported, since only
  a file-local interface referenced it
- `bazel-module/parser/fragments.ts` split from its inferred types, which
  now live in `parser/types.ts` and infer through a namespace import so
  each schema keeps its merged name
2026-08-27 19:52:09 +00:00

125 lines
3.1 KiB
TypeScript

import { dequal } from 'dequal';
import { DateTime } from 'luxon';
import * as packageCache from '../../../util/cache/package/index.ts';
import type { DockerHubTag } from './schema.ts';
import type { DockerHubCacheData } from './types.ts';
const cacheNamespace = 'datasource-docker-hub-cache';
export class DockerHubCache {
private isChanged = false;
private reconciledIds = new Set<number>();
private dockerRepository: string;
private cache: DockerHubCacheData;
private constructor(dockerRepository: string, cache: DockerHubCacheData) {
this.dockerRepository = dockerRepository;
this.cache = cache;
}
static async init(dockerRepository: string): Promise<DockerHubCache> {
let repoCache = await packageCache.get<DockerHubCacheData>(
cacheNamespace,
dockerRepository,
);
repoCache ??= {
items: {},
updatedAt: null,
};
return new DockerHubCache(dockerRepository, repoCache);
}
reconcile(items: DockerHubTag[], expectedCount: number): boolean {
let needNextPage = true;
let earliestDate = null;
let { updatedAt } = this.cache;
let latestDate = updatedAt ? DateTime.fromISO(updatedAt) : null;
for (const newItem of items) {
const id = newItem.id;
this.reconciledIds.add(id);
const oldItem = this.cache.items[id];
const itemDate = DateTime.fromISO(newItem.last_updated);
if (!earliestDate || earliestDate > itemDate) {
earliestDate = itemDate;
}
if (!latestDate || latestDate < itemDate) {
latestDate = itemDate;
updatedAt = newItem.last_updated;
}
if (dequal(oldItem, newItem)) {
needNextPage = false;
continue;
}
this.cache.items[newItem.id] = newItem;
this.isChanged = true;
}
this.cache.updatedAt = updatedAt;
if (earliestDate && latestDate) {
for (const [key, item] of Object.entries(this.cache.items)) {
const id = parseInt(key, 10);
const itemDate = DateTime.fromISO(item.last_updated);
if (
itemDate < earliestDate ||
itemDate > latestDate ||
this.reconciledIds.has(id)
) {
continue;
}
delete this.cache.items[id];
this.isChanged = true;
}
if (Object.keys(this.cache.items).length > expectedCount) {
return true;
}
}
return needNextPage;
}
async save(): Promise<void> {
if (this.isChanged) {
await packageCache.set(
cacheNamespace,
this.dockerRepository,
this.cache,
3 * 60 * 24 * 30,
);
}
}
getItems(): DockerHubTag[] {
return Object.values(this.cache.items);
}
getDigestForTag(tagName: string): string | null {
return (
this.getItems().find((item) => item.name === tagName)?.digest ?? null
);
}
getArchDigestForTag(tagName: string, architecture: string): string | null {
const tag = this.getItems().find((item) => item.name === tagName);
return (
tag?.images.find((img) => img.architecture === architecture)?.digest ??
null
);
}
}