Skip to content

Commit

Permalink
Merge pull request #1433 from balena-io/device-should-be-running-release
Browse files Browse the repository at this point in the history
Add the computed device.should_be_running__release field
  • Loading branch information
thgreasi committed Aug 30, 2024
2 parents be4661d + 7814bba commit 639f62b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
27 changes: 9 additions & 18 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ const getDeviceModel = function (
* you have to explicitly define them in a `$select` in the extra options:
* * `overall_status`
* * `overall_progress`
* * `should_be_running__release`
*
* @param {String|Number} slugOrUuidOrId - application slug (string), uuid (string) or id (number)
* @param {Object} [options={}] - extra pine options to use
Expand Down Expand Up @@ -511,6 +512,7 @@ const getDeviceModel = function (
* you have to explicitly define them in a `$select` in the extra options:
* * `overall_status`
* * `overall_progress`
* * `should_be_running__release`
*
* @param {String|Number} handleOrId - organization handle (string) or id (number).
* @param {Object} [options={}] - extra pine options to use
Expand Down Expand Up @@ -577,6 +579,7 @@ const getDeviceModel = function (
* you have to explicitly define them in a `$select` in the extra options:
* * `overall_status`
* * `overall_progress`
* * `should_be_running__release`
*
* @param {String|Number} uuidOrId - device uuid (string) or id (number)
* @param {Object} [options={}] - extra pine options to use
Expand Down Expand Up @@ -1974,29 +1977,17 @@ const getDeviceModel = function (
const deviceOptions = {
$select: 'id',
$expand: {
is_pinned_on__release: {
should_be_running__release: {
$select: 'commit',
},
belongs_to__application: {
$select: 'id',
$expand: { should_be_running__release: { $select: 'commit' } },
},
},
} as const;

const { is_pinned_on__release, belongs_to__application } =
(await exports.get(uuidOrId, deviceOptions)) as PineTypedResult<
Device,
typeof deviceOptions
>;
if (is_pinned_on__release.length > 0) {
return is_pinned_on__release[0]!.commit;
}
const targetRelease =
belongs_to__application[0].should_be_running__release[0];
if (targetRelease) {
return targetRelease.commit;
}
const { should_be_running__release } = (await exports.get(
uuidOrId,
deviceOptions,
)) as PineTypedResult<Device, typeof deviceOptions>;
return should_be_running__release[0]?.commit;
},

/**
Expand Down
3 changes: 3 additions & 0 deletions src/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ export interface Device {
should_be_operated_by__release: OptionalNavigationResource<Release>;
should_be_managed_by__release: OptionalNavigationResource<Release>;

/** This is a computed term that works like: `device.is_pinned_on__release ?? device.belongs_to__application[0].should_be_running__release` */
should_be_running__release: OptionalNavigationResource<Release>;

device_config_variable: ReverseNavigationResource<DeviceVariable>;
device_environment_variable: ReverseNavigationResource<DeviceVariable>;
device_tag: ReverseNavigationResource<DeviceTag>;
Expand Down
30 changes: 14 additions & 16 deletions tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,31 @@ export const IS_BROWSER = typeof window !== 'undefined' && window !== null;
export const apiVersion = 'v7';

export let balenaSdkExports: typeof BalenaSdk;
export let sdkOpts: BalenaSdk.SdkOptions;
export const sdkOpts: BalenaSdk.SdkOptions = {
isBrowser: IS_BROWSER,
requestBatchingChunkSize: 5,
retryRateLimitedRequests: true,
};
if (IS_BROWSER) {
require('js-polyfills/es6');
balenaSdkExports = window.balenaSdk;

const apiUrl = process.env.TEST_API_URL || 'https://api.balena-cloud.com';
sdkOpts = {
apiUrl,
builderUrl:
process.env.TEST_BUILDER_URL || apiUrl.replace('api.', 'builder.'),
};
sdkOpts.apiUrl = process.env.TEST_API_URL || 'https://api.balena-cloud.com';
} else {
balenaSdkExports = require('../..');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const settings = require('balena-settings-client');

const apiUrl = process.env.TEST_API_URL || settings.get('apiUrl');
sdkOpts = {
apiUrl,
builderUrl:
process.env.TEST_BUILDER_URL || apiUrl.replace('api.', 'builder.'),
dataDirectory: settings.get('dataDirectory'),
};
sdkOpts.apiUrl = process.env.TEST_API_URL || settings.get('apiUrl');
sdkOpts.dataDirectory = settings.get('dataDirectory');
}

if (sdkOpts.apiUrl == null) {
throw new Error(`sdkOpts.apiUrl was not defined: ${sdkOpts.apiUrl}`);
}

sdkOpts.isBrowser = IS_BROWSER;
sdkOpts.requestBatchingChunkSize = 5;
sdkOpts.builderUrl =
process.env.TEST_BUILDER_URL || sdkOpts.apiUrl.replace('api.', 'builder.');

const env = process.env as Dictionary<string>;
console.log(`Running SDK tests against: ${sdkOpts.apiUrl}`);
Expand Down

0 comments on commit 639f62b

Please sign in to comment.