Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding listGithubRepoContents() to lib #84

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
"errors": {
"fetchFail": "Failed to fetch contents: {{ errorMessage }}"
}
},
"listGitHubRepoContents": {
"errors": {
"fetchFail": "Failed to fetch contents: {{ errorMessage }}"
}
}
},
"hubdb": {
Expand Down
35 changes: 34 additions & 1 deletion lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs-extra';
import { throwError, throwErrorWithMessage } from '../errors/standardErrors';
import { extractZipArchive } from './archive';
import { logger } from './logging/logger';
import { BaseError } from '../types/Error';
import { GenericError, BaseError } from '../types/Error';
import { GithubReleaseData, GithubRepoFile } from '../types/Github';
import {
fetchRepoFile,
Expand Down Expand Up @@ -234,3 +234,36 @@ export async function downloadGithubRepoContents(
}
}
}

// Lists content from a public repository at the specified path
export async function listGithubRepoContents(
repoPath: RepoPath,
contentPath: string,
fileFilter?: 'file' | 'dir'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly typing the options here that are relevant to the response from fetching GitHub repo contents

): Promise<GithubRepoFile[]> {
try {
const { data: contentsResp } = await fetchRepoContents(
repoPath,
contentPath
);

const filteredFiles =
fileFilter && fileFilter != undefined
? contentsResp.filter(item => item.type === fileFilter)
: contentsResp;

return filteredFiles;
} catch (e) {
const error = e as GenericError;
if (error?.response?.data?.message) {
throwErrorWithMessage(
`${i18nKey}.downloadGithubRepoContents.errors.fetchFail`,
{
errorMessage: error.response.data.message,
}
);
} else {
throwError(error);
}
Comment on lines +257 to +267
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing the error with message here based on the github error response for fetching. Using GenericError as the type -- I'm not sure if it is preferred that I make a new type of Error - GithubErrorContext with the shape of the response to match. Above this function the downloadGithubRepoContents method is using BaseError as the type, but I would expect the response shape of the errors to be the same between these two. However, the error.error.message property path doesn't exist on the fetch fail response from GH.

}
}
4 changes: 2 additions & 2 deletions types/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Join<K, P> = K extends string | number
export type Leaves<T> = [10] extends [never]
? never
: T extends object
? { [K in keyof T]-?: Join<K, Leaves<T[K]>> }[keyof T]
: '';
? { [K in keyof T]-?: Join<K, Leaves<T[K]>> }[keyof T]
: '';
Loading