From d779e3b055d8669a77729ba1bc15d61ad132e870 Mon Sep 17 00:00:00 2001 From: npiyush97 Date: Sun, 31 Mar 2024 11:46:02 +0530 Subject: [PATCH 1/4] fix not to add git commit link in about card edit button Signed-off-by: npiyush97 --- .changeset/thirty-plums-shout.md | 5 ++ .../AnnotateLocationEntityProcessor.test.ts | 48 +++++++++++++++++++ .../core/AnnotateLocationEntityProcessor.ts | 6 ++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 .changeset/thirty-plums-shout.md diff --git a/.changeset/thirty-plums-shout.md b/.changeset/thirty-plums-shout.md new file mode 100644 index 0000000000..5b5ffad6a9 --- /dev/null +++ b/.changeset/thirty-plums-shout.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Added a regex test to check commit hash.If url is from git commit branch ignore the edit url. diff --git a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.test.ts index 66b6300df9..2656de1c7b 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.test.ts @@ -166,5 +166,53 @@ describe('AnnotateLocationEntityProcessor', () => { }, }); }); + it('should not render edit button in about for invalid or git hash commit branch', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + + const location: LocationSpec = { + type: 'url', + target: + 'https://github.com/backstage/backstage/blob/f1ba2bc6097a757c2827ff97fa301cff626de137/packages/app/catalog-info.yaml', + }; + const originLocation: LocationSpec = { + type: 'url', + target: + 'https://github.com/backstage/backstage/blob/f1ba2bc6097a757c2827ff97fa301cff626de137/catalog-info.yaml', + }; + + const integrations = ScmIntegrations.fromConfig(new ConfigReader({})); + const processor = new AnnotateLocationEntityProcessor({ integrations }); + + expect( + await processor.preProcessEntity( + entity, + location, + () => {}, + originLocation, + ), + ).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'backstage.io/managed-by-location': + 'url:https://github.com/backstage/backstage/blob/f1ba2bc6097a757c2827ff97fa301cff626de137/packages/app/catalog-info.yaml', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/backstage/backstage/blob/f1ba2bc6097a757c2827ff97fa301cff626de137/catalog-info.yaml', + 'backstage.io/view-url': + 'https://github.com/backstage/backstage/blob/f1ba2bc6097a757c2827ff97fa301cff626de137/packages/app/catalog-info.yaml', + 'backstage.io/source-location': + 'url:https://github.com/backstage/backstage/tree/f1ba2bc6097a757c2827ff97fa301cff626de137/packages/app/', + }, + }, + }); + }); }); }); diff --git a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts index d3329fa86c..1ff4963870 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts @@ -53,12 +53,16 @@ export class AnnotateLocationEntityProcessor implements CatalogProcessor { let viewUrl; let editUrl; let sourceLocation; + const gitCommitBranchURLPattern = /\b[0-9a-f]{5,40}\b/; if (location.type === 'url') { const scmIntegration = integrations.byUrl(location.target); viewUrl = location.target; - editUrl = scmIntegration?.resolveEditUrl(location.target); + + if (!gitCommitBranchURLPattern.test(location.target)) { + editUrl = scmIntegration?.resolveEditUrl(location.target); + } const sourceUrl = scmIntegration?.resolveUrl({ url: './', From 7c1540d46ed22ca1980e0fa89640c72ca103d3a4 Mon Sep 17 00:00:00 2001 From: npiyush97 Date: Tue, 16 Apr 2024 15:09:22 +0530 Subject: [PATCH 2/4] checking url is sha1 hash of len 40 Signed-off-by: npiyush97 --- .../src/modules/core/AnnotateLocationEntityProcessor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts index 1ff4963870..83638d205f 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts @@ -53,7 +53,7 @@ export class AnnotateLocationEntityProcessor implements CatalogProcessor { let viewUrl; let editUrl; let sourceLocation; - const gitCommitBranchURLPattern = /\b[0-9a-f]{5,40}\b/; + const gitCommitBranchURLPattern = /\b[0-9a-f]{40,}\b/; if (location.type === 'url') { const scmIntegration = integrations.byUrl(location.target); From 4668dc76c97150de63d108f7c17b05b2a10cffe6 Mon Sep 17 00:00:00 2001 From: npiyush97 Date: Mon, 13 May 2024 18:50:50 +0530 Subject: [PATCH 3/4] variable name change Signed-off-by: npiyush97 --- .../src/modules/core/AnnotateLocationEntityProcessor.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts index 83638d205f..f46f5cfd42 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts @@ -53,14 +53,14 @@ export class AnnotateLocationEntityProcessor implements CatalogProcessor { let viewUrl; let editUrl; let sourceLocation; - const gitCommitBranchURLPattern = /\b[0-9a-f]{40,}\b/; + const commitHashRegExp = /\b[0-9a-f]{40,}\b/; if (location.type === 'url') { const scmIntegration = integrations.byUrl(location.target); viewUrl = location.target; - if (!gitCommitBranchURLPattern.test(location.target)) { + if (!commitHashRegExp.test(location.target)) { editUrl = scmIntegration?.resolveEditUrl(location.target); } From 81a215d3e694b7814839a0fdef5b9500ce5e9833 Mon Sep 17 00:00:00 2001 From: npiyush97 Date: Mon, 13 May 2024 22:44:00 +0530 Subject: [PATCH 4/4] added changes Signed-off-by: npiyush97 --- .changeset/thirty-plums-shout.md | 2 +- .../src/modules/core/AnnotateLocationEntityProcessor.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/thirty-plums-shout.md b/.changeset/thirty-plums-shout.md index 5b5ffad6a9..56ac640326 100644 --- a/.changeset/thirty-plums-shout.md +++ b/.changeset/thirty-plums-shout.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend': patch --- -Added a regex test to check commit hash.If url is from git commit branch ignore the edit url. +Added a regex test to check commit hash. If url is from git commit branch ignore the edit url. diff --git a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts index f46f5cfd42..00864b69e7 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateLocationEntityProcessor.ts @@ -31,6 +31,7 @@ import { CatalogProcessorEmit, } from '@backstage/plugin-catalog-node'; +const commitHashRegExp = /\b[0-9a-f]{40,}\b/; /** @public */ export class AnnotateLocationEntityProcessor implements CatalogProcessor { constructor( @@ -53,7 +54,6 @@ export class AnnotateLocationEntityProcessor implements CatalogProcessor { let viewUrl; let editUrl; let sourceLocation; - const commitHashRegExp = /\b[0-9a-f]{40,}\b/; if (location.type === 'url') { const scmIntegration = integrations.byUrl(location.target);