Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5149 from goober/feature/bitbucket-discovery
Add a Bitbucket Discovery processor
- Loading branch information
Showing
9 changed files
with
552 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@backstage/plugin-catalog-backend': patch | ||
--- | ||
|
||
Add Bitbucket Server discovery processor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
id: discovery | ||
title: Bitbucket Discovery | ||
sidebar_label: Discovery | ||
description: | ||
Automatically discovering catalog entities from repositories in Bitbucket | ||
--- | ||
|
||
The Bitbucket integration has a special discovery processor for discovering | ||
catalog entities located in Bitbucket. The processor will crawl your Bitbucket | ||
account and register entities matching the configured path. This can be useful | ||
as an alternative to static locations or manually adding things to the catalog. | ||
|
||
> Note: The Bitbucket Discovery Processor currently only supports a self-hosted | ||
> Bitbucket Server, and not the hosted Bitbucket Cloud product. | ||
To use the discovery processor, you'll need a Bitbucket integration | ||
[set up](locations.md) with a `BITBUCKET_TOKEN` and a `BITBUCKET_API_BASE_URL`. | ||
Then you can add a location target to the catalog configuration: | ||
|
||
```yaml | ||
catalog: | ||
locations: | ||
- type: bitbucket-discovery | ||
target: https://bitbucket.mycompany.com/projects/my-project/repos/service-*/catalog-info.yaml | ||
``` | ||
|
||
Note the `bitbucket-discovery` type, as this is not a regular `url` processor. | ||
|
||
The target is composed of four parts: | ||
|
||
- The base instance URL, `https://bitbucket.mycompany.com` in this case | ||
- The project key to scan, which accepts \* wildcard tokens. This can simply be | ||
`*` to scan repositories from all projects. This example only scans for | ||
repositories in the `my-project` project. | ||
- The repository blob to scan, which accepts \* wildcard tokens. This can simply | ||
be `*` to scan all repositories in the project. This example only looks for | ||
repositories prefixed with `service-`. | ||
- The path within each repository to find the catalog YAML file. This will | ||
usually be `/catalog-info.yaml` or a similar variation for catalog files | ||
stored in the root directory of each repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
plugins/catalog-backend/src/ingestion/processors/BitbucketDiscoveryProcessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/* | ||
* Copyright 2021 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { getVoidLogger } from '@backstage/backend-common'; | ||
import { | ||
BitbucketDiscoveryProcessor, | ||
readBitbucketOrg, | ||
} from './BitbucketDiscoveryProcessor'; | ||
import { ConfigReader } from '@backstage/config'; | ||
import { LocationSpec } from '@backstage/catalog-model'; | ||
import { BitbucketClient, PagedResponse } from './bitbucket'; | ||
|
||
function pagedResponse(values: any): PagedResponse<any> { | ||
return { | ||
values: values, | ||
isLastPage: true, | ||
} as PagedResponse<any>; | ||
} | ||
|
||
describe('BitbucketDiscoveryProcessor', () => { | ||
const client: jest.Mocked<BitbucketClient> = { | ||
listProjects: jest.fn(), | ||
listRepositories: jest.fn(), | ||
} as any; | ||
|
||
afterEach(() => jest.resetAllMocks()); | ||
|
||
describe('reject unrelated entries', () => { | ||
it('rejects unknown types', async () => { | ||
const processor = BitbucketDiscoveryProcessor.fromConfig( | ||
new ConfigReader({ | ||
integrations: { | ||
bitbucket: [{ host: 'bitbucket.mycompany.com', token: 'blob' }], | ||
}, | ||
}), | ||
{ logger: getVoidLogger() }, | ||
); | ||
const location: LocationSpec = { | ||
type: 'not-bitbucket-discovery', | ||
target: 'https://bitbucket.mycompany.com', | ||
}; | ||
await expect( | ||
processor.readLocation(location, false, () => {}), | ||
).resolves.toBeFalsy(); | ||
}); | ||
|
||
it('rejects unknown targets', async () => { | ||
const processor = BitbucketDiscoveryProcessor.fromConfig( | ||
new ConfigReader({ | ||
integrations: { | ||
bitbucket: [ | ||
{ host: 'bitbucket.org', token: 'blob' }, | ||
{ host: 'bitbucket.mycompany.com', token: 'blob' }, | ||
], | ||
}, | ||
}), | ||
{ logger: getVoidLogger() }, | ||
); | ||
const location: LocationSpec = { | ||
type: 'bitbucket-discovery', | ||
target: 'https://not.bitbucket.mycompany.com/foobar', | ||
}; | ||
await expect( | ||
processor.readLocation(location, false, () => {}), | ||
).rejects.toThrow( | ||
/There is no Bitbucket integration that matches https:\/\/not.bitbucket.mycompany.com\/foobar/, | ||
); | ||
}); | ||
}); | ||
|
||
describe('handles repositories', () => { | ||
it('output all repositories', async () => { | ||
const target = | ||
'https://bitbucket.mycompany.com/projects/*/repos/*/catalog.yaml'; | ||
|
||
client.listProjects.mockResolvedValue( | ||
pagedResponse([{ key: 'backstage' }, { key: 'demo' }]), | ||
); | ||
client.listRepositories.mockResolvedValueOnce( | ||
pagedResponse([ | ||
{ | ||
slug: 'backstage', | ||
links: { | ||
self: [ | ||
{ | ||
href: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/backstage/browse', | ||
}, | ||
], | ||
}, | ||
}, | ||
]), | ||
); | ||
client.listRepositories.mockResolvedValueOnce( | ||
pagedResponse([ | ||
{ | ||
slug: 'demo', | ||
links: { | ||
self: [ | ||
{ | ||
href: | ||
'https://bitbucket.mycompany.com/projects/demo/repos/demo/browse', | ||
}, | ||
], | ||
}, | ||
}, | ||
]), | ||
); | ||
|
||
const actual = await readBitbucketOrg(client, target); | ||
expect(actual.scanned).toBe(2); | ||
expect(actual.matches).toContainEqual({ | ||
type: 'url', | ||
target: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/backstage/browse/catalog.yaml', | ||
}); | ||
expect(actual.matches).toContainEqual({ | ||
type: 'url', | ||
target: | ||
'https://bitbucket.mycompany.com/projects/demo/repos/demo/browse/catalog.yaml', | ||
}); | ||
}); | ||
|
||
it('output repositories with wildcards', async () => { | ||
const target = | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/techdocs-*/catalog.yaml'; | ||
|
||
client.listProjects.mockResolvedValue( | ||
pagedResponse([{ key: 'backstage' }]), | ||
); | ||
client.listRepositories.mockResolvedValueOnce( | ||
pagedResponse([ | ||
{ slug: 'backstage' }, | ||
{ | ||
slug: 'techdocs-cli', | ||
links: { | ||
self: [ | ||
{ | ||
href: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/techdocs-cli/browse', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
slug: 'techdocs-container', | ||
links: { | ||
self: [ | ||
{ | ||
href: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/techdocs-container/browse', | ||
}, | ||
], | ||
}, | ||
}, | ||
]), | ||
); | ||
|
||
const actual = await readBitbucketOrg(client, target); | ||
expect(actual.scanned).toBe(3); | ||
expect(actual.matches).toContainEqual({ | ||
type: 'url', | ||
target: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/techdocs-cli/browse/catalog.yaml', | ||
}); | ||
expect(actual.matches).toContainEqual({ | ||
type: 'url', | ||
target: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/techdocs-container/browse/catalog.yaml', | ||
}); | ||
}); | ||
it('filter unrelated repositories', async () => { | ||
const target = | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/test/catalog.yaml'; | ||
|
||
client.listProjects.mockResolvedValue( | ||
pagedResponse([{ key: 'backstage' }]), | ||
); | ||
client.listRepositories.mockResolvedValue( | ||
pagedResponse([ | ||
{ slug: 'abstest' }, | ||
{ slug: 'testxyz' }, | ||
{ | ||
slug: 'test', | ||
links: { | ||
self: [ | ||
{ | ||
href: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/test', | ||
}, | ||
], | ||
}, | ||
}, | ||
]), | ||
); | ||
|
||
const actual = await readBitbucketOrg(client, target); | ||
expect(actual.scanned).toBe(3); | ||
expect(actual.matches).toContainEqual({ | ||
type: 'url', | ||
target: | ||
'https://bitbucket.mycompany.com/projects/backstage/repos/test/catalog.yaml', | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.