Skip to content

Commit

Permalink
Release export/import code enhancements.
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Carlo Miguel F. Cruz <[email protected]>
  • Loading branch information
cmfcruz committed Sep 16, 2024
1 parent dd18000 commit b3f8105
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 38 deletions.
10 changes: 5 additions & 5 deletions docs/balena-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -3407,11 +3407,11 @@ the commit or ID of the release to finalize

## release import &#60;file&#62; &#60;fleet&#62;

Imports a release from a file to an app or fleet. The revision field of the release
is automatically omitted when importing a release. The backend will auto-increment
the revision field of the imported release if a release exists with the same semver.
A release will not be imported if a successful release with the same commit already
exists.
Imports a release from a file to an app, block or fleet. The revision field of the
release is automatically omitted when importing a release. The balena API will
auto-increment the revision field of the imported release if a release exists with
the same semver. A release will not be imported if a successful release with the
same commit already exists.

To export a release to a file, use 'balena release export'.

Expand Down
34 changes: 18 additions & 16 deletions src/commands/release/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,38 @@ export default class ReleaseExportCmd extends Command {
public async run() {
const { args: params, flags: options } = await this.parse(ReleaseExportCmd);

const balena = getBalenaSdk();

let versionInfo = '';

try {
let releaseDetails:
| string
| number
| { application: string | number; rawVersion: string }; // ReleaseRawVersionApplicationPair
const balena = getBalenaSdk();

let releaseDetails: string | { application: number; rawVersion: string }; // ReleaseRawVersionApplicationPair

if (options.version != null) {
versionInfo = ` version ${options.version}`;
const parsedVersion = semver.parse(options.version);
if (parsedVersion == null) {
throw new ExpectedError(`version must be valid SemVer`);
} else {
const { getApplication } = await import('../../utils/sdk');
const application = (
await getApplication(balena, params.commitOrFleet)
).id;
releaseDetails = { application, rawVersion: parsedVersion.raw };
}
const { getApplication } = await import('../../utils/sdk');
const { id: application } = await getApplication(
balena,
params.commitOrFleet,
);
releaseDetails = { application, rawVersion: parsedVersion.raw };
} else {
releaseDetails = params.commitOrFleet;
}

const release = await balena.models.release.get(releaseDetails, {
$select: ['id'],
});
const { id: releaseId } = await balena.models.release.get(
releaseDetails,
{
$select: ['id'],
},
);
const releaseBundle = await create({
sdk: balena,
releaseId: release.id,
releaseId,
});
await fs.writeFile(options.output, releaseBundle);
console.log(
Expand Down
12 changes: 6 additions & 6 deletions src/commands/release/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export default class ReleaseImportCmd extends Command {
public static description = stripIndent`
Imports a release from a file to an app or fleet.
Imports a release from a file to an app or fleet. The revision field of the release
is automatically omitted when importing a release. The backend will auto-increment
the revision field of the imported release if a release exists with the same semver.
A release will not be imported if a successful release with the same commit already
exists.
Imports a release from a file to an app, block or fleet. The revision field of the
release is automatically omitted when importing a release. The balena API will
auto-increment the revision field of the imported release if a release exists with
the same semver. A release will not be imported if a successful release with the
same commit already exists.
To export a release to a file, use 'balena release export'.
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class ReleaseImportCmd extends Command {
}

const { getApplication } = await import('../../utils/sdk');
const application = (await getApplication(balena, params.fleet)).id;
const { id: application } = await getApplication(balena, params.fleet);
if (application == null) {
throw new ExpectedError(`Fleet ${params.fleet} not found`);
}
Expand Down
6 changes: 1 addition & 5 deletions tests/commands/release/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('balena release export', function () {

this.beforeEach(async function () {
api = new BalenaAPIMock();
api.expectGetWhoAmI();
releaseFileBuffer = await fs.readFile(
path.join('tests', 'test-data', 'release.tar'),
);
Expand All @@ -38,7 +39,6 @@ describe('balena release export', function () {
});

itSS('should export a release to a file', async () => {
api.expectGetWhoAmI();
api.expectGetRelease();
releaseBundleCreateStub.resolves(stream.Readable.from(releaseFileBuffer));

Expand All @@ -54,7 +54,6 @@ describe('balena release export', function () {
});

itSS('should fail if the create throws an error', async () => {
api.expectGetWhoAmI();
api.expectGetRelease();
const expectedError = `BalenaReleaseNotFound: Release not found: ${appCommit}`;
releaseBundleCreateStub.rejects(new Error(expectedError));
Expand All @@ -69,7 +68,6 @@ describe('balena release export', function () {
});

itSS('should parse with application slug and version', async () => {
api.expectGetWhoAmI();
api.expectGetRelease();
api.expectGetApplication({ times: 2 });
releaseBundleCreateStub.resolves(stream.Readable.from(releaseFileBuffer));
Expand All @@ -86,7 +84,6 @@ describe('balena release export', function () {
});

it('should fail if the app slug is provided without the release version', async () => {
api.expectGetWhoAmI();
api.expectGetRelease({ notFound: true });
const expectedError = `Release not found: ${appSlug}`;

Expand All @@ -100,7 +97,6 @@ describe('balena release export', function () {
});

it('should fail if the semver is invalid', async () => {
api.expectGetWhoAmI();
const expectedError = 'version must be valid SemVer';

const { err } = await runCommand(
Expand Down
7 changes: 1 addition & 6 deletions tests/commands/release/import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const itSS = process.env.BALENA_CLI_TEST_TYPE === 'standalone' ? it.skip : it;
describe('balena release import', function () {
const appCommit = '4c8becf0780ca69d33b638ea8fa163d7';
const appSlug = 'myOrg/myFleet';
// const appVersion = '1.2.3+rev1';
const releasePath = path.join('tests', 'test-data', 'release.tar');
let api: BalenaAPIMock;
const releaseBundleApplyStub = sinon.stub();

this.beforeEach(async function () {
api = new BalenaAPIMock();
api.expectGetWhoAmI();
mock('@balena/release-bundle', {
apply: releaseBundleApplyStub,
});
Expand All @@ -30,7 +30,6 @@ describe('balena release import', function () {
});

itSS('should import a release to an app', async () => {
api.expectGetWhoAmI();
api.expectGetApplication();
releaseBundleApplyStub.resolves(123);

Expand All @@ -48,7 +47,6 @@ describe('balena release import', function () {
itSS(
'should import a release to an app with a version override',
async () => {
api.expectGetWhoAmI();
api.expectGetApplication();
releaseBundleApplyStub.resolves(123);

Expand All @@ -65,7 +63,6 @@ describe('balena release import', function () {
);

it('should fail if release file does not exist', async () => {
api.expectGetWhoAmI();
const nonExistentFile = path.join(
'tests',
'test-data',
Expand All @@ -83,7 +80,6 @@ describe('balena release import', function () {
});

itSS('should fail if overriding version is not a valid semver', async () => {
api.expectGetWhoAmI();
api.expectGetApplication();
const expectedError = `Manifest is malformed: Expected version to be a valid semantic version but found '${appCommit}'`;
releaseBundleApplyStub.rejects(new Error(expectedError));
Expand All @@ -100,7 +96,6 @@ describe('balena release import', function () {
itSS(
'should fail if a successful release with the same commit already exists',
async () => {
api.expectGetWhoAmI();
api.expectGetApplication();
const expectedError = `A successful release with commit ${appCommit} (1.2.3) already exists; nothing to do`;
releaseBundleApplyStub.rejects(new Error(expectedError));
Expand Down

0 comments on commit b3f8105

Please sign in to comment.