-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGitHubRepoParser.js
63 lines (57 loc) · 2.31 KB
/
GitHubRepoParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { Octokit } = require("@octokit/core");
class GitHubRepoParser {
constructor(GITHUB_API_KEY) {
this.GITHUB_API_KEY = GITHUB_API_KEY;
this.octokit = new Octokit({ auth: GITHUB_API_KEY });
}
getContentsUrl = async (pathsUrl) => {
return await this.octokit.request(`GET ${pathsUrl}`, {
owner: this.owner,
repo: this.repo,
tree_sha: this.GITHUB_API_KEY
})
.then(res => res.data)
}
gatherRawUrls = async (listOfFiles, level) => {
console.log(`Collecting Level #${++level} files.`)
listOfFiles.map(file => {
const dotIndex = file.download_url ? file.download_url.lastIndexOf('.') + 1 : file.download_url
let extension;
if (file.type === 'file') {
file.download_url.substring(dotIndex,).includes('/') ? extension = 'Miscellaneous' : extension = file.download_url.substring(dotIndex,)
this.rawUrls[extension] // if key exists in dictionary
?
this.rawUrls[extension].push(file.download_url)
:
this.rawUrls[extension] = [file.download_url]
}
})
if (listOfFiles.filter(file => file.type === 'dir').length === 0) {
return;
} else {
for (const dir of listOfFiles
.filter(file => file.type === 'dir')) {
await this.gatherRawUrls(await this.getContentsUrl(dir.url), level);
}
}
}
initializeRepoDetails = (url) => {
this.rawUrls = {};
this.url = url;
this.owner = url.split('https://github.com/')[1].split('/')[0];
this.repo = url.split('https://github.com/')[1].split('/')[1];
}
collectData = async (url) => {
await this.initializeRepoDetails(url);
await this.octokit.request(`GET /repos/${this.url.split('https://github.com/')[1]}`, {
owner: this.owner,
repo: this.repo,
tree_sha: this.GITHUB_API_KEY
})
.then(res => this.getContentsUrl(res.data.contents_url.split('{+path}')[0]))
.then(async listOfFiles => await this.gatherRawUrls(listOfFiles, 0))
.catch(e => console.log(e));
return this.rawUrls;
}
}
module.exports = GitHubRepoParser