catalog-backend: make UrlReaderProcessor backwards-compatible with old location types

This commit is contained in:
Patrik Oldsberg
2020-10-02 17:00:38 +02:00
parent 07d19aad96
commit 4978f06514
2 changed files with 31 additions and 4 deletions
@@ -88,7 +88,12 @@ export class LocationReaders implements LocationReader {
new BitbucketApiReaderProcessor(config),
new AzureApiReaderProcessor(config),
GithubOrgReaderProcessor.fromConfig(config),
options.reader ? new UrlReaderProcessor(options.reader) : [],
options.reader
? new UrlReaderProcessor({
reader: options.reader,
logger: options.logger,
})
: [],
new YamlProcessor(),
PlaceholderProcessor.default(),
new CodeOwnersProcessor(),
@@ -14,25 +14,47 @@
* limitations under the License.
*/
import { Logger } from 'winston';
import { UrlReader } from '@backstage/backend-common';
import { LocationSpec } from '@backstage/catalog-model';
import * as result from './results';
import { LocationProcessor, LocationProcessorEmit } from './types';
// TODO(Rugvip): Added for backwards compatibility when moving to UrlReader, this
// can be removed in a bit
const deprecatedTypes = [
'github',
'github/api',
'bitbucket/api',
'gitlab/api',
'azure/api',
];
type Options = {
reader: UrlReader;
logger: Logger;
};
export class UrlReaderProcessor implements LocationProcessor {
constructor(private readonly reader: UrlReader) {}
constructor(private readonly options: Options) {}
async readLocation(
location: LocationSpec,
optional: boolean,
emit: LocationProcessorEmit,
): Promise<boolean> {
if (location.type !== 'url') {
if (deprecatedTypes.includes(location.type)) {
// TODO(Rugvip): Let's not enable this warning yet, as we want to move over the example YAMLs
// in this repo to use the 'url' type first.
// this.options.logger.warn(
// `Using deprecated location type '${location.type}' for '${location.target}', use 'url' instead`,
// );
} else if (location.type !== 'url') {
return false;
}
try {
const data = await this.reader.read(location.target);
const data = await this.options.reader.read(location.target);
emit(result.data(location, data));
} catch (error) {
const message = `Unable to read ${location.type}, ${error}`;