paseo-office 0.2.1 → 0.2.2
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/OVERVIEW.md +1 -1
- package/README.md +1 -1
- package/client/host-switches.tsx +15 -10
- package/client/tracker-connections.tsx +252 -0
- package/docs/office.md +18 -7
- package/index.server.ts +107 -7
- package/package.json +1 -1
- package/server/incidents-jira.ts +19 -7
- package/server/incidents-schedule.ts +10 -10
- package/server/incidents-sentry.ts +34 -8
- package/server/paths.ts +5 -0
- package/server/tracker-credentials.ts +141 -0
- package/shared/contracts.ts +64 -1
package/OVERVIEW.md
CHANGED
|
@@ -18,7 +18,7 @@ The settings gear controls the floor plan, backdrop, lighting and sound. Under *
|
|
|
18
18
|
|
|
19
19
|
**Archive idle sessions** is off by default. Enable it under **Office settings → Automation** if you want an open 3D floor to archive a session when its worker goes home. Workers can go home after resting for at least 30 minutes at night or after evening drinks. The session must still be idle, with no pending approval or working subagent. Archiving removes it from the active list and keeps its conversation history.
|
|
20
20
|
|
|
21
|
-
Stand-up notes need a host folder. Jira and Sentry alerts need project settings and host
|
|
21
|
+
Stand-up notes need a host folder. Jira and Sentry alerts need project settings and a connection: a Jira address, email and API token, and a Sentry auth token, entered in the settings or set in the host environment. Entered tokens are stored unencrypted on the host in a file only its user can read. Weather from a service needs a configured URL. Linux hosts can also show CPU, memory and processes.
|
|
22
22
|
|
|
23
23
|
## Data access
|
|
24
24
|
|
package/README.md
CHANGED
|
@@ -55,7 +55,7 @@ Boards, duties, team chat and stand-up state are stored under `$PASEO_HOME/offic
|
|
|
55
55
|
|
|
56
56
|
Agent prompts, tool results and scheduled assignments go through the agent’s configured provider. A **Whole group** chat message can go to one available session with up to six recent group or scene messages for context. **To**, mentions and replies address a particular session.
|
|
57
57
|
|
|
58
|
-
Jira and Sentry use project settings and
|
|
58
|
+
Jira and Sentry use project settings and a connection saved from the settings or read from the host environment. Saved tokens stay on the host, unencrypted, in a file only its user can read. A configured weather service receives requests while Office is open, at most once every 15 minutes. Its URL is visible in host settings. On stock hosts using fallback timers, scheduled work resumes after daemon restart when Office or an agent activates the plugin. Missed runs are skipped. [Plugin overview](https://github.com/allrested/paseo-office/blob/main/OVERVIEW.md) · [Implementation and storage](https://github.com/allrested/paseo-office/blob/main/docs/office.md)
|
|
59
59
|
|
|
60
60
|
## Development
|
|
61
61
|
|
package/client/host-switches.tsx
CHANGED
|
@@ -13,6 +13,7 @@ import { wakeSummary } from "./board-text.js";
|
|
|
13
13
|
import { FLOOR_PLAN_LABEL, FloorPlanRow } from "./floor-plan-row.js";
|
|
14
14
|
import { LabeledSwitch } from "./labeled-switch.js";
|
|
15
15
|
import { NOTES_DAYS_LABEL, NotesDays } from "./notes-days.js";
|
|
16
|
+
import { JiraConnection, SentryConnection } from "./tracker-connections.js";
|
|
16
17
|
import type { OfficeStyles } from "./styles.js";
|
|
17
18
|
import type { BoardActions } from "./use-board-actions.js";
|
|
18
19
|
|
|
@@ -36,11 +37,13 @@ type HostSetting =
|
|
|
36
37
|
| WeatherSetting;
|
|
37
38
|
|
|
38
39
|
/** What the incidents fields need to know from the host: never a token's value. */
|
|
39
|
-
type TrackerState = Pick<
|
|
40
|
+
type TrackerState = Pick<
|
|
41
|
+
IncidentsState,
|
|
42
|
+
"jiraReady" | "sentryReady" | "scheduleError" | "connections"
|
|
43
|
+
>;
|
|
40
44
|
|
|
41
|
-
const JIRA_UNSET =
|
|
42
|
-
|
|
43
|
-
const SENTRY_UNSET = "SENTRY_AUTH_TOKEN is not set on this host";
|
|
45
|
+
const JIRA_UNSET = "No Jira connection on this host: save one above";
|
|
46
|
+
const SENTRY_UNSET = "No Sentry token on this host: save one above";
|
|
44
47
|
|
|
45
48
|
/** The line under a tracker's field: `missing` while the field is set, nothing while it is empty. */
|
|
46
49
|
function unsetLine(value: string | null, missing: string | null): string | null {
|
|
@@ -313,7 +316,7 @@ interface TrackerFieldsProps {
|
|
|
313
316
|
|
|
314
317
|
/**
|
|
315
318
|
* The trackers the incidents check reads, and its schedule's failure under them. A field set
|
|
316
|
-
* while the host
|
|
319
|
+
* while the host has no sign-in for the tracker says so; the token itself never comes back here.
|
|
317
320
|
*/
|
|
318
321
|
function TrackerFields({ values, incidents, change, saveProblem, styles }: TrackerFieldsProps) {
|
|
319
322
|
const saveJiraProjects = useCallback(
|
|
@@ -341,7 +344,7 @@ function TrackerFields({ values, incidents, change, saveProblem, styles }: Track
|
|
|
341
344
|
// Not known until the host has said: no line before then.
|
|
342
345
|
const jiraMissing = incidents?.jiraReady === false ? JIRA_UNSET : null;
|
|
343
346
|
const sentryMissing = incidents?.sentryReady === false ? SENTRY_UNSET : null;
|
|
344
|
-
// A refused save comes first; while the host refuses nothing, the
|
|
347
|
+
// A refused save comes first; while the host refuses nothing, the sign-in it lacks.
|
|
345
348
|
const jiraProblem = saveProblem("Jira projects") ?? unsetLine(jiraProjects, jiraMissing);
|
|
346
349
|
const sentryOrgProblem =
|
|
347
350
|
saveProblem("Sentry organisation") ?? unsetLine(sentryOrg, sentryMissing);
|
|
@@ -352,17 +355,19 @@ function TrackerFields({ values, incidents, change, saveProblem, styles }: Track
|
|
|
352
355
|
return (
|
|
353
356
|
<>
|
|
354
357
|
<View style={styles.headerFields}>
|
|
358
|
+
<JiraConnection connection={incidents?.connections?.jira} styles={styles} />
|
|
355
359
|
<SettingField
|
|
356
360
|
label="Jira projects"
|
|
357
|
-
hint="Project keys, comma separated.
|
|
361
|
+
hint="Project keys, comma separated. New bugs in them ring the incident alarm."
|
|
358
362
|
value={jiraProjects}
|
|
359
363
|
onSave={saveJiraProjects}
|
|
360
364
|
problem={jiraProblem}
|
|
361
365
|
styles={styles}
|
|
362
366
|
/>
|
|
367
|
+
<SentryConnection connection={incidents?.connections?.sentry} styles={styles} />
|
|
363
368
|
<SettingField
|
|
364
369
|
label="Sentry organisation"
|
|
365
|
-
hint="The organisation's slug.
|
|
370
|
+
hint="The organisation's slug."
|
|
366
371
|
value={sentryOrg}
|
|
367
372
|
onSave={saveSentryOrg}
|
|
368
373
|
problem={sentryOrgProblem}
|
|
@@ -370,7 +375,7 @@ function TrackerFields({ values, incidents, change, saveProblem, styles }: Track
|
|
|
370
375
|
/>
|
|
371
376
|
<SettingField
|
|
372
377
|
label="Sentry projects"
|
|
373
|
-
hint="Project slugs, comma separated.
|
|
378
|
+
hint="Project slugs, comma separated."
|
|
374
379
|
value={sentryProjects}
|
|
375
380
|
onSave={saveSentryProjects}
|
|
376
381
|
problem={sentryProjectsProblem}
|
|
@@ -378,7 +383,7 @@ function TrackerFields({ values, incidents, change, saveProblem, styles }: Track
|
|
|
378
383
|
/>
|
|
379
384
|
<SettingField
|
|
380
385
|
label="Sentry address"
|
|
381
|
-
hint="Where
|
|
386
|
+
hint="Where the Sentry token is sent. https, or http on the host itself."
|
|
382
387
|
value={sentryUrl}
|
|
383
388
|
onSave={saveSentryUrl}
|
|
384
389
|
problem={sentryUrlProblem}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
import { Text, View } from "react-native";
|
|
3
|
+
import { useRpc } from "@getpaseo/plugin/client";
|
|
4
|
+
import { TextInput } from "@getpaseo/plugin/client/ui";
|
|
5
|
+
import {
|
|
6
|
+
trackersSaveRpc,
|
|
7
|
+
trackersTestRpc,
|
|
8
|
+
type IncidentsState,
|
|
9
|
+
type TrackerSource,
|
|
10
|
+
type TrackersResult,
|
|
11
|
+
} from "../shared/contracts.js";
|
|
12
|
+
import { ActionButton } from "./action-button.js";
|
|
13
|
+
import type { OfficeStyles } from "./styles.js";
|
|
14
|
+
|
|
15
|
+
type Connections = NonNullable<IncidentsState["connections"]>;
|
|
16
|
+
|
|
17
|
+
/** Where the host's sign-in comes from, as the line under a connection says it. */
|
|
18
|
+
export function sourceLine(name: string, source: TrackerSource | undefined): string | null {
|
|
19
|
+
switch (source) {
|
|
20
|
+
case "saved":
|
|
21
|
+
return `${name} connection saved on this host`;
|
|
22
|
+
case "env":
|
|
23
|
+
return `${name} connection from the host's environment; a saved one replaces it`;
|
|
24
|
+
case "none":
|
|
25
|
+
return `No ${name} connection on this host`;
|
|
26
|
+
default:
|
|
27
|
+
// Not known until the host has said: nothing is claimed before then.
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The host's answer as a button takes it: a refusal becomes the button's error. */
|
|
33
|
+
function accept(result: TrackersResult): void {
|
|
34
|
+
if (!result.ok) throw new Error(result.problem);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface FieldProps {
|
|
38
|
+
label: string;
|
|
39
|
+
hint: string;
|
|
40
|
+
value: string;
|
|
41
|
+
onChangeText(next: string): void;
|
|
42
|
+
secret?: boolean;
|
|
43
|
+
styles: OfficeStyles;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Typed text that is kept until a button sends it, unlike a setting's field. */
|
|
47
|
+
function ConnectionField({ label, hint, value, onChangeText, secret, styles }: FieldProps) {
|
|
48
|
+
return (
|
|
49
|
+
<View style={styles.headerField}>
|
|
50
|
+
<Text style={styles.sheetLabel}>{label}</Text>
|
|
51
|
+
<TextInput
|
|
52
|
+
accessibilityLabel={label}
|
|
53
|
+
accessibilityHint={hint}
|
|
54
|
+
value={value}
|
|
55
|
+
onChangeText={onChangeText}
|
|
56
|
+
secureTextEntry={secret === true}
|
|
57
|
+
autoCapitalize="none"
|
|
58
|
+
autoCorrect={false}
|
|
59
|
+
spellCheck={false}
|
|
60
|
+
style={styles.fieldInput}
|
|
61
|
+
/>
|
|
62
|
+
<Text style={styles.fieldHint}>{hint}</Text>
|
|
63
|
+
</View>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface ButtonsProps {
|
|
68
|
+
name: string;
|
|
69
|
+
source: TrackerSource | undefined;
|
|
70
|
+
/** What the last test or save came to; null before one, and after a change it no longer fits. */
|
|
71
|
+
note: string | null;
|
|
72
|
+
test(): Promise<void>;
|
|
73
|
+
save(): Promise<void>;
|
|
74
|
+
clear(): Promise<void>;
|
|
75
|
+
styles: OfficeStyles;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function ConnectionButtons({ name, source, note, test, save, clear, styles }: ButtonsProps) {
|
|
79
|
+
const line = note ?? sourceLine(name, source);
|
|
80
|
+
return (
|
|
81
|
+
<View style={styles.headerField}>
|
|
82
|
+
{line === null ? null : <Text style={styles.fieldHint}>{line}</Text>}
|
|
83
|
+
<View style={styles.actions}>
|
|
84
|
+
<ActionButton
|
|
85
|
+
label="Test connection"
|
|
86
|
+
pendingLabel="Testing..."
|
|
87
|
+
name={`Test ${name} connection`}
|
|
88
|
+
onPress={test}
|
|
89
|
+
styles={styles}
|
|
90
|
+
/>
|
|
91
|
+
<ActionButton
|
|
92
|
+
label="Save connection"
|
|
93
|
+
pendingLabel="Saving..."
|
|
94
|
+
name={`Save ${name} connection`}
|
|
95
|
+
onPress={save}
|
|
96
|
+
primary
|
|
97
|
+
styles={styles}
|
|
98
|
+
/>
|
|
99
|
+
{source === "saved" ? (
|
|
100
|
+
<ActionButton
|
|
101
|
+
label="Remove"
|
|
102
|
+
pendingLabel="Removing..."
|
|
103
|
+
name={`Remove saved ${name} connection`}
|
|
104
|
+
onPress={clear}
|
|
105
|
+
styles={styles}
|
|
106
|
+
/>
|
|
107
|
+
) : null}
|
|
108
|
+
</View>
|
|
109
|
+
</View>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface JiraProps {
|
|
114
|
+
/** Absent until the host has said where its sign-in comes from. */
|
|
115
|
+
connection: Connections["jira"] | undefined;
|
|
116
|
+
styles: OfficeStyles;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Jira's address, email and API token, saved on the host in a file only the daemon's user can
|
|
121
|
+
* read. The token goes to the host and never comes back: its field is empty for the one kept.
|
|
122
|
+
*/
|
|
123
|
+
export function JiraConnection({ connection, styles }: JiraProps) {
|
|
124
|
+
const callTest = useRpc(trackersTestRpc);
|
|
125
|
+
const callSave = useRpc(trackersSaveRpc);
|
|
126
|
+
// What is typed; null shows what the host holds.
|
|
127
|
+
const [baseUrl, setBaseUrl] = useState<string | null>(null);
|
|
128
|
+
const [email, setEmail] = useState<string | null>(null);
|
|
129
|
+
const [token, setToken] = useState("");
|
|
130
|
+
const [note, setNote] = useState<string | null>(null);
|
|
131
|
+
const typed = useCallback(
|
|
132
|
+
() => ({
|
|
133
|
+
baseUrl: baseUrl ?? connection?.baseUrl ?? "",
|
|
134
|
+
email: email ?? connection?.email ?? "",
|
|
135
|
+
token,
|
|
136
|
+
}),
|
|
137
|
+
[baseUrl, email, token, connection],
|
|
138
|
+
);
|
|
139
|
+
const test = useCallback(async () => {
|
|
140
|
+
setNote(null);
|
|
141
|
+
accept(await callTest({ tracker: "jira", ...typed() }));
|
|
142
|
+
setNote("Jira accepted the sign-in");
|
|
143
|
+
}, [callTest, typed]);
|
|
144
|
+
const save = useCallback(async () => {
|
|
145
|
+
setNote(null);
|
|
146
|
+
accept(await callSave({ action: "save-jira", ...typed() }));
|
|
147
|
+
setBaseUrl(null);
|
|
148
|
+
setEmail(null);
|
|
149
|
+
setToken("");
|
|
150
|
+
}, [callSave, typed]);
|
|
151
|
+
const clear = useCallback(async () => {
|
|
152
|
+
setNote(null);
|
|
153
|
+
accept(await callSave({ action: "clear", tracker: "jira" }));
|
|
154
|
+
setBaseUrl(null);
|
|
155
|
+
setEmail(null);
|
|
156
|
+
setToken("");
|
|
157
|
+
}, [callSave]);
|
|
158
|
+
const kept = connection?.source === "saved";
|
|
159
|
+
return (
|
|
160
|
+
<>
|
|
161
|
+
<ConnectionField
|
|
162
|
+
label="Jira address"
|
|
163
|
+
hint="Your Jira site, such as https://your-site.atlassian.net."
|
|
164
|
+
value={baseUrl ?? connection?.baseUrl ?? ""}
|
|
165
|
+
onChangeText={setBaseUrl}
|
|
166
|
+
styles={styles}
|
|
167
|
+
/>
|
|
168
|
+
<ConnectionField
|
|
169
|
+
label="Jira email"
|
|
170
|
+
hint="The Atlassian account the API token belongs to."
|
|
171
|
+
value={email ?? connection?.email ?? ""}
|
|
172
|
+
onChangeText={setEmail}
|
|
173
|
+
styles={styles}
|
|
174
|
+
/>
|
|
175
|
+
<ConnectionField
|
|
176
|
+
label="Jira API token"
|
|
177
|
+
hint={
|
|
178
|
+
kept
|
|
179
|
+
? "Leave empty to keep the saved token. Stored on the host for its user only; never shown again."
|
|
180
|
+
: "Stored on the host for its user only; never shown again."
|
|
181
|
+
}
|
|
182
|
+
value={token}
|
|
183
|
+
onChangeText={setToken}
|
|
184
|
+
secret
|
|
185
|
+
styles={styles}
|
|
186
|
+
/>
|
|
187
|
+
<ConnectionButtons
|
|
188
|
+
name="Jira"
|
|
189
|
+
source={connection?.source}
|
|
190
|
+
note={note}
|
|
191
|
+
test={test}
|
|
192
|
+
save={save}
|
|
193
|
+
clear={clear}
|
|
194
|
+
styles={styles}
|
|
195
|
+
/>
|
|
196
|
+
</>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface SentryProps {
|
|
201
|
+
connection: Connections["sentry"] | undefined;
|
|
202
|
+
styles: OfficeStyles;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Sentry's auth token, kept as Jira's. A test asks the organisation and address set below. */
|
|
206
|
+
export function SentryConnection({ connection, styles }: SentryProps) {
|
|
207
|
+
const callTest = useRpc(trackersTestRpc);
|
|
208
|
+
const callSave = useRpc(trackersSaveRpc);
|
|
209
|
+
const [token, setToken] = useState("");
|
|
210
|
+
const [note, setNote] = useState<string | null>(null);
|
|
211
|
+
const test = useCallback(async () => {
|
|
212
|
+
setNote(null);
|
|
213
|
+
accept(await callTest({ tracker: "sentry", token }));
|
|
214
|
+
setNote("Sentry accepted the token");
|
|
215
|
+
}, [callTest, token]);
|
|
216
|
+
const save = useCallback(async () => {
|
|
217
|
+
setNote(null);
|
|
218
|
+
accept(await callSave({ action: "save-sentry", token }));
|
|
219
|
+
setToken("");
|
|
220
|
+
}, [callSave, token]);
|
|
221
|
+
const clear = useCallback(async () => {
|
|
222
|
+
setNote(null);
|
|
223
|
+
accept(await callSave({ action: "clear", tracker: "sentry" }));
|
|
224
|
+
setToken("");
|
|
225
|
+
}, [callSave]);
|
|
226
|
+
const kept = connection?.source === "saved";
|
|
227
|
+
return (
|
|
228
|
+
<>
|
|
229
|
+
<ConnectionField
|
|
230
|
+
label="Sentry auth token"
|
|
231
|
+
hint={
|
|
232
|
+
kept
|
|
233
|
+
? "A token is saved; type a new one to replace it. Stored on the host for its user only; never shown again."
|
|
234
|
+
: "Stored on the host for its user only; never shown again."
|
|
235
|
+
}
|
|
236
|
+
value={token}
|
|
237
|
+
onChangeText={setToken}
|
|
238
|
+
secret
|
|
239
|
+
styles={styles}
|
|
240
|
+
/>
|
|
241
|
+
<ConnectionButtons
|
|
242
|
+
name="Sentry"
|
|
243
|
+
source={connection?.source}
|
|
244
|
+
note={note}
|
|
245
|
+
test={test}
|
|
246
|
+
save={save}
|
|
247
|
+
clear={clear}
|
|
248
|
+
styles={styles}
|
|
249
|
+
/>
|
|
250
|
+
</>
|
|
251
|
+
);
|
|
252
|
+
}
|
package/docs/office.md
CHANGED
|
@@ -472,7 +472,7 @@ The gear in fullscreen opens **Office settings** over the floor. You can also op
|
|
|
472
472
|
| ---------- | -------------------------------------------------------------------------------------------- |
|
|
473
473
|
| Appearance | Office view, floor plan, outdoor scene, lighting, sound, scene language and shared branding. |
|
|
474
474
|
| Automation | Agent wake-ups, agent tools, stand-up notes folder, time zone and scheduled days. |
|
|
475
|
-
| Services | Jira
|
|
475
|
+
| Services | Jira and Sentry connections and projects, and the optional weather service. |
|
|
476
476
|
|
|
477
477
|
View and sound preferences apply to the device. The host stores the floor plan, branding, automation and integration settings. The [branding defaults](#custom-branding) can also come from the daemon environment.
|
|
478
478
|
|
|
@@ -589,12 +589,23 @@ office sets them from its settings each time it starts and at each save, and sto
|
|
|
589
589
|
## Incidents
|
|
590
590
|
|
|
591
591
|
The office rings my-office's incident alarm for a new Jira bug or Sentry issue. "Jira projects"
|
|
592
|
-
takes project keys, and a new issue of type Bug in one of them rings
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
592
|
+
takes project keys, and a new issue of type Bug in one of them rings. "Sentry organisation",
|
|
593
|
+
"Sentry projects" and "Sentry address" (`https://sentry.io` by default) do the same for a new
|
|
594
|
+
unresolved Sentry issue. Jira does not refuse a project key it does not know, so a mistyped key
|
|
595
|
+
rings nothing and shows no problem.
|
|
596
|
+
|
|
597
|
+
Each tracker needs a sign-in, set under Services in the office's settings. Jira takes its
|
|
598
|
+
address, the account's email and an API token; Sentry takes an auth token. "Save connection"
|
|
599
|
+
writes them to `office/trackers.json` in Paseo's home on the host, a file only the daemon's user
|
|
600
|
+
can read (mode 600). The tokens are stored there as typed, not encrypted. A saved token is never
|
|
601
|
+
sent back to the app: its field stays empty, and the settings say only where the sign-in comes
|
|
602
|
+
from. "Test connection" sends one signed-in request with what is typed and saves nothing; a
|
|
603
|
+
Sentry test asks the organisation and address already set. "Remove" deletes a saved connection.
|
|
604
|
+
A save holds from the next check, with no daemon restart.
|
|
605
|
+
|
|
606
|
+
A host with no saved connection reads `ATLASSIAN_BASE_URL`, `ATLASSIAN_EMAIL` and
|
|
607
|
+
`ATLASSIAN_API_TOKEN`, and `SENTRY_AUTH_TOKEN`, from the daemon's environment, as before. A saved
|
|
608
|
+
connection comes first, and a changed variable takes a daemon restart.
|
|
598
609
|
|
|
599
610
|
The first check records what exists and rings nothing. Each check reads from the last successful
|
|
600
611
|
one less 10 minutes, because Jira compares `created` to the minute; the office writes the time in
|
package/index.server.ts
CHANGED
|
@@ -34,14 +34,23 @@ import { createHub } from "./server/hub.js";
|
|
|
34
34
|
import { createWeatherService } from "./server/weather-service.js";
|
|
35
35
|
import { createWeatherReader } from "./server/weather-read.js";
|
|
36
36
|
import { createIncidentsCheck, type IncidentsCheckState } from "./server/incidents-check.js";
|
|
37
|
-
import {
|
|
37
|
+
import { TrackerProblem } from "./server/incidents-http.js";
|
|
38
|
+
import { probeJira } from "./server/incidents-jira.js";
|
|
38
39
|
import {
|
|
39
40
|
createIncidentsSchedule,
|
|
40
41
|
INCIDENTS_SCHEDULE,
|
|
41
42
|
incidentsReaders,
|
|
43
|
+
type IncidentsSettings,
|
|
42
44
|
} from "./server/incidents-schedule.js";
|
|
43
|
-
import {
|
|
45
|
+
import { probeSentry } from "./server/incidents-sentry.js";
|
|
44
46
|
import { createIncidentsStore, type IncidentsStore } from "./server/incidents-store.js";
|
|
47
|
+
import {
|
|
48
|
+
changeSavedTrackers,
|
|
49
|
+
createTrackerCredentialsStore,
|
|
50
|
+
NO_SAVED_TRACKERS,
|
|
51
|
+
trackerCredentials,
|
|
52
|
+
type TrackerCredentialsStore,
|
|
53
|
+
} from "./server/tracker-credentials.js";
|
|
45
54
|
import { tidyAgent } from "./server/janitor.js";
|
|
46
55
|
import {
|
|
47
56
|
liveAgentPorts,
|
|
@@ -83,6 +92,7 @@ import {
|
|
|
83
92
|
dutiesFile,
|
|
84
93
|
hostSkillFolders,
|
|
85
94
|
incidentsFile,
|
|
95
|
+
trackersFile,
|
|
86
96
|
notesWriterFile,
|
|
87
97
|
type SkillFolder,
|
|
88
98
|
} from "./server/paths.js";
|
|
@@ -97,6 +107,11 @@ import {
|
|
|
97
107
|
dutiesSaveRpc,
|
|
98
108
|
dutyActedRpc,
|
|
99
109
|
incidentsDismissRpc,
|
|
110
|
+
trackersSaveRpc,
|
|
111
|
+
trackersTestRpc,
|
|
112
|
+
type TrackersChange,
|
|
113
|
+
type TrackersProbe,
|
|
114
|
+
type TrackersResult,
|
|
100
115
|
janitorRpc,
|
|
101
116
|
machineSubscription,
|
|
102
117
|
notesReadRpc,
|
|
@@ -123,13 +138,15 @@ export interface OfficePluginDeps {
|
|
|
123
138
|
notesStore: NotesStore;
|
|
124
139
|
/** Where the incidents check keeps the open incidents and what each tracker has shown. */
|
|
125
140
|
incidentsStore: IncidentsStore;
|
|
141
|
+
/** Where the tracker connections saved from the settings are kept. */
|
|
142
|
+
trackerStore: TrackerCredentialsStore;
|
|
126
143
|
/** Where the team chat keeps its conversation. */
|
|
127
144
|
chatStore: ChatStore;
|
|
128
145
|
tenureFile: LifeFile<TenureLedger>;
|
|
129
146
|
liftFile: LifeFile<LiftKept>;
|
|
130
147
|
/** The owner's duty list. */
|
|
131
148
|
dutiesFile: LifeFile<DutiesFile>;
|
|
132
|
-
/** The daemon's environment:
|
|
149
|
+
/** The daemon's environment: a tracker with no saved connection takes its token from it. */
|
|
133
150
|
env: NodeJS.ProcessEnv;
|
|
134
151
|
clock: Clock;
|
|
135
152
|
log(message: string): void;
|
|
@@ -291,18 +308,40 @@ export function createOfficePlugin(
|
|
|
291
308
|
// The check's last state, kept so a save that changes the schedule's error pushes it again.
|
|
292
309
|
// Null until the store was read: the snapshot has no incidents before then.
|
|
293
310
|
let checked: IncidentsCheckState | null = null;
|
|
311
|
+
// The connections saved from the settings, read once and kept: a save replaces them here too.
|
|
312
|
+
let savedTrackers = NO_SAVED_TRACKERS;
|
|
313
|
+
const trackersLoaded = deps.trackerStore.load().then(
|
|
314
|
+
(saved) => {
|
|
315
|
+
savedTrackers = saved;
|
|
316
|
+
return undefined;
|
|
317
|
+
},
|
|
318
|
+
(error: unknown) => log(`[office] tracker connections failed to load: ${errorMessage(error)}`),
|
|
319
|
+
);
|
|
320
|
+
const credentials = () => trackerCredentials(savedTrackers, deps.env);
|
|
294
321
|
function pushIncidents(): void {
|
|
295
322
|
if (checked === null) return;
|
|
323
|
+
const { jira, sentry } = credentials();
|
|
296
324
|
hub.setIncidents({
|
|
297
325
|
...checked,
|
|
298
|
-
jiraReady:
|
|
299
|
-
sentryReady:
|
|
326
|
+
jiraReady: jira !== null,
|
|
327
|
+
sentryReady: sentry !== null,
|
|
328
|
+
connections: {
|
|
329
|
+
jira: {
|
|
330
|
+
source: jira?.source ?? "none",
|
|
331
|
+
baseUrl: jira?.baseUrl ?? "",
|
|
332
|
+
email: jira?.email ?? "",
|
|
333
|
+
},
|
|
334
|
+
sentry: { source: sentry?.source ?? "none" },
|
|
335
|
+
},
|
|
300
336
|
scheduleError: incidentsSchedule.error(),
|
|
301
337
|
});
|
|
302
338
|
}
|
|
303
339
|
// The settings are read at each run, so a save holds from the next check.
|
|
304
340
|
const incidents = createIncidentsCheck({
|
|
305
|
-
readers: async () =>
|
|
341
|
+
readers: async () => {
|
|
342
|
+
await trackersLoaded;
|
|
343
|
+
return incidentsReaders(await readHostSettings(), credentials());
|
|
344
|
+
},
|
|
306
345
|
store: deps.incidentsStore,
|
|
307
346
|
now: () => deps.clock.now(),
|
|
308
347
|
log,
|
|
@@ -416,7 +455,11 @@ export function createOfficePlugin(
|
|
|
416
455
|
await writer
|
|
417
456
|
.settingsChanged()
|
|
418
457
|
.catch((error: unknown) => log(`[office] notes state failed: ${errorMessage(error)}`));
|
|
419
|
-
|
|
458
|
+
await followTrackers(state.values);
|
|
459
|
+
}
|
|
460
|
+
async function followTrackers(values: IncidentsSettings): Promise<void> {
|
|
461
|
+
await trackersLoaded;
|
|
462
|
+
const trackersOn = Object.keys(incidentsReaders(values, credentials())).length > 0;
|
|
420
463
|
await incidentsSchedule.sync(trackersOn);
|
|
421
464
|
// With every tracker off no run comes again. One run with no reader drops the problems and
|
|
422
465
|
// closes what is a day old.
|
|
@@ -565,6 +608,59 @@ export function createOfficePlugin(
|
|
|
565
608
|
server.handle(incidentsDismissRpc, ({ ids }) =>
|
|
566
609
|
guarded("incidents dismiss", async () => ({ dismissed: await incidents.dismiss(ids) })),
|
|
567
610
|
);
|
|
611
|
+
async function saveTrackers(input: TrackersChange): Promise<TrackersResult> {
|
|
612
|
+
await trackersLoaded;
|
|
613
|
+
const changed = changeSavedTrackers(savedTrackers, input);
|
|
614
|
+
if ("problem" in changed) return { ok: false, problem: changed.problem };
|
|
615
|
+
await deps.trackerStore.save(changed.saved);
|
|
616
|
+
savedTrackers = changed.saved;
|
|
617
|
+
// The check's schedule follows the new connection; the settings may be unreadable.
|
|
618
|
+
await readHostSettings().then(followTrackers, pushIncidents);
|
|
619
|
+
return { ok: true };
|
|
620
|
+
}
|
|
621
|
+
async function probeTracker(input: TrackersProbe): Promise<void | string> {
|
|
622
|
+
const kept = credentials();
|
|
623
|
+
if (input.tracker === "jira") {
|
|
624
|
+
// The typed connection as a save would keep it, with the token in use when none is typed.
|
|
625
|
+
const token = input.token.trim() || (kept.jira?.token ?? "");
|
|
626
|
+
const typed = changeSavedTrackers(NO_SAVED_TRACKERS, {
|
|
627
|
+
action: "save-jira",
|
|
628
|
+
...input,
|
|
629
|
+
token,
|
|
630
|
+
});
|
|
631
|
+
if ("problem" in typed) return typed.problem;
|
|
632
|
+
if (typed.saved.jira === undefined) return "Enter the Jira connection";
|
|
633
|
+
return probeJira({ ...typed.saved.jira, tokenVariable: "the Jira API token" });
|
|
634
|
+
}
|
|
635
|
+
const token = input.token.trim() || (kept.sentry?.token ?? "");
|
|
636
|
+
if (token === "") return "Enter the Sentry auth token";
|
|
637
|
+
const { sentryUrl: url, sentryOrg: org } = await readHostSettings();
|
|
638
|
+
if (org.trim() === "") return "Set the Sentry organisation first";
|
|
639
|
+
return probeSentry({ url, org, token, tokenVariable: "the Sentry auth token" });
|
|
640
|
+
}
|
|
641
|
+
async function testTracker(input: TrackersProbe): Promise<TrackersResult> {
|
|
642
|
+
await trackersLoaded;
|
|
643
|
+
try {
|
|
644
|
+
const problem = await probeTracker(input);
|
|
645
|
+
return typeof problem === "string" ? { ok: false, problem } : { ok: true };
|
|
646
|
+
} catch (error) {
|
|
647
|
+
// A tracker's problem is written to be shown; anything else may quote what was typed.
|
|
648
|
+
if (error instanceof TrackerProblem) return { ok: false, problem: error.message };
|
|
649
|
+
throw error;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
// One save at a time: each starts from what the one before it kept.
|
|
653
|
+
let trackerSaves: Promise<unknown> = Promise.resolve();
|
|
654
|
+
server.handle(trackersSaveRpc, (input) => {
|
|
655
|
+
const run = trackerSaves.then(() =>
|
|
656
|
+
guarded("tracker connection save", () => saveTrackers(input)),
|
|
657
|
+
);
|
|
658
|
+
trackerSaves = run.catch(() => undefined);
|
|
659
|
+
return run;
|
|
660
|
+
});
|
|
661
|
+
server.handle(trackersTestRpc, (input) =>
|
|
662
|
+
guarded("tracker connection test", () => testTracker(input)),
|
|
663
|
+
);
|
|
568
664
|
server.handle(chatRpc, (input, context) => {
|
|
569
665
|
holder.remember(context.paseo);
|
|
570
666
|
return guarded("chat", async () => {
|
|
@@ -755,6 +851,10 @@ export default function contribute(server: PluginServerContext): () => Promise<v
|
|
|
755
851
|
}),
|
|
756
852
|
notesStore: createNotesStore({ file: notesWriterFile(process.env, homedir()), log }),
|
|
757
853
|
incidentsStore: createIncidentsStore({ file: incidentsFile(process.env, homedir()), log }),
|
|
854
|
+
trackerStore: createTrackerCredentialsStore({
|
|
855
|
+
file: trackersFile(process.env, homedir()),
|
|
856
|
+
log,
|
|
857
|
+
}),
|
|
758
858
|
tenureFile: createLifeFile(tenureFile(process.env, homedir()), TenureLedgerSchema),
|
|
759
859
|
liftFile: createLifeFile(liftFile(process.env, homedir()), LiftKeptSchema),
|
|
760
860
|
dutiesFile: dutiesFileStore(dutiesFile(process.env, homedir()), { now: () => Date.now(), log }),
|
package/package.json
CHANGED
package/server/incidents-jira.ts
CHANGED
|
@@ -14,6 +14,8 @@ export interface JiraEnv {
|
|
|
14
14
|
baseUrl: string;
|
|
15
15
|
email: string;
|
|
16
16
|
token: string;
|
|
17
|
+
/** What a refused token's message names; the environment's variable when absent. */
|
|
18
|
+
tokenVariable?: string;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/** Jira's site and sign-in from the daemon's environment; null unless all three are set. */
|
|
@@ -70,19 +72,29 @@ function jqlTime(since: number, timeZone: string): string {
|
|
|
70
72
|
return `${day} ${parts.get("hour")}:${parts.get("minute")}`;
|
|
71
73
|
}
|
|
72
74
|
|
|
75
|
+
function jiraTracker(env: JiraEnv, fetchFn: typeof fetch): Tracker {
|
|
76
|
+
const basic = Buffer.from(`${env.email}:${env.token}`).toString("base64");
|
|
77
|
+
return {
|
|
78
|
+
name: "Jira",
|
|
79
|
+
tokenVariable: env.tokenVariable ?? "ATLASSIAN_API_TOKEN",
|
|
80
|
+
headers: { Authorization: `Basic ${basic}` },
|
|
81
|
+
fetchFn,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One signed-in request, to tell whether Jira takes the sign-in. Rejects with a `TrackerProblem`. */
|
|
86
|
+
export async function probeJira(env: JiraEnv, fetchFn: typeof fetch = fetch): Promise<void> {
|
|
87
|
+
const tracker = jiraTracker(env, fetchFn);
|
|
88
|
+
await getJson({ tracker, url: `${env.baseUrl}/rest/api/3/myself`, schema: MyselfSchema });
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
/** New bugs in `projects`, keys already checked against the settings' pattern. */
|
|
74
92
|
export function createJiraReader(
|
|
75
93
|
env: JiraEnv,
|
|
76
94
|
projects: string[],
|
|
77
95
|
fetchFn: typeof fetch = fetch,
|
|
78
96
|
): TrackerReader {
|
|
79
|
-
const
|
|
80
|
-
const tracker: Tracker = {
|
|
81
|
-
name: "Jira",
|
|
82
|
-
tokenVariable: "ATLASSIAN_API_TOKEN",
|
|
83
|
-
headers: { Authorization: `Basic ${basic}` },
|
|
84
|
-
fetchFn,
|
|
85
|
-
};
|
|
97
|
+
const tracker = jiraTracker(env, fetchFn);
|
|
86
98
|
const api = `${env.baseUrl}/rest/api/3`;
|
|
87
99
|
|
|
88
100
|
async function read(since: number): Promise<FoundIssue[]> {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { IncidentSource } from "../shared/contracts.js";
|
|
2
2
|
import { JIRA_KEY, parseKeys, SENTRY_SLUG } from "../shared/settings.js";
|
|
3
3
|
import type { TrackerReader } from "./incidents-http.js";
|
|
4
|
-
import { createJiraReader
|
|
5
|
-
import { createSentryReader
|
|
4
|
+
import { createJiraReader } from "./incidents-jira.js";
|
|
5
|
+
import { createSentryReader } from "./incidents-sentry.js";
|
|
6
6
|
import type { OfficeCadence, OfficeSchedules } from "./local-schedules.js";
|
|
7
7
|
import { createScheduleFollower, type OfficeSchedule } from "./schedule-follower.js";
|
|
8
|
+
import type { TrackerCredentials } from "./tracker-credentials.js";
|
|
8
9
|
|
|
9
10
|
/** The check's schedule's name among the office's schedules, and the handler that answers it. */
|
|
10
11
|
export const INCIDENTS_SCHEDULE = "incidents";
|
|
@@ -23,29 +24,28 @@ export interface IncidentsSettings {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* The trackers that are on: each needs
|
|
27
|
-
*
|
|
27
|
+
* The trackers that are on: each needs a sign-in in `credentials` and a project in the settings,
|
|
28
|
+
* and Sentry an organisation too.
|
|
28
29
|
*/
|
|
29
30
|
export function incidentsReaders(
|
|
30
31
|
values: IncidentsSettings,
|
|
31
|
-
|
|
32
|
+
credentials: TrackerCredentials,
|
|
32
33
|
): Partial<Record<IncidentSource, TrackerReader>> {
|
|
33
34
|
const readers: Partial<Record<IncidentSource, TrackerReader>> = {};
|
|
34
|
-
const jira =
|
|
35
|
+
const { jira, sentry } = credentials;
|
|
35
36
|
const jiraProjects = parseKeys(values.jiraProjects, JIRA_KEY);
|
|
36
37
|
if (jira !== null && jiraProjects.length > 0) {
|
|
37
38
|
readers.jira = createJiraReader(jira, jiraProjects);
|
|
38
39
|
}
|
|
39
|
-
const token = sentryToken(env);
|
|
40
40
|
const org = values.sentryOrg.trim();
|
|
41
41
|
const sentryProjects = parseKeys(values.sentryProjects, SENTRY_SLUG);
|
|
42
|
-
|
|
43
|
-
if (sentryOn) {
|
|
42
|
+
if (sentry !== null && org !== "" && sentryProjects.length > 0) {
|
|
44
43
|
readers.sentry = createSentryReader({
|
|
45
44
|
url: values.sentryUrl,
|
|
46
45
|
org,
|
|
47
46
|
projects: sentryProjects,
|
|
48
|
-
token,
|
|
47
|
+
token: sentry.token,
|
|
48
|
+
tokenVariable: sentry.tokenVariable,
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
return readers;
|
|
@@ -23,6 +23,8 @@ export interface SentryReaderInput {
|
|
|
23
23
|
/** Project slugs, already checked against the settings' pattern. */
|
|
24
24
|
projects: string[];
|
|
25
25
|
token: string;
|
|
26
|
+
/** What a refused token's message names; the environment's variable when absent. */
|
|
27
|
+
tokenVariable?: string;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
const ProjectsSchema = z.array(z.object({ id: z.string(), slug: z.string() }));
|
|
@@ -36,6 +38,36 @@ const IssuesSchema = z.array(
|
|
|
36
38
|
}),
|
|
37
39
|
);
|
|
38
40
|
|
|
41
|
+
type SentrySignIn = Omit<SentryReaderInput, "projects">;
|
|
42
|
+
|
|
43
|
+
function sentryApi(input: SentrySignIn): string {
|
|
44
|
+
const site = new URL(input.url.trim());
|
|
45
|
+
const org = encodeURIComponent(input.org.trim());
|
|
46
|
+
return `${site.origin}${site.pathname.replace(/\/+$/, "")}/api/0/organizations/${org}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function sentryTracker(input: SentrySignIn, fetchFn: typeof fetch): Tracker {
|
|
50
|
+
return {
|
|
51
|
+
name: "Sentry",
|
|
52
|
+
tokenVariable: input.tokenVariable ?? "SENTRY_AUTH_TOKEN",
|
|
53
|
+
headers: { Authorization: `Bearer ${input.token}` },
|
|
54
|
+
fetchFn,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* One signed-in request, to tell whether Sentry takes the token for the organisation. Rejects
|
|
60
|
+
* with a `TrackerProblem`.
|
|
61
|
+
*/
|
|
62
|
+
export async function probeSentry(
|
|
63
|
+
input: SentrySignIn,
|
|
64
|
+
fetchFn: typeof fetch = fetch,
|
|
65
|
+
): Promise<void> {
|
|
66
|
+
const tracker = sentryTracker(input, fetchFn);
|
|
67
|
+
const url = `${sentryApi(input)}/projects/?per_page=1`;
|
|
68
|
+
await getJson({ tracker, url, schema: ProjectsSchema });
|
|
69
|
+
}
|
|
70
|
+
|
|
39
71
|
/** New unresolved issues in the listed projects. */
|
|
40
72
|
export function createSentryReader(
|
|
41
73
|
input: SentryReaderInput,
|
|
@@ -43,14 +75,8 @@ export function createSentryReader(
|
|
|
43
75
|
): TrackerReader {
|
|
44
76
|
const { projects } = input;
|
|
45
77
|
const org = input.org.trim();
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const tracker: Tracker = {
|
|
49
|
-
name: "Sentry",
|
|
50
|
-
tokenVariable: "SENTRY_AUTH_TOKEN",
|
|
51
|
-
headers: { Authorization: `Bearer ${input.token}` },
|
|
52
|
-
fetchFn,
|
|
53
|
-
};
|
|
78
|
+
const api = sentryApi(input);
|
|
79
|
+
const tracker = sentryTracker(input, fetchFn);
|
|
54
80
|
|
|
55
81
|
/**
|
|
56
82
|
* The listed projects' ids, in the order listed. The issue search takes slugs too, but only on
|
package/server/paths.ts
CHANGED
|
@@ -35,6 +35,11 @@ export function incidentsFile(env: NodeJS.ProcessEnv, home: string): string {
|
|
|
35
35
|
return join(officeDir(env, home), "incidents.json");
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/** The tracker connections saved from the settings. It holds tokens: owner-only. */
|
|
39
|
+
export function trackersFile(env: NodeJS.ProcessEnv, home: string): string {
|
|
40
|
+
return join(officeDir(env, home), "trackers.json");
|
|
41
|
+
}
|
|
42
|
+
|
|
38
43
|
/** The team chat's conversation, kept as upstream's engine writes it. */
|
|
39
44
|
export function chatFile(env: NodeJS.ProcessEnv, home: string): string {
|
|
40
45
|
return join(officeDir(env, home), "chat.json");
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import type { IncidentSource, TrackersChange } from "../shared/contracts.js";
|
|
5
|
+
import { isTokenSafe } from "../shared/settings.js";
|
|
6
|
+
import { jiraEnv, type JiraEnv } from "./incidents-jira.js";
|
|
7
|
+
import { sentryToken } from "./incidents-sentry.js";
|
|
8
|
+
|
|
9
|
+
const SavedTrackersSchema = z.object({
|
|
10
|
+
version: z.literal(1),
|
|
11
|
+
jira: z.object({ baseUrl: z.string(), email: z.string(), token: z.string() }).optional(),
|
|
12
|
+
sentry: z.object({ token: z.string() }).optional(),
|
|
13
|
+
});
|
|
14
|
+
/** The connections saved from the settings. They hold tokens: never sent to a client or logged. */
|
|
15
|
+
export type SavedTrackers = z.output<typeof SavedTrackersSchema>;
|
|
16
|
+
|
|
17
|
+
export const NO_SAVED_TRACKERS: SavedTrackers = { version: 1 };
|
|
18
|
+
|
|
19
|
+
export interface TrackerCredentialsStore {
|
|
20
|
+
/** A missing or unreadable file is no saved connection; any other read error rejects. */
|
|
21
|
+
load(): Promise<SavedTrackers>;
|
|
22
|
+
/** Rejects when the write fails. */
|
|
23
|
+
save(saved: SavedTrackers): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isMissing(error: unknown): boolean {
|
|
27
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The file is written for its owner only, and replaced whole so its mode is always that. */
|
|
31
|
+
export function createTrackerCredentialsStore(options: {
|
|
32
|
+
file: string;
|
|
33
|
+
log(message: string): void;
|
|
34
|
+
}): TrackerCredentialsStore {
|
|
35
|
+
const { file, log } = options;
|
|
36
|
+
let temp = 0;
|
|
37
|
+
|
|
38
|
+
async function load(): Promise<SavedTrackers> {
|
|
39
|
+
let text: string;
|
|
40
|
+
try {
|
|
41
|
+
text = await readFile(file, "utf8");
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (isMissing(error)) return NO_SAVED_TRACKERS;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
let json: unknown;
|
|
47
|
+
try {
|
|
48
|
+
json = JSON.parse(text);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
51
|
+
json = null;
|
|
52
|
+
}
|
|
53
|
+
const parsed = SavedTrackersSchema.safeParse(json);
|
|
54
|
+
if (parsed.success) return parsed.data;
|
|
55
|
+
// Only that it is unreadable: a parse error's message quotes the file, which holds tokens.
|
|
56
|
+
log(`[office] ${file} is unreadable; no tracker connection is saved until the next save`);
|
|
57
|
+
return NO_SAVED_TRACKERS;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function save(saved: SavedTrackers): Promise<void> {
|
|
61
|
+
temp += 1;
|
|
62
|
+
const tempFile = `${file}.tmp-${temp}`;
|
|
63
|
+
await mkdir(dirname(file), { recursive: true });
|
|
64
|
+
try {
|
|
65
|
+
await writeFile(tempFile, `${JSON.stringify(saved)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
66
|
+
await rename(tempFile, file);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
await rm(tempFile, { force: true }).catch(() => undefined);
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { load, save };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Where a tracker's sign-in comes from: the settings' saved connection, or the daemon's environment. */
|
|
77
|
+
export type CredentialSource = "saved" | "env";
|
|
78
|
+
|
|
79
|
+
interface Sourced {
|
|
80
|
+
source: CredentialSource;
|
|
81
|
+
/** What a refused token's message tells the owner to check. */
|
|
82
|
+
tokenVariable: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface TrackerCredentials {
|
|
86
|
+
jira: (JiraEnv & Sourced) | null;
|
|
87
|
+
sentry: ({ token: string } & Sourced) | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Each tracker's sign-in: the saved connection, else the daemon's environment, else none. */
|
|
91
|
+
export function trackerCredentials(
|
|
92
|
+
saved: SavedTrackers,
|
|
93
|
+
env: NodeJS.ProcessEnv,
|
|
94
|
+
): TrackerCredentials {
|
|
95
|
+
const fromEnv = jiraEnv(env);
|
|
96
|
+
const envToken = sentryToken(env);
|
|
97
|
+
let jira: TrackerCredentials["jira"] = null;
|
|
98
|
+
if (saved.jira !== undefined) {
|
|
99
|
+
jira = { ...saved.jira, source: "saved", tokenVariable: "the saved Jira token" };
|
|
100
|
+
} else if (fromEnv !== null) {
|
|
101
|
+
jira = { ...fromEnv, source: "env", tokenVariable: "ATLASSIAN_API_TOKEN" };
|
|
102
|
+
}
|
|
103
|
+
let sentry: TrackerCredentials["sentry"] = null;
|
|
104
|
+
if (saved.sentry !== undefined) {
|
|
105
|
+
sentry = { ...saved.sentry, source: "saved", tokenVariable: "the saved Sentry token" };
|
|
106
|
+
} else if (envToken !== null) {
|
|
107
|
+
sentry = { token: envToken, source: "env", tokenVariable: "SENTRY_AUTH_TOKEN" };
|
|
108
|
+
}
|
|
109
|
+
return { jira, sentry };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type SavedChange = { saved: SavedTrackers } | { problem: string };
|
|
113
|
+
|
|
114
|
+
function without(saved: SavedTrackers, tracker: IncidentSource): SavedTrackers {
|
|
115
|
+
const { [tracker]: _removed, ...kept } = saved;
|
|
116
|
+
return kept;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* `saved` after a change from the settings, or why the change is refused. A Jira save with no
|
|
121
|
+
* token keeps the saved one, so the address or email can change without typing it again.
|
|
122
|
+
*/
|
|
123
|
+
export function changeSavedTrackers(saved: SavedTrackers, change: TrackersChange): SavedChange {
|
|
124
|
+
if (change.action === "clear") return { saved: without(saved, change.tracker) };
|
|
125
|
+
const token = change.token.trim();
|
|
126
|
+
if (change.action === "save-sentry") {
|
|
127
|
+
if (token === "") return { problem: "Enter the Sentry auth token" };
|
|
128
|
+
return { saved: { ...saved, sentry: { token } } };
|
|
129
|
+
}
|
|
130
|
+
const baseUrl = change.baseUrl.trim().replace(/\/+$/, "");
|
|
131
|
+
const email = change.email.trim();
|
|
132
|
+
const kept = token === "" ? (saved.jira?.token ?? "") : token;
|
|
133
|
+
if (!isTokenSafe(baseUrl)) {
|
|
134
|
+
return {
|
|
135
|
+
problem: "The Jira address must be an https address (plain http only on this machine)",
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (email === "") return { problem: "Enter the Jira account's email" };
|
|
139
|
+
if (kept === "") return { problem: "Enter the Jira API token" };
|
|
140
|
+
return { saved: { ...saved, jira: { baseUrl, email, token: kept } } };
|
|
141
|
+
}
|
package/shared/contracts.ts
CHANGED
|
@@ -268,14 +268,32 @@ export const IncidentSchema = z.object({
|
|
|
268
268
|
});
|
|
269
269
|
export type Incident = z.output<typeof IncidentSchema>;
|
|
270
270
|
|
|
271
|
+
/** Where a tracker's sign-in is: saved from the settings, in the daemon's environment, or nowhere. */
|
|
272
|
+
export const TRACKER_SOURCES = ["saved", "env", "none"] as const;
|
|
273
|
+
export type TrackerSource = (typeof TRACKER_SOURCES)[number];
|
|
274
|
+
|
|
271
275
|
/** The open incidents, and what keeps a tracker from being checked. */
|
|
272
276
|
export const IncidentsStateSchema = z.object({
|
|
273
277
|
open: z.array(IncidentSchema),
|
|
274
278
|
/** Why a tracker's last check failed; absent once one succeeds. */
|
|
275
279
|
problems: z.object({ jira: z.string().optional(), sentry: z.string().optional() }),
|
|
276
|
-
/** The tracker
|
|
280
|
+
/** The tracker has a sign-in on the host, saved or in its environment. Never the values. */
|
|
277
281
|
jiraReady: z.boolean(),
|
|
278
282
|
sentryReady: z.boolean(),
|
|
283
|
+
/**
|
|
284
|
+
* Where each tracker's sign-in comes from, and Jira's address and email. Never a token. Absent
|
|
285
|
+
* from a server older than the settings' connections.
|
|
286
|
+
*/
|
|
287
|
+
connections: z
|
|
288
|
+
.object({
|
|
289
|
+
jira: z.object({
|
|
290
|
+
source: z.enum(TRACKER_SOURCES),
|
|
291
|
+
baseUrl: z.string(),
|
|
292
|
+
email: z.string(),
|
|
293
|
+
}),
|
|
294
|
+
sentry: z.object({ source: z.enum(TRACKER_SOURCES) }),
|
|
295
|
+
})
|
|
296
|
+
.optional(),
|
|
279
297
|
/** Why the check's schedule could not follow the settings; null once it does. */
|
|
280
298
|
scheduleError: z.string().nullable(),
|
|
281
299
|
});
|
|
@@ -732,6 +750,51 @@ export const notesWriteRpc = defineRpc({
|
|
|
732
750
|
output: z.object({ started: z.boolean() }),
|
|
733
751
|
});
|
|
734
752
|
|
|
753
|
+
const JiraConnectionSchema = z.object({
|
|
754
|
+
baseUrl: z.string().max(500),
|
|
755
|
+
email: z.string().max(320),
|
|
756
|
+
/** Empty for the token already saved. */
|
|
757
|
+
token: z.string().max(4000),
|
|
758
|
+
});
|
|
759
|
+
const SentryConnectionSchema = z.object({ token: z.string().max(4000) });
|
|
760
|
+
|
|
761
|
+
export const TrackersChangeSchema = z.discriminatedUnion("action", [
|
|
762
|
+
JiraConnectionSchema.extend({ action: z.literal("save-jira") }),
|
|
763
|
+
SentryConnectionSchema.extend({ action: z.literal("save-sentry") }),
|
|
764
|
+
z.object({ action: z.literal("clear"), tracker: z.enum(INCIDENT_SOURCES) }),
|
|
765
|
+
]);
|
|
766
|
+
export type TrackersChange = z.output<typeof TrackersChangeSchema>;
|
|
767
|
+
|
|
768
|
+
/** `problem` is why it failed, in words safe to show: never a token. */
|
|
769
|
+
export const TrackersResultSchema = z.union([
|
|
770
|
+
z.object({ ok: z.literal(true) }),
|
|
771
|
+
z.object({ ok: z.literal(false), problem: z.string() }),
|
|
772
|
+
]);
|
|
773
|
+
export type TrackersResult = z.output<typeof TrackersResultSchema>;
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Saves or clears a tracker's connection on the host, in a file only the daemon's user can read.
|
|
777
|
+
* The token is never sent back; the office's state names where each sign-in comes from.
|
|
778
|
+
*/
|
|
779
|
+
export const trackersSaveRpc = defineRpc({
|
|
780
|
+
name: "office.trackers.save.request",
|
|
781
|
+
input: TrackersChangeSchema,
|
|
782
|
+
output: TrackersResultSchema,
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
/** One signed-in request to the tracker with the connection as typed; nothing is saved. */
|
|
786
|
+
const TrackersProbeSchema = z.discriminatedUnion("tracker", [
|
|
787
|
+
JiraConnectionSchema.extend({ tracker: z.literal("jira") }),
|
|
788
|
+
SentryConnectionSchema.extend({ tracker: z.literal("sentry") }),
|
|
789
|
+
]);
|
|
790
|
+
export type TrackersProbe = z.output<typeof TrackersProbeSchema>;
|
|
791
|
+
|
|
792
|
+
export const trackersTestRpc = defineRpc({
|
|
793
|
+
name: "office.trackers.test.request",
|
|
794
|
+
input: TrackersProbeSchema,
|
|
795
|
+
output: TrackersResultSchema,
|
|
796
|
+
});
|
|
797
|
+
|
|
735
798
|
/** Closes the named open incidents, or all of them when none are named. Their keys stay seen. */
|
|
736
799
|
export const incidentsDismissRpc = defineRpc({
|
|
737
800
|
name: "office.incidents.dismiss.request",
|