-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.js
169 lines (159 loc) · 3.88 KB
/
generator.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/** Libraries */
const { Octokit } = require('octokit')
const fs = require('fs')
require('dotenv').config()
/** Config and Environment Variables */
const config = JSON.parse(fs.readFileSync('config.json'))
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
const octokit = new Octokit({ auth: GITHUB_TOKEN }) // Official clients for the GitHub API
/** Generate Data (nodes.json and edges.json) */
;(async function () {
/** GenerateEdgesandNodes Function */
const GenerateEdgesandNodes = (data, nodes, edges) => {
let root = nodes[0]
/** Nodes */
let counter = root.id + 1
data.map((v) => {
nodes.push({
id: counter,
label: v.name,
value: v.stargazers_count || 0,
url: v.html_url || v.url,
group: counter,
})
counter++
})
/** Edges */
edges = nodes.map((v) => ({ from: v.id, to: root.id }))
edges.splice(0, 1)
return { nodes, edges }
}
/** User Graph */
const UserGraph = async () => {
let nodes = [],
edges = []
let { username } = config
let { data } = await octokit.request('GET /users/{username}/repos', {
username,
})
nodes.push({ id: 0, label: config.username, group: 1 })
edges.push({ from: 0, to: nodes.length })
data = GenerateEdgesandNodes(
data,
[
{
id: nodes.length,
label: `Repository`,
group: nodes.length,
},
],
[]
)
nodes.push(...data.nodes)
edges.push(...data.edges)
return { nodes, edges }
}
/**
* Organization Graph
* @param data Consists of nodes and edges data from user
* */
const OrganizationsGraph = async (data) => {
let { nodes, edges } = data
let { organizations } = config
for (let i = 0; i < organizations.length; i++) {
let org = organizations[i]
let { data } = await octokit.request('GET /orgs/{org}/repos', { org })
data = GenerateEdgesandNodes(
data,
[
{
id: nodes.length,
label: org,
url: `https://github.com/${org}`,
group: nodes.length,
},
],
[]
)
edges.push({ from: 1, to: nodes.length })
nodes.push(...data.nodes)
edges.push(...data.edges)
}
return { nodes, edges }
}
/** Contribution Graph */
const ContributionsGraph = async (data) => {
let { nodes, edges } = data
let { contributions } = config
for (const [owner, repos] of Object.entries(contributions)) {
let RepoData = []
for (let i = 0; i < repos.length; i++) {
let repo = repos[i]
let { data } = await octokit.request('GET /repos/{owner}/{repo}', {
owner,
repo,
})
RepoData.push(data)
}
edges.push({ from: 1, to: nodes.length })
let EdgesandNodes = GenerateEdgesandNodes(
RepoData,
[
{
id: nodes.length,
label: owner,
url: `https://github.com/${owner}`,
group: nodes.length,
},
],
[]
)
nodes.push(...EdgesandNodes.nodes)
edges.push(...EdgesandNodes.edges)
}
return { nodes, edges }
}
/** Experience Graph */
const ExperienceGraph = async (data) => {
let { nodes, edges } = data
let { experiences } = config
edges.push({ from: 0, to: nodes.length })
let portfolioIndex = nodes.length
nodes.push({
id: nodes.length,
label: `Experiences`,
group: nodes.length,
})
experiences.map((ex) => {
let data = GenerateEdgesandNodes(
ex.portfolio,
[
{
id: nodes.length,
label: ex.organization,
url: ex.url,
group: nodes.length,
},
],
[]
)
edges.push({ from: portfolioIndex, to: nodes.length })
nodes.push(...data.nodes)
edges.push(...data.edges)
})
return { nodes, edges }
}
/** Generate Data */
let data = await UserGraph()
data = await OrganizationsGraph(data)
data = await ContributionsGraph(data)
data = await ExperienceGraph(data)
/** Write to File */
try {
fs.writeFileSync('nodes.json', JSON.stringify(data.nodes))
fs.writeFileSync('edges.json', JSON.stringify(data.edges))
console.log('✅ The file has been saved!')
} catch (e) {
console.log(e)
}
})()