Skip to content

Commit

Permalink
fix: handle empty repository as if it has no files (#4863)
Browse files Browse the repository at this point in the history
* test: add test for empty repository

* fix: handle empty repository as if it has no files

* fix lint
  • Loading branch information
chingor13 authored Jan 5, 2023
1 parent 365b8f1 commit 9c1d60e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
40 changes: 26 additions & 14 deletions packages/git-file-utils/src/git-file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
// limitations under the License.

import {Octokit} from '@octokit/rest';
import {RequestError} from '@octokit/types';
import {basename, extname} from 'path';
import {Minimatch} from 'minimatch';
/* eslint-disable-next-line node/no-extraneous-import */
import {RequestError} from '@octokit/request-error';

export const DEFAULT_FILE_MODE = '100644';

Expand Down Expand Up @@ -420,19 +421,30 @@ export class BranchFileCache {
sha: string,
recursive: boolean
): Promise<TreeResponse> {
const {
data: {tree, truncated},
} = await this.octokit.git.getTree({
owner: this.repository.owner,
repo: this.repository.repo,
tree_sha: sha,
// fetching tree non-recursively requires omitting the param
recursive: recursive ? 'true' : undefined,
});
return {
tree,
truncated,
};
try {
const {
data: {tree, truncated},
} = await this.octokit.git.getTree({
owner: this.repository.owner,
repo: this.repository.repo,
tree_sha: sha,
// fetching tree non-recursively requires omitting the param
recursive: recursive ? 'true' : undefined,
});
return {
tree,
truncated,
};
} catch (e) {
if (e instanceof RequestError && e.status === 409) {
// handle empty repository
return {
tree: [],
truncated: false,
};
}
throw e;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/git-file-utils/test/git-file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ describe('BranchFileCache', () => {
}, BranchNotFoundError);
req.done();
});

it('throws on empty repository', async () => {
const req = nock('https://api.github.com')
.get(
'/repos/testOwner/testRepo/git/trees/feature-branch?recursive=true'
)
.reply(409);
await assert.rejects(async () => {
await cache.getFileContents('missing-file');
}, FileNotFoundError);
req.done();
});
});

describe('with large repository', () => {
Expand Down

0 comments on commit 9c1d60e

Please sign in to comment.