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

feat(datasource): add Hackage datasource #32944

Merged
merged 6 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/modules/datasource/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { GlasskubePackagesDatasource } from './glasskube-packages';
import { GoDatasource } from './go';
import { GolangVersionDatasource } from './golang-version';
import { GradleVersionDatasource } from './gradle-version';
import { HackageDatasource } from './hackage';
import { HelmDatasource } from './helm';
import { HermitDatasource } from './hermit';
import { HexDatasource } from './hex';
Expand Down Expand Up @@ -111,6 +112,7 @@ api.set(GlasskubePackagesDatasource.id, new GlasskubePackagesDatasource());
api.set(GoDatasource.id, new GoDatasource());
api.set(GolangVersionDatasource.id, new GolangVersionDatasource());
api.set(GradleVersionDatasource.id, new GradleVersionDatasource());
api.set(HackageDatasource.id, new HackageDatasource());
api.set(HelmDatasource.id, new HelmDatasource());
api.set(HermitDatasource.id, new HermitDatasource());
api.set(HexDatasource.id, new HexDatasource());
Expand Down
59 changes: 59 additions & 0 deletions lib/modules/datasource/hackage/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getPkgReleases } from '..';
import * as httpMock from '../../../../test/http-mock';
import { HackageDatasource, versionToRelease } from './index';

const baseUrl = 'https://hackage.haskell.org/';

describe('modules/datasource/hackage/index', () => {
describe('versionToRelease', () => {
it('should make release with given version', () => {
expect(
versionToRelease('3.1.0', 'base', 'http://localhost').version,
).toBe('3.1.0');
});
});

describe('getReleases', () => {
it('return null with empty registryUrl', async () => {
expect(
await new HackageDatasource().getReleases({
packageName: 'base',
registryUrl: undefined,
}),
).toBeNull();
});

it('returns null for 404', async () => {
httpMock.scope(baseUrl).get('/package/base.json').reply(404);
expect(
await getPkgReleases({
datasource: HackageDatasource.id,
packageName: 'base',
}),
).toBeNull();
});

it('returns release for 200', async () => {
httpMock
.scope(baseUrl)
.get('/package/base.json')
.reply(200, { '4.20.0.1': 'normal' });
expect(
await getPkgReleases({
datasource: HackageDatasource.id,
packageName: 'base',
}),
).toEqual({
registryUrl: baseUrl,
releases: [
{
changelogUrl: baseUrl + 'package/base-4.20.0.1/changelog',
isStable: true,
sourceUrl: baseUrl + 'package/base-4.20.0.1/src',
version: '4.20.0.1',
},
],
});
});
});
});
61 changes: 61 additions & 0 deletions lib/modules/datasource/hackage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import is from '@sindresorhus/is';
import { joinUrlParts } from '../../../util/url';
import * as pvpVersioning from '../../versioning/pvp';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { HackagePackageMetadata } from './schema';

export class HackageDatasource extends Datasource {
static readonly id = 'hackage';

constructor() {
super(HackageDatasource.id);
}

override readonly defaultVersioning = pvpVersioning.id;
override readonly customRegistrySupport = false;
override readonly defaultRegistryUrls = ['https://hackage.haskell.org/'];

async getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
if (!is.nonEmptyString(config.registryUrl)) {
return null;
}
const massagedPackageName = encodeURIComponent(config.packageName);
const url = joinUrlParts(
config.registryUrl,
'package',
`${massagedPackageName}.json`,
);
const res = await this.http.getJson(url, HackagePackageMetadata);
const keys = Object.keys(res.body);
return {
releases: keys.map((version) =>
versionToRelease(version, config.packageName, config.registryUrl!),
rarkins marked this conversation as resolved.
Show resolved Hide resolved
),
};
}
}

export function versionToRelease(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these functions should usually not public. they should be in a utils or common files if separate tests are needed

version: string,
packageName: string,
registryUrl: string,
): Release {
return {
version,
releaseTimestamp: null,
isStable: true,
ysangkok marked this conversation as resolved.
Show resolved Hide resolved
changelogUrl: joinUrlParts(
registryUrl,
'package',
`${packageName}-${version}`,
'changelog',
),
sourceUrl: joinUrlParts(
registryUrl,
'package',
`${packageName}-${version}`,
'src',
),
rarkins marked this conversation as resolved.
Show resolved Hide resolved
};
}
7 changes: 7 additions & 0 deletions lib/modules/datasource/hackage/readme.md
secustor marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This datasource uses
[the Hackage JSON API](https://hackage.haskell.org/api#package-info-json)
to fetch versions for published Haskell packages.

While not all versions use [PVP](https://pvp.haskell.org), the majority does.
This manager assumes a default versioning set to PVP.
Versioning can be overwritten using `packageRules`, e.g. with `matchDatasources`.
3 changes: 3 additions & 0 deletions lib/modules/datasource/hackage/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from 'zod';

export const HackagePackageMetadata = z.record(z.string());
Loading