@certik/skynet 0.22.2 → 0.22.3

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/src/opsgenie.ts CHANGED
@@ -1,20 +1,126 @@
1
1
  import md5 from "md5";
2
2
 
3
- type OpsgenieResponse = {
4
- data: {
5
- success: boolean;
6
- action: string;
7
- processedAt: string;
8
- integrationId: string;
9
- isSuccess: boolean;
10
- status: string;
11
- alertId: string;
12
- alias: string;
13
- };
3
+ // ---- Request Types (Create Alert) ----
4
+ // See: https://docs.opsgenie.com/docs/alert-api#create-alert
5
+
6
+ export type OpsgeniePriority = "P1" | "P2" | "P3" | "P4" | "P5";
7
+
8
+ export interface TeamRefById {
9
+ id: string;
10
+ type: "team";
11
+ }
12
+ export interface TeamRefByName {
13
+ name: string;
14
+ type: "team";
15
+ }
16
+ export type TeamRef = TeamRefById | TeamRefByName;
17
+
18
+ export interface UserRefById {
19
+ id: string;
20
+ type: "user";
21
+ }
22
+ export interface UserRefByUsername {
23
+ username: string;
24
+ type: "user";
25
+ }
26
+ export type UserRef = UserRefById | UserRefByUsername;
27
+
28
+ export interface EscalationRefById {
29
+ id: string;
30
+ type: "escalation";
31
+ }
32
+ export interface EscalationRefByName {
33
+ name: string;
34
+ type: "escalation";
35
+ }
36
+ export type EscalationRef = EscalationRefById | EscalationRefByName;
37
+
38
+ export interface ScheduleRefById {
39
+ id: string;
40
+ type: "schedule";
41
+ }
42
+ export interface ScheduleRefByName {
43
+ name: string;
44
+ type: "schedule";
45
+ }
46
+ export type ScheduleRef = ScheduleRefById | ScheduleRefByName;
47
+
48
+ export type ResponderRef = TeamRef | UserRef | EscalationRef | ScheduleRef;
49
+
50
+ export interface VisibleToTeamById {
51
+ id: string;
52
+ type: "team";
53
+ }
54
+ export interface VisibleToTeamByName {
55
+ name: string;
56
+ type: "team";
57
+ }
58
+ export type VisibleToTeam = VisibleToTeamById | VisibleToTeamByName;
59
+ export interface VisibleToUserById {
60
+ id: string;
61
+ type: "user";
62
+ }
63
+ export interface VisibleToUserByUsername {
64
+ username: string;
65
+ type: "user";
66
+ }
67
+ export type VisibleToUser = VisibleToUserById | VisibleToUserByUsername;
68
+ export type VisibleToRef = VisibleToTeam | VisibleToUser;
69
+
70
+ export interface CreateAlertRequest {
71
+ message: string; // required
72
+ alias?: string;
73
+ description?: string;
74
+ responders?: ResponderRef[];
75
+ visibleTo?: VisibleToRef[];
76
+ actions?: string[]; // up to 10
77
+ tags?: string[]; // up to 20
78
+ details?: Record<string, string>;
79
+ entity?: string;
80
+ source?: string;
81
+ priority?: OpsgeniePriority; // default P3
82
+ user?: string; // display name of request owner
83
+ note?: string; // additional note added while creating
84
+ }
85
+
86
+ // ---- Generic Async Request Response (202 Accepted) ----
87
+ // Many Opsgenie alert mutation endpoints return only: { result, took, requestId }
88
+ export interface OpsgenieAcceptedResponse {
89
+ result: string; // "Request will be processed"
14
90
  took: number;
15
91
  requestId: string;
16
- result: string;
17
- };
92
+ }
93
+
94
+ // ---- Request Status (polling) Response ----
95
+ // When querying request status we receive a "data" envelope with success info.
96
+ export interface OpsgenieRequestStatusData {
97
+ success: boolean;
98
+ action: string; // e.g. Create, Acknowledge, etc.
99
+ processedAt: string; // ISO timestamp
100
+ integrationId: string;
101
+ isSuccess: boolean;
102
+ status: string; // human readable status e.g. "Created alert"
103
+ alertId: string; // may be empty string when failed
104
+ alias: string; // may be empty when failed
105
+ }
106
+
107
+ export interface OpsgenieRequestStatusResponse extends OpsgenieAcceptedResponse {
108
+ data: OpsgenieRequestStatusData;
109
+ }
110
+
111
+ // Some endpoints (create alert) return OpsgenieAcceptedResponse immediately, so our
112
+ // function currently returns whatever JSON body shape is present. We model the union.
113
+ export type OpsgenieResponse = OpsgenieAcceptedResponse | OpsgenieRequestStatusResponse;
114
+
115
+ // ---- Error Shape (from Opsgenie docs/experience) ----
116
+ // Keep loose to not block callers; refine if needed later.
117
+ export interface OpsgenieErrorResponse {
118
+ message?: string;
119
+ took?: number;
120
+ requestId?: string;
121
+ errors?: unknown;
122
+ status?: number | string;
123
+ }
18
124
 
19
125
  function getGenieKey(key?: string) {
20
126
  return key || process.env["SKYNET_OPSGENIE_API_KEY"];
@@ -25,14 +131,10 @@ function getGenieEndPoint() {
25
131
  }
26
132
 
27
133
  export async function postGenieMessage(
28
- body: {
29
- alias?: string;
30
- message: string;
31
- description?: string;
32
- },
134
+ body: CreateAlertRequest,
33
135
  apiKey?: string,
34
136
  verbose?: boolean,
35
- ) {
137
+ ): Promise<OpsgenieResponse> {
36
138
  try {
37
139
  const genieKey = apiKey || getGenieKey();
38
140
  const genieEndPoint = getGenieEndPoint();
@@ -56,15 +158,19 @@ export async function postGenieMessage(
56
158
  body: JSON.stringify(body),
57
159
  });
58
160
 
59
- const result = (await response.json()) as OpsgenieResponse;
161
+ const json = (await response.json()) as OpsgenieResponse | OpsgenieErrorResponse;
60
162
  if (verbose) {
61
- console.log(`Result of API call to Opsgenie... ${result}`);
163
+ console.log("Result of API call to Opsgenie...", json);
164
+ }
165
+
166
+ if ("result" in json) {
167
+ return json;
62
168
  }
63
169
 
64
- return result;
170
+ console.error("Error response from Opsgenie API", json);
171
+ throw new Error(json.message || 'Unknown error from Opsgenie API');
65
172
  } catch (error) {
66
173
  console.error("Failed to make opsgenie API call", error);
67
-
68
174
  throw error;
69
175
  }
70
176
  }
@@ -1,5 +0,0 @@
1
- {
2
- "conventionalCommits.scopes": [
3
- "lib-skynet"
4
- ]
5
- }
package/bun.lockb DELETED
Binary file