forgetop-server 1.12.0

Local dashboard server and embedded web UI for forgetop. Internal crate: no API stability guarantees, use the `forgetop` binary instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Write actions for the dashboard. Each resolves the capability-scoped source for a connection
//! and calls through to the provider — the same calls the TUI makes. All of these sit behind the
//! session-token auth layer (see `lib.rs`); the server binds localhost-only, so a merge or a
//! state change can't be triggered by another origin.

use std::sync::Arc;

use forgetop_core::domain::{ApprovalDecision, LineComment, Notification, NotificationDetail, ReviewVote};
use forgetop_core::provider::{ItemRef, MergeOptions, MergeStrategy, NotificationSource};
use forgetop_core::service::{ConfigService, SectionService};
use serde::Deserialize;

use crate::dto;

/// Why an action couldn't be performed: the connection/capability wasn't found (→ 404), or the
/// provider call itself failed (→ 502).
pub enum ActionError {
    NotFound,
    Failed(String),
}

fn failed(e: impl std::fmt::Display) -> ActionError {
    ActionError::Failed(e.to_string())
}

// ---- source resolvers ----
//
// Work-item and pipeline resolvers live in `dto` (shared with the read/detail endpoints);
// only the notification resolver is action-only.

async fn notif_source(sections: &SectionService, conn: &str) -> Option<Arc<dyn NotificationSource>> {
    sections.notification_feeds().await.ok()?.into_iter().find(|f| f.connection.connection_id() == conn).map(|f| f.source)
}

// ---- request bodies ----

#[derive(Deserialize)]
pub struct PrVoteReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub vote: ReviewVote,
}

#[derive(Deserialize)]
pub struct PrMergeReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    #[serde(default)]
    pub strategy: MergeStrategy,
    #[serde(default)]
    pub delete_source_ref: bool,
}

#[derive(Deserialize)]
pub struct PrRevertReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
}

#[derive(Deserialize)]
pub struct PrCommentReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub body: String,
}

#[derive(Deserialize)]
pub struct PrReplyReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub thread_id: String,
    pub body: String,
}

#[derive(Deserialize)]
pub struct PrReviewReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub event: ReviewVote,
    #[serde(default)]
    pub comments: Vec<LineComment>,
}

#[derive(Deserialize)]
pub struct PrResolveThreadReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub thread_id: String,
    /// `true` resolves the thread, `false` reopens it.
    pub resolved: bool,
}

#[derive(Deserialize)]
pub struct PrDraftReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    /// `true` converts to a draft, `false` marks it ready for review.
    pub draft: bool,
}

#[derive(Deserialize)]
pub struct PrClosedReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    /// `true` closes without merging, `false` reopens.
    pub closed: bool,
}

#[derive(Deserialize)]
pub struct PrRequestReviewerReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    /// The reviewer's id (from the reviewable-users list).
    pub user_id: String,
}

#[derive(Deserialize)]
pub struct WiStateReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub state: String,
}

#[derive(Deserialize)]
pub struct WiCommentReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub body: String,
}

#[derive(Deserialize)]
pub struct WiAssigneeReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    /// The chosen assignee's id (from the assignable-users list), or `None` to unassign.
    pub assignee_id: Option<String>,
}

#[derive(Deserialize)]
pub struct WiUpdateReq {
    pub conn: String,
    pub id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
}

#[derive(Deserialize)]
pub struct PipelineApprovalReq {
    pub conn: String,
    pub run_id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    pub approval_id: String,
    pub decision: ApprovalDecision,
    #[serde(default)]
    pub comment: Option<String>,
}

#[derive(Deserialize)]
pub struct PipelineTriggerReq {
    pub conn: String,
    pub definition_id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    #[serde(default)]
    pub branch: Option<String>,
}

#[derive(Deserialize)]
pub struct PipelineCancelReq {
    pub conn: String,
    pub run_id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
}

#[derive(Deserialize)]
pub struct PipelineRerunReq {
    pub conn: String,
    pub run_id: String,
    /// The item's **connection-relative** repository. Optional: a single-repository connection
    /// still resolves without it, which is what keeps links written before this change working.
    #[serde(default)]
    pub repo: Option<String>,
    /// Re-run only the failed jobs (the provider must support it).
    #[serde(default)]
    pub failed_only: bool,
}

#[derive(Deserialize)]
pub struct NotifReadReq {
    pub conn: String,
    pub id: String,
}

/// Several notifications on one connection — a thread's members, for "mark read" and "done".
#[derive(Deserialize)]
pub struct NotifIdsReq {
    pub conn: String,
    pub ids: Vec<String>,
}

// ---- actions ----

pub async fn pr_vote(sections: &SectionService, req: PrVoteReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.vote(&ItemRef::maybe(req.repo, req.id), req.vote).await.map_err(failed)
}

pub async fn pr_merge(sections: &SectionService, req: PrMergeReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    let options = MergeOptions { strategy: req.strategy, delete_source_ref: req.delete_source_ref };
    source.merge(&ItemRef::maybe(req.repo, req.id), &options).await.map_err(failed)
}

pub async fn pr_revert(sections: &SectionService, req: PrRevertReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.revert(&ItemRef::maybe(req.repo, req.id)).await.map_err(failed)
}

pub async fn pr_comment(sections: &SectionService, req: PrCommentReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.add_comment(&ItemRef::maybe(req.repo, req.id), &req.body).await.map_err(failed)
}

pub async fn pr_reply(sections: &SectionService, req: PrReplyReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.reply_to_thread(&ItemRef::maybe(req.repo, req.id), &req.thread_id, &req.body).await.map_err(failed)
}

pub async fn pr_review(sections: &SectionService, req: PrReviewReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.submit_review(&ItemRef::maybe(req.repo, req.id), req.event, &req.comments).await.map_err(failed)
}

pub async fn pr_resolve_thread(sections: &SectionService, req: PrResolveThreadReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.resolve_thread(&ItemRef::maybe(req.repo, req.id), &req.thread_id, req.resolved).await.map_err(failed)
}

pub async fn pr_set_draft(sections: &SectionService, req: PrDraftReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.set_draft(&ItemRef::maybe(req.repo, req.id), req.draft).await.map_err(failed)
}

pub async fn pr_set_closed(sections: &SectionService, req: PrClosedReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.set_closed(&ItemRef::maybe(req.repo, req.id), req.closed).await.map_err(failed)
}

/// The people who can be asked to review a pull request (for the request-reviewer menu).
pub async fn pr_reviewers(sections: &SectionService, conn: &str, item: &ItemRef) -> Option<Vec<forgetop_core::domain::User>> {
    let source = dto::pr_source(sections, conn).await?;
    Some(source.reviewable_users(item).await.unwrap_or_default())
}

pub async fn pr_request_reviewer(sections: &SectionService, req: PrRequestReviewerReq) -> Result<(), ActionError> {
    let source = dto::pr_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.request_reviewer(&ItemRef::maybe(req.repo, req.id), &req.user_id).await.map_err(failed)
}

/// The states a work item can move to (for the transition menu).
pub async fn wi_states(sections: &SectionService, conn: &str, item: &ItemRef) -> Option<Vec<String>> {
    let source = dto::wi_source(sections, conn).await?;
    Some(source.available_states(item).await.unwrap_or_default())
}

pub async fn wi_set_state(sections: &SectionService, req: WiStateReq) -> Result<(), ActionError> {
    let source = dto::wi_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.set_state(&ItemRef::maybe(req.repo, req.id), &req.state).await.map_err(failed)
}

pub async fn wi_comment(sections: &SectionService, req: WiCommentReq) -> Result<(), ActionError> {
    let source = dto::wi_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.add_comment(&ItemRef::maybe(req.repo, req.id), &req.body).await.map_err(failed)
}

pub async fn wi_assignees(sections: &SectionService, conn: &str, item: &ItemRef) -> Option<Vec<forgetop_core::domain::User>> {
    let source = dto::wi_source(sections, conn).await?;
    Some(source.assignable_users(item).await.unwrap_or_default())
}

pub async fn wi_set_assignee(sections: &SectionService, req: WiAssigneeReq) -> Result<(), ActionError> {
    let source = dto::wi_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.set_assignee(&ItemRef::maybe(req.repo, req.id), req.assignee_id.as_deref()).await.map_err(failed)
}

pub async fn wi_update(sections: &SectionService, req: WiUpdateReq) -> Result<(), ActionError> {
    let source = dto::wi_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.update_fields(&ItemRef::maybe(req.repo, req.id), req.title.as_deref(), req.description.as_deref()).await.map_err(failed)
}

pub async fn pipeline_approval(sections: &SectionService, req: PipelineApprovalReq) -> Result<(), ActionError> {
    let source = dto::pipe_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.respond_approval(&ItemRef::maybe(req.repo, req.run_id), &req.approval_id, req.decision, req.comment.as_deref()).await.map_err(failed)
}

pub async fn pipeline_trigger(sections: &SectionService, req: PipelineTriggerReq) -> Result<(), ActionError> {
    let source = dto::pipe_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.trigger(&ItemRef::maybe(req.repo, req.definition_id), req.branch.as_deref()).await.map_err(failed)
}

pub async fn pipeline_cancel(sections: &SectionService, req: PipelineCancelReq) -> Result<(), ActionError> {
    let source = dto::pipe_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.cancel_run(&ItemRef::maybe(req.repo, req.run_id)).await.map_err(failed)
}

/// Re-runs a run. `Ok(Some(id))` is the new run's id when the provider started a separate run;
/// `Ok(None)` when the same run was re-queued as its next attempt.
pub async fn pipeline_rerun(sections: &SectionService, req: PipelineRerunReq) -> Result<Option<String>, ActionError> {
    let source = dto::pipe_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.rerun_run(&ItemRef::maybe(req.repo, req.run_id), req.failed_only).await.map_err(failed)
}

pub async fn notif_read(sections: &SectionService, req: NotifReadReq) -> Result<(), ActionError> {
    let source = notif_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    source.mark_read(&req.id).await.map_err(failed)
}

/// Marks each of `ids` read at the forge, best-effort: one failing doesn't stop the rest. Ids the
/// current rows already show as read are skipped (on GitHub every call counts against the hourly
/// budget). Fails only when every call it made failed.
pub async fn notif_read_many(sections: &SectionService, rows: &[dto::NotifRow], req: &NotifIdsReq) -> Result<(), ActionError> {
    let source = notif_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    let (mut tried, mut failed) = (0, 0);
    let mut first_error = None;
    for id in &req.ids {
        let known = rows.iter().find(|r| r.connection_id == req.conn && r.notification.id == *id);
        if known.is_some_and(|r| !r.notification.unread) {
            continue;
        }
        tried += 1;
        if let Err(e) = source.mark_read(id).await {
            failed += 1;
            first_error.get_or_insert(e.to_string());
        }
    }
    match first_error {
        Some(e) if failed == tried => Err(ActionError::Failed(e)),
        _ => Ok(()),
    }
}

/// "Done" on a thread: its members are marked done at the forge, then dismissed locally under the
/// key the TUI uses, merged into the saved list so neither frontend drops the other's. Ids the
/// current rows don't know are marked done but not dismissed — there is no `updated_at` to key
/// them by.
pub async fn notif_done(sections: &SectionService, config: &ConfigService, rows: &[dto::NotifRow], req: &NotifIdsReq) -> Result<(), ActionError> {
    // Every id, read or not: done is the forge's own "done" (GitHub mark-as-done, GitLab's
    // to-do done) — a read notification stays listed until this. Best-effort per id, failing
    // only when every call failed.
    let source = notif_source(sections, &req.conn).await.ok_or(ActionError::NotFound)?;
    let mut first_error = None;
    let mut misses = 0;
    for id in &req.ids {
        if let Err(e) = source.mark_done(id).await {
            misses += 1;
            first_error.get_or_insert(e.to_string());
        }
    }
    if let Some(e) = first_error.filter(|_| misses == req.ids.len()) {
        return Err(ActionError::Failed(e));
    }
    let keys: Vec<String> = req
        .ids
        .iter()
        .filter_map(|id| rows.iter().find(|r| r.connection_id == req.conn && r.notification.id == *id))
        .map(|r| r.dismiss_key.clone())
        .collect();
    config.dismiss_notifications(keys).await.map_err(failed)
}

/// The actor and excerpt of one notification its feed entry left out (GitHub's latest comment).
pub async fn notif_detail(sections: &SectionService, conn: &str, n: &Notification) -> Result<NotificationDetail, ActionError> {
    let source = notif_source(sections, conn).await.ok_or(ActionError::NotFound)?;
    source.detail(n).await.map_err(failed)
}