From e6b85c9cf8784b8b4056795a35ed208570aa4164 Mon Sep 17 00:00:00 2001
From: Scott Lowe <scott@balena.io>
Date: Thu, 4 Feb 2021 07:41:49 +0100
Subject: [PATCH] Modify push to pass app slug to builder

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
---
 lib/commands/push.ts        | 32 ++++----------------------------
 lib/utils/remote-build.ts   | 14 ++++----------
 tests/commands/push.spec.ts |  3 +--
 3 files changed, 9 insertions(+), 40 deletions(-)

diff --git a/lib/commands/push.ts b/lib/commands/push.ts
index b4b36bfa..6a933354 100644
--- a/lib/commands/push.ts
+++ b/lib/commands/push.ts
@@ -20,7 +20,7 @@ import Command from '../command';
 import * as cf from '../utils/common-flags';
 import { getBalenaSdk, stripIndent } from '../utils/lazy';
 import { dockerignoreHelp, registrySecretsHelp } from '../utils/messages';
-import type { BalenaSDK, Application, Organization } from 'balena-sdk';
+import type { BalenaSDK } from 'balena-sdk';
 import { ExpectedError, instanceOf } from '../errors';
 import { isV13 } from '../utils/version';
 import { RegistrySecrets } from 'resin-multibuild';
@@ -353,32 +353,9 @@ export default class PushCmd extends Command {
 			sdk.settings.get('balenaUrl'),
 		]);
 
-		const application = (await getApplication(sdk, appNameOrSlug, {
-			$expand: {
-				organization: {
-					$select: ['handle'],
-				},
-			},
+		const application = await getApplication(sdk, appNameOrSlug, {
 			$select: ['app_name', 'slug'],
-		})) as Application & {
-			organization: [Organization];
-		};
-
-		// * Temporary fix *
-		// When doing `$expand organization` on a public app (when not the owner)
-		// a partial document is returned without any organization data.
-		// So e.g. `balena push balena-sound` will break this commands logic.
-		// The balena cloud builder api will soon change to accept application slugs
-		// instead of `owner, app`, after which we will not need to do the expand
-		//
-		// Users should not be pushing to public apps anyway, so for now catch this situation:
-		if (!application.organization[0]) {
-			throw new ExpectedError(stripIndent`
-				You do not have permissions to push to application ${application.slug}.
-				If you are trying to deploy a public app, please make sure that you have created
-				your own application first.
-			`);
-		}
+		});
 
 		const opts = {
 			dockerfilePath,
@@ -390,8 +367,7 @@ export default class PushCmd extends Command {
 			convertEol: !options['noconvert-eol'],
 		};
 		const args = {
-			app: application.app_name,
-			owner: application.organization[0].handle,
+			appSlug: application.slug,
 			source: options.source,
 			auth: token,
 			baseUrl,
diff --git a/lib/utils/remote-build.ts b/lib/utils/remote-build.ts
index 3f7aa58d..bdc59688 100644
--- a/lib/utils/remote-build.ts
+++ b/lib/utils/remote-build.ts
@@ -45,16 +45,13 @@ export interface BuildOpts {
 }
 
 export interface RemoteBuild {
-	app: string;
-	owner: string;
+	appSlug: string;
 	source: string;
 	auth: string;
 	baseUrl: string;
 	nogitignore: boolean;
 	opts: BuildOpts;
-
 	sdk: BalenaSDK;
-
 	// For internal use
 	releaseId?: number;
 	hadError?: boolean;
@@ -85,14 +82,12 @@ export class RemoteBuildFailedError extends ExpectedError {
 
 async function getBuilderEndpoint(
 	baseUrl: string,
-	owner: string,
-	app: string,
+	appSlug: string,
 	opts: BuildOpts,
 ): Promise<string> {
 	const querystring = await import('querystring');
 	const args = querystring.stringify({
-		owner,
-		app,
+		slug: appSlug,
 		dockerfilePath: opts.dockerfilePath,
 		emulated: opts.emulated,
 		nocache: opts.nocache,
@@ -384,8 +379,7 @@ async function getRemoteBuildStream(
 ): Promise<[request.Request, Stream.Stream]> {
 	const builderUrl = await getBuilderEndpoint(
 		build.baseUrl,
-		build.owner,
-		build.app,
+		build.appSlug,
 		build.opts,
 	);
 	let stream: Stream.Stream;
diff --git a/tests/commands/push.spec.ts b/tests/commands/push.spec.ts
index 0d5f9919..b092c464 100644
--- a/tests/commands/push.spec.ts
+++ b/tests/commands/push.spec.ts
@@ -66,8 +66,7 @@ const commonResponseLines = {
 };
 
 const commonQueryParams = [
-	['owner', 'gh_user'],
-	['app', 'testApp'],
+	['slug', 'gh_user/testApp'],
 	['dockerfilePath', ''],
 	['emulated', 'false'],
 	['nocache', 'false'],