-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchghpush.htm
226 lines (210 loc) · 5.71 KB
/
fetchghpush.htm
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
<body>
<script>
/* git auth token extracted like this from windows box
sh-4.4$ GIT_TRACE=1 GIT_TRACE_PACK_ACCESS=1 GIT_TRACE_PACKET=1 GIT_TRACE_PERFORMANCE=1 GIT_TRACE_SETUP=1 GIT_MERGE_VERBOSITY=1 GIT_CURL_VERBOSE=1 GIT_TRACE_SHALLOW=1 GCM_TRACE=1 GIT_TRACE_REDACT=0 git push
code taken from
https://github.com/renovatebot/renovate/blob/5f213255d088054500cdd980b62092f4d22f5f4c/lib/platform/github/storage.js
*/
var SECERT = undefined;
var get = {};
async function got (url, options) {return ajaxRun("GET", url, options)}
async function post (url, options) {return ajaxRun("POST", url, options)}
async function patch (url, options) {return ajaxRun("PATCH", url, options)}
async function ajaxRun (method, url, options) {
url = await fetch('https://api.github.com/' + url, {
method: method,
headers: {
authorization: SECERT
}
, ...(options && {body: JSON.stringify(options)})
});
return await url.json();
};
var config = {
repository: 'bulk88/tinymta',
};
let branchFiles = {};
var global = {
gitAuthor: {
name: "RoutesBot",
email: "[email protected]",
}
};
//need time zone like moment.js does
function toIsoString(date) {
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function (num) {
return (num < 10 ? '0' : '') + num;
};
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
dif + pad(Math.floor(Math.abs(tzo) / 60)) +
':' + pad(Math.abs(tzo) % 60);
}
// Create a commit and return commit SHA
async function createCommit(parent, tree, message) {
/* unused
const {
gitPrivateKey
} = config;
*/
/* const now = moment(); */
let author;
if (global.gitAuthor) {
author = {
name: global.gitAuthor.name,
email: global.gitAuthor.email,
date: toIsoString(new Date()) /* now.format() */,
};
}
const body = {
message,
parents: [parent],
tree,
};
if (author) {
body.author = author;
/* unused
if (gitPrivateKey) {
const privKeyObj = openpgp.key.readArmored(gitPrivateKey).keys[0];
const commit = `tree ${tree}\nparent ${parent}\nauthor ${
author.name
} <${author.email}> ${now.format('X ZZ')}\ncommitter ${
author.name
} <${author.email}> ${now.format('X ZZ')}\n\n${message}`;
const {
signature
} = await openpgp.sign({
data: openpgp.util.str2Uint8Array(commit),
privateKeys: privKeyObj,
detached: true,
armor: true,
});
body.signature = signature;
}
*/
}
return (await post(`repos/${config.repository}/git/commits`, body))
.sha;
}
// Internal: Updates an existing branch to new commit sha
async function updateBranch(branchName, commit) {
const options = {
sha: commit,
force: true,
};
try {
await patch(
`repos/${config.repository}/git/refs/heads/${branchName}`,
options);
} catch (err) {
if (err.statusCode === 422) {
console.log(err + ' Branch no longer exists - exiting');
throw new Error('repository-changed');
}
throw err;
}
}
// Low-level commit operations
// Return the commit SHA for a branch
async function getBranchCommit(branchName) {
try {
const res = await got(
`repos/${config.repository}/git/refs/heads/${branchName}`);
return res.object.sha;
} catch (err) {
if (err.statusCode === 404) {
throw new Error('repository-changed');
}
if (err.statusCode === 409) {
throw new Error('empty');
}
throw err;
}
}
// Return the tree SHA for a commit
async function getCommitTree(commit) {
return (await got(`repos/${config.repository}/git/commits/${commit}`))
.tree.sha;
}
async function createBlob(fileContents) {
const options = {
encoding: 'base64',
content: btoa(fileContents)
};
return (await post(`repos/${config.repository}/git/blobs`, options))
.sha;
}
// Create a tree and return SHA
async function createTree(baseTree, files) {
const body = {
base_tree: baseTree,
tree: [],
};
files.forEach(file => {
body.tree.push({
path: file.name,
mode: '100644',
type: 'blob',
sha: file.blob,
});
});
return (await post(`repos/${config.repository}/git/trees`, body))
.sha;
}
// Add a new commit, create branch if not existing
async function commitFilesToBranch(
branchName,
files,
message,
parentBranch = config.baseBranch) {
try {
delete branchFiles[branchName];
const fileBlobs = [];
// Create blobs
for (const file of files) {
const blob = createBlob(file.contents);
fileBlobs.push({
name: file.name,
blob,
});
}
const parentCommit = await getBranchCommit(parentBranch);
const parentTree = await getCommitTree(parentCommit);
// Create tree
for(var i=0;i<fileBlobs.length; i++) {
fileBlobs[i].blob = await fileBlobs[i].blob;
}
const tree = await createTree(parentTree, fileBlobs);
const commit = await createCommit(parentCommit, tree, message);
/*
const isBranchExisting = await branchExists(branchName);
if (isBranchExisting) { */
await updateBranch(branchName, commit);
return 'updated';
/*
}
await createBranch(branchName, commit);
if (branchList) {
branchList.push(branchName);
}
return 'created';
*/
} catch (err) {
if (err.statusCode === 404) {
throw new Error('repository-changed');
}
throw err;
}
}
commitFilesToBranch("master", [{
name: "t.txt",
contents: ""+new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
}
], "Routes "+new Date().toLocaleString("en-US", {timeZone: "America/New_York"}), "master");
</script></body>