refactor(dart): migrate to class based datasource (#10134)

This commit is contained in:
Jamie Magee 2021-05-24 21:42:14 -07:00 committed by GitHub
parent 94f05138b8
commit 7b0934592f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 56 deletions

View file

@ -2,7 +2,7 @@ import * as bitbucketTags from './bitbucket-tags';
import { CdnJsDatasource } from './cdnjs'; import { CdnJsDatasource } from './cdnjs';
import { ClojureDatasource } from './clojure'; import { ClojureDatasource } from './clojure';
import * as crate from './crate'; import * as crate from './crate';
import * as dart from './dart'; import { DartDatasource } from './dart';
import * as docker from './docker'; import * as docker from './docker';
import * as galaxy from './galaxy'; import * as galaxy from './galaxy';
import * as galaxyCollection from './galaxy-collection'; import * as galaxyCollection from './galaxy-collection';
@ -39,7 +39,7 @@ api.set('bitbucket-tags', bitbucketTags);
api.set('cdnjs', new CdnJsDatasource()); api.set('cdnjs', new CdnJsDatasource());
api.set('clojure', new ClojureDatasource()); api.set('clojure', new ClojureDatasource());
api.set('crate', crate); api.set('crate', crate);
api.set('dart', dart); api.set('dart', new DartDatasource());
api.set('docker', docker); api.set('docker', docker);
api.set('galaxy', galaxy); api.set('galaxy', galaxy);
api.set('galaxy-collection', galaxyCollection); api.set('galaxy-collection', galaxyCollection);

View file

@ -1,7 +1,7 @@
import { getPkgReleases } from '..'; import { getPkgReleases } from '..';
import * as httpMock from '../../../test/http-mock'; import * as httpMock from '../../../test/http-mock';
import { getName, loadJsonFixture } from '../../../test/util'; import { getName, loadJsonFixture } from '../../../test/util';
import { id as datasource } from '.'; import { DartDatasource } from '.';
const body = loadJsonFixture('shared_preferences.json'); const body = loadJsonFixture('shared_preferences.json');
@ -20,7 +20,10 @@ describe(getName(), () => {
it('returns null for empty result', async () => { it('returns null for empty result', async () => {
httpMock.scope(baseUrl).get('/non_sense').reply(200, null); httpMock.scope(baseUrl).get('/non_sense').reply(200, null);
expect( expect(
await getPkgReleases({ datasource, depName: 'non_sense' }) await getPkgReleases({
datasource: DartDatasource.id,
depName: 'non_sense',
})
).toBeNull(); ).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot(); expect(httpMock.getTrace()).toMatchSnapshot();
}); });
@ -35,7 +38,7 @@ describe(getName(), () => {
.reply(200, withoutVersions); .reply(200, withoutVersions);
expect( expect(
await getPkgReleases({ await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}) })
).toBeNull(); ).toBeNull();
@ -50,7 +53,7 @@ describe(getName(), () => {
.reply(200, withoutLatest); .reply(200, withoutLatest);
expect( expect(
await getPkgReleases({ await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}) })
).toBeNull(); ).toBeNull();
@ -61,7 +64,7 @@ describe(getName(), () => {
httpMock.scope(baseUrl).get('/shared_preferences').reply(404); httpMock.scope(baseUrl).get('/shared_preferences').reply(404);
expect( expect(
await getPkgReleases({ await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}) })
).toBeNull(); ).toBeNull();
@ -72,7 +75,7 @@ describe(getName(), () => {
let e; let e;
try { try {
await getPkgReleases({ await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}); });
} catch (err) { } catch (err) {
@ -86,7 +89,7 @@ describe(getName(), () => {
httpMock.scope(baseUrl).get('/shared_preferences').replyWithError(''); httpMock.scope(baseUrl).get('/shared_preferences').replyWithError('');
expect( expect(
await getPkgReleases({ await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}) })
).toBeNull(); ).toBeNull();
@ -95,7 +98,7 @@ describe(getName(), () => {
it('processes real data', async () => { it('processes real data', async () => {
httpMock.scope(baseUrl).get('/shared_preferences').reply(200, body); httpMock.scope(baseUrl).get('/shared_preferences').reply(200, body);
const res = await getPkgReleases({ const res = await getPkgReleases({
datasource, datasource: DartDatasource.id,
depName: 'shared_preferences', depName: 'shared_preferences',
}); });
expect(res).toMatchSnapshot(); expect(res).toMatchSnapshot();

View file

@ -1,57 +1,56 @@
import { ExternalHostError } from '../../types/errors/external-host-error'; import { HttpResponse } from '../../util/http';
import { Http, HttpResponse } from '../../util/http'; import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types'; import type { GetReleasesConfig, ReleaseResult } from '../types';
import type { DartResult } from './types'; import type { DartResult } from './types';
export const id = 'dart'; export class DartDatasource extends Datasource {
export const defaultRegistryUrls = ['https://pub.dartlang.org/']; static readonly id = 'dart';
export const customRegistrySupport = false;
const http = new Http(id); constructor() {
super(DartDatasource.id);
export async function getReleases({
lookupName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
let result: ReleaseResult = null;
const pkgUrl = `${registryUrl}api/packages/${lookupName}`;
let raw: HttpResponse<DartResult> = null;
try {
raw = await http.getJson<DartResult>(pkgUrl);
} catch (err) {
if (
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
) {
throw new ExternalHostError(err);
}
throw err;
} }
const body = raw?.body; readonly customRegistrySupport = false;
if (body) {
const { versions, latest } = body;
if (versions && latest) {
result = {
releases: body.versions.map(({ version, published }) => ({
version,
releaseTimestamp: published,
})),
};
const pubspec = latest.pubspec; readonly defaultRegistryUrls = ['https://pub.dartlang.org/'];
if (pubspec) {
if (pubspec.homepage) {
result.homepage = pubspec.homepage;
}
if (pubspec.repository) { async getReleases({
result.sourceUrl = pubspec.repository; lookupName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
let result: ReleaseResult = null;
const pkgUrl = `${registryUrl}api/packages/${lookupName}`;
let raw: HttpResponse<DartResult> = null;
try {
raw = await this.http.getJson<DartResult>(pkgUrl);
} catch (err) {
this.handleGenericErrors(err);
}
const body = raw?.body;
if (body) {
const { versions, latest } = body;
if (versions && latest) {
result = {
releases: body.versions.map(({ version, published }) => ({
version,
releaseTimestamp: published,
})),
};
const pubspec = latest.pubspec;
if (pubspec) {
if (pubspec.homepage) {
result.homepage = pubspec.homepage;
}
if (pubspec.repository) {
result.sourceUrl = pubspec.repository;
}
} }
} }
} }
return result;
} }
return result;
} }

View file

@ -1,5 +1,5 @@
import { safeLoad } from 'js-yaml'; import { safeLoad } from 'js-yaml';
import * as datasourceDart from '../../datasource/dart'; import { DartDatasource } from '../../datasource/dart';
import { logger } from '../../logger'; import { logger } from '../../logger';
import type { PackageDependency, PackageFile } from '../types'; import type { PackageDependency, PackageFile } from '../types';
@ -54,7 +54,7 @@ export function extractPackageFile(
if (deps.length) { if (deps.length) {
return { return {
packageFile, packageFile,
datasource: datasourceDart.id, datasource: DartDatasource.id,
deps, deps,
}; };
} }