Skip to content

Commit f3f1812

Browse files
committed
Fix fetchRepo test case error (#29)
Signed-off-by: Dae❤️ <74119677+daeisbae@users.noreply.github.com>
1 parent 2d83552 commit f3f1812

File tree

3 files changed

+151
-114
lines changed

3 files changed

+151
-114
lines changed

src/github/fetchrepo.ts

Lines changed: 142 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,104 @@
1-
import axios from "axios";
2-
import { GithubAuthConfig } from "@/github/config";
1+
import axios from 'axios'
2+
import { GithubAuthConfig } from '@/github/config'
33

44
interface RepoResponse {
5-
owner: {
6-
login: string;
7-
};
8-
name: string;
9-
html_url: string;
10-
topics: string[];
11-
language: string;
12-
description: string;
13-
stargazers_count: number;
14-
forks_count: number;
15-
default_branch: string;
16-
pushed_at: Date;
5+
owner: {
6+
login: string
7+
}
8+
name: string
9+
html_url: string
10+
topics: string[]
11+
language: string
12+
description: string
13+
stargazers_count: number
14+
forks_count: number
15+
default_branch: string
16+
pushed_at: Date
1717
}
1818

1919
interface TreeResponse {
20-
sha: string;
21-
tree: TreeItem[];
20+
sha: string
21+
tree: TreeItem[]
2222
}
2323

2424
interface TreeItem {
25-
path: string;
26-
type: 'file' | 'dir';
27-
sha: string;
28-
url: string;
25+
path: string
26+
type: 'file' | 'dir'
27+
sha: string
28+
url: string
2929
}
3030

3131
interface RepoDetails {
32-
repoOwner: string;
33-
repoName: string;
34-
url: string;
35-
topics: string[];
36-
language: string;
37-
description: string;
38-
stars: number;
39-
forks: number;
40-
defaultBranch: string;
41-
sha: string;
42-
commitAt: Date;
32+
repoOwner: string
33+
repoName: string
34+
url: string
35+
topics: string[]
36+
language: string
37+
description: string
38+
stars: number
39+
forks: number
40+
defaultBranch: string
41+
sha: string
42+
commitAt: Date
4343
}
4444

4545
export interface RepoTreeResult {
46-
path: string;
47-
files: string[];
48-
subdirectories: RepoTreeResult[];
46+
path: string
47+
files: string[]
48+
subdirectories: RepoTreeResult[]
4949
}
5050

5151
/**
5252
* Fetches details about a GitHub repository
5353
* @param {string} owner - The repository owner
5454
* @param {string} repo - The repository name
55+
* @param {boolean} [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
5556
* @returns {Promise<{language: string, description: string, stars: number, forks: number, url: string, topics: string[], repo_owner: string, repo_name: string, default_branch: string, sha: string}>}
5657
*/
5758
export async function fetchGithubRepoDetails(
58-
owner: string,
59-
repo: string
59+
owner: string,
60+
repo: string,
61+
useAuth: boolean = false
6062
): Promise<RepoDetails> {
61-
const repoUrl = `https://api.github.com/repos/${owner}/${repo}`;
62-
63-
try {
64-
const repoResp = await axios.get<RepoResponse>(repoUrl, GithubAuthConfig);
65-
const repoData = repoResp.data;
66-
67-
const treeUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/${repoData.default_branch}`;
68-
const treeResp = await axios.get<TreeResponse>(treeUrl, GithubAuthConfig);
69-
const treeData = treeResp.data;
70-
71-
return {
72-
repoOwner: repoData.owner.login,
73-
repoName: repoData.name,
74-
url: repoData.html_url,
75-
topics: repoData.topics,
76-
language: repoData.language,
77-
description: repoData.description,
78-
stars: repoData.stargazers_count,
79-
forks: repoData.forks_count,
80-
defaultBranch: repoData.default_branch,
81-
sha: treeData.sha,
82-
commitAt: repoData.pushed_at
83-
};
84-
} catch (error) {
85-
if (axios.isAxiosError(error)) {
86-
throw new Error(`GitHub API Error: ${error.response?.data?.message || error.message}`);
63+
const repoUrl = `https://api.github.com/repos/${owner}/${repo}`
64+
65+
try {
66+
const repoResp = await axios.get<RepoResponse>(
67+
repoUrl,
68+
useAuth ? GithubAuthConfig : undefined
69+
)
70+
const repoData = repoResp.data
71+
72+
const treeUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/${repoData.default_branch}`
73+
const treeResp = await axios.get<TreeResponse>(
74+
treeUrl,
75+
useAuth ? GithubAuthConfig : undefined
76+
)
77+
const treeData = treeResp.data
78+
79+
return {
80+
repoOwner: repoData.owner.login,
81+
repoName: repoData.name,
82+
url: repoData.html_url,
83+
topics: repoData.topics,
84+
language: repoData.language,
85+
description: repoData.description,
86+
stars: repoData.stargazers_count,
87+
forks: repoData.forks_count,
88+
defaultBranch: repoData.default_branch,
89+
sha: treeData.sha,
90+
commitAt: repoData.pushed_at,
91+
}
92+
} catch (error) {
93+
if (axios.isAxiosError(error)) {
94+
throw new Error(
95+
`GitHub API Error: ${
96+
error.response?.data?.message || error.message
97+
}`
98+
)
99+
}
100+
throw error
87101
}
88-
throw error;
89-
}
90102
}
91103

92104
/**
@@ -95,40 +107,54 @@ export async function fetchGithubRepoDetails(
95107
* @param {string} repo - The repository name
96108
* @param {string} sha - The commit sha of the repository
97109
* @param {string} [path=''] - Optional path within repository
110+
* @param {boolean} [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
98111
* @returns {Promise<{path: string, files: Array<string>, subdirectories: Array}>}
99112
*/
100113
export async function fetchGithubRepoTree(
101-
owner: string,
102-
repo: string,
103-
sha: string,
104-
path: string = ''
114+
owner: string,
115+
repo: string,
116+
sha: string,
117+
path: string = '',
118+
useAuth: boolean = false
105119
): Promise<RepoTreeResult> {
106-
const contentsUrl = `https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${sha}`;
107-
108-
try {
109-
const { data } = await axios.get<TreeItem[]>(contentsUrl, GithubAuthConfig);
110-
const result: RepoTreeResult = {
111-
path,
112-
files: [],
113-
subdirectories: []
114-
};
115-
116-
for (const item of data) {
117-
if (item.type === 'file') {
118-
result.files.push(item.path);
119-
} else if (item.type === 'dir') {
120-
const subDir = await fetchGithubRepoTree(owner, repo, sha, item.path);
121-
result.subdirectories.push(subDir);
122-
}
123-
}
124-
125-
return result;
126-
} catch (error) {
127-
if (axios.isAxiosError(error)) {
128-
throw new Error(`Failed to fetch repo contents: ${error.response?.data?.message || error.message}`);
120+
const contentsUrl = `https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${sha}`
121+
122+
try {
123+
const { data } = await axios.get<TreeItem[]>(
124+
contentsUrl,
125+
useAuth ? GithubAuthConfig : undefined
126+
)
127+
const result: RepoTreeResult = {
128+
path,
129+
files: [],
130+
subdirectories: [],
131+
}
132+
133+
for (const item of data) {
134+
if (item.type === 'file') {
135+
result.files.push(item.path)
136+
} else if (item.type === 'dir') {
137+
const subDir = await fetchGithubRepoTree(
138+
owner,
139+
repo,
140+
sha,
141+
item.path
142+
)
143+
result.subdirectories.push(subDir)
144+
}
145+
}
146+
147+
return result
148+
} catch (error) {
149+
if (axios.isAxiosError(error)) {
150+
throw new Error(
151+
`Failed to fetch repo contents: ${
152+
error.response?.data?.message || error.message
153+
}`
154+
)
155+
}
156+
throw error
129157
}
130-
throw error;
131-
}
132158
}
133159

134160
/**
@@ -137,24 +163,32 @@ export async function fetchGithubRepoTree(
137163
* @param {string} repo - The repository name
138164
* @param {string} sha - The commit sha of the file to fetch
139165
* @param {string} path - The path of the file to fetch
166+
* @param {boolean} [useAuth=false] - Whether to use authentication for the request (for github rate limiting)
140167
* @returns {Promise<String>}
141168
*/
142169
export async function fetchGithubRepoFile(
143-
owner: string,
144-
repo: string,
145-
sha: string,
146-
path: string
170+
owner: string,
171+
repo: string,
172+
sha: string,
173+
path: string,
174+
useAuth: boolean = false
147175
): Promise<string> {
148-
const codeUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${sha}/${path}`;
149-
150-
try {
151-
const response = await axios.get<string>(codeUrl, GithubAuthConfig);
152-
return response.data;
153-
} catch (error) {
154-
if (axios.isAxiosError(error)) {
155-
throw new Error(`Failed to fetch file: ${error.response?.data?.message || error.message}`);
176+
const codeUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${sha}/${path}`
177+
178+
try {
179+
const response = await axios.get<string>(
180+
codeUrl,
181+
useAuth ? GithubAuthConfig : undefined
182+
)
183+
return response.data
184+
} catch (error) {
185+
if (axios.isAxiosError(error)) {
186+
throw new Error(
187+
`Failed to fetch file: ${
188+
error.response?.data?.message || error.message
189+
}`
190+
)
191+
}
192+
throw error
156193
}
157-
throw error;
158-
}
159194
}
160-

src/service/insert-db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class InsertRepoService {
2222
owner: string,
2323
repo: string,
2424
): Promise<RepositoryData | null> {
25-
const repoDetails = await fetchGithubRepoDetails(owner, repo)
25+
const repoDetails = await fetchGithubRepoDetails(owner, repo, true)
2626
const repositoryData = await this.repository.insert(repoDetails.url, repoDetails.repoOwner, repoDetails.repoName, repoDetails.language, repoDetails.description, repoDetails.defaultBranch, repoDetails.topics, repoDetails.stars, repoDetails.forks)
2727

2828
if(!repositoryData) {
@@ -35,7 +35,7 @@ export class InsertRepoService {
3535

3636
const branchId = branchCommit.branch_id
3737

38-
const tree = await fetchGithubRepoTree(owner, repo, repoDetails.sha)
38+
const tree = await fetchGithubRepoTree(owner, repo, repoDetails.sha, '', true)
3939

4040
await this.recursiveInsertFolder(owner, repo, sha, tree, branchId, null)
4141

@@ -61,7 +61,7 @@ export class InsertRepoService {
6161

6262
for(const file of allowedFiles) {
6363
console.log(`Inserting file ${file}`)
64-
await this.insertFile(file, folderData.folder_id, await fetchGithubRepoFile(owner, repo, sha, file))
64+
await this.insertFile(file, folderData.folder_id, await fetchGithubRepoFile(owner, repo, sha, file, true))
6565
}
6666
for(const subfolder of allowedFolders) {
6767
console.log(`Traversing and Inserting folder ${subfolder.path}`)

src/test/github/filterfile.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { whitelistedFile } from '@/github/filterfile'
22

3-
describe('filterFile', () => {
3+
describe('whitelistedFile', () => {
44
it('should allow files that match a simple pattern', () => {
55
const files = ['index.js', 'README.md', 'node_modules/library.js']
66
const regexFilter = ['node_modules']
7-
const result = filterFile(files, regexFilter)
7+
const result = whitelistedFile(files, regexFilter)
88
expect(result).toEqual(['node_modules/library.js'])
99
})
1010

@@ -24,7 +24,10 @@ describe('filterFile', () => {
2424
]
2525
const regexFilter = ['node_modules', '\\.github']
2626
const result = whitelistedFile(files, regexFilter)
27-
expect(result).toEqual(['index.js', 'README.md'])
27+
expect(result).toEqual([
28+
'.github/config.yml',
29+
'node_modules/library.js',
30+
])
2831
})
2932

3033
it('should remove all files if regex patterns do not match any file', () => {

0 commit comments

Comments
 (0)