godprotocol 1.2.21 → 1.2.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Objects/Manager.js +4 -1
- package/Objects/Oracle.js +13 -12
- package/Objects/Repository.js +1 -0
- package/package.json +1 -1
- package/repos/github.js +129 -41
package/Objects/Manager.js
CHANGED
|
@@ -482,7 +482,10 @@ class Manager {
|
|
|
482
482
|
|
|
483
483
|
address = `${acc}/${address}`;
|
|
484
484
|
|
|
485
|
-
let folder = await this.ds.folder(address, {
|
|
485
|
+
let folder = await this.ds.folder(address, {
|
|
486
|
+
account: acc,
|
|
487
|
+
indexes: options.indexes,
|
|
488
|
+
});
|
|
486
489
|
|
|
487
490
|
return folder;
|
|
488
491
|
};
|
package/Objects/Oracle.js
CHANGED
|
@@ -205,25 +205,26 @@ class Oracle extends Handlers {
|
|
|
205
205
|
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
206
206
|
let reps = await this.merge_repos(
|
|
207
207
|
conf_repos,
|
|
208
|
-
await this.filter_repos(path)
|
|
208
|
+
await this.filter_repos(`.oracle/${path}`)
|
|
209
209
|
);
|
|
210
|
+
conf = {
|
|
211
|
+
...conf,
|
|
212
|
+
content,
|
|
213
|
+
repositories: reps.map((r) => r.repo._id || r.repo.get_id()),
|
|
214
|
+
};
|
|
210
215
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
repositories: reps.map((r) => r.repo._id || r.repo.get_id()),
|
|
217
|
-
}),
|
|
218
|
-
{ repos: reps }
|
|
219
|
-
);
|
|
216
|
+
this.configs[sli] = conf;
|
|
217
|
+
|
|
218
|
+
await this.set_to_repos(`.oracle/${path}`, JSON.stringify(conf), {
|
|
219
|
+
repos: reps,
|
|
220
|
+
});
|
|
220
221
|
} else {
|
|
221
222
|
conf = await this.get_config(address, true);
|
|
222
223
|
|
|
223
224
|
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
224
225
|
let reps = await this.merge_repos(
|
|
225
226
|
conf_repos,
|
|
226
|
-
await this.filter_repos(path)
|
|
227
|
+
await this.filter_repos(`.storage/${path}`)
|
|
227
228
|
);
|
|
228
229
|
await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
|
|
229
230
|
}
|
|
@@ -243,7 +244,7 @@ class Oracle extends Handlers {
|
|
|
243
244
|
let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
|
|
244
245
|
let reps = await this.merge_repos(
|
|
245
246
|
conf_repos,
|
|
246
|
-
await this.filter_repos(path)
|
|
247
|
+
await this.filter_repos(`.storage/${path}`)
|
|
247
248
|
);
|
|
248
249
|
|
|
249
250
|
content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
|
package/Objects/Repository.js
CHANGED
package/package.json
CHANGED
package/repos/github.js
CHANGED
|
@@ -1,97 +1,185 @@
|
|
|
1
|
-
import Repository from
|
|
1
|
+
import Repository from "../Objects/Repository.js";
|
|
2
2
|
|
|
3
|
-
class Github extends Repository{
|
|
4
|
-
constructor({ key, username, repo }) {
|
|
5
|
-
super(
|
|
6
|
-
|
|
7
|
-
this.base_url =
|
|
3
|
+
class Github extends Repository {
|
|
4
|
+
constructor({ key, username, repo, branch }) {
|
|
5
|
+
super("github");
|
|
6
|
+
|
|
7
|
+
this.base_url = "https://api.github.com";
|
|
8
8
|
this.auth_token = key;
|
|
9
9
|
this.owner = username;
|
|
10
10
|
this.repo = repo;
|
|
11
|
+
this.branch = branch || "master";
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
// Headers for API calls
|
|
14
15
|
get_headers = () => ({
|
|
15
16
|
Authorization: `Bearer ${this.auth_token}`,
|
|
16
|
-
Accept:
|
|
17
|
+
Accept: "application/vnd.github.v3+json",
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
// Write (create or update) a file
|
|
20
|
-
write_file = async (file_path, content, options={}) => {
|
|
21
|
-
|
|
22
|
-
let sha = null,
|
|
21
|
+
write_file = async (file_path, content, options = {}) => {
|
|
22
|
+
let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
23
|
+
let sha = null,
|
|
24
|
+
exists;
|
|
23
25
|
|
|
24
26
|
// Check if file exists, get its SHA
|
|
25
|
-
try{
|
|
27
|
+
try {
|
|
26
28
|
exists = await this.file_exists(file_path);
|
|
27
|
-
|
|
29
|
+
|
|
28
30
|
if (exists) {
|
|
29
|
-
|
|
31
|
+
let res = await fetch(url, { headers: this.get_headers() });
|
|
30
32
|
if (res.ok) {
|
|
31
|
-
|
|
33
|
+
let data = await res.json();
|
|
32
34
|
sha = data.sha;
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
|
-
if (options.just_exists) return exists
|
|
36
|
-
}catch(e){
|
|
37
|
-
console.log(e.message)
|
|
38
|
-
return
|
|
37
|
+
if (options.just_exists) return exists;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.log(e.message);
|
|
40
|
+
return;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
// Build payload
|
|
42
44
|
let body;
|
|
43
|
-
try{
|
|
45
|
+
try {
|
|
44
46
|
body = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}catch(e){
|
|
47
|
+
message: exists ? "Update file" : "Create file",
|
|
48
|
+
content: Buffer.from(content).toString("base64"),
|
|
49
|
+
...(sha && { sha }),
|
|
50
|
+
};
|
|
51
|
+
} catch (e) {}
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const response = await fetch(url, {
|
|
55
|
-
method: 'PUT',
|
|
53
|
+
try {
|
|
54
|
+
let response = await fetch(url, {
|
|
55
|
+
method: "PUT",
|
|
56
56
|
headers: this.get_headers(),
|
|
57
57
|
body: JSON.stringify(body),
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
return response.json();
|
|
61
|
-
}catch(e){
|
|
62
|
-
console.log(e.message)
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.log(e.message);
|
|
63
63
|
}
|
|
64
|
-
|
|
65
64
|
};
|
|
66
65
|
|
|
67
66
|
// Read a file
|
|
68
67
|
read_file = async (file_path) => {
|
|
69
|
-
|
|
68
|
+
let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
70
69
|
|
|
71
70
|
let response;
|
|
72
|
-
try{
|
|
73
|
-
response = await fetch(url, { headers: this.get_headers() })
|
|
71
|
+
try {
|
|
72
|
+
response = await fetch(url, { headers: this.get_headers() });
|
|
74
73
|
|
|
75
74
|
if (!response.ok) return null;
|
|
76
75
|
|
|
77
|
-
|
|
78
|
-
return Buffer.from(fileData.content,
|
|
79
|
-
}catch(e){
|
|
80
|
-
console.log(e.message)
|
|
76
|
+
let fileData = await response.json();
|
|
77
|
+
return Buffer.from(fileData.content, "base64").toString("utf-8");
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.log(e.message);
|
|
81
80
|
return;
|
|
82
81
|
}
|
|
83
82
|
};
|
|
84
83
|
|
|
85
84
|
// Check if file exists
|
|
86
85
|
file_exists = async (file_path) => {
|
|
87
|
-
|
|
86
|
+
let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
88
87
|
try {
|
|
89
|
-
|
|
88
|
+
let response = await fetch(url, { headers: this.get_headers() });
|
|
90
89
|
return response.ok;
|
|
91
90
|
} catch {
|
|
92
91
|
return false;
|
|
93
92
|
}
|
|
94
93
|
};
|
|
94
|
+
|
|
95
|
+
// --- Delete File ---
|
|
96
|
+
delete_file = async (file_path, message = "Delete file") => {
|
|
97
|
+
let url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
let res = await fetch(url, { headers: this.get_headers() });
|
|
101
|
+
if (!res.ok) throw new Error("File not found");
|
|
102
|
+
let data = await res.json();
|
|
103
|
+
let sha = data.sha;
|
|
104
|
+
|
|
105
|
+
let body = { message, sha };
|
|
106
|
+
|
|
107
|
+
let del = await fetch(url, {
|
|
108
|
+
method: "DELETE",
|
|
109
|
+
headers: this.get_headers(),
|
|
110
|
+
body: JSON.stringify(body),
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!del.ok) {
|
|
114
|
+
let err = await del.text();
|
|
115
|
+
throw new Error(`Failed to delete file: ${err}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return await del.json();
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.log(e.message);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
delete_folder = async (folder_path, message = "Delete folder") => {
|
|
126
|
+
let headers = this.get_headers();
|
|
127
|
+
|
|
128
|
+
// 1️⃣ Get the latest commit SHA
|
|
129
|
+
let refUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/ref/heads/${this.branch}`;
|
|
130
|
+
let refRes = await fetch(refUrl, { headers });
|
|
131
|
+
let refData = await refRes.json();
|
|
132
|
+
let commitSha = refData.object.sha;
|
|
133
|
+
|
|
134
|
+
// 2️⃣ Get the tree of that commit
|
|
135
|
+
let commitUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/commits/${commitSha}`;
|
|
136
|
+
let commitRes = await fetch(commitUrl, { headers });
|
|
137
|
+
let commitData = await commitRes.json();
|
|
138
|
+
let treeSha = commitData.tree.sha;
|
|
139
|
+
|
|
140
|
+
// 3️⃣ Get the full tree (recursive)
|
|
141
|
+
let treeUrl = `${this.base_url}/repos/${this.owner}/${this.repo}/git/trees/${treeSha}?recursive=1`;
|
|
142
|
+
let treeRes = await fetch(treeUrl, { headers });
|
|
143
|
+
let treeData = await treeRes.json();
|
|
144
|
+
|
|
145
|
+
// 4️⃣ Filter out the folder you want to delete
|
|
146
|
+
let filtered = treeData.tree.filter((t) => !t.path.startsWith(folder_path));
|
|
147
|
+
|
|
148
|
+
// 5️⃣ Create a new tree without that folder
|
|
149
|
+
let newTreeRes = await fetch(
|
|
150
|
+
`${this.base_url}/repos/${this.owner}/${this.repo}/git/trees`,
|
|
151
|
+
{
|
|
152
|
+
method: "POST",
|
|
153
|
+
headers,
|
|
154
|
+
body: JSON.stringify({ tree: filtered }),
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
let newTreeData = await newTreeRes.json();
|
|
158
|
+
|
|
159
|
+
// 6️⃣ Create a new commit pointing to the new tree
|
|
160
|
+
let newCommitRes = await fetch(
|
|
161
|
+
`${this.base_url}/repos/${this.owner}/${this.repo}/git/commits`,
|
|
162
|
+
{
|
|
163
|
+
method: "POST",
|
|
164
|
+
headers,
|
|
165
|
+
body: JSON.stringify({
|
|
166
|
+
message,
|
|
167
|
+
tree: newTreeData.sha,
|
|
168
|
+
parents: [commitSha],
|
|
169
|
+
}),
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
let newCommitData = await newCommitRes.json();
|
|
173
|
+
|
|
174
|
+
// 7️⃣ Update the branch ref
|
|
175
|
+
await fetch(refUrl, {
|
|
176
|
+
method: "PATCH",
|
|
177
|
+
headers,
|
|
178
|
+
body: JSON.stringify({ sha: newCommitData.sha }),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return { success: true, message: "Folder deleted via new commit" };
|
|
182
|
+
};
|
|
95
183
|
}
|
|
96
184
|
|
|
97
185
|
export default Github;
|