From 6171d96fa7c7478be01e09c5238ccd0d3b8faf39 Mon Sep 17 00:00:00 2001
From: Felipe Lalanne <1822826+pipex@users.noreply.github.com>
Date: Wed, 15 Nov 2023 14:32:51 -0300
Subject: [PATCH] Add containerName to Service interface

This allows to compare the current and target container names and
trigger an `updateMetadata` step if they are different. This allows us
to normalize the container name format with a new supervisor update.

Change-type: minor
---
 src/compose/app.ts             |  5 ++++-
 src/compose/service-manager.ts |  2 +-
 src/compose/service.ts         | 10 +++++++++-
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/compose/app.ts b/src/compose/app.ts
index 43aa767184..c9e786d3bd 100644
--- a/src/compose/app.ts
+++ b/src/compose/app.ts
@@ -658,7 +658,10 @@ export class App {
 
 	private generateContainerStep(current: Service, target: Service) {
 		// if the services release doesn't match, then rename the container...
-		if (current.commit !== target.commit) {
+		if (
+			current.commit !== target.commit ||
+			current.containerName !== target.containerName
+		) {
 			return generateStep('updateMetadata', { current, target });
 		} else if (target.config.running !== current.config.running) {
 			if (target.config.running) {
diff --git a/src/compose/service-manager.ts b/src/compose/service-manager.ts
index 78208c17ea..b7f1f02e3f 100644
--- a/src/compose/service-manager.ts
+++ b/src/compose/service-manager.ts
@@ -156,7 +156,7 @@ export async function updateMetadata(service: Service, target: Service) {
 	}
 
 	await docker.getContainer(svc.containerId).rename({
-		name: `${service.serviceName}_${target.commit}`,
+		name: target.containerName,
 	});
 }
 
diff --git a/src/compose/service.ts b/src/compose/service.ts
index 95dff4b4f5..ad0750835a 100644
--- a/src/compose/service.ts
+++ b/src/compose/service.ts
@@ -70,6 +70,7 @@ export class Service {
 	public serviceId: number;
 	public imageName: string | null;
 	public containerId: string | null;
+	public containerName: string;
 	public exitErrorMessage: string | null;
 
 	public dependsOn: string[] | null;
@@ -153,6 +154,9 @@ export class Service {
 		service.commit = appConfig.commit;
 		service.appUuid = appConfig.appUuid;
 
+		// The target container name
+		service.containerName = `${service.serviceName}_${service.commit}`;
+
 		// dependsOn is used by other parts of the step
 		// calculation so we delete it from the composition
 		service.dependsOn = appConfig.composition?.dependsOn || null;
@@ -636,6 +640,9 @@ export class Service {
 			);
 		}
 
+		// The current container name, minus the initial '/'
+		svc.containerName = container.Name.slice(1);
+
 		// If we have not renamed the service yet we can still use the image id
 		svc.imageId = parseInt(nameMatch[1], 10);
 		svc.releaseId = parseInt(nameMatch[2], 10);
@@ -893,7 +900,8 @@ export class Service {
 	): boolean {
 		return (
 			this.isEqualConfig(service, currentContainerIds) &&
-			this.commit === service.commit
+			this.commit === service.commit &&
+			this.containerName === service.containerName
 		);
 	}