-
Notifications
You must be signed in to change notification settings - Fork 0
/
unused.js
304 lines (276 loc) · 9.54 KB
/
unused.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
function isUrlFormat(repository) {
const urlRegex = /^(https?:\/\/)?([^\/]+)\/([^\/]+)\/([^\/]+)\/?$/;
return urlRegex.test(repository);
}
async function executeAddCommand(repositoryLink) {
console.log(repositoryLink)
const parsedRepo = parseIdentifier(repositoryLink)
console.log(typeof parsedRepo)
console.log(parsedRepo)
let repository, remote, branch;
try {
repository = parsedRepo.repository;
remote = parsedRepo.remote;
branch = parsedRepo.branch
}
catch (error) {
console.log(error)
console.log("There was an error processing the repository link. Please check your repository link again")
process.exit(-1)
}
if (typeof repository === 'undefined') {
console.log("Error: Invalid repository name. Enter github link, e.g. https://github.com/facebook/react")
process.exit(-1)
}
const processRepo = await getRepo(repository);
const repoInfo = await getRepoInfo(repository, remote, branch);
try {
if (debugMode) {
console.log(repoInfo)
}
if (repoInfo.responses[0]) {
//pass
}
else {
// Check whether this is supposed to be here
if (repoInfo.failed[0] && repoInfo.failed[0].repository == repository) {
if (repoInfo.failed[0].statusCode === 400) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Bad Request`);
} else if (repoInfo.failed[0].statusCode === 401) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Unauthorized`);
} else if (repoInfo.failed[0].statusCode === 404) {
if (repoInfo.failed[0].message && repoInfo.failed[0].message == "Repository not processed by Onboard.") {
// writeRepoToFile(repositoryLink);
const processRepo = await getRepo(repository);
if (debugMode) {
console.log(processRepo)
}
}
else {
console.log(`Error ${repoInfo.failed[0].statusCode}: Not Found`);
}
} else if (repoInfo.failed[0].statusCode === 500) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Internal Server Error`);
} else {
console.log(`Error ${repoInfo.failed[0].statusCode}: Unhandled Status Code`);
}
process.exit(1)
}
await getRepo(repository);
}
} catch (error) {
if (debugMode) { console.error(error) }
if (repoInfo.failed[0] && repoInfo.failed[0].repository == repository) {
if (repoInfo.failed[0].statusCode === 400) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Bad Request`);
} else if (repoInfo.failed[0].statusCode === 401) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Unauthorized`);
} else if (repoInfo.failed[0].statusCode === 404) {
if (repoInfo.failed[0].message && repoInfo.failed[0].message == "Repository not processed by Onboard.") {
// writeRepoToFile(repositoryLink);
const processRepo = await getRepo(repository);
if (debugMode) { console.log(processRepo) }
}
else {
console.log(`Error ${repoInfo.failed[0].statusCode}: Not Found`);
}
} else if (repoInfo.failed[0].statusCode === 500) {
console.log(`Error ${repoInfo.failed[0].statusCode}: Internal Server Error`);
} else {
console.log(`Error ${repoInfo.failed[0].statusCode}: Unhandled Status Code`);
}
process.exit(1)
}
}
// console.log(response)
// Write the updated session data back to the file
}
function writeRepoToFile(repositoryLink) {
let sessionData;
try {
const sessionFile = fs.readFileSync(sessionPath, 'utf-8');
sessionData = JSON.parse(sessionFile);
} catch (error) {
// If the file doesn't exist or has invalid JSON, start with an empty session
sessionData = {
repositories: []
};
}
// Check if the repository link already exists
if (!sessionData.repositories.includes(repositoryLink)) {
try {
sessionData.repositories.push(repositoryLink);
const sessionFile = JSON.stringify(sessionData, null, 2);
fs.writeFileSync(sessionPath, sessionFile, 'utf-8');
console.log(`Repository '${repositoryLink}' added to the session.`);
} catch (error) {
console.error('Error writing session data to file:', error);
}
} else {
console.log(`Repository '${repositoryLink}' already exists in the session.`);
}
}
function isAuthenticated() {
try {
const configFile = fs.readFileSync(configPath, 'utf-8');
const configFileData = JSON.parse(configFile)
if (configFileData.github.access_token != null) {
access_token = configFileData.github.access_token
return true;
}
else {
return false;
}
} catch (error) {
if (debugMode) {
console.log(error)
}
return {};
}
}
async function handleCreatingPullRequest({ octokit, payload }) {
async function createPullRequest(owner, repo, branchName, baseBranch, toAddFiles, title, body) {
try {
// Get the reference of the base branch
const baseRef = await octokit.rest.git.getRef({
owner,
repo,
ref: `heads/${baseBranch}`,
});
let newBranchRef;
try {
await octokit.rest.repos.getBranch({
owner,
repo,
branch: branchName,
});
// const newBranchRef = await octokit.rest.git.updateRef({
// owner,
// repo,
// ref: `heads/${branchName}`,
// sha: baseRef.data.object.sha,
// });
}
catch (error) {
console.log(error)
const newBranchRef = await octokit.rest.git.createRef({
owner,
repo,
ref: `refs/heads/${branchName}`,
sha: baseRef.data.object.sha,
});
}
// const existingFile = await octokit.rest.repos.getContent({
// owner,
// repo,
// path: filePath,
// ref: baseBranch,
// });
// Encode the content of the file
const content = Buffer.from(existingFile.data.content, 'base64').toString('utf-8');
// Make the desired changes to the file content
changes(content);
// Create a new blob with the updated content
const newBlob = await octokit.rest.git.createBlob({
owner,
repo,
content: Buffer.from(content).toString('base64'),
encoding: 'base64',
});
let newTreeContent = []
toAddFiles.forEach(async (file) => {
let newBlob = await octokit.rest.git.createBlob({
owner,
repo,
content: Buffer.from(file.updatedContent).toString('base64'),
encoding: 'base64',
});
newTreeContent.push({
path: file.path,
mode: '100644',
type: 'blob',
sha: newBlob.data.sha,
})
})
// Create a new tree with the updated blob
let newTree = await octokit.rest.git.createTree({
owner,
repo,
base_tree: baseRef.data.object.sha, // Use the SHA of the base tree
tree: newTreeContent
// tree: [{
// path: filePath,
// mode: '100644',
// type: 'blob',
// sha: newBlob.data.sha,
// }],
});
// Create a new commit with the updated tree
const newCommit = await octokit.rest.git.createCommit({
owner,
repo,
message: title,
tree: newTree.data.sha,
parents: [baseRef.data.object.sha],
});
// Update the reference of the new branch to the new commit
await octokit.rest.git.updateRef({
owner,
repo,
// ref: newBranchRef.data.ref.replace('ref/', ''),
ref: 'heads/random-2',
sha: newCommit.data.sha,
force: true
});
// Create the pull request
const pullRequest = await octokit.rest.pulls.create({
owner,
repo,
title,
body,
head: branchName,
base: baseBranch,
});
console.log("e")
// console.log(`Pull request created: ${pullRequest.data.html_url}`);
} catch (error) {
console.error(`Error creating pull request: ${error.message}`);
}
}
const repoOwner = 'dhruv317'; // GitHub username or organization name
const repoName = 'helicone'; // Name of your GitHub repository
function generateBranchName() {
const timestamp = new Date().getTime();
return `docs-${timestamp}`;
}
const branchName = generateBranchName();
const baseBranch = 'main'; // Replace with the base branch of your repository
const filePath = 'DIAGRAMS.md'; // Replace with the path to the file you want to modify
const title = 'Update DIAGRAMS.md content again';
const body = 'This pull request updates the content of the file.';
// Call the function to create a pull request
if (payload.pusher.name != 'new-docwriter-app[bot]') {
createPullRequest(repoOwner, repoName, branchName, baseBranch, toAddFiles, title, body);
}
}
async function getCommitInfo(commits) {
// Log information about each commit
commits.forEach((commit) => {
console.log(`Commit by ${commit.author.name} (${commit.author.email}):`);
console.log(` Message: ${commit.message}`);
console.log(` SHA: ${commit.id}`);
console.log(` Timestamp: ${commit.timestamp}`);
// Log details about file changes in the commit
console.log(" Changes:");
commit.added.forEach((addedFile) => {
console.log(` Added: ${addedFile}`);
});
commit.modified.forEach((modifiedFile) => {
console.log(` Modified: ${modifiedFile}`);
});
commit.removed.forEach((removedFile) => {
console.log(` Removed: ${removedFile}`);
});
console.log("---");
});
}