-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
+123
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff0c808
feat(datasource): add Hackage datasource
ysangkok 3bd6332
chore(datasource): Address Hackage PR comments
ysangkok 663600c
chore(datasource): Remove unnecessary exclamation mark
ysangkok cb241f6
chore(datasource): Extract object props
ysangkok 2359e97
chore(datasource): Remove defaults
ysangkok 4603531
chore(datasource): Remove sourceUrl
ysangkok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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', | ||
version: '4.20.0.1', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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> { | ||
const { registryUrl, packageName } = config; | ||
if (!is.nonEmptyString(registryUrl)) { | ||
return null; | ||
} | ||
const massagedPackageName = encodeURIComponent(packageName); | ||
const url = joinUrlParts( | ||
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, packageName, registryUrl), | ||
), | ||
}; | ||
} | ||
} | ||
|
||
export function versionToRelease( | ||
version: string, | ||
packageName: string, | ||
registryUrl: string, | ||
): Release { | ||
return { | ||
version, | ||
changelogUrl: joinUrlParts( | ||
registryUrl, | ||
'package', | ||
`${packageName}-${version}`, | ||
'changelog', | ||
), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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