Use scm integration
Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
@@ -11,5 +11,5 @@ builder. To add it to your instance, add it to your `CatalogBuilder` using
|
||||
|
||||
```typescript
|
||||
const builder = new CatalogBuilder(env);
|
||||
builder.addProcessor(new AnnotateScmSlugEntityProcessor());
|
||||
builder.addProcessor(AnnotateScmSlugEntityProcessor.fromConfig(env.config));
|
||||
```
|
||||
|
||||
+14
-4
@@ -14,8 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Entity, LocationSpec } from '@backstage/catalog-model';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { AnnotateScmSlugEntityProcessor } from './AnnotateScmSlugEntityProcessor';
|
||||
|
||||
const config = new ConfigReader({
|
||||
/* integrations: {
|
||||
github: [
|
||||
{
|
||||
host: 'github.com',
|
||||
},
|
||||
],
|
||||
},*/
|
||||
});
|
||||
|
||||
describe('AnnotateScmSlugEntityProcessor', () => {
|
||||
describe('github', () => {
|
||||
it('adds annotation', async () => {
|
||||
@@ -32,7 +43,7 @@ describe('AnnotateScmSlugEntityProcessor', () => {
|
||||
'https://github.com/backstage/backstage/blob/master/catalog-info.yaml',
|
||||
};
|
||||
|
||||
const processor = new AnnotateScmSlugEntityProcessor();
|
||||
const processor = AnnotateScmSlugEntityProcessor.fromConfig(config);
|
||||
|
||||
expect(await processor.preProcessEntity(entity, location)).toEqual({
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
@@ -63,7 +74,7 @@ describe('AnnotateScmSlugEntityProcessor', () => {
|
||||
'https://github.com/backstage/backstage/blob/master/catalog-info.yaml',
|
||||
};
|
||||
|
||||
const processor = new AnnotateScmSlugEntityProcessor();
|
||||
const processor = AnnotateScmSlugEntityProcessor.fromConfig(config);
|
||||
|
||||
expect(await processor.preProcessEntity(entity, location)).toEqual({
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
@@ -91,14 +102,13 @@ describe('AnnotateScmSlugEntityProcessor', () => {
|
||||
'https://gitlab.com/backstage/backstage/-/blob/master/catalog-info.yaml',
|
||||
};
|
||||
|
||||
const processor = new AnnotateScmSlugEntityProcessor();
|
||||
const processor = AnnotateScmSlugEntityProcessor.fromConfig(config);
|
||||
|
||||
expect(await processor.preProcessEntity(entity, location)).toEqual({
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'Component',
|
||||
metadata: {
|
||||
name: 'my-component',
|
||||
annotations: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Entity, LocationSpec } from '@backstage/catalog-model';
|
||||
import { Config } from '@backstage/config';
|
||||
import {
|
||||
ScmIntegrationRegistry,
|
||||
ScmIntegrations,
|
||||
} from '@backstage/integration';
|
||||
import parseGitUrl from 'git-url-parse';
|
||||
import { identity, merge, pickBy } from 'lodash';
|
||||
import { CatalogProcessor } from './types';
|
||||
@@ -21,6 +26,16 @@ import { CatalogProcessor } from './types';
|
||||
const GITHUB_ACTIONS_ANNOTATION = 'github.com/project-slug';
|
||||
|
||||
export class AnnotateScmSlugEntityProcessor implements CatalogProcessor {
|
||||
constructor(
|
||||
private readonly opts: { scmIntegrationRegistry: ScmIntegrationRegistry },
|
||||
) {}
|
||||
|
||||
static fromConfig(config: Config): AnnotateScmSlugEntityProcessor {
|
||||
return new AnnotateScmSlugEntityProcessor({
|
||||
scmIntegrationRegistry: ScmIntegrations.fromConfig(config),
|
||||
});
|
||||
}
|
||||
|
||||
async preProcessEntity(
|
||||
entity: Entity,
|
||||
location: LocationSpec,
|
||||
@@ -29,11 +44,19 @@ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor {
|
||||
return entity;
|
||||
}
|
||||
|
||||
const scmIntegration = this.opts.scmIntegrationRegistry.byUrl(
|
||||
location.target,
|
||||
);
|
||||
|
||||
if (!scmIntegration || scmIntegration.type !== 'github') {
|
||||
return entity;
|
||||
}
|
||||
|
||||
const gitUrl = parseGitUrl(location.target);
|
||||
let githubProjectSlug =
|
||||
entity.metadata.annotations?.[GITHUB_ACTIONS_ANNOTATION];
|
||||
|
||||
if (gitUrl.source === 'github.com' && !githubProjectSlug) {
|
||||
if (!githubProjectSlug) {
|
||||
githubProjectSlug = `${gitUrl.owner}/${gitUrl.name}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user