Pass processor logger to repository parser

Signed-off-by: Mathias Åhsberg <mathias.ahsberg@resurs.se>
This commit is contained in:
Mathias Åhsberg
2021-04-28 15:52:33 +00:00
parent d5b892d041
commit 34a4aa2c1e
5 changed files with 31 additions and 51 deletions
@@ -22,11 +22,11 @@ import {
} from '@backstage/integration';
import { LocationSpec } from '@backstage/catalog-model';
import {
Bitbucket,
BitbucketRepositoryParser,
BitbucketClient,
defaultRepositoryParser,
paginated,
BitbucketRepository,
} from './bitbucket';
import { CatalogProcessor, CatalogProcessorEmit } from './types';
@@ -66,20 +66,19 @@ export class BitbucketDiscoveryProcessor implements CatalogProcessor {
return false;
}
const bitbucketConfig = this.integrations.bitbucket.byUrl(location.target)
?.config;
if (!bitbucketConfig) {
const integration = this.integrations.bitbucket.byUrl(location.target);
if (!integration) {
throw new Error(
`There is no Bitbucket integration that matches ${location.target}. Please add a configuration entry for it under integrations.bitbucket`,
);
} else if (bitbucketConfig.host === 'bitbucket.org') {
} else if (integration.config.host === 'bitbucket.org') {
throw new Error(
`Component discovery for Bitbucket Cloud is not yet supported`,
);
}
const client = new BitbucketClient({
config: bitbucketConfig,
config: integration.config,
});
const startTimestamp = Date.now();
this.logger.info(`Reading Bitbucket repositories from ${location.target}`);
@@ -90,9 +89,9 @@ export class BitbucketDiscoveryProcessor implements CatalogProcessor {
for (const repository of result.matches) {
for await (const entity of this.parser({
client: client,
repository: repository,
path: catalogPath,
integration: integration,
target: `${repository.links.self[0]}${catalogPath}`,
logger: this.logger,
})) {
emit(entity);
}
@@ -159,5 +158,5 @@ function escapeRegExp(str: string): RegExp {
type Result = {
scanned: number;
matches: Bitbucket.Repository[];
matches: BitbucketRepository[];
};
@@ -14,9 +14,9 @@
* limitations under the License.
*/
import { defaultRepositoryParser } from './BitbucketRepositoryParser';
import { Bitbucket } from './types';
import { BitbucketClient } from './client';
import { results } from '../index';
import { getVoidLogger } from '@backstage/backend-common';
import { BitbucketIntegration } from '@backstage/integration';
describe('BitbucketRepositoryParser', () => {
describe('defaultRepositoryParser', () => {
@@ -34,15 +34,9 @@ describe('BitbucketRepositoryParser', () => {
),
];
const actual = await defaultRepositoryParser({
client: {} as BitbucketClient,
repository: {
project: {} as Bitbucket.Project,
slug: 'repo-slug',
links: {
self: [{ href: browseUrl }],
},
} as Bitbucket.Repository,
path: path,
integration: {} as BitbucketIntegration,
target: `${browseUrl}${path}`,
logger: getVoidLogger(),
});
let i = 0;
@@ -13,25 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Bitbucket } from './types';
import { CatalogProcessorResult } from '../types';
import { results } from '../index';
import { BitbucketClient } from './client';
import { Logger } from 'winston';
import { BitbucketIntegration } from '@backstage/integration';
export type BitbucketRepositoryParser = (options: {
client: BitbucketClient;
repository: Bitbucket.Repository;
path: string;
integration: BitbucketIntegration;
target: string;
logger: Logger;
}) => AsyncIterable<CatalogProcessorResult>;
export const defaultRepositoryParser: BitbucketRepositoryParser = async function* defaultRepositoryParser({
repository,
path,
target,
}) {
yield results.location(
{
type: 'url',
target: `${repository.links.self[0].href}${path}`,
target: target,
},
// Not all locations may actually exist, since the user defined them as a wildcard pattern.
// Thus, we emit them as optional and let the downstream processor find them while not outputting
@@ -41,15 +41,6 @@ export class BitbucketClient {
);
}
async getRaw(
projectKey: string,
repo: string,
path: string,
): Promise<Response> {
const request = `${this.config.apiBaseUrl}/projects/${projectKey}/repos/${repo}/raw/${path}`;
return fetch(request, getBitbucketRequestOptions(this.config));
}
private async pagedRequest(
endpoint: string,
options?: ListOptions,
@@ -13,18 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export namespace Bitbucket {
export type Project = {
export type BitbucketRepository = {
project: {
key: string;
};
export type Repository = {
project: Project;
slug: string;
links: Record<string, Link[]>;
};
export type Link = {
href: string;
};
}
slug: string;
links: Record<
string,
{
href: string;
}[]
>;
};