From 286647dd9dc9ea6f0151b655436ed7e778e00c65 Mon Sep 17 00:00:00 2001 From: Raghunandan Balachandran Date: Mon, 16 Sep 2024 15:31:44 +0200 Subject: [PATCH] Make catalog client optional in github env create action Signed-off-by: Raghunandan Balachandran --- .../api-report.md | 2 +- .../package.json | 1 + .../src/actions/githubEnvironment.ts | 19 ++++++++++++------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/api-report.md b/plugins/scaffolder-backend-module-github/api-report.md index f145eba829..86964b7623 100644 --- a/plugins/scaffolder-backend-module-github/api-report.md +++ b/plugins/scaffolder-backend-module-github/api-report.md @@ -67,7 +67,7 @@ export function createGithubDeployKeyAction(options: { // @public export function createGithubEnvironmentAction(options: { integrations: ScmIntegrationRegistry; - catalogClient: CatalogApi; + catalogClient?: CatalogApi; }): TemplateAction< { repoUrl: string; diff --git a/plugins/scaffolder-backend-module-github/package.json b/plugins/scaffolder-backend-module-github/package.json index e4fadf57de..fad69f590c 100644 --- a/plugins/scaffolder-backend-module-github/package.json +++ b/plugins/scaffolder-backend-module-github/package.json @@ -45,6 +45,7 @@ "@backstage/backend-common": "workspace:^", "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", + "@backstage/catalog-model": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/integration": "workspace:^", diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts index d121c32872..29c0a3ce38 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts @@ -25,6 +25,7 @@ import { Octokit } from 'octokit'; import Sodium from 'libsodium-wrappers'; import { examples } from './gitHubEnvironment.examples'; import { CatalogApi } from '@backstage/catalog-client'; +import { Entity } from '@backstage/catalog-model'; /** * Creates an `github:environment:create` Scaffolder action that creates a Github Environment. @@ -33,7 +34,7 @@ import { CatalogApi } from '@backstage/catalog-client'; */ export function createGithubEnvironmentAction(options: { integrations: ScmIntegrationRegistry; - catalogClient: CatalogApi; + catalogClient?: CatalogApi; }) { const { integrations, catalogClient } = options; // For more information on how to define custom actions, see @@ -187,11 +188,15 @@ export function createGithubEnvironmentAction(options: { // convert reviewers from catalog entity to Github user or team const githubReviewers: { type: 'User' | 'Team'; id: number }[] = []; if (reviewers) { + let reviewersEntityRefs: Array = []; // Fetch reviewers from Catalog - const { items: reviewersEntityRefs } = - await catalogClient.getEntitiesByRefs({ - entityRefs: reviewers, - }); + const catalogResponse = await catalogClient?.getEntitiesByRefs({ + entityRefs: reviewers, + }); + if (catalogResponse?.items?.length) { + reviewersEntityRefs = catalogResponse.items; + } + for (const reviewerEntityRef of reviewersEntityRefs) { if (reviewerEntityRef?.kind === 'User') { try { @@ -203,7 +208,7 @@ export function createGithubEnvironmentAction(options: { id: user.data.id, }); } catch (error) { - console.log('User not found:', error); + ctx.logger.error('User not found:', error); } } else if (reviewerEntityRef?.kind === 'Group') { try { @@ -216,7 +221,7 @@ export function createGithubEnvironmentAction(options: { id: team.data.id, }); } catch (error) { - console.log('Team not found:', error); + ctx.logger.error('Team not found:', error); } } }