Merge pull request #7086 from backstage/blam/root-fodlers

fix `subpath` filtering for `AzureDevOps` `readTree`
This commit is contained in:
Ben Lambert
2021-09-08 11:45:29 +02:00
committed by GitHub
5 changed files with 26 additions and 49 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Fixing issue with `AzureUrlReader` that doesn't do `subpath` directories correctly
@@ -220,6 +220,21 @@ describe('AzureUrlReader', () => {
expect(indexMarkdownFile.toString()).toBe('# Test\n');
});
it('returns the wanted files from an archive when a subpath is passed through', async () => {
const response = await processor.readTree(
'https://dev.azure.com/organization/project/_git/repository?path=%2Fdocs',
);
expect(response.etag).toBe('123abc2');
const files = await response.files();
expect(files.length).toBe(1);
const indexMarkdownFile = await files[0].content();
expect(indexMarkdownFile.toString()).toBe('# Test\n');
});
it('creates a directory with the wanted files', async () => {
const response = await processor.readTree(
'https://dev.azure.com/organization/project/_git/repository',
@@ -94,8 +94,6 @@ export class AzureUrlReader implements UrlReader {
url: string,
options?: ReadTreeOptions,
): Promise<ReadTreeResponse> {
// TODO: Support filepath based reading tree feature like other providers
// Get latest commit SHA
const commitsAzureResponse = await fetch(
@@ -129,10 +127,13 @@ export class AzureUrlReader implements UrlReader {
throw new Error(message);
}
const { filepath } = parseGitUrl(url);
return await this.deps.treeResponseFactory.fromZipArchive({
stream: archiveAzureResponse.body as unknown as Readable,
etag: commitSha,
filter: options?.filter,
subpath: filepath,
});
}
@@ -62,36 +62,6 @@ describe('ZipArchiveResponse', () => {
]);
});
it('should read files and strip root dir if requested', async () => {
const stream = fs.createReadStream('/test-archive-with-extra-root-dir.zip');
const res = new ZipArchiveResponse(
stream,
'',
'/tmp',
'etag',
undefined,
true,
);
const files = await res.files();
expect(files).toEqual([
{
path: 'mkdocs.yml',
content: expect.any(Function),
},
{
path: 'docs/index.md',
content: expect.any(Function),
},
]);
const contents = await Promise.all(files.map(f => f.content()));
expect(contents.map(c => c.toString('utf8').trim())).toEqual([
'site_name: Test',
'# Test',
]);
});
it('should read files with filter', async () => {
const stream = fs.createReadStream('/test-archive.zip');
@@ -24,7 +24,6 @@ import {
ReadTreeResponseDirOptions,
ReadTreeResponseFile,
} from '../types';
import { stripFirstDirectoryFromPath } from './util';
/**
* Wraps a zip archive stream into a tree response reader.
@@ -38,7 +37,6 @@ export class ZipArchiveResponse implements ReadTreeResponse {
private readonly workDir: string,
public readonly etag: string,
private readonly filter?: (path: string, info: { size: number }) => boolean,
private readonly stripFirstDirectory?: boolean,
) {
if (subPath) {
if (!subPath.endsWith('/')) {
@@ -68,12 +66,8 @@ export class ZipArchiveResponse implements ReadTreeResponse {
}
private shouldBeIncluded(entry: Entry): boolean {
const strippedPath = this.stripFirstDirectory
? stripFirstDirectoryFromPath(entry.path)
: entry.path;
if (this.subPath) {
if (!strippedPath.startsWith(this.subPath)) {
if (!entry.path.startsWith(this.subPath)) {
return false;
}
}
@@ -102,11 +96,7 @@ export class ZipArchiveResponse implements ReadTreeResponse {
if (this.shouldBeIncluded(entry)) {
files.push({
path: this.getInnerPath(
this.stripFirstDirectory
? stripFirstDirectoryFromPath(entry.path)
: entry.path,
),
path: this.getInnerPath(entry.path),
content: () => entry.buffer(),
});
} else {
@@ -154,11 +144,7 @@ export class ZipArchiveResponse implements ReadTreeResponse {
// Ignore directory entries since we handle that with the file entries
// as a zip can have files with directories without directory entries
if (entry.type === 'File' && this.shouldBeIncluded(entry)) {
const entryPath = this.getInnerPath(
this.stripFirstDirectory
? stripFirstDirectoryFromPath(entry.path)
: entry.path,
);
const entryPath = this.getInnerPath(entry.path);
const dirname = platformPath.dirname(entryPath);
if (dirname) {
await fs.mkdirp(platformPath.join(dir, dirname));