From d54d3c674889a9533c89b79685ec64b5e2d4bfab Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Mon, 11 Mar 2024 10:20:07 +0100 Subject: [PATCH] Made config truly optional Signed-off-by: Andre Wanlin --- .../src/api/AzureDevOpsApi.ts | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts index 0376c82258..18bad8c330 100644 --- a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts +++ b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts @@ -108,10 +108,17 @@ export class AzureDevOpsApi { // If no host or org is provided we fall back to the values from the `azureDevOps` config section // these may have been setup in the `integrations.azure` config section // which is why use them here and not just falling back on them entirely - const validHost = host ?? this.config.getString('azureDevOps.host'); - const validOrg = org ?? this.config.getString('azureDevOps.organization'); - const url = `https://${validHost}/${encodeURIComponent(validOrg)}`; + const validHost = host ?? this.config.getOptionalString('azureDevOps.host'); + const validOrg = + org ?? this.config.getOptionalString('azureDevOps.organization'); + if (!validHost || !validOrg) { + throw new Error( + "No 'host' or 'org' provided in annotations or configuration, unable to retrieve needed credentials", + ); + } + + const url = `https://${validHost}/${encodeURIComponent(validOrg)}`; const credentials = await this.credentialsProvider.getCredentials({ url, }); @@ -120,12 +127,15 @@ export class AzureDevOpsApi { if (!credentials) { // No credentials found for the provided host and org in the `integrations.azure` config section // use the fall back personal access token from `azureDevOps.token` - const token = this.config.getString('azureDevOps.token'); - if (token) { - this.logger.warn( - "Using the token from 'azureDevOps.token' has been deprecated, use 'integrations.azure' instead, for more details see: https://backstage.io/docs/integrations/azure/locations", + const token = this.config.getOptionalString('azureDevOps.token'); + if (!token) { + throw new Error( + "No 'azureDevOps.token' provided in configuration and credentials were not found in 'integrations.azure', unable to proceed", ); } + this.logger.warn( + "Using the token from 'azureDevOps.token' has been deprecated, use 'integrations.azure' instead, for more details see: https://backstage.io/docs/integrations/azure/locations", + ); authHandler = getPersonalAccessTokenHandler(token); } else { authHandler = getHandlerFromToken(credentials.token);