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

Support optionalDependencies in [GithubPackageJson]; also test [npm] #6749

Merged
merged 2 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 14 additions & 9 deletions services/github/github-package-json.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GithubPackageJsonDependencyVersion extends ConditionalGithubAuthV3Service
static route = {
base: 'github/package-json/dependency-version',
pattern:
':user/:repo/:kind(dev|peer)?/:scope(@[^/]+)?/:packageName/:branch*',
':user/:repo/:kind(dev|peer|optional)?/:scope(@[^/]+)?/:packageName/:branch*',
queryParamSchema: dependencyQueryParamSchema,
}

Expand Down Expand Up @@ -146,14 +146,18 @@ class GithubPackageJsonDependencyVersion extends ConditionalGithubAuthV3Service
{ user, repo, kind, branch = 'HEAD', scope, packageName },
{ filename = 'package.json' }
) {
const { dependencies, devDependencies, peerDependencies } =
await fetchJsonFromRepo(this, {
schema: isPackageJsonWithDependencies,
user,
repo,
branch,
filename,
})
const {
dependencies,
devDependencies,
peerDependencies,
optionalDependencies,
} = await fetchJsonFromRepo(this, {
schema: isPackageJsonWithDependencies,
user,
repo,
branch,
filename,
})

const wantedDependency = scope ? `${scope}/${packageName}` : packageName
const { range } = getDependencyVersion({
Expand All @@ -162,6 +166,7 @@ class GithubPackageJsonDependencyVersion extends ConditionalGithubAuthV3Service
dependencies,
devDependencies,
peerDependencies,
optionalDependencies,
})

return this.constructor.render({
Expand Down
7 changes: 7 additions & 0 deletions services/github/github-package-json.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ t.create('Peer dependency version')
message: semverRange,
})

t.create('Optional dependency version')
.get('/dependency-version/IcedFrisby/IcedFrisby/optional/@hapi/joi.json')
.expectBadge({
label: '@hapi/joi',
message: semverRange,
})

t.create('Dev dependency version')
.get(
'/dependency-version/paulmelnikow/react-boxplot/dev/react.json?label=react%20tested'
Expand Down
21 changes: 11 additions & 10 deletions services/package-json-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const isPackageJsonWithDependencies = Joi.object({
dependencies: isDependencyMap,
devDependencies: isDependencyMap,
peerDependencies: isDependencyMap,
optionalDependencies: isDependencyMap,
}).required()

function getDependencyVersion({
Expand All @@ -21,19 +22,19 @@ function getDependencyVersion({
dependencies,
devDependencies,
peerDependencies,
optionalDependencies,
}) {
let dependenciesOfKind
if (kind === 'peer') {
dependenciesOfKind = peerDependencies
} else if (kind === 'dev') {
dependenciesOfKind = devDependencies
} else if (kind === 'prod') {
dependenciesOfKind = dependencies
} else {
throw Error(`Not very kind: ${kind}`)
const dependencyMaps = {
peer: peerDependencies,
optional: optionalDependencies,
dev: devDependencies,
prod: dependencies,
}

const range = dependenciesOfKind[wantedDependency]
if (!(kind in dependencyMaps)) {
throw Error(`Not very kind: ${kind}`)
}
const range = dependencyMaps[kind][wantedDependency]
if (range === undefined) {
throw new InvalidParameter({
prettyMessage: `${kind} dependency not found`,
Expand Down