const PREVIEW_STATUS_PHASES = Object.freeze([ 'waiting_for_mail', 'invalid_mail', 'ambiguous_mail', 'retry_pending', 'uploaded', 'complete', 'needs_attention', 'blocked', 'queued', ]); const PREVIEW_STATUS_VISIBILITIES = Object.freeze(['public', 'unlisted', 'private']); function syncPreviewStatus() { const result = reportPreviewStatus_(); console.log(JSON.stringify(result)); return result; } function syncPreviewStatusAfterChange_(properties, previousStatus) { try { const previous = previewStatusState_(previousStatus); const current = previewStatusState_(properties.getProperty('publisher:last')); if (previous.phase !== current.phase || previous.day !== current.day) { reportPreviewStatus_(properties); } } catch (_) { // Status sync is independent of the publishing job. } } function previewStatusState_(raw) { const last = JSON.parse(raw || 'null'); const phase = PREVIEW_STATUS_PHASES.includes(last?.phase) ? last.phase : null; const candidateDay = last?.day; const date = typeof candidateDay === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(candidateDay) ? new Date(candidateDay + 'T00:00:00.000Z') : null; const day = date && Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === candidateDay ? candidateDay : null; return { phase, day }; } function reportPreviewStatus_(properties) { try { properties = properties || PropertiesService.getScriptProperties(); const url = properties.getProperty('VS_PREVIEW_STATUS_URL') || ''; const token = properties.getProperty('VS_PREVIEW_INSTALLATION_TOKEN') || ''; if (!url && !token) return { reported: false, outcome: 'not_configured' }; const endpoint = /^https:\/\/((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)\/api\/installations\/status$/; if (url.length > 286 || !endpoint.test(url) || !/^[a-f0-9]{64}$/.test(token)) { return { reported: false, outcome: 'invalid_configuration' }; } const activeConfig = properties.getProperty('publisher:active-config'); const config = activeConfig ? JSON.parse(activeConfig) : publisherConfig_(properties); if (typeof config.timeZone !== 'string' || !config.timeZone || !/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(config.startTime) || !PREVIEW_STATUS_VISIBILITIES.includes(config.visibility)) { return { reported: false, outcome: 'invalid_worker_settings' }; } new Intl.DateTimeFormat('en', { timeZone: config.timeZone }); const last = previewStatusState_(properties.getProperty('publisher:last')); const payload = { enabled: Boolean(activeConfig), triggerCount: publisherTriggers_().length, timeZone: config.timeZone, startTime: config.startTime, visibility: config.visibility, phase: last.phase, updatedAt: new Date().toISOString(), day: last.day, }; const response = UrlFetchApp.fetch(url, { method: 'put', contentType: 'application/json', headers: { Authorization: `Bearer ${token}` }, payload: JSON.stringify(payload), followRedirects: false, muteHttpExceptions: true, validateHttpsCertificates: true, }); const httpStatus = response.getResponseCode(); return { reported: httpStatus >= 200 && httpStatus < 300, httpStatus }; } catch (_) { return { reported: false, outcome: 'sync_failed' }; } }