-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
289 lines (242 loc) · 8.28 KB
/
index.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
const axios = require("axios");
/**
* Handles interactions between the client and a GitHub repository.
*/
class GitHandler {
static gitPlatforms = {
github: "https://api.github.com/repos/:owner/:repo",
gitlab: "https://gitlab.com/api/v4/projects/:repo",
bitbucket: "https://api.bitbucket.org/2.0/repositories/:owner/:repo",
azuredevops: "https://dev.azure.com/:owner/repositories/:repo",
};
/**
* @param {object} gitApiRepoPath Your Git platform's repo api url. The `GitHandler.gitPlatforms` object contains several common paths.
* @param {string} authToken The authorization key provided by your Git platform.
* @param {string} repositoryOwner The name of the repository owner.
* @param {string} repositoryName The repository's name.
*/
constructor(
gitApiRepoPath = this.constructor.gitPlatforms.github,
authToken,
repositoryOwner,
repositoryName
) {
// // Usage example
// createFile("new-file.txt", "File content")
// .then(() => commitAndPushChanges("main", "Added a new file"))
// .catch((error) => console.error(error));
this.gitApiRepoPath = gitApiRepoPath;
this.authToken = authToken;
this.repositoryOwner = repositoryOwner;
this.repositoryName =
repositoryName &&
(repositoryName.endsWith(".git")
? repositoryName.replace(".git", "")
: repositoryName);
}
get repositoryURL() {
try {
const { gitApiRepoPath, repositoryOwner, repositoryName } = this;
const customPath = !Object.values(
this.constructor.gitPlatforms
).includes(gitApiRepoPath);
const updatedPath = customPath
? gitApiRepoPath
: gitApiRepoPath
.replace(/:owner/g, repositoryOwner)
.replace(/:repo/g, repositoryName);
return `${updatedPath}`;
} catch (err) {
console.error("Failed to determine repository URL:", err);
}
}
/**
* Handle caught errors.
* @param {Array<*>} errors The array of error messages or objects that were thrown.
*/
errorHandler = (...errors) => {
const errorMapper = (error) => {
if (typeof error === "string") return error;
else {
if (error.isAxiosError) {
return error.response.data.message;
} else return error.toString();
}
};
const errorMap = errors.map((error) => errorMapper(error)).join(" ");
console.error(errorMap);
return errorMap;
};
/**
* Makes an authenticated API request to the Git hosting platform.
* @param {string} url The URL of the API endpoint.
* @param {string} method The HTTP method for the request (e.g., GET, POST, PATCH).
* @param {Object} [data] The request payload data (optional).
* @returns {Promise<any>} A Promise that resolves with the response data.
* @throws {Error} If there was an error making the API request or if the response indicates an error.
*/
apiRequest = async (url, method, data = {}) => {
const { authToken } = this;
const response = await axios({
method,
url,
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
data,
});
return response.data;
};
/**
* Creates a new file in the Git repository.
* @param {string} filePath The path of the file to create.
* @param {string} content The content of the file.
* @param {boolean} overwrite Set to `true` to overwrite existing files.
* @param {string} message An optional message to attach to the commit.
* @returns {Promise<void>} A Promise that resolves when the file is created successfully.
*/
commitFileAndPush = async (
filePath,
content,
overwrite = false,
message = "Created new file."
) => {
try {
const fileExists = await this.doesFileExist(filePath);
if (!overwrite && fileExists)
throw "A file with that name already exists in that directory.";
const { repositoryURL } = this;
const fileContent = Buffer.from(content).toString("base64");
const apiUrl = `${repositoryURL}/contents/${filePath}`;
const data = {
message: message,
content: fileContent,
sha: fileExists,
};
const response = await this.apiRequest(apiUrl, "PUT", data);
console.log(
`File ${fileExists ? "updated" : "created"}:`,
response.commit.sha
);
} catch (err) {
this.errorHandler("Error creating file:", err);
}
};
/**
* Checks if a file exists in the Git repository.
* @param {string} filePath The path of the file to check.
* @returns {Promise<FileCheckResult>} A Promise that resolves with the result of the file check.
*/
doesFileExist = async (filePath) => {
try {
const { repositoryURL } = this;
const apiUrl = `${repositoryURL}/contents/${filePath}`;
const { sha } = await this.apiRequest(apiUrl, "GET");
return sha;
} catch (err) {
// If a 404 is provided, that means the file doesn't exist.
if (err.response && err.response.status === 404) {
return false;
}
this.errorHandler("Error checking file:", err);
}
};
/**
* Retrieves the contents of a file from the Git repository.
* @param {string} filePath The path of the file to retrieve.
* @returns {Promise<string>} A Promise that resolves with the contents of the file.
*/
getFileContents = async (filePath) => {
try {
if (!(await this.doesFileExist(filePath)))
throw "File does not exist.";
const { repositoryURL } = this;
const apiUrl = `${repositoryURL}/contents/${filePath}`;
const file = await this.apiRequest(apiUrl, "GET");
const content = Buffer.from(file.content, "base64").toString(
"utf-8"
);
return content;
} catch (err) {
this.errorHandler("Error retrieving file:", err);
}
};
/**
* Deletes a file from the Git repository.
* @param {string} filePath The path of the file to delete.
* @param {string} message An optional message to attach to the commit.
* @returns {Promise<void>} A Promise that resolves when the file is deleted successfully.
*/
deleteFile = async (filePath, message = "Deleted file.") => {
try {
const fileExists = await this.doesFileExist(filePath);
if (!fileExists) throw "File does not exist.";
const { repositoryURL } = this;
const apiUrl = `${repositoryURL}/contents/${filePath}`;
const data = {
message: message,
sha: fileExists, // Provide the existing file's SHA to delete it
};
await this.apiRequest(apiUrl, "DELETE", data);
console.log(`File at path "${filePath}" deleted.`);
} catch (err) {
this.errorHandler("Error deleting file:", err);
}
};
/**
* Renames a file in the Git repository.
* @param {string} filePath The current path of to file.
* @param {string} newPath The new path for the file.
* @param {string} message An optional message to attach to the commit.
* @returns {Promise<void>} A Promise that resolves when the file is renamed successfully.
*/
renameFile = async (filePath, newPath, message = "Renamed file.") => {
try {
const { repositoryURL } = this;
const currentFileUrl = `${repositoryURL}/contents/${filePath}`;
const newFileUrl = `${repositoryURL}/contents/${newPath}`;
const currentFile = await this.apiRequest(currentFileUrl, "GET");
const data = {
message: message,
content: currentFile.content,
sha: currentFile.sha, // Provide the existing file's SHA to rename it
};
// Create the new file at the new path
await this.apiRequest(newFileUrl, "PUT", data);
// Delete the old file from the current path
await this.apiRequest(currentFileUrl, "DELETE", {
message: `Deleted file ${filePath}`,
sha: currentFile.sha,
});
console.log(`"${filePath}" is now "${newPath}"`);
} catch (err) {
this.errorHandler("Error renaming file:", err);
}
};
/**
* Lists the file tree of the Git repository recursively.
* @param {string} [path] The path of the directory to list the file tree. (optional, defaults to the root directory)
* @returns {Promise<FileTreeNode>} A Promise that resolves with the file tree.
*/
getFileTree = async (path = "") => {
try {
const { repositoryURL } = this;
const apiUrl = `${repositoryURL}/contents/${path}`;
const fileTree = await this.apiRequest(apiUrl, "GET");
const fileTreeNodes = await Promise.all(
fileTree.map(async (item) => {
if (item.type === "dir") {
const children = await this.getFileTree(item.path);
return { ...item, children };
}
return item;
})
);
return fileTreeNodes;
} catch (err) {
this.errorHandler("Error listing file tree:", err);
}
};
}
module.exports = GitHandler;