Skip to content

Commit

Permalink
os.getSupportedOsUpdateVersions: Add the option to include draft rele…
Browse files Browse the repository at this point in the history
…ases

Change-type: minor
  • Loading branch information
thgreasi committed Jan 19, 2024
1 parent 734ac24 commit b77818b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
17 changes: 9 additions & 8 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const sdk = fromSharedOptions();
* [.download(options)](#balena.models.os.download) ⇒ <code>Promise</code>
* [.getConfig(slugOrUuidOrId, options)](#balena.models.os.getConfig) ⇒ <code>Promise</code>
* [.isSupportedOsUpdate(deviceType, currentVersion, targetVersion)](#balena.models.os.isSupportedOsUpdate) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion)](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion, [includeDraft])](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.getSupervisorImageForDeviceType(deviceTypeId, version)](#balena.models.os.getSupervisorImageForDeviceType) ⇒ <code>Promise.&lt;String&gt;</code>
* [.config](#balena.models.config) : <code>object</code>
Expand Down Expand Up @@ -774,7 +774,7 @@ balena.models.device.get(123).catch(function (error) {
* [.download(options)](#balena.models.os.download) ⇒ <code>Promise</code>
* [.getConfig(slugOrUuidOrId, options)](#balena.models.os.getConfig) ⇒ <code>Promise</code>
* [.isSupportedOsUpdate(deviceType, currentVersion, targetVersion)](#balena.models.os.isSupportedOsUpdate) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion)](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion, [includeDraft])](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.getSupervisorImageForDeviceType(deviceTypeId, version)](#balena.models.os.getSupervisorImageForDeviceType) ⇒ <code>Promise.&lt;String&gt;</code>
* [.config](#balena.models.config) : <code>object</code>
Expand Down Expand Up @@ -5261,7 +5261,7 @@ balena.models.organization.remove(123);
* [.download(options)](#balena.models.os.download) ⇒ <code>Promise</code>
* [.getConfig(slugOrUuidOrId, options)](#balena.models.os.getConfig) ⇒ <code>Promise</code>
* [.isSupportedOsUpdate(deviceType, currentVersion, targetVersion)](#balena.models.os.isSupportedOsUpdate) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion)](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.getSupportedOsUpdateVersions(deviceType, currentVersion, [includeDraft])](#balena.models.os.getSupportedOsUpdateVersions) ⇒ <code>Promise</code>
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.getSupervisorImageForDeviceType(deviceTypeId, version)](#balena.models.os.getSupervisorImageForDeviceType) ⇒ <code>Promise.&lt;String&gt;</code>

Expand Down Expand Up @@ -5467,7 +5467,7 @@ balena.models.os.isSupportedOsUpgrade('raspberry-pi', '2.9.6+rev2.prod', '2.29.2
```
<a name="balena.models.os.getSupportedOsUpdateVersions"></a>

##### os.getSupportedOsUpdateVersions(deviceType, currentVersion) ⇒ <code>Promise</code>
##### os.getSupportedOsUpdateVersions(deviceType, currentVersion, [includeDraft]) ⇒ <code>Promise</code>
**Kind**: static method of [<code>os</code>](#balena.models.os)
**Summary**: Returns the supported OS update targets for the provided device type
**Access**: public
Expand All @@ -5478,10 +5478,11 @@ containing exact version numbers that OS update is supported
that is _not_ pre-release, can be `null`
* current - the provided current version after normalization

| Param | Type | Description |
| --- | --- | --- |
| deviceType | <code>String</code> | device type slug |
| currentVersion | <code>String</code> | semver-compatible version for the starting OS version |
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| deviceType | <code>String</code> | | device type slug |
| currentVersion | <code>String</code> | | semver-compatible version for the starting OS version |
| [includeDraft] | <code>Boolean</code> | <code>false</code> | Whether prerelease OS versions should be included |

**Example**
```js
Expand Down
4 changes: 3 additions & 1 deletion src/models/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ const getOsModel = function (
*
* @param {String} deviceType - device type slug
* @param {String} currentVersion - semver-compatible version for the starting OS version
* @param {Boolean} [includeDraft=false] - Whether prerelease OS versions should be included
* @fulfil {Object} - the versions information, of the following structure:
* * versions - an array of strings,
* containing exact version numbers that OS update is supported
Expand All @@ -892,9 +893,10 @@ const getOsModel = function (
const getSupportedOsUpdateVersions = async (
deviceType: string,
currentVersion: string,
includeDraft: boolean = false,
): Promise<OsUpdateVersions> => {
deviceType = await _getNormalizedDeviceTypeSlug(deviceType);
const allVersions = (await getAvailableOsVersions(deviceType))
const allVersions = (await getAvailableOsVersions(deviceType, includeDraft))
.filter((v) => v.osType === OsTypes.DEFAULT)
.map((v) => v.raw_version);
// use bSemver.compare to find the current version in the OS list
Expand Down
72 changes: 55 additions & 17 deletions tests/integration/models/os.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,24 +858,62 @@ describe('OS model', function () {
);
}));

describe('given a valid device slug', () =>
it('should return the list of supported hup targets', () =>
balena.models.os
.getSupportedOsUpdateVersions('raspberrypi3', '2.9.6+rev1.prod')
.then(function ({ current, recommended, versions }) {
expect(current).to.equal('2.9.6+rev1.prod');
expect(recommended).to.be.a('string');
expect(versions).to.be.an('array');
expect(versions).to.not.have.length(0);
versions.forEach(function (v) {
expect(v).to.be.a('string');
expect(bSemver.gte(v, current)).to.be.true;
});
describe('given a valid device slug', () => {
it('should return the list of supported hup targets', async () => {
const { current, recommended, versions } =
await balena.models.os.getSupportedOsUpdateVersions(
'raspberrypi3',
'2.9.6+rev1.prod',
);
expect(current).to.equal('2.9.6+rev1.prod');
expect(recommended).to.be.a('string');
expect(versions).to.be.an('array');
expect(versions).to.not.have.length(0);
versions.forEach(function (v) {
expect(v).to.be.a('string');
expect(bSemver.gte(v, current)).to.be.true;
});

expect(versions.length > 2).to.be.true;
const sortedVersions = versions.slice().sort(bSemver.rcompare);
expect(versions).to.deep.equal(sortedVersions);
});

it('should not include draft OS releases when the respective flag is not used', async () => {
const { current, recommended, versions } =
await balena.models.os.getSupportedOsUpdateVersions(
'raspberrypi3',
'2.9.6+rev1.prod',
);
expect(current).to.equal('2.9.6+rev1.prod');
expect(recommended).to.be.a('string');
expect(versions).to.be.an('array');
expect(versions).to.not.have.length(0);

const draftVersions = versions.filter(
(v) => bSemver.parse(v)?.prerelease.length ?? 0 > 0,
);
expect(draftVersions).to.have.lengthOf(0);
});

it('should include draft OS releases when the respective flag is used', async () => {
const { current, recommended, versions } =
await balena.models.os.getSupportedOsUpdateVersions(
'raspberrypi3',
'2.9.6+rev1.prod',
true,
);
expect(current).to.equal('2.9.6+rev1.prod');
expect(recommended).to.be.a('string');
expect(versions).to.be.an('array');
expect(versions).to.not.have.length(0);

expect(versions.length > 2).to.be.true;
const sortedVersions = versions.slice().sort(bSemver.rcompare);
expect(versions).to.deep.equal(sortedVersions);
})));
const draftVersions = versions.filter(
(v) => bSemver.parse(v)?.prerelease.length ?? 0 > 0,
);
expect(draftVersions).to.have.length.greaterThan(0);
});
});
});

describe('when logged in as a user with a single application', function () {
Expand Down

0 comments on commit b77818b

Please sign in to comment.