Skip to content

Commit

Permalink
feat(crate): Support releaseTimestamp (#31467)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
zharinov and rarkins authored Oct 11, 2024
1 parent b1e42a0 commit 748b258
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
42 changes: 42 additions & 0 deletions lib/modules/datasource/crate/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,46 @@ describe('modules/datasource/crate/index', () => {
).toReject();
});
});

describe('postprocessRelease', () => {
const datasource = new CrateDatasource();

it('no-op for registries other than crates.io', async () => {
const releaseOrig = { version: '4.5.17' };

const res = await datasource.postprocessRelease(
{
packageName: 'clap',
registryUrl: 'https://example.com',
},
releaseOrig,
);

expect(res).toBe(releaseOrig);
});

it('fetches releaseTimestamp', async () => {
httpMock
.scope(API_BASE_URL)
.get('/crates/clap/4.5.17')
.reply(200, {
version: {
created_at: '2024-09-04T19:16:41.355243+00:00',
},
});

const res = await datasource.postprocessRelease(
{
packageName: 'clap',
registryUrl: 'https://crates.io',
},
{ version: '4.5.17' },
);

expect(res).toEqual({
version: '4.5.17',
releaseTimestamp: '2024-09-04T19:16:41.355243+00:00',
});
});
});
});
36 changes: 35 additions & 1 deletion lib/modules/datasource/crate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import { newlineRegex, regEx } from '../../../util/regex';
import { joinUrlParts, parseUrl } from '../../../util/url';
import * as cargoVersioning from '../../versioning/cargo';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import type {
GetReleasesConfig,
PostprocessReleaseConfig,
PostprocessReleaseResult,
Release,
ReleaseResult,
} from '../types';
import { ReleaseTimestampSchema } from './schema';
import type {
CrateMetadata,
CrateRecord,
Expand Down Expand Up @@ -371,4 +378,31 @@ export class CrateDatasource extends Datasource {

return [packageName.slice(0, 2), packageName.slice(2, 4), packageName];
}

@cache({
namespace: `datasource-crate`,
key: (
{ registryUrl, packageName }: PostprocessReleaseConfig,
{ version }: Release,
) => `postprocessRelease:${registryUrl}:${packageName}:${version}`,
ttlMinutes: 7 * 24 * 60,
cacheable: ({ registryUrl }: PostprocessReleaseConfig, _: Release) =>
registryUrl === 'https://crates.io',
})
override async postprocessRelease(
{ packageName, registryUrl }: PostprocessReleaseConfig,
release: Release,
): Promise<PostprocessReleaseResult> {
if (registryUrl !== 'https://crates.io') {
return release;
}

const url = `https://crates.io/api/v1/crates/${packageName}/${release.version}`;
const { body: releaseTimestamp } = await this.http.getJson(
url,
ReleaseTimestampSchema,
);
release.releaseTimestamp = releaseTimestamp;
return release;
}
}
11 changes: 11 additions & 0 deletions lib/modules/datasource/crate/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from 'zod';

export const ReleaseTimestampSchema = z
.object({
version: z.object({
created_at: z.string(),
}),
})
.transform(({ version: { created_at } }) => created_at)
.nullable()
.catch(null);

0 comments on commit 748b258

Please sign in to comment.