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 the computed device.should_be_running__release field #1433

Merged
merged 2 commits into from
Aug 30, 2024
Merged
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
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 @@
* 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 @@
* 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 @@
* 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 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 Expand Up @@ -2243,7 +2234,7 @@
getDeviceOsSemverWithVariant({
os_version,
os_variant,
}) || os_version;

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (18.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (22.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (22.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (ubuntu-20.04)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (macos-12)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2237 in src/models/device.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (windows-2019)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

// if the os_version couldn't be parsed
// rely on getHUPActionType to throw an error
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
Loading