forked from DannoPeters/git-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-sync.js
672 lines (532 loc) · 27.9 KB
/
git-sync.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Git-Sync.js
JavaScript Script to synchronize two remote GitHub repos (Master to Slave)
Copyright SuperDARNCanada
Authors: Marina Schmidt and Danno Peters
*/
//Global Variables
var actionArray = new Array(); //Array to store information about actions taken
var today = new Date();
var startTime = `${today.getUTCDate()}/${(today.getUTCMonth()+1)}/${today.getUTCFullYear()} ${(today.getUTCHours())}:${(today.getUTCMinutes())}:${today.getUTCSeconds()} UTC`;
var lastSync = 'Never';
var lastModified = 'None';
var lastAdded = 'None';
var lastRemoved = 'None';
var lastCommit = 'None';
var radar_abbrev = 'sas';
//Import Required
let http = require(`http`); //import http library
let crypto = require(`crypto`); //import crypto library
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
var execSync = require(`child_process`).execSync; //include child_process library so we can exicute shell commands
var fs = require("fs"); //required to write to files
const dns = require('dns'); //required to resolve domain name for log file
var config = require('./git-sync_config.js');
/* Webserver
Purpose: Creates a webserver in order to recieve websocket requests
Inputs: req - object containing http request event
res - object containing server responce event
Wait it does:
- Record request in log file
- Verify the JSON is authentic using the secret
- call githubwebHook function if it is an authentic request
- otherwise record error and keep listening
Returned: None
Passes: chunk - JSON file sent to web server
req - the request event object
*/
http.createServer(function (req, res) { //create webserver
req.on(`data`, function(chunk) {
//Grab source IP and soscket for log file, multiple methods used for diffrently formatted requests
var jsonIP = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || (req.connection.socket ? req.connection.socket.remoteAddress : null);
var jsonPort = req.connection.remoteAddress || req.socket.remoteAddress || (req.connection.socket ? req.connection.socket.remoteAddress : null);
log(`OP`, `NEW OPERATION: File Recieved from ${jsonIP}${jsonPort}`, 1, '\n');
let sigA = "sha1=" + crypto.createHmac(`sha1`, config.Auth.secretA).update(chunk.toString()).digest(`hex`); //verify message is authentic (correct secret)
let sigB = "sha1=" + crypto.createHmac(`sha1`, config.Auth.secretB).update(chunk.toString()).digest(`hex`); //verify message is authentic (correct secret)
if (req.headers[`x-hub-signature`] == sigA) {
log(`OP`, `JSON: WebHook ${config.Setup.gitA} Signature Verified: ${sigA}`, 2);
githubHook(chunk,req);
} else if (req.headers[`x-hub-signature`] == sigB) {
log(`OP`, `JSON: WebHook ${config.Setup.gitB} Signature Verified: ${sigB}`, 2);
githubHook(chunk,req);
} else {
var signature = req.headers[`x-hub-signature`];
log(`ALL`, `ERROR: Incorrect Signature: ${signature}`, 2);
}
});
console.log("HTML Running");
res.write(`<html><center><h3>If you are reading this, git-sync.JS is running. :-)</h3> </html></br><img src="https://res.cloudinary.com/dwktbavf8/image/upload/v1524441964/SuperDARN/superDARN-logo.png" alt="SuperDarn Logo"></html></br>Copyright: SuperDARN Canada <br><a href="https://superdarn.ca">SuperDARN.ca</a> <br><br>Authors: Marina Schmidt and Danno Peters <br><br><br> <strong>Git-Sync.JS Settings</strong><br><u>Remote</u><br> Repo A: <i>${config.Setup.gitA}/${config.Setup.dirA}</i><br> Repo B: <i>${config.Setup.gitB}/${config.Setup.dirB}</i><br><br><u>Settings</u><br> Contains: <i>${config.Setup.sync.nameContains}</i> Deliminator: <i>${config.Setup.sync.typeDeliminator}</i> Position: <i>${config.Setup.sync.typePosition} <br><br><u>Local</u><br> Repo A: <i>${config.Setup.repoA}</i><br> Repo B: <i>${config.Setup.repoB}</i> <br><br> Server User: <i>${config.Setup.user}</i> <br><br> Running Since: <i>${startTime}</i><br><br> Last Sync: <i>${lastSync}</i> <br> Last Commit: <i>${lastCommit}</i><br> <u>Last Modified</u> <br><i>${lastModified}</i><br> <u>Last Added</u> <br><i>${lastAdded}</i><br> <u>Last Removed</u> <br><i>${lastRemoved}</i>`)
res.end('');
}).listen(config.Setup.port, (err) => {
if (err) return log(`ALL`, `\n ERROR: Issue with init of server: ${err}`, 0);
log(`ALL`, `INIT: Node.js server listening on ${config.Setup.port}`, 0, '\n');
});
/* runCmd
Purpose: runs commands in synchronus (serial) terminal
Inputs: cmd - command to be run, string
Wait it does:
- log command to be exicuted
- tries to execute command
- catch will log error and exit gracefully
Returned: None
Passes: none
*/
function runCmd(cmd) {
log(`OP`, `SYNC: Exicuted ${cmd}`, 2);
try{
execSync(`${cmd}`);
}
catch(error){
log(`ALL`, `ERROR: Terminal Command Failed: ${error}`, 2);
return;
}
}
/* githubJSON
Purpose: creates a linked list of all important information from JSON
Inputs: file - the JSOn file sent from the webhook
event - type of github event (important since JSON formatting is diffrent for each)
Wait it does:
- parces json file into githubWebHook variable
- tries to store data in repo based on webhook event
- catch logs error message and exists gracefully with no return
- otherwise returns repo
Returned: repo - linked list of important informationf fromt the recieved JSON
Passes: none
*/
function githubJSON(file, event) {
var githubWebHook = JSON.parse(file); //Parse the JSON datafile from the push
switch(event){
case "push":
try{ //Test if push formatting is correct.
var repo = {
gitFullName: githubWebHook.repository.full_name, //full name of the repository
gitID: githubWebHook.repository.id, //ID of the repository
gitURL: githubWebHook.repository.html_url, //URL of the repository
modifiedFiles: githubWebHook.commits[0].modified, //Create list of files modified in Push
addedFiles: githubWebHook.commits[0].added, //Create list of files added in Push
removedFiles: githubWebHook.commits[0].removed, //Create list of files removed in Push
commitMessage: githubWebHook.commits[0].message, //Read commit message for use in push to repo-B
username: githubWebHook.commits[0].author.username, //User which pushed the files
finalCommitMessage: ""
}
}
catch(error) {
log(`OP`, `JSON: Push Data Formatting Incorrect ${error}`, 2);
return;
};
default:
break;
}
return repo;
}
/* githubHook
Purpose: based on github event and source repo runs diffrent code for syncing and sync confirmation
Inputs: chunk - JSON file sent to web server
req - object containing http request event
Wait it does:
- if push webhook from repoA
- check if user other than this server
- git pull RepoA and repoB on server
- cp repo A clone to repo B clone
- git add repoB
- git commit -m repo A push commit message
- git push to repo B
- if push webhook from repoB
- check that user pushing is this server
- check that same file names added, modified, or removed were edited on push
- check that commit message is correct
Returned: none
Passes: cmd - commands to be exicited by runCmd
chunk - JSON file to be pased into linked list (repo) by githubJSON function
req.headers['x-github-event'] - event type specified in server request passed to githubJSON to place proper variabel sinto linked list
commitedFiles - list of all added, modified, or removed git files sent to fileType to confirm specified type of file to trigger sync, and fileLoc to ensure specified location to trigger sync
repo - sorted linked list of JSON data sent to queue to confirm with webhook from repoB
*/
function githubHook(chunk, req) {
//Test if file has GitHub Event info
try {
repo = githubJSON(chunk,req.headers['x-github-event']);
}
catch (error) {
log(`OP`, `JSON: GitHub Data Formatting Incorrect ${error}`, 2);
return;
}
if (req.headers['x-github-event'] == "push") { //if event type is push run following code
try {
switch (repo.gitFullName){
case config.Setup.gitA: //sync to repo B
if (repo.username == config.Setup.user) { //confirm push is not from this server (to prevent push loop)
log(`OP`, `JSON: GitHub user "${repo.username}" (This Server) pushed to ${repo.gitFullName}`, 2);
log(`OP`, `JSON: No further action will be taken (Prevents accidental push loop)`, 2);
} else {
log(`OP`, `JSON: GitHub user "${repo.username}" pushed to ${repo.gitFullName}`, 2);
//Pull from github repoA to local repo
var cmd = `cd ${config.Setup.repoA} && git pull origin ${config.Setup.repoA_branch}`;
runCmd(cmd);
//Pull request for repoB
var cmd = `cd ${config.Setup.repoB} && git pull origin ${config.Setup.repoB_branchA}`;
runCmd(cmd);
if (config.Setup.branch.constant == false) {
radar_abbrev = repo.modifiedFiles[0].split(config.Setup.branch.typeDeliminator)[config.Setup.branch.typePosition]
} else {
radar_abbrev = ''
}
//console.log(radar_abbrev)
//delete existing repo b branch
var cmd = `cd ${config.Setup.repoB} && git branch -d ${config.Setup.branch.prefix}${radar_abbrev}${config.Setup.branch.suffix}`;
runCmd(cmd);
//Create new branch and switch to it for repo B
var cmd = `cd ${config.Setup.repoB} && git checkout -b ${config.Setup.branch.prefix}${radar_abbrev}${config.Setup.branch.suffix}`
runCmd(cmd);
var type = `hdw`;
var commitedFiles = repo.modifiedFiles.concat(repo.addedFiles);
if (fileType(commitedFiles, config.Setup.sync.nameContains, config.Setup.sync.typePosition, config.Setup.sync.typeDeliminator) && fileLoc(commitedFiles, `${config.Setup.dirA}`)) {
//Copy only commited Files by using file paths of each file
for (filePath in commitedFiles){
splitFilePath = commitedFiles[filePath].split('/');
var copyPath = '';
for (var i=0; i < splitFilePath.length-1; i++){
copyPath = copyPath.concat(`${splitFilePath[i]}/`);
}
try {
var cmd = `cp ${config.Setup.repoA}/${commitedFiles[filePath]} ${config.Setup.repoB}/${config.Setup.dirB}/${copyPath} -a`;
log(`OP`, `SYNC: Exicuted ${cmd}`, 2);
execSync(`${cmd}`);
}
catch (unlogged_error) { //error is not logged as expected in normal operation whne new folder is pushed to git
try { //if copying each file directly fails (ie new folder created) then recursively sync whole directory
log(`ALL`, `ERROR: Copy Command failed, Attempting to recursive copy directory`, 2);
var cmd = `cp ${config.Setup.repoA}/${config.Setup.dirA} ${config.Setup.repoB}/${config.Setup.dirB} -a`;
log(`OP`, `SYNC: Exicuted ${cmd}`, 2);
execSync(`${cmd}`);
}
catch (error) {
log(`ALL`, `ERROR: Recursive copy failed: ${error}`, 2);
}
}
}
//add all files to git
var cmd = `cd ${config.Setup.repoB} && git add --all`;
runCmd(cmd);
//Commit changes to local repoB with message from GitHub repo
var cmd = `cd ${config.Setup.repoB} && git commit -m "User: ${repo.username} Message: ${repo.commitMessage}"`;
repo.finalCommitMessage = `User: ${repo.username} Message:${repo.commitMessage}`;
runCmd(cmd);
//Push local repoB to GitHub
var cmd = `cd ${config.Setup.repoB} && git push origin ${config.Setup.branch.prefix}${radar_abbrev}${config.Setup.branch.suffix}`;
runCmd(cmd);
//Store information to confirm proper push to repo B
//sort all lists of files to ensure they are comapred correctly
repo.modifiedFiles = repo.modifiedFiles.sort();
repo.addedFiles = repo.addedFiles.sort();
repo.removedFiles = repo.removedFiles.sort();
queueAdd(actionArray, repo)
//leave discriptive log detailing which test for files to sync failed
} else if (fileType(commitedFiles, config.Setup.sync.nameContains, config.Setup.sync.typePosition, config.Setup.sync.typeDeliminator)){
log(`OP`, `SYNC: Only changes to files of type "${type}" found outside of ${config.Setup.gitA}/${config.Setup.dirA}`, 2);
log(`OP`, `SYNC: No Push to ${config.Setup.gitB} Required`, 2);
} else if (fileLoc(commitedFiles, `${config.Setup.dirA}`)){
log(`OP`, `SYNC: No changes to files of type "${type}" found in push`, 2);
log(`OP`, `SYNC: No Push to ${config.Setup.gitB} Required`, 2);
} else {
log(`OP`, `SYNC: No changes to files of type "${type}" AND no chnges found in ${config.Setup.gitA}/${config.Setup.dirA}`, 2);
log(`OP`, `SYNC: No Push to ${config.Setup.gitB} Required`, 2);
}
}
break;
case config.Setup.gitB: //Verify that push to repo B was correct
try{
if (repo.username != config.Setup.user) { //ensure push came from this server (to prevent falase positives)
log(`OP`, `JSON: GitHub user "${repo.username}" pushed to ${repo.gitFullName}`, 2);
log(`OP`, `JSON: No further action required (prevents false push confirm)`, 2);
} else {
log(`OP`, `JSON: GitHub user "${repo.username}" (This Server) pushed to ${repo.gitFullName}`, 2);
//sort all lists of files to ensure they are comapred correctly
repo.modifiedFiles = repo.modifiedFiles.sort();
repo.addedFiles = repo.addedFiles.sort();
repo.removedFiles = repo.removedFiles.sort();
if (repo.modifiedFiles != ""){
lastModified = repo.modifiedFiles
} else {
lastModified = "None"
}
if (repo.addedFiles != ""){
lastAdded = repo.addedFiles
} else {
lastAdded = "None"
}
if (repo.removedFiles != ""){
lastRemoved = repo.removedFiles
} else {
lastRemoved = "None"
}
lastCommit = repo.commitMessage
console.log("Made it");
//retrieve past repo data from queue
var pastRepo = queueGet(actionArray);
pullReq(pastRepo, `${config.Setup.branch.prefix}${radar_abbrev}${config.Setup.branch.suffix}`)
//Check all added, modified, and deleted files match those in last push to repo B and commit is correct
var testModified = checkFiles(repo.modifiedFiles, pastRepo.modifiedFiles, '/');
var testAdded = checkFiles(repo.addedFiles, pastRepo.addedFiles, '/');
var testRemoved = checkFiles(repo.removedFiles, pastRepo.removedFiles, '/');
var testCommit = (repo.commitMessage == pastRepo.finalCommitMessage);
//Write to log file confirming sucesses and errors
// only prints one message if sucessful, otherwise details which tests passed and which failed
if (testModified && testAdded && testRemoved && testCommit) {
log(`OP`, `CONFIRM: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} was sucessful :-)`, 2);
//Since push was sucessful start a pull request
} else {
if (testModified == true){
log(`OP`, `CONFIRM: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} modified files was sucessful`, 2);
} else {
log(`ALL`, `ERROR: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} modified files synced incorrectly`, 2);
}
if (testAdded == true){
log(`OP`, `CONFIRM: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} added files was sucessful`, 2);
} else {
log(`ALL`, `ERROR: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} added files synced incorrectly`, 2);
}
if (testRemoved == true){
log(`OP`, `CONFIRM: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} removed files was sucessful`, 2);
} else {
log(`ALL`, `ERROR: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} removed files synced incorrectly`, 2);
}
if (testCommit == true){
log(`OP`, `CONFIRM: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} commit was sucessful`, 2);
} else {
log(`ALL`, `ERROR: Git Sync between ${config.Setup.gitA} and ${config.Setup.gitB} commit is incorrect`, 2);
}
}
}
//Update date and time of last sync
var today = new Date();
lastSync = `${today.getUTCDate()}/${(today.getUTCMonth()+1)}/${today.getUTCFullYear()} ${(today.getUTCHours())}:${(today.getUTCMinutes())}:${today.getUTCSeconds()} UTC`;
}
catch(error){
log(`ALL`, `ERROR: ${config.Setup.gitB} push confirmation failed: ${error}`, 2);
return;
}
break;
default:
log(`ALL`, `ERROR: Source "${repo.gitFullName}" Not Recognized`, 2);
break;
}
}
catch (error){
log(`ALL`, `ERROR: Source "${repo.gitFullName}" Not Recognized: ${error}`, 2);
}
}
}
/* queueAdd
Purpose: adds value to a specifed queue
Inputs: queue - the queue to send the value to
value - the value to be sent to the queue
Wait it does:
- pushes value to specified queue
- logs push to queue
Returned: none
Passes: value - pushes to stack
*/
function queueAdd(queue, value) {
queue.push(value);
log(`OP`, `STACK: previos repo data pushed to queue`, 2);
}
/* queueGet
Purpose: retrieves a value from specified queue
Inputs: queue - the queue to send the value to
value - the value to be sent to the queue
Wait it does:
- retrieves data from specified queue
- returns value and logs retreval
Returned: retrievedQueueValue - value retrieved from queue
Passes: none
*/
function queueGet(queue) {
var retrievedQueueValue = queue.shift();
if(retrievedQueueValue) {
log(`OP`, `STACK: previos repo data retrieved from queue`, 2);
return retrievedQueueValue;
}
else {
log(`ALL`, `ERROR: STACK: no data to be retrieved from queue`, 2);
return null;
}
}
/* log
Purpose: create and write data to log files on server
Inputs: stream - the log file which should be written to
message - string to be written tot eh log file
level - indent level of the text to be added
prefix - prefix to be placed infront of the log message
Wait it does:
- writes to log files with data and time (UTC) and specifed message
Returned: none
Passes: none
*/
function log (stream, message, level, prefix){
prefix = prefix || '';
var today = new Date();
var operation = fs.createWriteStream(`./git-syncJS_Log-Files/git-syncJS_${today.getUTCFullYear()}_Operation.log`, {flags:'a'});
var error = fs.createWriteStream(`./git-syncJS_Log-Files/git-syncJS_${today.getUTCFullYear()}_Error.log`, {flags:'a'});
var date = `${today.getUTCDate()}/${(today.getUTCMonth()+1)}/${today.getUTCFullYear()}`;
var time = `${(today.getUTCHours())}:${(today.getUTCMinutes())}:${today.getUTCSeconds()}`;
switch (stream){
case 'OP':
operation.write(`${prefix}${date} ${time} UTC${new Array(level*5+1).join(' ')} ${message}\n`);
operation.end();
break;
case 'ER':
error.write(`${prefix}${date} ${time} UTC${new Array(level*5+1).join(' ')} ${message}\n`);
error.end();
break;
case 'ALL':
error.write(`${prefix}${date} ${time} UTC${new Array(level*5+1).join(' ')} ${message}\n`);
operation.write(`${prefix}${date} ${time} UTC${new Array(level*5+1).join(' ')} ${message}\n`);
error.end();
operation.end();
break;
default:
break;
}
}
/* arraySplit
Purpose: create and write data to log files on server
Inputs: array - array of strings to be split
char - the char to split the strings by
Wait it does:
- splits each string in an array into susequent strings
- returns each split string as a sub array of the main array
Returned: array1 - array of sub arrays containing each split string
Passes: none
*/
function arraySplit (array, char) {
var array1 = new Array(array.length);
if ((undefined === array)){ //check if array in undefined
return array1
} else {
var i = 0;
for (element in array) {
var split = array[element].split(char); // just split once
array1[i] = split; //push to nested array
i++;
}
return array1
}
}
/* fileType
Purpose: check the type of file
Inputs: repo - linked list of information from github JSON
file - string to recognize file by
rank - rank in the file name to find file string
char - delimiting char between ranks
Wait it does:
- writes to log files with data and time (UTC) and specifed message
Returned: True - if file of specified type is found
False - if no files of specified type are found
Passes: none
*/
function fileType (repo, file, rank, char){
var modified = arraySplit(repo, '/');
//check modified files
for (F in modified){
fileName = modified[F][modified[F].length-1];
if (char == "none"){
if (fileName.includes(file)){
return true;
}
} else {
var split = fileName.split(char);
if (rank == "any"){
for (let i = 0; i < split.length; i++){
if (split[i] == file){
return true;
}
}
} else {
if (split[rank] == file){
return true;
}
}
}
}
return false;
}
/* checkFiles
Purpose: checks if two file names/paths are the same
Inputs: A - First file name/path to check
B - Second file name/path to compare to first
char - char to split file names by (usually '/')
Wait it does:
- writes to log files with data and time (UTC) and specifed message
Returned: none
Passes: none
*/
function checkFiles (A,B,char){
splitA = arraySplit(A, char);
splitB = arraySplit(B, char);
var test = true;
for (F in splitA){
fileNameA = splitA[F][splitA[F].length-1];
fileNameB = splitB[F][splitB[F].length-1];
if (fileNameA != fileNameB){
log(`ALL`, `ERROR: File ${fileNameA} in ${config.Setup.gitA} does not match ${fileNameB} in ${config.Setup.gitB}`, 2);
test = false;
}
}
return test
}
/* fileLoc
Purpose: checks if any of listed files are in specifed folder
Inputs: files - list of files synced to repoB
location - location where thay should have been place in repoB
Wait it does:
- splits up both locations of each file and specified location using array split
- returns true if any of the files are found to be in the specifdied location
Returned: True - if all files are in correct location
Passes: files - to array split to divide up file path
*/
function fileLoc (files, location){
if (location == ""){
return true;
} else {
var splitLocation = location.split('/');
var fileLocations = arraySplit(files, '/');
for (F in fileLocations){
for (var i = 0; i < splitLocation.length; i++) {
if (splitLocation[i] == fileLocations[F][i]){
return true;
}
}
} }
return false;
}
/* pullReq
Purpose: creates a pull request from synced branch to specified branch (usually dev or main)
Inputs: pastRepo - information about last repo push (used for label and message fo pull request)
branch - branch to generate a pull request from
config.Setup.repoB_branchB - brnach to generate a pull request to
Wait it does:
- generates a pull request for files synced from repo-A to repo-B
Returned: None
Passes: JSON file to GitHub Rest API
*/
function pullReq (pastRepo, branch){
console.log(`Starting Pull Request`);
var pullJSON = new Object();
console.log(pastRepo.finalCommitMessage)
pullJSON.title = `${pastRepo.finalCommitMessage}`;
pullJSON.head = `${branch}`;
pullJSON.base = `${config.Setup.repoB_branchB}`;
pullJSON.body = `Modified:${pastRepo.modifiedFiles}`;
pullJSON.maintainer_can_modify = true;
var jsonString = JSON.stringify(pullJSON);
console.log(`${jsonString}`)
log(`OP`, `JSON: pull request JSON generated`, 2);
let request = new XMLHttpRequest();
request.open('POST', `https://api.github.com/repos/${config.Setup.gitB}/pulls?access_token=${config.Auth.personal_access_token}`);
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
request.send(jsonString);
console.log(`https://api.github.com/repos/${config.Setup.gitB}/pulls?access_token=${config.Auth.personal_access_token}`)
log(`OP`, `JSON: pull request JSON sent to https://api.github.com/repos/${config.Setup.gitB}/pulls`, 2);
console.log('Pull Request Sent');
}