2022-03-03 02:35:26 -07:00
|
|
|
import { logger } from '../../../logger';
|
|
|
|
import { cache } from '../../../util/cache/package/decorator';
|
2024-06-08 01:12:18 -06:00
|
|
|
import { joinUrlParts } from '../../../util/url';
|
2021-01-21 05:11:35 -07:00
|
|
|
import * as hexVersioning from '../../versioning/hex';
|
2021-06-29 00:03:47 -06:00
|
|
|
import { Datasource } from '../datasource';
|
2021-03-02 13:44:55 -07:00
|
|
|
import type { GetReleasesConfig, ReleaseResult } from '../types';
|
2023-11-10 00:55:37 -07:00
|
|
|
import { HexRelease } from './schema';
|
2020-03-01 00:01:12 -07:00
|
|
|
|
2021-06-29 00:03:47 -06:00
|
|
|
export class HexDatasource extends Datasource {
|
|
|
|
static readonly id = 'hex';
|
2019-02-08 06:13:36 -07:00
|
|
|
|
2021-06-29 00:03:47 -06:00
|
|
|
constructor() {
|
|
|
|
super(HexDatasource.id);
|
|
|
|
}
|
|
|
|
|
2024-06-08 01:12:18 -06:00
|
|
|
override readonly defaultRegistryUrls = ['https://hex.pm'];
|
2021-06-29 00:03:47 -06:00
|
|
|
|
|
|
|
override readonly defaultVersioning = hexVersioning.id;
|
2020-04-03 05:45:55 -06:00
|
|
|
|
2024-05-21 01:04:12 -06:00
|
|
|
override readonly releaseTimestampSupport = true;
|
|
|
|
override readonly releaseTimestampNote =
|
|
|
|
'The release timestamp is determined the `inserted_at` field in the results.';
|
|
|
|
override readonly sourceUrlSupport = 'package';
|
|
|
|
override readonly sourceUrlNote =
|
|
|
|
'The source URL is determined from the `Github` field in the results.';
|
|
|
|
|
2021-06-29 00:03:47 -06:00
|
|
|
@cache({
|
|
|
|
namespace: `datasource-${HexDatasource.id}`,
|
2022-03-03 08:08:43 -07:00
|
|
|
key: ({ packageName }: GetReleasesConfig) => packageName,
|
2021-06-29 00:03:47 -06:00
|
|
|
})
|
|
|
|
async getReleases({
|
2022-03-03 08:08:43 -07:00
|
|
|
packageName,
|
2021-06-29 00:03:47 -06:00
|
|
|
registryUrl,
|
|
|
|
}: GetReleasesConfig): Promise<ReleaseResult | null> {
|
2022-02-03 08:36:29 -07:00
|
|
|
// istanbul ignore if
|
|
|
|
if (!registryUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-03 08:08:43 -07:00
|
|
|
// Get dependency name from packageName.
|
|
|
|
// If the dependency is private packageName contains organization name as following:
|
2021-06-29 00:03:47 -06:00
|
|
|
// hexPackageName:organizationName
|
|
|
|
// hexPackageName is used to pass it in hex dep url
|
|
|
|
// organizationName is used for accessing to private deps
|
2022-03-03 08:08:43 -07:00
|
|
|
const [hexPackageName, organizationName] = packageName.split(':');
|
2021-08-12 03:34:27 -06:00
|
|
|
const organizationUrlPrefix = organizationName
|
|
|
|
? `repos/${organizationName}/`
|
|
|
|
: '';
|
2024-06-08 01:12:18 -06:00
|
|
|
|
|
|
|
const hexUrl = joinUrlParts(
|
|
|
|
registryUrl,
|
|
|
|
`/api/${organizationUrlPrefix}packages/${hexPackageName}`,
|
|
|
|
);
|
2021-06-29 00:03:47 -06:00
|
|
|
|
2023-11-10 00:55:37 -07:00
|
|
|
const { val: result, err } = await this.http
|
|
|
|
.getJsonSafe(hexUrl, HexRelease)
|
|
|
|
.onError((err) => {
|
|
|
|
logger.warn({ datasource: 'hex', packageName, err }, `Error fetching ${hexUrl}`); // prettier-ignore
|
|
|
|
})
|
|
|
|
.unwrap();
|
2019-10-04 01:13:14 -06:00
|
|
|
|
2023-11-10 00:55:37 -07:00
|
|
|
if (err) {
|
|
|
|
this.handleGenericErrors(err);
|
2019-02-08 06:13:36 -07:00
|
|
|
}
|
2019-10-04 01:13:14 -06:00
|
|
|
|
2019-02-08 06:13:36 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|