-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4376 from FlowFuse/4362-bom-api
Software bill of materials API
- Loading branch information
Showing
28 changed files
with
1,647 additions
and
55 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
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,72 @@ | ||
let app | ||
|
||
module.exports = { | ||
init: (appInstance) => { | ||
app = appInstance | ||
app.addSchema({ | ||
$id: 'dependency', | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
version: { | ||
type: 'object', | ||
properties: { | ||
semver: { type: 'string' }, | ||
installed: { type: 'string', nullable: true } | ||
} | ||
} | ||
} | ||
}) | ||
app.addSchema({ | ||
$id: 'dependant', | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
name: { type: 'string' }, | ||
type: { | ||
type: 'string', | ||
enum: ['instance', 'device'] | ||
}, | ||
ownerType: { | ||
type: 'string', | ||
enum: ['instance', 'application'], | ||
nullable: true | ||
}, | ||
ownerId: { type: 'string', nullable: true }, | ||
dependencies: { type: 'array', items: { $ref: 'dependency' } } | ||
} | ||
}) | ||
}, | ||
|
||
dependency (name, semverVersion, installedVersion = null) { | ||
const result = { | ||
name, | ||
version: { | ||
semver: semverVersion, | ||
installed: installedVersion || null | ||
} | ||
} | ||
return result | ||
}, | ||
|
||
dependant (model, dependencies) { | ||
const type = model instanceof app.db.models.Project ? 'instance' : model instanceof app.db.models.Device ? 'device' : null | ||
if (type !== null) { | ||
const dependenciesArray = Object.entries(dependencies || {}).map(([name, version]) => app.db.views.BOM.dependency(name, version?.semver, version?.installed)) | ||
if (type === 'device') { | ||
const { hashid, name, ownerType } = model | ||
let ownerId = null | ||
if (ownerType === 'instance') { | ||
ownerId = model.ProjectId | ||
} else if (ownerType === 'application') { | ||
ownerId = model.Application ? model.Application.id : app.db.models.Application.encodeHashid(model.ApplicationId) | ||
} | ||
return { id: hashid, name, type, ownerType, ownerId, dependencies: dependenciesArray } | ||
} else if (type === 'instance') { | ||
const { id, name } = model | ||
return { id, name, type, dependencies: dependenciesArray } | ||
} | ||
} | ||
return null | ||
} | ||
} |
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
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,47 @@ | ||
const applicationShared = require('../../../routes/api/shared/application.js') | ||
|
||
module.exports = async function (app) { | ||
app.addHook('preHandler', applicationShared.defaultPreHandler.bind(null, app)) | ||
|
||
/** | ||
* Get the application BOM | ||
* @name /api/v1/application/:applicationId/bom | ||
* @memberof forge.routes.api.application | ||
*/ | ||
app.get('/:applicationId/bom', { | ||
preHandler: app.needsPermission('application:bom'), | ||
schema: { | ||
summary: 'Get application BOM', | ||
tags: ['Applications'], | ||
params: { | ||
type: 'object', | ||
properties: { | ||
applicationId: { type: 'string' } | ||
} | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
$ref: 'ApplicationBom' | ||
}, | ||
'4xx': { | ||
$ref: 'APIError' | ||
} | ||
} | ||
} | ||
}, async (request, reply) => { | ||
const teamType = await request.application.Team.getTeamType() | ||
if (!teamType.getFeatureProperty('bom', false)) { | ||
return reply.code(404).send({ code: 'unexpected_error', error: 'Feature not enabled.' }) | ||
} | ||
|
||
const dependants = await request.application.getChildren({ includeDependencies: true }) | ||
const childrenView = dependants.map(child => app.db.views.BOM.dependant(child.model, child.dependencies)) | ||
const result = { | ||
id: request.application.hashid, | ||
name: request.application.name, | ||
children: childrenView | ||
} | ||
reply.send(result) | ||
}) | ||
} |
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
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.