From d328b6986b4721b31c606e1da3ba93afce1864b7 Mon Sep 17 00:00:00 2001 From: Nima Kaviani <17132353+nimakaviani@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:49:29 -0700 Subject: [PATCH] fix tsc errors (#25) --- .../app/src/components/catalog/EntityPage.tsx | 4 --- .../src/service/router.test.ts | 21 +++++++++++ .../terraform-backend/src/service/router.ts | 26 +++++++------- plugins/terraform/dev/index.tsx | 4 +-- plugins/terraform/src/api/Terraform.test.ts | 35 +++++++++++++++++-- plugins/terraform/src/api/Terraform.ts | 6 ++-- .../MainPageFetchComponent.tsx | 4 +-- 7 files changed, 74 insertions(+), 26 deletions(-) diff --git a/packages/app/src/components/catalog/EntityPage.tsx b/packages/app/src/components/catalog/EntityPage.tsx index e6aef17..81ce432 100644 --- a/packages/app/src/components/catalog/EntityPage.tsx +++ b/packages/app/src/components/catalog/EntityPage.tsx @@ -179,10 +179,6 @@ const overviewContent = ( ); -const terraFormContent = ( - -); - const serviceEntityPage = ( diff --git a/plugins/terraform-backend/src/service/router.test.ts b/plugins/terraform-backend/src/service/router.test.ts index 4f50797..88684e6 100644 --- a/plugins/terraform-backend/src/service/router.test.ts +++ b/plugins/terraform-backend/src/service/router.test.ts @@ -1,4 +1,5 @@ import { getVoidLogger } from '@backstage/backend-common'; +import { ConfigApi } from "@backstage/core-plugin-api" import express from 'express'; import request from 'supertest'; @@ -7,9 +8,29 @@ import { createRouter } from './router'; describe('createRouter', () => { let app: express.Express; + const mockConfig: jest.Mocked = { + has: jest.fn(), + keys: jest.fn(), + get: jest.fn(), + getOptional: jest.fn(), + getConfig: jest.fn(), + getOptionalConfig: jest.fn(), + getConfigArray: jest.fn(), + getOptionalConfigArray: jest.fn(), + getNumber: jest.fn(), + getOptionalNumber: jest.fn(), + getBoolean: jest.fn(), + getOptionalBoolean: jest.fn(), + getString: jest.fn(), + getOptionalString: jest.fn(), + getStringArray: jest.fn(), + getOptionalStringArray: jest.fn(), + } + beforeAll(async () => { const router = await createRouter({ logger: getVoidLogger(), + config: mockConfig, }); app = express().use(router); }); diff --git a/plugins/terraform-backend/src/service/router.ts b/plugins/terraform-backend/src/service/router.ts index db06e5a..0002358 100644 --- a/plugins/terraform-backend/src/service/router.ts +++ b/plugins/terraform-backend/src/service/router.ts @@ -1,10 +1,12 @@ import { errorHandler } from '@backstage/backend-common'; -import { coreServices } from '@backstage/backend-plugin-api'; +import { LoggerService } from '@backstage/backend-plugin-api'; +import { Config } from '@backstage/config'; import express from 'express'; import Router from 'express-promise-router'; import {DefaultAwsCredentialsManager} from '@backstage/integration-aws-node'; import {S3Client, ListObjectsV2Command, GetObjectCommand} from "@aws-sdk/client-s3"; import * as fs from 'fs'; + const {inflate} = require('pako'); type ListObjectsInput = { @@ -14,8 +16,8 @@ type ListObjectsInput = { }; export interface RouterOptions { - logger: coreServices.logger; - config: coreServices.rootConfig, + logger: LoggerService; + config: Config, } export async function createRouter( @@ -40,10 +42,10 @@ export async function createRouter( let jsonData:any = {}; if(req.body.tfState) { - var bytes = []; + const bytes = []; const inputString = atob(req.body.tfState); - for (var i = 0; i < inputString.length; i++) { - var abyte = inputString.charCodeAt(i) & 0xff; + for (let i = 0; i < inputString.length; i++) { + const abyte = inputString.charCodeAt(i) & 0xff; bytes.push(abyte); } const binData = new Uint8Array(bytes); @@ -58,7 +60,7 @@ export async function createRouter( let responseObject: any = []; let token: string | undefined = "1"; while (token) { - let input: ListObjectsInput = { + const input: ListObjectsInput = { Bucket: req.body.Bucket, Prefix: req.body.Key, } @@ -75,15 +77,15 @@ export async function createRouter( }); router.post('/getLocalFileList', async (req, res) => { - let responseObject: any[] = []; + const responseObject: any[] = []; try { const fsstat = fs.lstatSync(req.body.FileLocation); if (fsstat.isDirectory()) { const filenames = fs.readdirSync(req.body.FileLocation); - for (let i in filenames) { + for (const i in filenames) { responseObject.push({ - Key: req.body.FileLocation + "/" + filenames[i] + Key: `${req.body.FileLocation }/${ filenames[i]}` }); } } else if (fsstat.isFile()) { @@ -92,7 +94,7 @@ export async function createRouter( }); } } catch (e) { - logger.error(e) + logger.error(String(e)) } res.json(responseObject); @@ -114,7 +116,7 @@ export async function createRouter( const data = fs.readFileSync(req.body.Key, {encoding: 'utf8', flag: 'r'}); jsonData = JSON.parse(data); } catch (e) { - logger.error(e); + logger.error(String(e)); } res.json(jsonData); diff --git a/plugins/terraform/dev/index.tsx b/plugins/terraform/dev/index.tsx index ce55a27..a701e2d 100644 --- a/plugins/terraform/dev/index.tsx +++ b/plugins/terraform/dev/index.tsx @@ -1,11 +1,11 @@ import React from 'react'; import { createDevApp } from '@backstage/dev-utils'; -import { terraformPlugin, TerraformPage } from '../src/plugin'; +import { terraformPlugin, TerraformPluginPage } from '../src/plugin'; createDevApp() .registerPlugin(terraformPlugin) .addPage({ - element: , + element: , title: 'Root Page', path: '/terraform' }) diff --git a/plugins/terraform/src/api/Terraform.test.ts b/plugins/terraform/src/api/Terraform.test.ts index 319cda1..6e60196 100644 --- a/plugins/terraform/src/api/Terraform.test.ts +++ b/plugins/terraform/src/api/Terraform.test.ts @@ -2,16 +2,45 @@ import { Terraform } from "./Terraform"; import { KubernetesApi } from "@backstage/plugin-kubernetes"; import { FrontendHostDiscovery } from "@backstage/core-app-api"; import { UserIdentity } from "@backstage/core-components"; +import { IdentityApi, ConfigApi } from "@backstage/core-plugin-api" describe("TerraformClient", () => { const mockKClient: jest.Mocked = { getObjectsByEntity: jest.fn(), getClusters: jest.fn(), + getCluster: jest.fn(), getWorkloadsByEntity: jest.fn(), getCustomObjectsByEntity: jest.fn(), proxy: jest.fn(), }; + const mockIdentity: jest.Mocked = { + getProfileInfo:jest.fn(), + getBackstageIdentity: jest.fn(), + getCredentials: jest.fn(), + signOut: jest.fn(), + + } + + const mockConfig: jest.Mocked = { + has: jest.fn(), + keys: jest.fn(), + get: jest.fn(), + getOptional: jest.fn(), + getConfig: jest.fn(), + getOptionalConfig: jest.fn(), + getConfigArray: jest.fn(), + getOptionalConfigArray: jest.fn(), + getNumber: jest.fn(), + getOptionalNumber: jest.fn(), + getBoolean: jest.fn(), + getOptionalBoolean: jest.fn(), + getString: jest.fn(), + getOptionalString: jest.fn(), + getStringArray: jest.fn(), + getOptionalStringArray: jest.fn(), + } + beforeAll(() => { jest .spyOn(FrontendHostDiscovery.prototype, "getBaseUrl") @@ -35,7 +64,7 @@ describe("TerraformClient", () => { ok: true, text: async () => "teststring", } as Response); - const a = new Terraform(mockKClient); + const a = new Terraform(mockKClient, mockIdentity, mockConfig); const spy = jest.spyOn(mockKClient, "proxy"); const resp = await a.getSecret("abc", "default", "test"); expect(resp).toBeDefined(); @@ -57,7 +86,7 @@ describe("TerraformClient", () => { }, ]); - const a = new Terraform(mockKClient); + const a = new Terraform(mockKClient, mockIdentity, mockConfig); const spy = jest.spyOn(a, "getFirstCluster"); const resp = await a.getSecret(undefined, "default", "test"); expect(resp).toBeDefined(); @@ -71,7 +100,7 @@ describe("TerraformClient", () => { text: async () => "oh no", } as Response); - const a = new Terraform(mockKClient); + const a = new Terraform(mockKClient, mockIdentity, mockConfig); await expect( a.getSecret("abc", "default", "test") ).rejects.toEqual( diff --git a/plugins/terraform/src/api/Terraform.ts b/plugins/terraform/src/api/Terraform.ts index bd1b150..0809d8e 100644 --- a/plugins/terraform/src/api/Terraform.ts +++ b/plugins/terraform/src/api/Terraform.ts @@ -23,7 +23,7 @@ export class Terraform implements TerraformApi { async fetchURL(url: string, type: string, requestBody: any) { const { token } = await this.identityApi.getCredentials(); const backendUrl = this.configApi.getString('backend.baseUrl'); - const response = await fetch(backendUrl+""+url, { + const response = await fetch(`${backendUrl}${url}`, { method: type, body: JSON.stringify(requestBody), headers: { @@ -95,7 +95,7 @@ export class Terraform implements TerraformApi { Bucket:string, file:any ):Promise { - let bodyObj:any = { + const bodyObj:any = { Key: file.Key }; if(Bucket) { @@ -156,4 +156,4 @@ export class Terraform implements TerraformApi { } return Promise.reject("no clusters found in configuration"); } -} \ No newline at end of file +} diff --git a/plugins/terraform/src/components/MainPageFetchComponent/MainPageFetchComponent.tsx b/plugins/terraform/src/components/MainPageFetchComponent/MainPageFetchComponent.tsx index caa6647..00129da 100644 --- a/plugins/terraform/src/components/MainPageFetchComponent/MainPageFetchComponent.tsx +++ b/plugins/terraform/src/components/MainPageFetchComponent/MainPageFetchComponent.tsx @@ -201,7 +201,7 @@ export const MainPageFetchComponent = () => { const [resources, setResources] = useState([]); const [outputs, setOutputs] = useState([]); const [loading, setLoading] = useState(true); - const [error, setError] = useState(); + const [error] = useState(); function parseResources(resourcesArr:any[]) { let resourcesObj:any = {}; @@ -338,4 +338,4 @@ export const MainPageFetchComponent = () => { ; -}; \ No newline at end of file +};