@theholocron/cli 5.0.0-alpha.217 โ†’ 5.0.0-alpha.219

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -548,7 +548,7 @@ commands and the config are provider-neutral; Discord is the first provider.
548
548
  | `holocron notifications sync-roles` | Makes who holds a role match a GitHub team, through the links file (`--keep` only adds; `--provider` picks one) |
549
549
  | `holocron doctor notifications [--built-only] [--deliver]` | Step by step: does the server match the layout, do the secrets exist, and does a person (not the bot) own it. A full run also leaves one `๐Ÿงช test:` post in the layout's `testChannel`; `--built-only` posts nothing; `--deliver` posts to every channel and removes each post |
550
550
 
551
- Channels fed by another service are declared with `feeds` on the channel: `{ name: "uptime", webhook: true, feeds: { monitoring: true } }` (the object form `{ name?, webhook?, avatar? }` overrides the alert channel's name, default `Message to #<channel>`, the dedicated webhook's name, default the provider's, and gives that webhook a picture, a file path or `https://` URL, else the layout's `webhookAvatar`). `setup` gives the channel a dedicated webhook and hands its URL straight to the `monitoring` provider through `Monitoring.ensureAlertChannel({ name, format: "discord", url? })`, which finds an alert channel of that name or creates one and subscribes every check; the URL is never stored as a secret. It needs a `monitoring` provider that implements `ensureAlertChannel` (Checkly does). Two feeds may not share an alert channel name, and a feed's webhook may not share the channel's own webhook name (compared without regard to case).
551
+ Channels fed by another service are declared with `feeds` on the channel: `{ name: "uptime", webhook: true, feeds: { monitoring: true } }` (the object form `{ name?, webhook?, avatar? }` overrides the alert channel's name, default `Message to #<channel>`, the dedicated webhook's name, default the provider's, and gives that webhook a picture, a file path or `https://` URL, or `{ url, image }` (the URL is tried first, the file in the repo is the fallback), else the provider's own icon (`Monitoring.icon`, a URL and an embedded `data:` image; Checkly provides one), else the layout's `webhookAvatar`, else the layout's `webhookAvatar`). `setup` gives the channel a dedicated webhook and hands its URL straight to the `monitoring` provider through `Monitoring.ensureAlertChannel({ name, format: "discord", url? })`, which finds an alert channel of that name or creates one and subscribes every check; the URL is never stored as a secret. It needs a `monitoring` provider that implements `ensureAlertChannel` (Checkly does). Two feeds may not share an alert channel name, and a feed's webhook may not share the channel's own webhook name (compared without regard to case).
552
552
 
553
553
  Pinned messages are declared under the provider's `layout` as `pinned: [{ channel, content }]` (`channel` is a channel in the layout, `content` up to 2000 characters); the layout is the whole set of messages the bot has pinned in those channels.
554
554
 
package/dist/cli.mjs CHANGED
@@ -2690,6 +2690,7 @@ const tooBig = () => /* @__PURE__ */ new Error(`it is larger than ${MAX_AVATAR_B
2690
2690
  * links keep their tokens. A file path is returned as written.
2691
2691
  */
2692
2692
  function describeAvatarSource(source) {
2693
+ if (/^data:/i.test(source)) return "the built-in icon";
2693
2694
  if (!/^https:\/\//i.test(source)) return source;
2694
2695
  try {
2695
2696
  const url = new URL(source);
@@ -2698,9 +2699,28 @@ function describeAvatarSource(source) {
2698
2699
  return "an unreadable https URL";
2699
2700
  }
2700
2701
  }
2702
+ /**
2703
+ * Read a picture from the first of several sources that works, so a URL can be backed by a file in the repo. Says which
2704
+ * were skipped through `onSkip`; throws, naming every source and why, when none can be read.
2705
+ */
2706
+ async function loadFirstAvatar(sources, read, onSkip = () => {}) {
2707
+ const failures = [];
2708
+ for (const [i, source] of sources.entries()) try {
2709
+ return await read(source);
2710
+ } catch (err) {
2711
+ const reason = (err instanceof Error ? err.message : String(err)).split(source).join(describeAvatarSource(source));
2712
+ failures.push(`\`${describeAvatarSource(source)}\`: ${reason}`);
2713
+ onSkip(source, reason, i < sources.length - 1);
2714
+ }
2715
+ throw new Error(failures.join("; "));
2716
+ }
2701
2717
  async function loadAvatar(source, baseDir = process.cwd(), fetchImpl = fetch) {
2702
2718
  let bytes;
2703
- if (/^https:\/\//i.test(source)) {
2719
+ if (/^data:/i.test(source)) {
2720
+ const base64 = /^data:[^;,]+;base64,(.*)$/is.exec(source)?.[1];
2721
+ if (base64 === void 0) throw new Error("it is not a base64 data URI");
2722
+ bytes = Buffer.from(base64, "base64");
2723
+ } else if (/^https:\/\//i.test(source)) {
2704
2724
  const res = await fetchImpl(source, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS$2) });
2705
2725
  if (!res.ok) throw new Error(`fetching it answered ${res.status}`);
2706
2726
  if (Number(res.headers.get("content-length")) > 8388608) throw tooBig();
@@ -4894,9 +4914,20 @@ async function withElevation(run, deps) {
4894
4914
  function webhookAvatarOf(layout, webhook) {
4895
4915
  return (typeof webhook === "object" ? webhook.avatar : void 0) ?? layout.webhookAvatar;
4896
4916
  }
4897
- /** The picture a feed's webhook should have: its own, else the layout's, else none. */
4898
- function feedAvatarOf(layout, feed) {
4899
- return (typeof feed === "object" ? feed.avatar : void 0) ?? layout.webhookAvatar;
4917
+ /**
4918
+ * The pictures a feed's webhook may wear, in order of preference: the config's own (a single source, or `{ url, image }`,
4919
+ * the URL first and the file in the repo as its fallback), else the provider's own icon (its URL, then the image it carries),
4920
+ * else the layout's, else none.
4921
+ */
4922
+ function feedAvatarOf(layout, feed, monitoring) {
4923
+ const own = typeof feed === "object" ? feed.avatar : void 0;
4924
+ if (own === void 0) {
4925
+ const icon = [monitoring?.icon?.url, monitoring?.icon?.dataUri].filter((source) => Boolean(source));
4926
+ if (icon.length > 0) return icon;
4927
+ return layout.webhookAvatar ? [layout.webhookAvatar] : [];
4928
+ }
4929
+ if (typeof own === "string") return [own];
4930
+ return [own.url, own.image].filter((source) => Boolean(source));
4900
4931
  }
4901
4932
  /** The names a channel's monitoring feed uses: the alert channel (`Message to #uptime`) and its dedicated webhook (`Checkly`). */
4902
4933
  function monitoringFeed(channel, feed, monitoring) {
@@ -4917,6 +4948,12 @@ function feedProblem(layout, monitoring) {
4917
4948
  for (const category of layout.categories ?? []) for (const channel of category.channels ?? []) {
4918
4949
  if (!channel.feeds?.monitoring) continue;
4919
4950
  if (!monitoring.ensureAlertChannel) return `channel \`${channel.name}\` is fed by monitoring, but the \`${monitoring.providerName}\` provider cannot manage alert channels`;
4951
+ const picture = typeof channel.feeds.monitoring === "object" ? channel.feeds.monitoring.avatar : void 0;
4952
+ if (typeof picture === "object") {
4953
+ if (!picture.url && !picture.image) return `channel \`${channel.name}\`: \`feeds.monitoring.avatar\` needs a \`url\`, an \`image\`, or both`;
4954
+ if (picture.url && !/^https:\/\//i.test(picture.url)) return `channel \`${channel.name}\`: \`feeds.monitoring.avatar.url\` must be an https:// URL`;
4955
+ if (picture.image && /^https?:\/\//i.test(picture.image)) return `channel \`${channel.name}\`: \`feeds.monitoring.avatar.image\` must be a file in the repo, not a URL`;
4956
+ }
4920
4957
  const feed = monitoringFeed(channel.name, channel.feeds.monitoring, monitoring);
4921
4958
  const own = (typeof channel.webhook === "object" ? channel.webhook.name : void 0) ?? channel.name;
4922
4959
  if (channel.webhook && feed.webhook.toLowerCase() === own.toLowerCase()) return `channel \`${channel.name}\`: the monitoring webhook and the channel's own webhook are both named ${own} โ€” rename one`;
@@ -5077,18 +5114,22 @@ async function runNotificationsSetup(input$1) {
5077
5114
  const problem = feedProblem(layout, monitoring);
5078
5115
  if (problem) return refuse(problem);
5079
5116
  }
5117
+ const feedAvatarsOf = (channel) => channel.feeds?.monitoring ? feedAvatarOf(layout, channel.feeds.monitoring, monitoring) : [];
5080
5118
  const avatars = /* @__PURE__ */ new Map();
5081
- const read = input$1.loadAvatar ?? loadAvatar;
5119
+ const read = input$1.loadAvatar ?? ((source) => loadAvatar(source, input$1.context.repoRoot));
5082
5120
  for (const channel of (layout.categories ?? []).flatMap((c) => c.channels ?? [])) {
5083
- const feed = channel.feeds?.monitoring;
5084
- const sources = [channel.webhook ? webhookAvatarOf(layout, channel.webhook) : void 0, feed ? feedAvatarOf(layout, feed) : void 0];
5085
- for (const source of sources) {
5086
- if (source === void 0 || avatars.has(source)) continue;
5087
- try {
5088
- avatars.set(source, await read(source));
5089
- } catch (err) {
5090
- return refuse(`webhook avatar \`${describeAvatarSource(source)}\`: ${errorMessage(err)}`);
5091
- }
5121
+ const own = channel.webhook ? webhookAvatarOf(layout, channel.webhook) : void 0;
5122
+ if (own !== void 0 && !avatars.has(own)) try {
5123
+ avatars.set(own, await read(own));
5124
+ } catch (err) {
5125
+ return refuse(`webhook avatar \`${describeAvatarSource(own)}\`: ${errorMessage(err)}`);
5126
+ }
5127
+ const feedSources = feedAvatarsOf(channel);
5128
+ const feedKey = JSON.stringify(feedSources);
5129
+ if (feedSources.length > 0 && !avatars.has(feedKey)) try {
5130
+ avatars.set(feedKey, await loadFirstAvatar(feedSources, read, (source, reason, moreToTry) => print(` ${style.skip(`avatar ${describeAvatarSource(source)} could not be read (${reason})${moreToTry ? "; trying the next" : ""}`)}`)));
5131
+ } catch (err) {
5132
+ return refuse(`webhook avatar ${errorMessage(err)}`);
5092
5133
  }
5093
5134
  }
5094
5135
  if (secrets && scope && !dryRun) try {
@@ -5220,7 +5261,7 @@ async function runNotificationsSetup(input$1) {
5220
5261
  ...webhook ? { webhook } : {},
5221
5262
  ...channel.feeds?.monitoring ? { feeds: { monitoring: {
5222
5263
  ...monitoringFeed(channel.name, channel.feeds.monitoring, monitoring),
5223
- ...feedAvatarOf(layout, channel.feeds.monitoring) ? { avatar: avatars.get(feedAvatarOf(layout, channel.feeds.monitoring)) } : {}
5264
+ ...feedAvatarsOf(channel).length > 0 ? { avatar: avatars.get(JSON.stringify(feedAvatarsOf(channel))) } : {}
5224
5265
  } } } : {}
5225
5266
  });
5226
5267
  report.channels.push(outcome);