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

Add getLatestOsVersions in hostapp model #1062

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ const sdk = fromSharedOptions();
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>
* [.config](#balena.models.config) : <code>object</code>
* [.getAll()](#balena.models.config.getAll) ⇒ <code>Promise</code>
* ~~[.getDeviceTypes()](#balena.models.config.getDeviceTypes) ⇒ <code>Promise</code>~~
Expand Down Expand Up @@ -671,6 +672,7 @@ balena.models.device.get(123).catch(function (error) {
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>
* [.config](#balena.models.config) : <code>object</code>
* [.getAll()](#balena.models.config.getAll) ⇒ <code>Promise</code>
* ~~[.getDeviceTypes()](#balena.models.config.getDeviceTypes) ⇒ <code>Promise</code>~~
Expand Down Expand Up @@ -6273,6 +6275,11 @@ console.log(result2);
**Beta** The hostapp methods are still in beta and might change in a non-major release.

**Kind**: static namespace of [<code>models</code>](#balena.models)

* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>

<a name="balena.models.hostapp.getAllOsVersions"></a>

##### hostapp.getAllOsVersions(deviceTypes) ⇒ <code>Promise</code>
Expand All @@ -6288,6 +6295,21 @@ console.log(result2);
```js
balena.models.hostapp.getAllOsVersions(['fincm3', 'raspberrypi3']);
```
<a name="balena.models.hostapp.getLatestOsVersions"></a>

##### hostapp.getLatestOsVersions(deviceTypes) ⇒ <code>Promise</code>
**Kind**: static method of [<code>hostapp</code>](#balena.models.hostapp)
**Summary**: Get latest OS versions for the passed device types
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| deviceTypes | <code>Array.&lt;String&gt;</code> | device type slugs |

**Example**
```js
balena.models.hostapp.getLatestOsVersions(['fincm3', 'raspberrypi3']);
```
<a name="balena.models.config"></a>

#### models.config : <code>object</code>
Expand Down
47 changes: 47 additions & 0 deletions lib/models/hostapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from '../types/models';
import { Dictionary } from '../../typings/utils';
import { getAuthDependentMemoize } from '../util/cache';
import { maxSatisfying } from 'semver';

const RELEASE_POLICY_TAG_NAME = 'release-policy';
const ESR_NEXT_TAG_NAME = 'esr-next';
Expand Down Expand Up @@ -161,6 +162,12 @@ const getHostappModel = function (deps: InjectedDependenciesParam) {
return res;
};

const mapVersions = (versions: OsVersion[], type: OsTypes) => {
return versions
.filter((version) => version.osType === type)
.map((v) => v.strippedVersion);
};

// This mutates the passed object.
const transformVersionSets = (
osVersionsByDeviceType: OsVersionsByDeviceType,
Expand Down Expand Up @@ -261,9 +268,49 @@ const getHostappModel = function (deps: InjectedDependenciesParam) {
return memoizedGetTransformedOsVersions(sortedDeviceTypes);
};

/**
* @summary Get latest OS versions for the passed device types
* @name getLatestOsVersions
* @public
* @function
* @memberof balena.models.hostapp
*
* @param {String[]} deviceTypes - device type slugs
* @returns {Promise}
*
* @example
* balena.models.hostapp.getLatestOsVersions(['fincm3', 'raspberrypi3']);
*/
const getLatestOsVersions = async (
deviceTypes: string[],
): Promise<OsVersionsByDeviceType> => {
const allOsVersions = await getAllOsVersions(deviceTypes);
return Object.entries(allOsVersions).reduce(
(osVersionsByDeviceType: OsVersionsByDeviceType, [key, versions]) => {
const latestOSVersion = maxSatisfying(
mapVersions(versions, OsTypes.DEFAULT),
'>0.0.0',
);
const latestESRVersion = maxSatisfying(
mapVersions(versions, OsTypes.ESR),
'>0.0.0',
);
const filteredVersions = versions.filter(
(v) =>
v.strippedVersion === latestOSVersion ||
v.strippedVersion === latestESRVersion,
)!;
osVersionsByDeviceType[key] = filteredVersions;
return osVersionsByDeviceType;
},
{},
);
};

return {
OsTypes,
getAllOsVersions,
getLatestOsVersions,
};
};

Expand Down
18 changes: 18 additions & 0 deletions tests/integration/models/hostapp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@ describe('Hostapp model', function () {
expect(firstRes).to.equal(secondRes);
});
});

parallel('balena.models.hostapp.getLatestOsVersions()', function () {
it('should contain latest balenaOS and balenaOS ESR OS types', async () => {
const res = await balena.models.hostapp.getLatestOsVersions([
'fincm3',
'raspberrypi3',
]);

['fincm3', 'raspberrypi3'].forEach((key) => {
expect(res[key]).to.be.an('Array');
expect(res[key]).to.have.lengthOf(4);
expect(res[key].filter((r) => r.osType === 'esr')).to.have.lengthOf(2);
expect(res[key].filter((r) => r.osType === 'default')).to.have.lengthOf(
2,
);
});
});
});
});