-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datasource): Add datasource for buildpack registry (#32721)
Co-authored-by: Nicolas Bender <nicolas.bender@sap.com> Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Co-authored-by: Pavel Busko <pavel.busko@sap.com> Co-authored-by: Johannes Dillmann <j.dillmann@sap.com> Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
- Loading branch information
1 parent
c3814ab
commit d581af5
Showing
11 changed files
with
321 additions
and
14 deletions.
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,66 @@ | ||
import { getPkgReleases } from '..'; | ||
import * as httpMock from '../../../../test/http-mock'; | ||
import { BuildpacksRegistryDatasource } from '.'; | ||
|
||
const baseUrl = 'https://registry.buildpacks.io/api/v1/buildpacks/'; | ||
|
||
describe('modules/datasource/buildpacks-registry/index', () => { | ||
describe('getReleases', () => { | ||
it('processes real data', async () => { | ||
httpMock | ||
.scope(baseUrl) | ||
.get('/heroku/python') | ||
.reply(200, { | ||
latest: { | ||
version: '0.17.1', | ||
namespace: 'heroku', | ||
name: 'python', | ||
description: "Heroku's buildpack for Python applications.", | ||
homepage: 'https://github.com/heroku/buildpacks-python', | ||
licenses: ['BSD-3-Clause'], | ||
stacks: ['*'], | ||
id: '75946bf8-3f6a-4af0-a757-614bebfdfcd6', | ||
}, | ||
versions: [ | ||
{ | ||
version: '0.17.1', | ||
_link: | ||
'https://registry.buildpacks.io//api/v1/buildpacks/heroku/python/0.17.1', | ||
}, | ||
{ | ||
version: '0.17.0', | ||
_link: | ||
'https://registry.buildpacks.io//api/v1/buildpacks/heroku/python/0.17.0', | ||
}, | ||
], | ||
}); | ||
const res = await getPkgReleases({ | ||
datasource: BuildpacksRegistryDatasource.id, | ||
packageName: 'heroku/python', | ||
}); | ||
expect(res).toEqual({ | ||
registryUrl: 'https://registry.buildpacks.io', | ||
releases: [{ version: '0.17.0' }, { version: '0.17.1' }], | ||
sourceUrl: 'https://github.com/heroku/buildpacks-python', | ||
}); | ||
}); | ||
|
||
it('returns null on empty result', async () => { | ||
httpMock.scope(baseUrl).get('/heroku/empty').reply(200, {}); | ||
const res = await getPkgReleases({ | ||
datasource: BuildpacksRegistryDatasource.id, | ||
packageName: 'heroku/empty', | ||
}); | ||
expect(res).toBeNull(); | ||
}); | ||
|
||
it('handles not found', async () => { | ||
httpMock.scope(baseUrl).get('/heroku/notexisting').reply(404); | ||
const res = await getPkgReleases({ | ||
datasource: BuildpacksRegistryDatasource.id, | ||
packageName: 'heroku/notexisting', | ||
}); | ||
expect(res).toBeNull(); | ||
}); | ||
}); | ||
}); |
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,72 @@ | ||
import urlJoin from 'url-join'; | ||
import { ZodError } from 'zod'; | ||
import { logger } from '../../../logger'; | ||
import { cache } from '../../../util/cache/package/decorator'; | ||
import { Result } from '../../../util/result'; | ||
import { Datasource } from '../datasource'; | ||
import { ReleasesConfig } from '../schema'; | ||
import type { GetReleasesConfig, Release, ReleaseResult } from '../types'; | ||
import { BuildpacksRegistryResponseSchema } from './schema'; | ||
|
||
export class BuildpacksRegistryDatasource extends Datasource { | ||
static readonly id = 'buildpacks-registry'; | ||
|
||
constructor() { | ||
super(BuildpacksRegistryDatasource.id); | ||
} | ||
|
||
override readonly customRegistrySupport = false; | ||
|
||
override readonly defaultRegistryUrls = ['https://registry.buildpacks.io']; | ||
|
||
override readonly releaseTimestampSupport = true; | ||
override readonly releaseTimestampNote = | ||
'The release timestamp is determined from the `published_at` field in the results.'; | ||
override readonly sourceUrlSupport = 'release'; | ||
override readonly sourceUrlNote = | ||
'The source URL is determined from the `source_code_url` field of the release object in the results.'; | ||
|
||
@cache({ | ||
namespace: `datasource-${BuildpacksRegistryDatasource.id}`, | ||
key: ({ registryUrl, packageName }: GetReleasesConfig) => | ||
`${registryUrl}:${packageName}`, | ||
}) | ||
async getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> { | ||
const result = Result.parse(config, ReleasesConfig) | ||
.transform(({ packageName, registryUrl }) => { | ||
const url = urlJoin( | ||
registryUrl, | ||
'api', | ||
'v1', | ||
'buildpacks', | ||
packageName, | ||
); | ||
|
||
return this.http.getJsonSafe(url, BuildpacksRegistryResponseSchema); | ||
}) | ||
.transform(({ versions, latest }): ReleaseResult => { | ||
const releases: Release[] = versions; | ||
|
||
const res: ReleaseResult = { releases }; | ||
|
||
if (latest?.homepage) { | ||
res.homepage = latest.homepage; | ||
} | ||
|
||
return res; | ||
}); | ||
|
||
const { val, err } = await result.unwrap(); | ||
|
||
if (err instanceof ZodError) { | ||
logger.debug({ err }, 'buildpacks: validation error'); | ||
return null; | ||
} | ||
|
||
if (err) { | ||
this.handleGenericErrors(err); | ||
} | ||
|
||
return val; | ||
} | ||
} |
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,43 @@ | ||
import { BuildpacksRegistryResponseSchema } from './schema'; | ||
|
||
describe('modules/datasource/buildpacks-registry/schema', () => { | ||
it('parses buildpack-registry schema', () => { | ||
const response = { | ||
latest: { | ||
version: '0.17.1', | ||
namespace: 'heroku', | ||
name: 'python', | ||
description: "Heroku's buildpack for Python applications.", | ||
homepage: 'https://github.com/heroku/buildpacks-python', | ||
licenses: ['BSD-3-Clause'], | ||
stacks: ['*'], | ||
id: '75946bf8-3f6a-4af0-a757-614bebfdfcd6', | ||
}, | ||
versions: [ | ||
{ | ||
version: '0.2.0', | ||
_link: | ||
'https://registry.buildpacks.io//api/v1/buildpacks/heroku/python/0.2.0', | ||
}, | ||
{ | ||
version: '0.1.0', | ||
_link: | ||
'https://registry.buildpacks.io//api/v1/buildpacks/heroku/python/0.1.0', | ||
}, | ||
], | ||
}; | ||
expect(BuildpacksRegistryResponseSchema.parse(response)).toMatchObject({ | ||
latest: { | ||
homepage: 'https://github.com/heroku/buildpacks-python', | ||
}, | ||
versions: [ | ||
{ | ||
version: '0.2.0', | ||
}, | ||
{ | ||
version: '0.1.0', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
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,17 @@ | ||
import { z } from 'zod'; | ||
|
||
/** | ||
* Response from registry.buildpacks.io | ||
*/ | ||
export const BuildpacksRegistryResponseSchema = z.object({ | ||
latest: z | ||
.object({ | ||
homepage: z.string().optional(), | ||
}) | ||
.optional(), | ||
versions: z | ||
.object({ | ||
version: z.string(), | ||
}) | ||
.array(), | ||
}); |
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
Oops, something went wrong.