Merge pull request #23908 from npiyush97/annotate

fix not to add git commit link in about card edit button
This commit is contained in:
Patrik Oldsberg
2024-05-28 15:21:25 +02:00
committed by GitHub
3 changed files with 58 additions and 1 deletions
+5
View File
@@ -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.
@@ -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/',
},
},
});
});
});
});
@@ -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(
@@ -58,7 +59,10 @@ export class AnnotateLocationEntityProcessor implements CatalogProcessor {
const scmIntegration = integrations.byUrl(location.target);
viewUrl = location.target;
editUrl = scmIntegration?.resolveEditUrl(location.target);
if (!commitHashRegExp.test(location.target)) {
editUrl = scmIntegration?.resolveEditUrl(location.target);
}
const sourceUrl = scmIntegration?.resolveUrl({
url: './',