-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
1136 lines (972 loc) · 38.3 KB
/
Copy pathCode.gs
File metadata and controls
1136 lines (972 loc) · 38.3 KB
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* GitScript — Google Apps Script → GitHub Cloud Sync
* Backend v1.2
*
* Required Script Properties:
* GITHUB_TOKEN GitHub fine-grained/classic token with the repo permissions GitScript needs.
*
* Optional Script Properties:
* GITHUB_OWNER Default owner/org used by Create Repository. If omitted, authenticated user is used.
* CONFIG_SPREADSHEET_ID Configuration spreadsheet. Created automatically when omitted.
* SCHEDULE_TIMEZONE IANA timezone for scheduled syncs. Defaults to the Apps Script project timezone.
*
* Important:
* - Keep GITHUB_TOKEN server-side in Script Properties only.
* - Associate this Apps Script project with a standard Google Cloud project.
* - Enable the Google Apps Script API in that Cloud project.
* - Turn on Apps Script API access in the Apps Script account settings.
* - Deploy the web app as an identity that can read every mapped Apps Script project.
* - Time-based scheduling uses ONE 15-minute dispatcher trigger for all active scheduled integrations.
* - Supported schedules: recurring intervals, daily at an hour, or weekly on a chosen weekday + hour.
*/
const APP = Object.freeze({
NAME: 'GitScript',
SHEET_NAME: 'Sync Configurations',
GITHUB_API: 'https://api.github.com',
SCRIPT_API: 'https://script.googleapis.com/v1',
SCHEDULER_HANDLER: 'runScheduledSyncs',
SCHEDULER_INTERVAL_MINUTES: 15,
ALLOWED_INTERVAL_HOURS: [1, 2, 4, 6, 12, 24],
WEEKDAYS: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
LIFECYCLES: ['active', 'inactive', 'archived'],
HEADERS: [
'ID',
'Nickname',
'Script ID',
'Repository',
'Branch',
'Created At',
'Last Push',
'Sync Status',
'Lifecycle',
'Last Error',
'Schedule Enabled',
'Schedule Type',
'Schedule Interval Hours',
'Schedule Hour',
'Schedule Weekday',
'Schedule Timezone',
'Next Scheduled Sync',
'Last Scheduled Sync',
'Archived At'
]
});
/* ========================================================================== */
/* WEB APP */
/* ========================================================================== */
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index')
.setTitle(APP.NAME)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function getConnectedProfileEmail() {
return Session.getActiveUser().getEmail() || Session.getEffectiveUser().getEmail() || '';
}
/* ========================================================================== */
/* GITHUB */
/* ========================================================================== */
function getGitHubRepositories() {
const repos = githubRequest_('/user/repos?per_page=100&sort=updated&affiliation=owner,collaborator,organization_member', {
method: 'get'
});
return (repos || [])
.filter(repo => repo && repo.permissions && repo.permissions.push)
.map(repo => ({
id: repo.id,
name: repo.name,
fullName: repo.full_name,
private: Boolean(repo.private),
defaultBranch: repo.default_branch || 'main'
}));
}
function createGitHubRepository(input) {
const payload = input || {};
const name = String(payload.name || '').trim();
if (!/^[A-Za-z0-9._-]+$/.test(name)) {
throw new Error('Invalid repository name. Use letters, numbers, periods, underscores, or hyphens only.');
}
const props = PropertiesService.getScriptProperties();
const owner = String(props.getProperty('GITHUB_OWNER') || '').trim();
const authUser = githubRequest_('/user', { method: 'get' });
const isOrgTarget = owner && authUser && owner.toLowerCase() !== String(authUser.login || '').toLowerCase();
const endpoint = isOrgTarget ? '/orgs/' + encodeURIComponent(owner) + '/repos' : '/user/repos';
const repo = githubRequest_(endpoint, {
method: 'post',
payload: {
name: name,
private: Boolean(payload.private),
auto_init: true,
description: 'Source mirror managed by GitScript.'
}
});
return {
id: repo.id,
name: repo.name,
fullName: repo.full_name,
private: Boolean(repo.private),
defaultBranch: repo.default_branch || 'main'
};
}
/* ========================================================================== */
/* CONFIGURATION */
/* ========================================================================== */
/**
* Returns active, inactive, and archived integrations.
* The frontend can group/filter them without making separate requests.
*/
function getSyncConfigurations() {
return listConfigurations_().map(toPublicConfiguration_).reverse();
}
function saveSyncConfiguration(input) {
const data = input || {};
const nickname = String(data.nickname || '').trim();
const scriptId = normalizeScriptId_(data.scriptId || data.gasProjectLink);
const repository = String(data.repository || '').trim();
const branch = normalizeBranch_(data.branch || 'main');
if (!nickname) throw new Error('Project nickname is required.');
if (!repository || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repository)) {
throw new Error('Select a valid GitHub repository.');
}
// Fail early with an actionable Apps Script error instead of waiting for first sync.
validateAppsScriptProjectAccess_(scriptId);
// Verify GitHub access before persisting the route.
githubRequest_('/repos/' + repository, { method: 'get' });
const existing = listConfigurations_().find(item =>
item.lifecycle !== 'archived' &&
item.scriptId === scriptId &&
item.repository.toLowerCase() === repository.toLowerCase() &&
item.branch === branch
);
if (existing) {
throw new Error('This Apps Script project is already mapped to the selected repository and branch.');
}
const createdAt = new Date();
const id = Utilities.getUuid();
const sheet = getConfigSheet_();
const headers = getHeaders_(sheet);
appendObjectRow_(sheet, headers, {
'ID': id,
'Nickname': nickname,
'Script ID': scriptId,
'Repository': repository,
'Branch': branch,
'Created At': createdAt,
'Last Push': '',
'Sync Status': 'ready',
'Lifecycle': 'active',
'Last Error': '',
'Schedule Enabled': false,
'Schedule Type': 'manual',
'Schedule Interval Hours': '',
'Schedule Hour': '',
'Schedule Weekday': '',
'Schedule Timezone': getScheduleTimeZone_(),
'Next Scheduled Sync': '',
'Last Scheduled Sync': '',
'Archived At': ''
});
const config = findConfiguration_(id);
return toPublicConfiguration_(config);
}
function deactivateIntegration(configurationId) {
return setIntegrationLifecycle_(configurationId, 'inactive');
}
function reactivateIntegration(configurationId) {
return setIntegrationLifecycle_(configurationId, 'active');
}
function archiveIntegration(configurationId) {
return setIntegrationLifecycle_(configurationId, 'archived');
}
/** Alias used by the UI for archived records. */
function restoreIntegration(configurationId) {
return setIntegrationLifecycle_(configurationId, 'active');
}
function setIntegrationLifecycle_(configurationId, lifecycle) {
const desired = String(lifecycle || '').toLowerCase();
if (APP.LIFECYCLES.indexOf(desired) === -1) throw new Error('Invalid integration lifecycle.');
const config = requireConfiguration_(configurationId);
const sheet = getConfigSheet_();
const map = getHeaderMap_(sheet);
sheet.getRange(config.row, map['Lifecycle']).setValue(desired);
if (desired === 'archived') {
sheet.getRange(config.row, map['Archived At']).setValue(new Date());
} else {
sheet.getRange(config.row, map['Archived At']).clearContent();
}
// A lifecycle change can make the shared scheduler necessary or unnecessary.
reconcileSchedulerTrigger_();
return toPublicConfiguration_(requireConfiguration_(configurationId));
}
/* ========================================================================== */
/* SCHEDULING */
/* ========================================================================== */
/**
* Returns scheduler metadata for the schedule modal / diagnostics UI.
*/
function getSchedulerInfo() {
const triggers = getSchedulerTriggers_();
return {
timezone: getScheduleTimeZone_(),
dispatcherMinutes: APP.SCHEDULER_INTERVAL_MINUTES,
allowedIntervalHours: APP.ALLOWED_INTERVAL_HOURS.slice(),
weekdays: APP.WEEKDAYS.map((name, value) => ({ value: value, name: name })),
triggerActive: triggers.length > 0,
triggerCount: triggers.length
};
}
/**
* input examples:
* { enabled: false }
* { enabled: true, type: 'interval', intervalHours: 4 }
* { enabled: true, type: 'daily', hour: 8 }
* { enabled: true, type: 'weekly', weekday: 1, hour: 8 } // Monday at 8 AM
*/
function setIntegrationSchedule(configurationId, input) {
const config = requireConfiguration_(configurationId);
if (config.lifecycle === 'archived') {
throw new Error('Restore this integration before changing its schedule.');
}
const data = input || {};
const enabled = toBoolean_(data.enabled);
const timezone = getScheduleTimeZone_();
const sheet = getConfigSheet_();
const map = getHeaderMap_(sheet);
if (!enabled) {
setRowValuesByHeader_(sheet, config.row, map, {
'Schedule Enabled': false,
'Schedule Type': 'manual',
'Schedule Interval Hours': '',
'Schedule Hour': '',
'Schedule Weekday': '',
'Schedule Timezone': timezone,
'Next Scheduled Sync': ''
});
reconcileSchedulerTrigger_();
return toPublicConfiguration_(requireConfiguration_(configurationId));
}
const type = String(data.type || '').toLowerCase();
if (['interval', 'daily', 'weekly'].indexOf(type) === -1) {
throw new Error('Choose an interval, daily, or weekly schedule.');
}
let intervalHours = '';
let hour = '';
let weekday = '';
if (type === 'interval') {
intervalHours = Number(data.intervalHours);
if (APP.ALLOWED_INTERVAL_HOURS.indexOf(intervalHours) === -1) {
throw new Error('Unsupported interval. Choose 1, 2, 4, 6, 12, or 24 hours.');
}
}
if (type === 'daily' || type === 'weekly') {
hour = Number(data.hour);
if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
throw new Error('Schedule hour must be between 0 and 23.');
}
}
if (type === 'weekly') {
weekday = Number(data.weekday);
if (!Number.isInteger(weekday) || weekday < 0 || weekday > 6) {
throw new Error('Choose a valid day of the week.');
}
}
const schedule = {
enabled: true,
type: type,
intervalHours: intervalHours,
hour: hour,
weekday: weekday,
timezone: timezone
};
const nextRun = computeNextScheduledRun_(schedule, new Date());
setRowValuesByHeader_(sheet, config.row, map, {
'Schedule Enabled': true,
'Schedule Type': type,
'Schedule Interval Hours': intervalHours,
'Schedule Hour': hour,
'Schedule Weekday': weekday,
'Schedule Timezone': timezone,
'Next Scheduled Sync': nextRun
});
reconcileSchedulerTrigger_();
return toPublicConfiguration_(requireConfiguration_(configurationId));
}
function clearIntegrationSchedule(configurationId) {
return setIntegrationSchedule(configurationId, { enabled: false });
}
/**
* Installable time-based trigger handler.
* One dispatcher evaluates every active scheduled integration.
*/
function runScheduledSyncs() {
const now = new Date();
const due = listConfigurations_().filter(config => {
if (config.lifecycle !== 'active') return false;
if (!config.schedule.enabled) return false;
if (!config.schedule.nextRun) return true;
const next = new Date(config.schedule.nextRun);
return !isNaN(next.getTime()) && next.getTime() <= now.getTime();
});
const results = [];
due.forEach(config => {
let ok = false;
let message = '';
try {
const result = syncConfiguration_(config.id, 'scheduled');
ok = true;
message = 'Synced ' + (result.commitSha ? result.commitSha.substring(0, 7) : 'successfully');
} catch (error) {
message = error && error.message ? error.message : String(error);
console.error('Scheduled sync failed for ' + config.nickname + ': ' + message);
} finally {
const latest = requireConfiguration_(config.id);
const sheet = getConfigSheet_();
const map = getHeaderMap_(sheet);
const nextRun = computeNextScheduledRun_(latest.schedule, new Date());
setRowValuesByHeader_(sheet, latest.row, map, {
'Last Scheduled Sync': new Date(),
'Next Scheduled Sync': nextRun || ''
});
}
results.push({
id: config.id,
nickname: config.nickname,
ok: ok,
message: message
});
});
reconcileSchedulerTrigger_();
return {
checkedAt: now.toISOString(),
dueCount: due.length,
results: results
};
}
function reconcileSchedulerTrigger_() {
const needsTrigger = listConfigurations_().some(config =>
config.lifecycle === 'active' && config.schedule.enabled
);
const triggers = getSchedulerTriggers_();
if (needsTrigger && triggers.length === 0) {
ScriptApp.newTrigger(APP.SCHEDULER_HANDLER)
.timeBased()
.everyMinutes(APP.SCHEDULER_INTERVAL_MINUTES)
.create();
return;
}
if (!needsTrigger) {
triggers.forEach(trigger => ScriptApp.deleteTrigger(trigger));
return;
}
// Defensive cleanup if a prior deployment created duplicates.
if (triggers.length > 1) {
triggers.slice(1).forEach(trigger => ScriptApp.deleteTrigger(trigger));
}
}
function getSchedulerTriggers_() {
return ScriptApp.getProjectTriggers().filter(trigger =>
trigger.getHandlerFunction() === APP.SCHEDULER_HANDLER
);
}
function computeNextScheduledRun_(schedule, fromDate) {
if (!schedule || !schedule.enabled) return null;
const now = fromDate instanceof Date ? fromDate : new Date(fromDate || Date.now());
if (schedule.type === 'interval') {
const hours = Number(schedule.intervalHours);
if (APP.ALLOWED_INTERVAL_HOURS.indexOf(hours) === -1) return null;
return new Date(now.getTime() + hours * 60 * 60 * 1000);
}
if (schedule.type === 'daily') {
const hour = Number(schedule.hour);
if (!Number.isInteger(hour) || hour < 0 || hour > 23) return null;
const timezone = schedule.timezone || getScheduleTimeZone_();
const parts = getLocalDateParts_(now, timezone);
let target = dateFromLocalParts_(parts.year, parts.month, parts.day, hour, 0, timezone);
if (target.getTime() <= now.getTime() + 60 * 1000) {
const tomorrow = addLocalDays_(parts.year, parts.month, parts.day, 1);
target = dateFromLocalParts_(tomorrow.year, tomorrow.month, tomorrow.day, hour, 0, timezone);
}
return target;
}
if (schedule.type === 'weekly') {
const hour = Number(schedule.hour);
const weekday = Number(schedule.weekday);
if (!Number.isInteger(hour) || hour < 0 || hour > 23) return null;
if (!Number.isInteger(weekday) || weekday < 0 || weekday > 6) return null;
const timezone = schedule.timezone || getScheduleTimeZone_();
const parts = getLocalDateParts_(now, timezone);
const currentWeekday = getWeekdayForLocalDate_(parts.year, parts.month, parts.day);
let daysAhead = (weekday - currentWeekday + 7) % 7;
let targetParts = addLocalDays_(parts.year, parts.month, parts.day, daysAhead);
let target = dateFromLocalParts_(targetParts.year, targetParts.month, targetParts.day, hour, 0, timezone);
if (daysAhead === 0 && target.getTime() <= now.getTime() + 60 * 1000) {
targetParts = addLocalDays_(parts.year, parts.month, parts.day, 7);
target = dateFromLocalParts_(targetParts.year, targetParts.month, targetParts.day, hour, 0, timezone);
}
return target;
}
return null;
}
function getScheduleTimeZone_() {
const configured = String(PropertiesService.getScriptProperties().getProperty('SCHEDULE_TIMEZONE') || '').trim();
const fallback = Session.getScriptTimeZone() || 'Etc/UTC';
const timezone = configured || fallback;
try {
Utilities.formatDate(new Date(), timezone, 'yyyy-MM-dd HH:mm');
return timezone;
} catch (_) {
return fallback;
}
}
function getLocalDateParts_(date, timezone) {
const value = Utilities.formatDate(date, timezone, 'yyyy-MM-dd-HH-mm');
const p = value.split('-').map(Number);
return { year: p[0], month: p[1], day: p[2], hour: p[3], minute: p[4] };
}
function addLocalDays_(year, month, day, amount) {
const d = new Date(Date.UTC(year, month - 1, day));
d.setUTCDate(d.getUTCDate() + amount);
return {
year: d.getUTCFullYear(),
month: d.getUTCMonth() + 1,
day: d.getUTCDate()
};
}
/**
* Returns JavaScript weekday numbering for a local calendar date:
* 0 = Sunday ... 6 = Saturday.
*/
function getWeekdayForLocalDate_(year, month, day) {
return new Date(Date.UTC(year, month - 1, day)).getUTCDay();
}
/** Converts a local wall-clock time in an IANA timezone to a real Date. */
function dateFromLocalParts_(year, month, day, hour, minute, timezone) {
const utcWallClock = Date.UTC(year, month - 1, day, hour, minute, 0, 0);
let candidate = new Date(utcWallClock);
// Two iterations handle most DST offset boundaries correctly.
for (let i = 0; i < 2; i++) {
const offsetMinutes = parseTimezoneOffsetMinutes_(Utilities.formatDate(candidate, timezone, 'Z'));
candidate = new Date(utcWallClock - offsetMinutes * 60 * 1000);
}
return candidate;
}
function parseTimezoneOffsetMinutes_(value) {
const text = String(value || '+0000');
const match = text.match(/^([+-])(\d{2})(\d{2})$/);
if (!match) return 0;
const sign = match[1] === '-' ? -1 : 1;
return sign * (Number(match[2]) * 60 + Number(match[3]));
}
/* ========================================================================== */
/* SYNC */
/* ========================================================================== */
function syncNow(configurationId) {
return syncConfiguration_(configurationId, 'manual');
}
function syncConfiguration_(configurationId, source) {
const lock = LockService.getScriptLock();
if (!lock.tryLock(1500)) {
throw new Error('Another sync is currently running. Try again shortly.');
}
try {
const config = requireConfiguration_(configurationId);
if (config.lifecycle === 'inactive') {
throw new Error('This integration is inactive. Reactivate it before syncing.');
}
if (config.lifecycle === 'archived') {
throw new Error('This integration is archived. Restore it before syncing.');
}
const logs = [];
logs.push(source === 'scheduled' ? 'Scheduled sync started…' : 'Fetching Apps Script source…');
const files = getAppsScriptProjectContent_(config.scriptId);
if (!files.length) throw new Error('No Apps Script project files were returned.');
logs.push(files.length + ' source files indexed');
const repoParts = config.repository.split('/');
const owner = repoParts[0];
const repo = repoParts[1];
const branch = config.branch || 'main';
logs.push('Resolving GitHub branch…');
const headSha = ensureBranch_(owner, repo, branch);
const baseCommit = githubRequest_('/repos/' + owner + '/' + repo + '/git/commits/' + headSha, { method: 'get' });
const baseTreeSha = baseCommit.tree.sha;
logs.push('Creating Git blobs…');
const treeEntries = files.map(file => {
const blob = githubRequest_('/repos/' + owner + '/' + repo + '/git/blobs', {
method: 'post',
payload: {
content: String(file.source || ''),
encoding: 'utf-8'
}
});
return {
path: toGitHubFileName_(file),
mode: '100644',
type: 'blob',
sha: blob.sha
};
});
logs.push('Building source tree…');
const tree = githubRequest_('/repos/' + owner + '/' + repo + '/git/trees', {
method: 'post',
payload: {
base_tree: baseTreeSha,
tree: treeEntries
}
});
const commit = githubRequest_('/repos/' + owner + '/' + repo + '/git/commits', {
method: 'post',
payload: {
message: source === 'scheduled'
? 'Scheduled Apps Script sync via GitScript'
: 'Sync Apps Script source via GitScript',
tree: tree.sha,
parents: [headSha]
}
});
logs.push('Publishing commit to ' + branch + '…');
githubRequest_('/repos/' + owner + '/' + repo + '/git/refs/heads/' + encodeURIComponent(branch), {
method: 'patch',
payload: { sha: commit.sha, force: false }
});
const now = new Date();
updateSyncState_(config.row, {
lastPush: now,
syncStatus: 'ready',
lastError: ''
});
logs.push('Sync completed · ' + commit.sha.substring(0, 7));
return {
ok: true,
source: source,
lastPush: now.toISOString(),
commitSha: commit.sha,
commitUrl: 'https://github.com/' + owner + '/' + repo + '/commit/' + commit.sha,
logs: logs
};
} catch (error) {
try {
const config = findConfiguration_(configurationId);
if (config) {
updateSyncState_(config.row, {
syncStatus: 'error',
lastError: error && error.message ? error.message : String(error)
});
}
} catch (_) {}
throw error;
} finally {
lock.releaseLock();
}
}
/* ========================================================================== */
/* APPS SCRIPT API */
/* ========================================================================== */
function validateAppsScriptProjectAccess_(scriptId) {
const files = getAppsScriptProjectContent_(scriptId);
if (!Array.isArray(files)) throw new Error('Unable to validate the Apps Script project.');
return true;
}
function getAppsScriptProjectContent_(scriptId) {
const normalized = normalizeScriptId_(scriptId);
const token = ScriptApp.getOAuthToken();
const url = APP.SCRIPT_API + '/projects/' + encodeURIComponent(normalized) + '/content';
const response = UrlFetchApp.fetch(url, {
method: 'get',
headers: { Authorization: 'Bearer ' + token },
muteHttpExceptions: true
});
const code = response.getResponseCode();
const body = response.getContentText();
if (code < 200 || code >= 300) {
let message = extractApiMessage_(body);
if (code === 400) {
message += ' Confirm that you copied Project Settings → Script ID, not a Web App deployment URL or AKfy… deployment ID.';
} else if (code === 403) {
message += ' Confirm that the Apps Script API is enabled for GitScript’s standard Google Cloud project and that this Google account has access to the target script.';
} else if (code === 404) {
message += ' Confirm that the Script ID exists and that the signed-in Google account can access the project.';
}
throw new Error('Apps Script API error (' + code + '): ' + message);
}
const parsed = JSON.parse(body || '{}');
return Array.isArray(parsed.files) ? parsed.files : [];
}
/**
* Accepts:
* - https://script.google.com/home/projects/SCRIPT_ID/edit
* - https://script.google.com/u/0/home/projects/SCRIPT_ID/edit
* - https://script.google.com/d/SCRIPT_ID/edit
* - raw Script ID
*
* Explicitly rejects Web App deployment URLs and AKfy… deployment IDs.
*/
function normalizeScriptId_(value) {
const text = String(value || '').trim();
if (!text) throw new Error('Apps Script editor URL or Script ID is required.');
if (/script\.google\.com\/macros\/s\//i.test(text) || /^AKfy/i.test(text)) {
throw new Error(
'A Web App deployment ID was detected. GitScript needs the project Script ID. ' +
'Open the target Apps Script project → Project Settings → Script ID → Copy.'
);
}
const homeMatch = text.match(/script\.google\.com\/(?:u\/\d+\/)?home\/projects\/([A-Za-z0-9_-]{20,})/i);
if (homeMatch) return homeMatch[1];
const editorMatch = text.match(/script\.google\.com\/d\/([A-Za-z0-9_-]{20,})/i);
if (editorMatch) return editorMatch[1];
if (/^[A-Za-z0-9_-]{20,}$/.test(text)) return text;
throw new Error(
'Invalid Apps Script project identifier. Paste the Apps Script editor URL or Project Settings → Script ID.'
);
}
/* ========================================================================== */
/* GITHUB HELPERS */
/* ========================================================================== */
function ensureBranch_(owner, repo, branch) {
const path = '/repos/' + owner + '/' + repo + '/git/ref/heads/' + encodeURIComponent(branch);
try {
const ref = githubRequest_(path, { method: 'get' });
return ref.object.sha;
} catch (err) {
if (!String(err.message || '').includes('(404)')) throw err;
}
const repoInfo = githubRequest_('/repos/' + owner + '/' + repo, { method: 'get' });
const defaultBranch = repoInfo.default_branch || 'main';
const defaultRef = githubRequest_(
'/repos/' + owner + '/' + repo + '/git/ref/heads/' + encodeURIComponent(defaultBranch),
{ method: 'get' }
);
const sha = defaultRef.object.sha;
githubRequest_('/repos/' + owner + '/' + repo + '/git/refs', {
method: 'post',
payload: { ref: 'refs/heads/' + branch, sha: sha }
});
return sha;
}
function toGitHubFileName_(file) {
const name = String(file.name || 'Untitled');
const type = String(file.type || '').toUpperCase();
if (type === 'SERVER_JS') return name.endsWith('.gs') ? name : name + '.gs';
if (type === 'HTML') return name.endsWith('.html') ? name : name + '.html';
if (type === 'JSON') return name.endsWith('.json') ? name : name + '.json';
return name;
}
function githubRequest_(path, options) {
const token = PropertiesService.getScriptProperties().getProperty('GITHUB_TOKEN');
if (!token) {
throw new Error('Administrator setup required: GITHUB_TOKEN is missing from Script Properties.');
}
const opts = options || {};
const params = {
method: String(opts.method || 'get').toLowerCase(),
muteHttpExceptions: true,
headers: {
Authorization: 'Bearer ' + token,
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'GitScript'
}
};
if (opts.payload !== undefined) {
params.contentType = 'application/json';
params.payload = JSON.stringify(opts.payload);
}
const response = UrlFetchApp.fetch(APP.GITHUB_API + path, params);
const code = response.getResponseCode();
const text = response.getContentText();
let body = null;
try {
body = text ? JSON.parse(text) : {};
} catch (_) {
body = { message: text };
}
if (code < 200 || code >= 300) {
const message = body && body.message ? body.message : 'GitHub request failed.';
throw new Error('GitHub API error (' + code + '): ' + message);
}
return body;
}
/* ========================================================================== */
/* CONFIGURATION STORE */
/* ========================================================================== */
function getConfigSheet_() {
const props = PropertiesService.getScriptProperties();
let spreadsheetId = props.getProperty('CONFIG_SPREADSHEET_ID');
let spreadsheet;
if (spreadsheetId) {
spreadsheet = SpreadsheetApp.openById(spreadsheetId);
} else {
spreadsheet = SpreadsheetApp.create('GitScript — Configuration');
spreadsheetId = spreadsheet.getId();
props.setProperty('CONFIG_SPREADSHEET_ID', spreadsheetId);
}
let sheet = spreadsheet.getSheetByName(APP.SHEET_NAME);
if (!sheet) sheet = spreadsheet.insertSheet(APP.SHEET_NAME);
ensureSchema_(sheet);
return sheet;
}
/**
* Non-destructive migration from the original 8-column schema.
* Existing "Status" is renamed to "Sync Status" and missing columns are appended.
*/
function ensureSchema_(sheet) {
if (sheet.getLastRow() === 0 || sheet.getLastColumn() === 0) {
sheet.getRange(1, 1, 1, APP.HEADERS.length).setValues([APP.HEADERS]);
styleHeader_(sheet);
return;
}
const width = Math.max(1, sheet.getLastColumn());
const current = sheet.getRange(1, 1, 1, width).getValues()[0].map(value => String(value || '').trim());
// Preserve the old Status data while moving to separate sync-state + lifecycle fields.
const legacyStatusIndex = current.indexOf('Status');
if (legacyStatusIndex !== -1 && current.indexOf('Sync Status') === -1) {
current[legacyStatusIndex] = 'Sync Status';
sheet.getRange(1, legacyStatusIndex + 1).setValue('Sync Status');
}
const refreshed = sheet.getRange(1, 1, 1, Math.max(sheet.getLastColumn(), current.length)).getValues()[0]
.map(value => String(value || '').trim());
const missing = APP.HEADERS.filter(header => refreshed.indexOf(header) === -1);
if (missing.length) {
const startColumn = sheet.getLastColumn() + 1;
sheet.getRange(1, startColumn, 1, missing.length).setValues([missing]);
}
styleHeader_(sheet);
initializeLegacyRows_(sheet);
}
function styleHeader_(sheet) {
sheet.setFrozenRows(1);
sheet.getRange(1, 1, 1, sheet.getLastColumn()).setFontWeight('bold');
}
function initializeLegacyRows_(sheet) {
if (sheet.getLastRow() < 2) return;
const map = getHeaderMap_(sheet);
const rowCount = sheet.getLastRow() - 1;
const lifecycleRange = sheet.getRange(2, map['Lifecycle'], rowCount, 1);
const lifecycleValues = lifecycleRange.getValues();
let lifecycleChanged = false;
lifecycleValues.forEach(row => {
if (!String(row[0] || '').trim()) {
row[0] = 'active';
lifecycleChanged = true;
}
});
if (lifecycleChanged) lifecycleRange.setValues(lifecycleValues);
const statusRange = sheet.getRange(2, map['Sync Status'], rowCount, 1);
const statusValues = statusRange.getValues();
let statusChanged = false;
statusValues.forEach(row => {
if (!String(row[0] || '').trim()) {
row[0] = 'ready';
statusChanged = true;
}
});
if (statusChanged) statusRange.setValues(statusValues);
const scheduleEnabledRange = sheet.getRange(2, map['Schedule Enabled'], rowCount, 1);
const enabledValues = scheduleEnabledRange.getValues();
let enabledChanged = false;
enabledValues.forEach(row => {
if (row[0] === '') {
row[0] = false;
enabledChanged = true;
}
});
if (enabledChanged) scheduleEnabledRange.setValues(enabledValues);
const scheduleTypeRange = sheet.getRange(2, map['Schedule Type'], rowCount, 1);
const typeValues = scheduleTypeRange.getValues();
let typeChanged = false;
typeValues.forEach(row => {
if (!String(row[0] || '').trim()) {
row[0] = 'manual';
typeChanged = true;
}
});
if (typeChanged) scheduleTypeRange.setValues(typeValues);
const timezoneRange = sheet.getRange(2, map['Schedule Timezone'], rowCount, 1);
const tzValues = timezoneRange.getValues();
const timezone = getScheduleTimeZone_();
let tzChanged = false;
tzValues.forEach(row => {
if (!String(row[0] || '').trim()) {
row[0] = timezone;
tzChanged = true;
}
});
if (tzChanged) timezoneRange.setValues(tzValues);
}
function getHeaders_(sheet) {
return sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
.map(value => String(value || '').trim());
}
function getHeaderMap_(sheet) {
const headers = getHeaders_(sheet);
const map = {};
headers.forEach((header, index) => { if (header) map[header] = index + 1; });
return map;
}
function appendObjectRow_(sheet, headers, object) {
const row = headers.map(header => Object.prototype.hasOwnProperty.call(object, header) ? object[header] : '');
sheet.appendRow(row);
}
function setRowValuesByHeader_(sheet, row, map, object) {
Object.keys(object).forEach(header => {
if (!map[header]) throw new Error('Configuration schema is missing column: ' + header);
const range = sheet.getRange(row, map[header]);
const value = object[header];
if (value === '' || value === null || value === undefined) range.clearContent();
else range.setValue(value);
});
}
function listConfigurations_() {
const sheet = getConfigSheet_();
const lastRow = sheet.getLastRow();
if (lastRow < 2) return [];
const headers = getHeaders_(sheet);
const values = sheet.getRange(2, 1, lastRow - 1, headers.length).getValues();
return values
.map((row, index) => rowToConfiguration_(row, headers, index + 2))
.filter(config => config && config.id);
}
function findConfiguration_(id) {
const target = String(id || '').trim();
if (!target) return null;
return listConfigurations_().find(config => config.id === target) || null;
}
function requireConfiguration_(id) {
const config = findConfiguration_(id);
if (!config) throw new Error('Integration mapping not found.');
return config;
}
function rowToConfiguration_(row, headers, rowNumber) {
const obj = {};
headers.forEach((header, index) => { obj[header] = row[index]; });
const id = String(obj['ID'] || '').trim();
if (!id) return null;
const lifecycle = normalizeLifecycle_(obj['Lifecycle']);
const syncStatus = String(obj['Sync Status'] || 'ready').trim().toLowerCase() || 'ready';
const timezone = String(obj['Schedule Timezone'] || getScheduleTimeZone_()).trim() || getScheduleTimeZone_();
return {
row: rowNumber,
id: id,
nickname: String(obj['Nickname'] || ''),
scriptId: String(obj['Script ID'] || ''),
repository: String(obj['Repository'] || ''),
branch: String(obj['Branch'] || 'main'),
createdAt: serializeDate_(obj['Created At']),
lastPush: serializeDate_(obj['Last Push']),