@theholocron/holocron-plugin-checkly 5.0.0-alpha.216 → 5.0.0-alpha.217

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/dist/index.d.mts CHANGED
@@ -25,8 +25,25 @@ declare class ChecklyMonitoring implements Monitoring {
25
25
  * kept up to date, not duplicated. Every check on the account is subscribed, since a check made through the API is
26
26
  * never subscribed on its own; subscriptions already there (including ones to checks outside Holocron) are kept.
27
27
  * An existing channel keeps its own message template and send settings.
28
+ *
29
+ * Subscriptions go through Checkly's subscriptions call and are then read back: its create and update accept a
30
+ * `subscriptions` list but were seen not to apply it, so the count returned is what Checkly holds, and a check that
31
+ * could not be subscribed is an error and not a number.
28
32
  */
29
33
  ensureAlertChannel(input: MonitoringAlertChannelInput): Promise<MonitoringAlertChannelResult>;
34
+ /**
35
+ * After an update replaced the channel: read it back and put back any subscription it had that did not survive, of
36
+ * every kind (a group, a check outside this account) and in the state it had (a deactivated one stays deactivated).
37
+ */
38
+ private restoreSubscriptions;
39
+ /** One subscribe call; Checkly's 409 means the subscription is already there, which is what was wanted. */
40
+ private subscribeTolerating;
41
+ /**
42
+ * Subscribe the checks a channel lacks, then read it back. A subscription someone switched off is a choice: it is left
43
+ * off, not added a second time, and not counted. A call that fails does not stop the others: the channel is read back
44
+ * and the shortfall reported, with the first failure's reason. Returns how many were added and how many checks now alert.
45
+ */
46
+ private subscribeMissing;
30
47
  /** The id of every check on the account, read a page at a time. */
31
48
  private allCheckIds;
32
49
  /** Checkly's `search` param is a case-insensitive partial match — narrow to the exact name ourselves. */
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthError, createResolveToken, createRestClient } from "@theholocron/cli";
1
+ import { AuthError, ProviderApiError, createResolveToken, createRestClient } from "@theholocron/cli";
2
2
  import { DISCORD_TEMPLATE, createChecklyClient } from "@theholocron/checkly-client";
3
3
  import { errorMessage } from "@theholocron/misc-utils";
4
4
  //#region src/auth.ts
@@ -10,6 +10,17 @@ const resolveToken = createResolveToken({
10
10
  });
11
11
  //#endregion
12
12
  //#region src/capabilities/monitoring.ts
13
+ /**
14
+ * `monitoring` capability for Checkly.
15
+ *
16
+ * `ensureCheck` is a find-or-create-or-update by name: Checkly's API has
17
+ * no separate idempotency key, so a `search` list call (case-insensitive
18
+ * partial match) narrows to candidates, then an exact-name match picks
19
+ * the one to update. `checks` carries whatever this provider was
20
+ * constructed with (from the provider config tuple's second argument —
21
+ * same place `Deployment.domain` comes from) for `holocron repo setup`
22
+ * to loop over generically.
23
+ */
13
24
  const SOURCE_MAP = {
14
25
  status_code: "STATUS_CODE",
15
26
  response_time: "RESPONSE_TIME",
@@ -88,6 +99,10 @@ var ChecklyMonitoring = class {
88
99
  * kept up to date, not duplicated. Every check on the account is subscribed, since a check made through the API is
89
100
  * never subscribed on its own; subscriptions already there (including ones to checks outside Holocron) are kept.
90
101
  * An existing channel keeps its own message template and send settings.
102
+ *
103
+ * Subscriptions go through Checkly's subscriptions call and are then read back: its create and update accept a
104
+ * `subscriptions` list but were seen not to apply it, so the count returned is what Checkly holds, and a check that
105
+ * could not be subscribed is an error and not a number.
91
106
  */
92
107
  async ensureAlertChannel(input) {
93
108
  const client = this.client();
@@ -95,7 +110,7 @@ var ChecklyMonitoring = class {
95
110
  const existing = channels.find((c) => c.type === "WEBHOOK" && c.config.name === input.name);
96
111
  if (!existing) {
97
112
  if (!input.url) throw new Error(`cannot create alert channel "${input.name}": its webhook already exists and Discord shows a webhook URL only once, so delete that webhook and run setup again`);
98
- await client.alertChannels.create({
113
+ const created = await client.alertChannels.create({
99
114
  type: "WEBHOOK",
100
115
  config: {
101
116
  name: input.name,
@@ -109,66 +124,116 @@ var ChecklyMonitoring = class {
109
124
  sendFailure: true,
110
125
  sendRecovery: true,
111
126
  sendDegraded: true,
112
- autoSubscribe: false,
113
- subscriptions: checkIds.map((checkId) => ({
114
- checkId,
115
- activated: true
116
- }))
127
+ autoSubscribe: false
117
128
  });
129
+ const { active } = await this.subscribeMissing(created.id, input.name, checkIds);
118
130
  return {
119
131
  created: true,
120
132
  updated: [],
121
- subscribed: checkIds.length
133
+ subscribed: active
122
134
  };
123
135
  }
124
- const known = new Set(existing.subscriptions.flatMap((s) => s.checkId ? [s.checkId] : []));
125
- const active = new Set(existing.subscriptions.flatMap((s) => s.checkId && s.activated ? [s.checkId] : []));
126
- const missing = checkIds.filter((id) => !known.has(id));
127
136
  const urlChanged = input.url !== void 0 && existing.config.url !== input.url;
128
137
  const formatChanged = existing.config.webhookType !== "WEBHOOK_DISCORD";
129
- const updated = [
130
- ...urlChanged ? ["url"] : [],
131
- ...formatChanged ? ["format"] : [],
132
- ...missing.length > 0 ? ["subscriptions"] : []
133
- ];
134
- const total = active.size + missing.length;
135
- if (updated.length === 0) return {
138
+ let current;
139
+ if (urlChanged || formatChanged) {
140
+ const fresh = await client.alertChannels.get(existing.id);
141
+ const replacement = {
142
+ type: "WEBHOOK",
143
+ config: {
144
+ ...existing.config,
145
+ ...urlChanged ? { url: input.url } : {},
146
+ ...formatChanged ? {
147
+ webhookType: "WEBHOOK_DISCORD",
148
+ template: DISCORD_TEMPLATE
149
+ } : {}
150
+ },
151
+ sendRecovery: existing.sendRecovery,
152
+ sendFailure: existing.sendFailure,
153
+ sendDegraded: existing.sendDegraded,
154
+ sslExpiry: existing.sslExpiry,
155
+ sslExpiryThreshold: existing.sslExpiryThreshold,
156
+ autoSubscribe: existing.autoSubscribe,
157
+ subscriptions: fresh.subscriptions.map((s) => s.checkId ? {
158
+ checkId: s.checkId,
159
+ activated: s.activated
160
+ } : {
161
+ groupId: s.groupId,
162
+ activated: s.activated
163
+ })
164
+ };
165
+ await client.alertChannels.update(existing.id, replacement);
166
+ current = await this.restoreSubscriptions(existing.id, fresh.subscriptions);
167
+ }
168
+ const { added, active } = await this.subscribeMissing(existing.id, input.name, checkIds, current);
169
+ return {
136
170
  created: false,
137
- updated,
138
- subscribed: total
171
+ updated: [
172
+ ...urlChanged ? ["url"] : [],
173
+ ...formatChanged ? ["format"] : [],
174
+ ...added > 0 ? ["subscriptions"] : []
175
+ ],
176
+ subscribed: active
139
177
  };
140
- const replacement = {
141
- type: "WEBHOOK",
142
- config: {
143
- ...existing.config,
144
- ...urlChanged ? { url: input.url } : {},
145
- ...formatChanged ? {
146
- webhookType: "WEBHOOK_DISCORD",
147
- template: DISCORD_TEMPLATE
148
- } : {}
149
- },
150
- sendRecovery: existing.sendRecovery,
151
- sendFailure: existing.sendFailure,
152
- sendDegraded: existing.sendDegraded,
153
- sslExpiry: existing.sslExpiry,
154
- sslExpiryThreshold: existing.sslExpiryThreshold,
155
- autoSubscribe: existing.autoSubscribe,
156
- subscriptions: [...existing.subscriptions.map((s) => s.checkId ? {
157
- checkId: s.checkId,
158
- activated: s.activated
178
+ }
179
+ /**
180
+ * After an update replaced the channel: read it back and put back any subscription it had that did not survive, of
181
+ * every kind (a group, a check outside this account) and in the state it had (a deactivated one stays deactivated).
182
+ */
183
+ async restoreSubscriptions(channelId, had) {
184
+ const client = this.client();
185
+ const after = await client.alertChannels.get(channelId);
186
+ const held = (s) => after.subscriptions.some((a) => s.checkId ? a.checkId === s.checkId : a.groupId === s.groupId);
187
+ const lost = had.filter((s) => !held(s));
188
+ for (const s of lost) await this.subscribeTolerating(channelId, s);
189
+ return lost.length > 0 ? await client.alertChannels.get(channelId) : after;
190
+ }
191
+ /** One subscribe call; Checkly's 409 means the subscription is already there, which is what was wanted. */
192
+ async subscribeTolerating(channelId, subscription) {
193
+ try {
194
+ await this.client().alertChannels.subscribe(channelId, subscription.checkId ? {
195
+ checkId: subscription.checkId,
196
+ activated: subscription.activated
159
197
  } : {
160
- groupId: s.groupId,
161
- activated: s.activated
162
- }), ...missing.map((checkId) => ({
198
+ groupId: subscription.groupId,
199
+ activated: subscription.activated
200
+ });
201
+ return;
202
+ } catch (err) {
203
+ if (err instanceof ProviderApiError && err.status === 409) return void 0;
204
+ return err;
205
+ }
206
+ }
207
+ /**
208
+ * Subscribe the checks a channel lacks, then read it back. A subscription someone switched off is a choice: it is left
209
+ * off, not added a second time, and not counted. A call that fails does not stop the others: the channel is read back
210
+ * and the shortfall reported, with the first failure's reason. Returns how many were added and how many checks now alert.
211
+ */
212
+ async subscribeMissing(channelId, name, checkIds, current) {
213
+ const client = this.client();
214
+ const checkIdsOf = (channel, onlyActive) => new Set(channel.subscriptions.flatMap((s) => s.checkId && (!onlyActive || s.activated) ? [s.checkId] : []));
215
+ const before = current ?? await client.alertChannels.get(channelId);
216
+ const known = checkIdsOf(before, false);
217
+ const missing = checkIds.filter((id) => !known.has(id));
218
+ const failures = [];
219
+ for (const checkId of missing) {
220
+ const failure = await this.subscribeTolerating(channelId, {
163
221
  checkId,
164
222
  activated: true
165
- }))]
166
- };
167
- await client.alertChannels.update(existing.id, replacement);
223
+ });
224
+ if (failure) failures.push(failure);
225
+ }
226
+ const after = missing.length > 0 ? await client.alertChannels.get(channelId) : before;
227
+ const present = checkIdsOf(after, false);
228
+ const active = checkIdsOf(after, true);
229
+ const lacking = checkIds.filter((id) => !present.has(id) || missing.includes(id) && !active.has(id));
230
+ if (lacking.length > 0) {
231
+ const cause = failures[0] instanceof Error ? `: ${failures[0].message}` : "";
232
+ throw new Error(`Checkly did not subscribe ${lacking.length} of ${checkIds.length} checks to alert channel "${name}" (it holds ${active.size})${cause}; subscribe them in Checkly's alert settings and run setup again`);
233
+ }
168
234
  return {
169
- created: false,
170
- updated,
171
- subscribed: total
235
+ added: missing.length,
236
+ active: active.size
172
237
  };
173
238
  }
174
239
  /** The id of every check on the account, read a page at a time. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-checkly",
3
- "version": "5.0.0-alpha.216",
3
+ "version": "5.0.0-alpha.217",
4
4
  "description": "Holocron plugin for Checkly. Implements the monitoring capability against Checkly's REST API, plus exports verifyToken + AUTH_HINT for `holocron auth`.",
5
5
  "keywords": [
6
6
  "checkly",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "devDependencies": {
33
- "@theholocron/checkly-client": "^1.52.0",
33
+ "@theholocron/checkly-client": "^1.53.0",
34
34
  "@theholocron/http-client": "^1.43.1",
35
35
  "@theholocron/misc-utils": "^1.16.0",
36
36
  "@theholocron/tsconfig": "^8.8.2",
@@ -42,12 +42,12 @@
42
42
  "tsx": "4.23.15",
43
43
  "typescript": "^5.9.3",
44
44
  "vitest": "^4.1.11",
45
- "@theholocron/cli": "5.0.0-alpha.216"
45
+ "@theholocron/cli": "5.0.0-alpha.217"
46
46
  },
47
47
  "peerDependencies": {
48
- "@theholocron/checkly-client": "^1.52.0",
48
+ "@theholocron/checkly-client": "^1.53.0",
49
49
  "@theholocron/misc-utils": "^1.16.0",
50
- "@theholocron/cli": "5.0.0-alpha.216"
50
+ "@theholocron/cli": "5.0.0-alpha.217"
51
51
  },
52
52
  "engines": {
53
53
  "node": ">=22"