From c6332546e6d8445db5d8dbccabfb73d2cd809035 Mon Sep 17 00:00:00 2001 From: Marek Calus Date: Wed, 18 Nov 2020 23:23:54 +0100 Subject: [PATCH] Apply Code review fixes --- plugins/catalog-backend/src/service/router.ts | 2 +- plugins/catalog-import/package.json | 12 +++++------ .../src/api/CatalogImportApi.ts | 1 - .../src/api/CatalogImportClient.ts | 20 ++++++++++++++----- plugins/catalog-import/src/plugin.ts | 6 ++++-- .../catalog-import/src/util/useGithubRepos.ts | 6 +----- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/plugins/catalog-backend/src/service/router.ts b/plugins/catalog-backend/src/service/router.ts index fd9062bf94..e850792936 100644 --- a/plugins/catalog-backend/src/service/router.ts +++ b/plugins/catalog-backend/src/service/router.ts @@ -144,7 +144,7 @@ export async function createRouter( router.post('/analyze-location', async (req, res) => { const input = await validateRequestBody(req, analyzeLocationSchema); const output = await locationAnalyzer.generateConfig(input); - res.status(201).send(output); + res.status(200).send(output); }); } diff --git a/plugins/catalog-import/package.json b/plugins/catalog-import/package.json index d6a08a35c7..ee157235ca 100644 --- a/plugins/catalog-import/package.json +++ b/plugins/catalog-import/package.json @@ -1,6 +1,6 @@ { "name": "@backstage/plugin-catalog-import", - "version": "0.1.1-alpha.26", + "version": "0.2.0", "main": "src/index.ts", "types": "src/index.ts", "license": "Apache-2.0", @@ -21,11 +21,11 @@ "clean": "backstage-cli clean" }, "dependencies": { - "@backstage/catalog-model": "^0.1.1-alpha.26", - "@backstage/core": "^0.1.1-alpha.26", - "@backstage/plugin-catalog": "^0.1.1-alpha.26", - "@backstage/plugin-catalog-backend": "^0.1.1-alpha.26", - "@backstage/theme": "^0.1.1-alpha.26", + "@backstage/catalog-model": "^0.2.0", + "@backstage/core": "^0.2.0", + "@backstage/plugin-catalog": "^0.2.0", + "@backstage/plugin-catalog-backend": "^0.2.0", + "@backstage/theme": "^0.2.0", "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", "@material-ui/lab": "4.0.0-alpha.45", diff --git a/plugins/catalog-import/src/api/CatalogImportApi.ts b/plugins/catalog-import/src/api/CatalogImportApi.ts index 7ecb4a543b..49012764e2 100644 --- a/plugins/catalog-import/src/api/CatalogImportApi.ts +++ b/plugins/catalog-import/src/api/CatalogImportApi.ts @@ -24,7 +24,6 @@ export const catalogImportApiRef = createApiRef({ export interface CatalogImportApi { submitPrToRepo(options: { - oAuthToken: string; owner: string; repo: string; fileContent: string; diff --git a/plugins/catalog-import/src/api/CatalogImportClient.ts b/plugins/catalog-import/src/api/CatalogImportClient.ts index cef4b2da0e..f8b4e5d17e 100644 --- a/plugins/catalog-import/src/api/CatalogImportClient.ts +++ b/plugins/catalog-import/src/api/CatalogImportClient.ts @@ -15,16 +15,26 @@ */ import { Octokit } from '@octokit/rest'; -import { DiscoveryApi } from '@backstage/core'; +import { + DiscoveryApi, + githubAuthApiRef, + OAuthApi, + useApi, +} from '@backstage/core'; import { CatalogImportApi } from './CatalogImportApi'; import { AnalyzeLocationResponse } from '@backstage/plugin-catalog-backend'; import { PartialEntity } from '../util/types'; export class CatalogImportClient implements CatalogImportApi { private readonly discoveryApi: DiscoveryApi; + private readonly githubAuthApi: OAuthApi; - constructor(options: { discoveryApi: DiscoveryApi }) { + constructor(options: { + discoveryApi: DiscoveryApi; + githubAuthApi: OAuthApi; + }) { this.discoveryApi = options.discoveryApi; + this.githubAuthApi = options.githubAuthApi; } async generateEntityDefinitions({ @@ -83,18 +93,18 @@ export class CatalogImportClient implements CatalogImportApi { } async submitPrToRepo({ - oAuthToken, owner, repo, fileContent, }: { - oAuthToken: string; owner: string; repo: string; fileContent: string; }): Promise<{ link: string; location: string }> { + const token = await this.githubAuthApi.getAccessToken(['repo']); + const octo = new Octokit({ - auth: oAuthToken, + auth: token, }); const branchName = 'backstage-integration'; diff --git a/plugins/catalog-import/src/plugin.ts b/plugins/catalog-import/src/plugin.ts index caac764513..3ce4918553 100644 --- a/plugins/catalog-import/src/plugin.ts +++ b/plugins/catalog-import/src/plugin.ts @@ -19,6 +19,7 @@ import { createPlugin, createRouteRef, discoveryApiRef, + githubAuthApiRef, } from '@backstage/core'; import { catalogImportApiRef } from './api/CatalogImportApi'; import { CatalogImportClient } from './api/CatalogImportClient'; @@ -33,8 +34,9 @@ export const plugin = createPlugin({ apis: [ createApiFactory({ api: catalogImportApiRef, - deps: { discoveryApi: discoveryApiRef }, - factory: ({ discoveryApi }) => new CatalogImportClient({ discoveryApi }), + deps: { discoveryApi: discoveryApiRef, githubAuthApi: githubAuthApiRef }, + factory: ({ discoveryApi, githubAuthApi }) => + new CatalogImportClient({ discoveryApi, githubAuthApi }), }), ], }); diff --git a/plugins/catalog-import/src/util/useGithubRepos.ts b/plugins/catalog-import/src/util/useGithubRepos.ts index 84c594c913..a18edc2777 100644 --- a/plugins/catalog-import/src/util/useGithubRepos.ts +++ b/plugins/catalog-import/src/util/useGithubRepos.ts @@ -15,21 +15,17 @@ */ import * as YAML from 'yaml'; -import { useApi, githubAuthApiRef } from '@backstage/core'; +import { useApi } from '@backstage/core'; import { catalogImportApiRef } from '../api/CatalogImportApi'; import { ConfigSpec } from '../components/ImportComponentPage'; export function useGithubRepos() { const api = useApi(catalogImportApiRef); - const auth = useApi(githubAuthApiRef); const submitPrToRepo = async (selectedRepo: ConfigSpec) => { - const token = await auth.getAccessToken(['repo']); - const [ownerName, repoName] = selectedRepo.location.split('/').slice(-2); const submitPRResponse = await api .submitPrToRepo({ - oAuthToken: token, owner: ownerName, repo: repoName, fileContent: selectedRepo.config