Handle missing commits in GitlabUrlReader.readTree

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-12-08 11:23:08 +01:00
parent 6ac7bb92de
commit 840f2113c6
3 changed files with 43 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Fix `GitlabUrlReader.readTree` bug when there were no matching commits
@@ -229,22 +229,28 @@ describe('GitlabUrlReader', () => {
path.resolve(__dirname, '__fixtures__/gitlab-archive.tar.gz'),
);
const projectGitlabApiResponse = {
id: 11111111,
default_branch: 'main',
};
let projectGitlabApiResponse: any;
let commitsGitlabApiResponse: any;
let specificPathCommitsGitlabApiResponse: any;
const commitsGitlabApiResponse = [
{
id: 'sha123abc',
},
];
beforeEach(() => {
projectGitlabApiResponse = {
id: 11111111,
default_branch: 'main',
};
const specificPathCommitsGitlabApiResponse = [
{
id: 'sha456def',
},
];
commitsGitlabApiResponse = [
{
id: 'sha123abc',
},
];
specificPathCommitsGitlabApiResponse = [
{
id: 'sha456def',
},
];
});
beforeEach(() => {
worker.use(
@@ -494,6 +500,23 @@ describe('GitlabUrlReader', () => {
};
await expect(fnGitlab).rejects.toThrow(NotFoundError);
});
it('should gracefully handle no matching commits', async () => {
commitsGitlabApiResponse = [];
const response = await gitlabProcessor.readTree(
'https://gitlab.com/backstage/mock/tree/main',
);
const files = await response.files();
expect(files.length).toBe(2);
const indexMarkdownFile = await files[0].content();
const mkDocsFile = await files[1].content();
expect(mkDocsFile.toString()).toBe('site_name: Test\n');
expect(indexMarkdownFile.toString()).toBe('# Test\n');
});
});
describe('search', () => {
@@ -188,8 +188,7 @@ export class GitlabUrlReader implements UrlReader {
throw new Error(message);
}
const commitSha = (await commitsGitlabResponse.json())[0].id;
const commitSha = (await commitsGitlabResponse.json())[0]?.id ?? '';
if (etag && etag === commitSha) {
throw new NotModifiedError();
}