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

Allow the environment variable for space to be an Id or a Name #102

Merged
merged 11 commits into from
Jul 21, 2022
35 changes: 35 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,39 @@ describe("client", () => {
};
await expect(Client.create(clientConfiguration)).rejects.toThrow();
});

test("connects using space id", async () => {
const clientConfiguration: ClientConfiguration = {
apiKey: process.env["OCTOPUS_API_KEY"] || "",
apiUri: process.env["OCTOPUS_HOST"] || "",
space: "Spaces-1",
autoConnect: true
};

const client = await Client.create(clientConfiguration);
expect(client.isConnected()).toBe(true);
});

test("connects using space name", async () => {
const clientConfiguration: ClientConfiguration = {
apiKey: process.env["OCTOPUS_API_KEY"] || "",
apiUri: process.env["OCTOPUS_HOST"] || "",
space: "Default",
autoConnect: true
};

const client = await Client.create(clientConfiguration);
expect(client.isConnected()).toBe(true);
});

test("throws with invalid space", async () => {
const clientConfiguration: ClientConfiguration = {
apiKey: process.env["OCTOPUS_API_KEY"] || "",
apiUri: process.env["OCTOPUS_HOST"] || "",
space: "NonExistent",
autoConnect: true
};

await expect(Client.create(clientConfiguration)).rejects.toThrow();
})
});
30 changes: 25 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { GlobalRootLinks, OctopusError, RootResource, SpaceRootLinks, SpaceRootResource } from "@octopusdeploy/message-contracts";
import type {
GlobalRootLinks,
PagingCollection,
OctopusError,
RootResource,
SpaceRootLinks,
SpaceResource,
SpaceRootResource,
} from "@octopusdeploy/message-contracts";
import ApiClient from "./apiClient";
import { ClientConfiguration, processConfiguration } from "./clientConfiguration";
import type { ClientErrorResponseDetails } from "./clientErrorResponseDetails";
Expand Down Expand Up @@ -39,7 +47,7 @@ export class Client {
if (error instanceof Error) client.error("Could not connect", error);
throw error;
}
if (configuration.space !== null && configuration.space !== undefined) {
if (configuration.space !== undefined && configuration.space !== "") {
try {
await client.switchToSpace(configuration.space);
} catch (error: unknown) {
Expand Down Expand Up @@ -170,15 +178,27 @@ export class Client {
return new Client(this.session, this.resolver, this.rootDocument, null, null, this.configuration);
}

async switchToSpace(spaceId: string): Promise<void> {
async switchToSpace(spaceIdOrName: string): Promise<void> {
if (this.rootDocument === null) {
throw new Error(
"Root document is null; this document is required for the API client. Please ensure that the API endpoint is accessible along with its root document."
);
}

this.spaceId = spaceId;
this.spaceRootDocument = await this.get<SpaceRootResource>(this.rootDocument.Links["SpaceHome"], { spaceId: this.spaceId });
const spaceList = await this.get<PagingCollection<SpaceResource>>(this.rootDocument.Links["Spaces"]);
const uppercaseSpaceIdOrName = spaceIdOrName.toUpperCase();
var spaceResources = spaceList.Items.filter(
(s: SpaceResource) => s.Name.toUpperCase() === uppercaseSpaceIdOrName || s.Id.toUpperCase() === uppercaseSpaceIdOrName
);

if (spaceResources.length == 1) {
const spaceResource = spaceResources[0];
this.spaceId = spaceResource.Id;
this.spaceRootDocument = await this.get<SpaceRootResource>(spaceResource.Links["SpaceHome"]);
return;
}

throw new Error(`Unable to uniquely identify a space using '${spaceIdOrName}'.`);
}

switchToSystem(): void {
Expand Down
4 changes: 2 additions & 2 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ export class Repository implements OctopusSpaceRepository, OctopusSystemReposito
return new Repository(this.client.forSystem());
}

switchToSpace(spaceId: string): Promise<void> {
return this.client.switchToSpace(spaceId);
switchToSpace(spaceIdOrName: string): Promise<void> {
return this.client.switchToSpace(spaceIdOrName);
}

switchToSystem(): void {
Expand Down