{ "always_run_in_app": false, "icon": { "color": "blue", "glyph": "sf:globe.europe.africa.fill" }, "name": "Netlify usage", "script": "// Variables used by Scriptable.\n// These must be at the very top of the file. Do not edit.\n// icon-color: blue; icon-glyph: sf:globe.europe.africa.fill;\n\n/*\n * Netlify Build Minutes\n * iOS Scriptable widget\n * Diego Zamboni, November 2026\n *\n * Supports small and medium widgets.\n * Run in Scriptable for initial setup.\n */\n\n// ── Configuration ───────────────────────────\n\nconst SETTINGS = {\n account: \"zzamboni\",\n refreshMinutes: 60,\n previewSize: \"small\",\n warningThreshold: 75,\n criticalThreshold: 90,\n keychainToken: \"netlify-widget-token\",\n keychainAccount: \"netlify-widget-account\",\n keychainCache: \"netlify-widget-cache\"\n};\n\nconst COLORS = {\n background: \"#101D26\",\n backgroundEnd: \"#172C36\",\n accent: \"#32D6C5\",\n primary: \"#FFFFFF\",\n secondary: \"#A2B5BF\",\n track: \"#30414B\",\n warning: \"#FFBE55\",\n critical: \"#FF6464\"\n};\n\n// ── Configuration and credentials ────────────\n\n\nasync function setup() {\n const alert = new Alert();\n alert.title = \"Netlify API Token\";\n alert.message =\n \"Copy your Netlify personal access token \" +\n \"to the clipboard before continuing.\";\n\n alert.addAction(\"Import from clipboard\");\n alert.addCancelAction(\"Cancel\");\n\n if (await alert.presentAlert() === -1) {\n throw new Error(\"Setup cancelled\");\n }\n\n const token = (Pasteboard.pasteString() || \"\").trim();\n\n if (!token) {\n throw new Error(\"Clipboard is empty\");\n }\n\n if (/\\s/.test(token)) {\n throw new Error(\"Invalid token format\");\n }\n\n Keychain.set(SETTINGS.keychainAccount, SETTINGS.account);\n Keychain.set(SETTINGS.keychainToken, token);\n\n if (Keychain.contains(SETTINGS.keychainCache)) {\n Keychain.remove(SETTINGS.keychainCache);\n }\n\n // Remove the token from the clipboard\n Pasteboard.copyString(\"\");\n}\n\n\n// ── Netlify API ──────────────────────────────\n\nasync function fetchUsage() {\n const account = Keychain.get(\n SETTINGS.keychainAccount\n );\n\n const token = Keychain.get(\n SETTINGS.keychainToken\n );\n\n const endpoint =\n \"https://api.netlify.com/api/v1/\" +\n encodeURIComponent(account) +\n \"/builds/status\";\n\n const request = new Request(endpoint);\n request.headers = {\n \"Authorization\": \"Bearer \" + token,\n \"Accept\": \"application/json\"\n };\n request.timeoutInterval = 15;\n\n const response = await request.loadJSON();\n const status = request.response.statusCode;\n\n if (status !== 200) {\n throw new Error(\"Netlify API: HTTP \" + status);\n }\n\n // Handle either an object or an array response.\n const data = Array.isArray(response)\n ? response[0]\n : response;\n\n if (!data || !data.minutes) {\n throw new Error(\"Build minute data unavailable\");\n }\n\n const m = data.minutes;\n\n if (\n m.current == null ||\n m.included_minutes == null\n ) {\n throw new Error(\"Build minute allowance unavailable\");\n }\n\n const used = Number(m.current);\n const included = Number(m.included_minutes);\n\n const withPacks = Number(\n m.included_minutes_with_packs\n );\n\n const limit = Math.max(\n included,\n Number.isFinite(withPacks) ? withPacks : 0\n );\n\n if (\n !Number.isFinite(used) ||\n !Number.isFinite(limit) ||\n used < 0 ||\n limit <= 0\n ) {\n throw new Error(\"Invalid build minute allowance\");\n }\n\n return {\n used: used,\n limit: limit,\n remaining: Math.max(0, limit - used),\n percentage: used / limit * 100,\n previous: m.previous ?? null,\n periodStart: m.period_start_date,\n periodEnd: m.period_end_date,\n updated: m.last_updated_at || new Date().toISOString(),\n fetched: new Date().toISOString(),\n builds: data.build_count ?? null,\n active: data.active ?? 0,\n queued: data.enqueued ?? 0,\n cached: false\n };\n}\n\nasync function getUsage() {\n try {\n const data = await fetchUsage();\n\n Keychain.set(\n SETTINGS.keychainCache,\n JSON.stringify(data)\n );\n\n return data;\n } catch (error) {\n console.warn(String(error));\n\n if (Keychain.contains(SETTINGS.keychainCache)) {\n const data = JSON.parse(\n Keychain.get(SETTINGS.keychainCache)\n );\n\n data.cached = true;\n return data;\n }\n\n throw error;\n }\n}\n\n// ── Formatting helpers ───────────────────────\n\nfunction formatNumber(n) {\n if (n == null || !Number.isFinite(Number(n))) {\n return \"–\";\n }\n\n return Number(n).toLocaleString(\n undefined,\n { maximumFractionDigits: 1 }\n );\n}\n\nfunction formatDate(value, pattern) {\n const date = new Date(value);\n\n if (!value || isNaN(date.getTime())) {\n return \"–\";\n }\n\n const formatter = new DateFormatter();\n formatter.dateFormat = pattern;\n return formatter.string(date);\n}\n\nfunction daysUntil(value) {\n const date = new Date(value);\n\n if (!value || isNaN(date.getTime())) {\n return null;\n }\n\n return Math.max(\n 0,\n Math.ceil(\n (date.getTime() - Date.now()) / 86400000\n )\n );\n}\n\nfunction usageColor(percentage) {\n if (percentage >= SETTINGS.criticalThreshold) {\n return COLORS.critical;\n }\n\n if (percentage >= SETTINGS.warningThreshold) {\n return COLORS.warning;\n }\n\n return COLORS.accent;\n}\n\nfunction addText(\n parent,\n value,\n size,\n color,\n weight = \"regular\"\n) {\n const text = parent.addText(String(value));\n\n const fonts = {\n regular: Font.systemFont,\n medium: Font.mediumSystemFont,\n semibold: Font.semiboldSystemFont,\n bold: Font.boldSystemFont\n };\n\n text.font = fonts[weight](size);\n text.textColor = new Color(color);\n text.lineLimit = 1;\n text.minimumScaleFactor = 0.65;\n\n return text;\n}\n\n// ── Progress bar ─────────────────────────────\n\nfunction progressBar(percentage, color, width) {\n const height = 8;\n const ctx = new DrawContext();\n\n ctx.size = new Size(width, height);\n ctx.opaque = false;\n ctx.respectScreenScale = true;\n\n function roundedRect(rect, radius, fill) {\n const path = new Path();\n path.addRoundedRect(rect, radius, radius);\n ctx.addPath(path);\n ctx.setFillColor(new Color(fill));\n ctx.fillPath();\n }\n\n roundedRect(\n new Rect(0, 0, width, height),\n 4,\n COLORS.track\n );\n\n const ratio = Math.min(\n 1, Math.max(0, percentage / 100)\n );\n\n const filled = width * ratio;\n\n if (filled > 0) {\n roundedRect(\n new Rect(0, 0, filled, height),\n Math.min(4, filled / 2),\n color\n );\n }\n\n return ctx.getImage();\n}\n\n// ── Widget appearance ────────────────────────\n\nfunction baseWidget() {\n const widget = new ListWidget();\n\n const gradient = new LinearGradient();\n gradient.colors = [\n new Color(COLORS.background),\n new Color(COLORS.backgroundEnd)\n ];\n gradient.locations = [0, 1];\n\n widget.backgroundGradient = gradient;\n widget.setPadding(14, 14, 14, 14);\n\n widget.refreshAfterDate = new Date(\n Date.now() + SETTINGS.refreshMinutes * 60000\n );\n\n if (Keychain.contains(SETTINGS.keychainAccount)) {\n const account = Keychain.get(\n SETTINGS.keychainAccount\n );\n\n widget.url =\n \"https://app.netlify.com/teams/\" +\n encodeURIComponent(account) +\n \"/builds\";\n }\n\n return widget;\n}\n\nfunction addHeader(widget, data, medium) {\n const header = widget.addStack();\n header.centerAlignContent();\n\n const symbol = SFSymbol.named(\"globe\");\n const icon = header.addImage(symbol.image);\n icon.imageSize = new Size(14, 14);\n icon.tintColor = new Color(COLORS.accent);\n\n header.addSpacer(5);\n\n addText(\n header, \"NETLIFY\", 11,\n COLORS.accent, \"bold\"\n );\n\n header.addSpacer();\n\n if (data.cached) {\n const warning = header.addImage(\n SFSymbol.named(\n \"exclamationmark.arrow.triangle.2.circlepath\"\n ).image\n );\n\n warning.imageSize = new Size(12, 12);\n warning.tintColor = new Color(COLORS.warning);\n } else if (medium) {\n addText(\n header,\n \"Updated \" + formatDate(data.updated, \"HH:mm\"),\n 10,\n COLORS.secondary\n );\n }\n}\n\nfunction addUsage(parent, data, medium) {\n const color = usageColor(data.percentage);\n\n addText(\n parent,\n formatNumber(data.used) +\n \" / \" + formatNumber(data.limit),\n medium ? 28 : 24,\n COLORS.primary,\n \"bold\"\n );\n\n parent.addSpacer(2);\n\n addText(\n parent,\n \"Build minutes used\",\n 11,\n COLORS.secondary\n );\n\n parent.addSpacer(8);\n\n const width = medium ? 170 : 120;\n\n const bar = parent.addImage(\n progressBar(data.percentage, color, width)\n );\n\n bar.imageSize = new Size(width, 8);\n\n parent.addSpacer(5);\n\n const row = parent.addStack();\n row.centerAlignContent();\n\n addText(\n row,\n Math.round(data.percentage) + \"%\",\n 12,\n color,\n \"semibold\"\n );\n\n row.addSpacer();\n\n addText(\n row,\n formatNumber(data.remaining) + \" left\",\n 11,\n COLORS.secondary\n );\n}\n\nfunction createWidget(data, medium) {\n const widget = baseWidget();\n\n addHeader(widget, data, medium);\n widget.addSpacer();\n\n if (medium) {\n const body = widget.addStack();\n\n const usage = body.addStack();\n usage.layoutVertically();\n\n addUsage(usage, data, true);\n\n body.addSpacer(18);\n\n const stats = body.addStack();\n stats.layoutVertically();\n\n addText(\n stats, \"Builds this period\",\n 10, COLORS.secondary\n );\n\n addText(\n stats, formatNumber(data.builds),\n 22, COLORS.primary, \"bold\"\n );\n\n stats.addSpacer(5);\n\n addText(\n stats, \"Previous period\",\n 10, COLORS.secondary\n );\n\n addText(\n stats,\n formatNumber(data.previous) + \" min\",\n 14, COLORS.primary, \"medium\"\n );\n\n if (data.active > 0 || data.queued > 0) {\n stats.addSpacer(4);\n\n addText(\n stats,\n data.active + \" active · \" +\n data.queued + \" queued\",\n 10, COLORS.accent\n );\n }\n } else {\n addUsage(widget, data, false);\n }\n\n widget.addSpacer();\n\n const days = daysUntil(data.periodEnd);\n const resetDate = formatDate(\n data.periodEnd, \"MMM d\"\n );\n\n const expired = data.cached &&\n data.periodEnd &&\n new Date(data.periodEnd).getTime() <= Date.now();\n\n let footer = expired\n ? \"Cached: previous billing period\"\n : \"Resets \" + resetDate;\n\n if (!expired && days !== null) {\n footer += \" · \" + days + \"d\";\n }\n\n addText(\n widget, footer, 10, COLORS.secondary\n );\n\n if (data.cached) {\n widget.addSpacer(2);\n\n addText(\n widget,\n \"Cached · \" +\n formatDate(data.fetched, \"MMM d HH:mm\"),\n 9,\n COLORS.warning\n );\n }\n\n return widget;\n}\n\nfunction errorWidget(message) {\n const widget = baseWidget();\n\n addText(\n widget, \"NETLIFY\", 12,\n COLORS.accent, \"bold\"\n );\n\n widget.addSpacer();\n\n addText(\n widget, \"Unavailable\", 20,\n COLORS.primary, \"bold\"\n );\n\n widget.addSpacer(5);\n\n const detail = widget.addText(message);\n detail.font = Font.systemFont(11);\n detail.textColor = new Color(COLORS.secondary);\n detail.lineLimit = 4;\n\n widget.addSpacer();\n\n return widget;\n}\n\n// ── Main ─────────────────────────────────────\n\nlet widget;\nlet medium = config.runsInWidget\n ? config.widgetFamily === \"medium\"\n : SETTINGS.previewSize === \"medium\";\n\ntry {\n const configured =\n Keychain.contains(SETTINGS.keychainToken) &&\n Keychain.contains(SETTINGS.keychainAccount);\n\n if (!configured) {\n if (config.runsInWidget) {\n throw new Error(\n \"Open this script in Scriptable to configure.\"\n );\n }\n\n await setup();\n } else if (!config.runsInWidget) {\n const menu = new Alert();\n menu.title = \"Netlify Build Minutes\";\n menu.addAction(\"Preview small widget\");\n menu.addAction(\"Preview medium widget\");\n menu.addAction(\"Change credentials\");\n menu.addCancelAction(\"Cancel\");\n\n const choice = await menu.presentSheet();\n\n if (choice === -1) {\n Script.complete();\n return;\n }\n\n if (choice === 0) medium = false;\n if (choice === 1) medium = true;\n if (choice === 2) await setup();\n }\n\n const data = await getUsage();\n widget = createWidget(data, medium);\n\n} catch (error) {\n widget = errorWidget(error.message || String(error));\n}\n\nScript.setWidget(widget);\n\nif (!config.runsInWidget) {\n if (medium) {\n await widget.presentMedium();\n } else {\n await widget.presentSmall();\n }\n}\n\nScript.complete();\n", "share_sheet_inputs": [] }