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

refactor(rubygems): Extract v1 API handling #23474

Merged
merged 18 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/modules/datasource/rubygems/common.ts
@@ -0,0 +1,43 @@
import type { Http, SafeJsonError } from '../../../util/http';
import type { AsyncResult } from '../../../util/result';
import { joinUrlParts } from '../../../util/url';
import type { ReleaseResult } from '../types';
import { GemMetadata, GemVersions } from './schema';

export function getV1Releases(
http: Http,
registryUrl: string,
packageName: string
): AsyncResult<ReleaseResult, SafeJsonError> {
return http
.getJsonSafe(
joinUrlParts(registryUrl, '/api/v1/versions', `${packageName}.json`),
GemVersions
)
.transform(
(releases): Promise<ReleaseResult> =>
http
.getJsonSafe(
joinUrlParts(registryUrl, '/api/v1/gems', `${packageName}.json`),
GemMetadata
)
.transform(({ changelogUrl, sourceUrl, homepage }) => {
zharinov marked this conversation as resolved.
Show resolved Hide resolved
const result: ReleaseResult = { releases };

if (changelogUrl) {
result.changelogUrl = changelogUrl;
}

if (sourceUrl) {
result.sourceUrl = sourceUrl;
}

if (homepage) {
result.homepage = homepage;
}

return result;
})
.unwrap({ releases })
);
}
69 changes: 22 additions & 47 deletions lib/modules/datasource/rubygems/metadata-cache.ts
@@ -1,10 +1,9 @@
import hasha from 'hasha';
import { logger } from '../../../logger';
import * as packageCache from '../../../util/cache/package';
import type { Http } from '../../../util/http';
import { joinUrlParts, parseUrl } from '../../../util/url';
import { parseUrl } from '../../../util/url';
import type { ReleaseResult } from '../types';
import { GemMetadata, GemVersions } from './schema';
import { getV1Releases } from './common';

interface CacheRecord {
hash: string;
Expand All @@ -27,49 +26,25 @@ export class MetadataCache {
return oldCache.data;
}

try {
const { body: releases } = await this.http.getJson(
joinUrlParts(registryUrl, '/api/v1/versions', `${packageName}.json`),
GemVersions
);

const { body: metadata } = await this.http.getJson(
joinUrlParts(registryUrl, '/api/v1/gems', `${packageName}.json`),
GemMetadata
);

const data: ReleaseResult = { releases };

if (metadata.changelogUrl) {
data.changelogUrl = metadata.changelogUrl;
}

if (metadata.sourceUrl) {
data.sourceUrl = metadata.sourceUrl;
}

if (metadata.homepage) {
data.homepage = metadata.homepage;
}

const registryHostname = parseUrl(registryUrl)?.hostname;
if (registryHostname === 'rubygems.org') {
const newCache: CacheRecord = { hash, data };
const ttlMinutes = 100 * 24 * 60;
const ttlRandomDelta = Math.floor(Math.random() * 10 * 24 * 60);
await packageCache.set(
cacheNs,
cacheKey,
newCache,
ttlMinutes + ttlRandomDelta
);
}

return data;
} catch (err) {
logger.debug({ err }, 'Rubygems: failed to fetch metadata');
const releases = versions.map((version) => ({ version }));
return { releases };
}
return getV1Releases(this.http, registryUrl, packageName)
.transform(async (result) => {
const registryHostname = parseUrl(registryUrl)?.hostname;
if (registryHostname === 'rubygems.org') {
const newCache: CacheRecord = { hash, data: result };
const ttlMinutes = 100 * 24 * 60;
const ttlRandomDelta = Math.floor(Math.random() * 10 * 24 * 60);
await packageCache.set(
cacheNs,
cacheKey,
newCache,
ttlMinutes + ttlRandomDelta
);
}

return result;
})
.unwrap({
releases: versions.map((version) => ({ version })),
});
}
}