gitpoulpi-hosting 1.4.0

Hosting and issue-tracker API clients (GitHub, GitLab, Gitea/Forgejo, Azure DevOps, Bitbucket, Gerrit, Jira, Trello, Linear, YouTrack, Redmine, local Markdown issues): project listing, clone helpers, SSH key generation and upload, remote-URL provider matching.
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! The conversation of an Azure DevOps pull request (threads) or work item
//! (a flat comment list): the header, the timeline and posting, replying,
//! editing and deleting comments.
//!
//! **Pull requests.** `.../pullRequests/{id}/threads` answers every thread
//! at once, so the window is cut from the whole and `older` is the index
//! where the window starts. A thread without a file context is a general
//! conversation; with `threadContext.filePath` it is a code thread; a thread
//! made of system comments (`commentType: system`, or a
//! `CodeReviewThreadType` property) is an event, and a `VoteUpdate` is a
//! verdict. A comment id is `"{thread}.{comment}"` because an edit or a
//! delete needs both; the discussion id of a comment is the thread id. A
//! reply answers the thread's first comment (comment ids start at 1).
//!
//! **Work items.** `.../wit/workItems/{id}/comments` (preview API), newest
//! first with `continuationToken` as the `older` cursor. Services answers and
//! takes Markdown (`format=markdown`); Server (7.0) answers HTML bodies,
//! shown through the HTML importer, and takes HTML, generated from the
//! Markdown. The description is always an HTML field. There is no thread and
//! no event history.

use serde_json::{json, Value};

use super::AzureDevopsClient;
use crate::azure_devops::map::is_closed_state;
use crate::azure_devops::spec::split_project;
use crate::client::encode_component as enc;
use crate::conversation::compat::{check_body, sort_by_time, token_source};
use crate::conversation::time::is_edited;
use crate::conversation::{
    Actor, ActorKind, CodeThread, Comment, Event, EventKind, IdentityKind, ItemHeader, ItemKey,
    ItemKind, Markup, MarkupFlavor, PageCursor, PostingIdentity, ReviewEntry, TimelineEntry,
    TimelineOpts, TimelinePage, WINDOW,
};
use crate::http::HttpRequest;
use crate::markup::{html_to_markdown, markdown_to_html};
use crate::review::invalid;
use crate::review::model::{DiffSide, ReviewState};
use crate::{Error, Result};
use gitpoulpi_config::IntegrationKind;

const FLAVOR: MarkupFlavor = MarkupFlavor::AzureMd;
const PAGE: usize = 100;
const PAGES_PER_WINDOW: usize = WINDOW / PAGE;

fn text(v: &Value, key: &str) -> String {
    v.get(key)
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string()
}

fn opt_text(v: &Value, key: &str) -> Option<String> {
    v.get(key)
        .and_then(Value::as_str)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
}

/// An identity (`displayName`, `uniqueName`) or a `"Name <email>"` string.
fn actor_of(v: Option<&Value>) -> Actor {
    match v {
        Some(Value::Object(o)) => {
            let unique = o
                .get("uniqueName")
                .and_then(Value::as_str)
                .unwrap_or_default();
            let display = o
                .get("displayName")
                .and_then(Value::as_str)
                .unwrap_or_default();
            let login = if unique.is_empty() { display } else { unique };
            if login.is_empty() {
                return Actor::unknown();
            }
            let mut actor = Actor::new(
                login,
                if display.is_empty() { login } else { display },
                ActorKind::User,
            );
            actor.web_url = o
                .get("url")
                .and_then(Value::as_str)
                .filter(|u| u.starts_with("http"))
                .map(str::to_string);
            actor
        }
        Some(Value::String(s)) => {
            let name = s.split('<').next().unwrap_or(s).trim();
            let email = s
                .split_once('<')
                .and_then(|(_, r)| r.strip_suffix('>'))
                .unwrap_or(name);
            if name.is_empty() {
                Actor::unknown()
            } else {
                Actor::new(email, name, ActorKind::User)
            }
        }
        _ => Actor::unknown(),
    }
}

/// HTML written by Azure DevOps Server or an older Services comment.
fn looks_like_html(body: &str) -> bool {
    let b = body.trim_start();
    b.starts_with('<')
        || ["<div", "<p>", "<p ", "<br", "<a ", "<span", "<ul", "<ol"]
            .iter()
            .any(|t| body.contains(t))
}

fn pr_comment_of(thread: u64, c: &Value) -> Comment {
    let created_at = text(c, "publishedDate");
    let updated = opt_text(c, "lastContentUpdatedDate").or_else(|| opt_text(c, "lastUpdatedDate"));
    let id = c["id"].as_u64().unwrap_or_default();
    let parent = c["parentCommentId"].as_u64().filter(|p| *p > 0);
    Comment {
        id: format!("{thread}.{id}"),
        author: actor_of(c.get("author")),
        body: Markup::new(text(c, "content"), FLAVOR),
        edited: is_edited(&created_at, updated.as_deref()),
        created_at,
        updated_at: updated,
        reactions: Vec::new(),
        in_reply_to: parent.map(|p| format!("{thread}.{p}")),
        discussion_id: Some(thread.to_string()),
        internal: false,
        can_edit: None,
        can_delete: None,
        web_url: None,
    }
}

fn thread_property(thread: &Value, name: &str) -> Option<String> {
    let p = &thread["properties"][name];
    p["$value"]
        .as_str()
        .map(str::to_string)
        .or_else(|| p["$value"].as_i64().map(|n| n.to_string()))
}

/// A system thread as an event or a verdict.
fn system_entry(thread: &Value, comments: &[&Value]) -> Option<TimelineEntry> {
    let first = comments.first()?;
    let content = text(first, "content");
    let at = text(first, "publishedDate");
    let id = thread["id"].as_u64().unwrap_or_default().to_string();
    let actor = actor_of(first.get("author"));
    let kind = thread_property(thread, "CodeReviewThreadType").unwrap_or_default();
    if kind == "VoteUpdate" {
        let vote: i32 = thread_property(thread, "CodeReviewVoteResult")
            .and_then(|v| v.parse().ok())
            .unwrap_or(0);
        let state = match vote {
            v if v > 0 => ReviewState::Approved,
            v if v < 0 => ReviewState::ChangesRequested,
            _ => {
                return Some(TimelineEntry::Event(Event {
                    id,
                    actor: Some(actor),
                    at,
                    kind: EventKind::Other { text: content },
                }))
            }
        };
        let voter = thread_property(thread, "CodeReviewVotedByIdentity")
            .map(|_| actor.clone())
            .unwrap_or(actor);
        return Some(TimelineEntry::Review(ReviewEntry {
            id,
            author: voter,
            state,
            body: Markup::new(content, FLAVOR),
            submitted_at: at,
        }));
    }
    let lower = content.to_ascii_lowercase();
    let event = if kind == "StatusUpdate" || kind.is_empty() {
        if lower.contains("completed") {
            EventKind::Merged
        } else if lower.contains("abandoned") {
            EventKind::Closed
        } else if lower.contains("reactivated") {
            EventKind::Reopened
        } else {
            EventKind::Other { text: content }
        }
    } else if kind == "ReviewerAdded" {
        EventKind::ReviewRequested {
            who: content
                .trim_end_matches(" as a reviewer")
                .trim_end_matches(" as a required reviewer")
                .rsplit(" added ")
                .next()
                .unwrap_or_default()
                .to_string(),
        }
    } else {
        EventKind::Other { text: content }
    };
    Some(TimelineEntry::Event(Event {
        id,
        actor: Some(actor),
        at,
        kind: event,
    }))
}

fn resolved_status(status: &str) -> bool {
    matches!(
        status.to_ascii_lowercase().as_str(),
        "fixed" | "closed" | "wontfix" | "bydesign"
    )
}

fn code_thread(thread: &Value, comments: Vec<Comment>) -> Option<CodeThread> {
    let ctx = &thread["threadContext"];
    let path = ctx["filePath"].as_str().filter(|p| !p.is_empty())?;
    let num = |v: &Value| v["line"].as_u64().map(|n| n as u32);
    let (side, start, end) = if ctx["rightFileStart"].is_object() {
        (
            DiffSide::New,
            num(&ctx["rightFileStart"]),
            num(&ctx["rightFileEnd"]),
        )
    } else {
        (
            DiffSide::Old,
            num(&ctx["leftFileStart"]),
            num(&ctx["leftFileEnd"]),
        )
    };
    let line = end.or(start);
    Some(CodeThread {
        id: thread["id"].as_u64().unwrap_or_default().to_string(),
        path: path.trim_start_matches('/').to_string(),
        line,
        original_line: line,
        side,
        start_line: start.filter(|s| Some(*s) != line),
        resolved: resolved_status(thread["status"].as_str().unwrap_or_default()),
        outdated: false,
        resolvable: true,
        diff_hunk: None,
        comments,
    })
}

fn pr_entries(thread: &Value, out: &mut Vec<TimelineEntry>, events: bool) {
    if thread["isDeleted"].as_bool() == Some(true) {
        return;
    }
    let live: Vec<&Value> = thread["comments"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(|c| c["isDeleted"].as_bool() != Some(true))
        .collect();
    if live.is_empty() {
        return;
    }
    let is_system = live
        .iter()
        .all(|c| c["commentType"].as_str() == Some("system"))
        || thread["properties"]["CodeReviewThreadType"].is_object();
    if is_system {
        let entry = system_entry(thread, &live);
        if let Some(e) = entry {
            if events || matches!(e, TimelineEntry::Review(_)) {
                out.push(e);
            }
        }
        return;
    }
    let id = thread["id"].as_u64().unwrap_or_default();
    let comments: Vec<Comment> = live
        .iter()
        .filter(|c| c["commentType"].as_str() != Some("system"))
        .map(|c| pr_comment_of(id, c))
        .collect();
    if comments.is_empty() {
        return;
    }
    match code_thread(thread, comments.clone()) {
        Some(t) => out.push(TimelineEntry::Thread(t)),
        None => out.extend(comments.into_iter().map(TimelineEntry::Comment)),
    }
}

impl AzureDevopsClient {
    pub(super) fn is_server(&self) -> bool {
        self.integration.kind == IntegrationKind::AzureDevopsServer
    }

    /// The version the work item comment routes are in (still a preview).
    fn comments_version(&self) -> &'static str {
        if self.is_server() {
            "7.0-preview.3"
        } else {
            "7.1-preview.4"
        }
    }

    /// `{root}/{project}/_apis/{path}?{query&}api-version={version}`.
    fn conv_url(&self, project: &str, path: &str, query: &str, version: &str) -> Result<String> {
        let mut url = self.url(Some(project), path, query)?;
        let cut = url.rfind("api-version=").unwrap_or(url.len());
        url.truncate(cut);
        url.push_str("api-version=");
        url.push_str(version);
        Ok(url)
    }

    async fn pr_threads_url(
        &self,
        project: &str,
        id: u64,
        rest: &str,
        query: &str,
    ) -> Result<String> {
        let (p, _) = split_project(project);
        let repo = self.repo_id(project).await?;
        self.url(
            Some(p),
            &format!(
                "git/repositories/{}/pullRequests/{id}/threads{rest}",
                enc(&repo)
            ),
            query,
        )
    }

    async fn conv_json(&self, request: HttpRequest, what: &str) -> Result<Value> {
        self.send(request, what).await?.json()
    }

    pub(super) async fn conv_timeline(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
        opts: &TimelineOpts,
    ) -> Result<TimelinePage> {
        match key.kind {
            ItemKind::PullRequest => self.pr_timeline(project, key, older, opts).await,
            ItemKind::Issue => self.item_timeline_wi(project, key, older).await,
        }
    }

    async fn pr_timeline(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
        opts: &TimelineOpts,
    ) -> Result<TimelinePage> {
        let end: Option<usize> = match older {
            Some(c) => Some(
                c.0.parse()
                    .map_err(|_| invalid("reading a conversation", "bad page cursor"))?,
            ),
            None => None,
        };
        let (p, r) = split_project(project);
        let repo = self.repo_id(project).await?;
        let pull_url = self.url(
            Some(p),
            &format!(
                "git/repositories/{}/pullRequests/{}",
                enc(&repo),
                key.number
            ),
            "",
        )?;
        let row = self
            .conv_json(self.get(pull_url), "reading an Azure DevOps pull request")
            .await?;
        let (state_label, open) = match row["status"].as_str().unwrap_or_default() {
            "completed" => ("merged", false),
            "abandoned" => ("abandoned", false),
            _ if row["isDraft"].as_bool() == Some(true) => ("draft", true),
            _ => ("open", true),
        };
        let spec = crate::spec::spec_for(gitpoulpi_config::ApiFamily::AzureDevops);
        let header = ItemHeader {
            key: key.clone(),
            reference: format!("!{}", key.number),
            title: text(&row, "title"),
            state_label: state_label.to_string(),
            open,
            author: actor_of(row.get("createdBy")),
            body: Markup::new(text(&row, "description"), FLAVOR),
            created_at: text(&row, "creationDate"),
            web_url: (spec.pr_web_url)(&self.root()?, &format!("{p}/{r}"), key.number)
                .unwrap_or_default(),
            labels: row["labels"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(|l| l["name"].as_str())
                .map(str::to_string)
                .collect(),
        };
        let threads_url = self.pr_threads_url(project, key.number, "", "").await?;
        let list = self
            .conv_json(
                self.get(threads_url),
                "reading the threads of a pull request",
            )
            .await?;
        let mut entries = Vec::new();
        for thread in list["value"].as_array().into_iter().flatten() {
            pr_entries(thread, &mut entries, opts.events);
        }
        sort_by_time(&mut entries);
        let end = end.unwrap_or(entries.len()).min(entries.len());
        let start = end.saturating_sub(WINDOW);
        let total = entries.len() as u32;
        let window: Vec<TimelineEntry> = entries.drain(start..end).collect();
        Ok(TimelinePage {
            header,
            entries: window,
            older: (start > 0).then(|| PageCursor(start.to_string())),
            total_hint: older.is_none().then_some(total),
        })
    }

    fn wi_comment_of(c: &Value) -> Comment {
        let body = text(c, "text");
        let format = c["format"].as_str().unwrap_or_default();
        let markdown = if format == "html" || (format != "markdown" && looks_like_html(&body)) {
            html_to_markdown(&body)
        } else {
            body
        };
        let created_at = text(c, "createdDate");
        let updated = opt_text(c, "modifiedDate");
        let id = c["id"].as_u64().unwrap_or_default().to_string();
        Comment {
            id,
            author: actor_of(c.get("createdBy")),
            body: Markup::new(markdown, FLAVOR),
            edited: is_edited(&created_at, updated.as_deref()),
            created_at,
            updated_at: updated,
            reactions: Vec::new(),
            in_reply_to: None,
            discussion_id: None,
            internal: false,
            can_edit: None,
            can_delete: None,
            web_url: None,
        }
    }

    async fn item_timeline_wi(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
    ) -> Result<TimelinePage> {
        let (p, _) = split_project(project);
        let item = self
            .conv_json(
                self.get(self.url(
                    Some(p),
                    &format!("wit/workItems/{}", key.number),
                    "fields=System.Title,System.State,System.CreatedBy,System.CreatedDate,\
                     System.Description,System.Tags,System.TeamProject",
                )?),
                "reading an Azure DevOps work item",
            )
            .await?;
        let fields = &item["fields"];
        let state = text(fields, "System.State");
        let spec = crate::spec::spec_for(gitpoulpi_config::ApiFamily::AzureDevops);
        let header = ItemHeader {
            key: key.clone(),
            reference: format!("#{}", key.number),
            title: text(fields, "System.Title"),
            open: !is_closed_state(&state),
            state_label: state.to_ascii_lowercase(),
            author: actor_of(fields.get("System.CreatedBy")),
            body: Markup::new(
                html_to_markdown(&text(fields, "System.Description")),
                FLAVOR,
            ),
            created_at: text(fields, "System.CreatedDate"),
            web_url: (spec.issue_web_url)(&self.root()?, p, &key.number.to_string())
                .unwrap_or_default(),
            labels: text(fields, "System.Tags")
                .split(';')
                .map(str::trim)
                .filter(|t| !t.is_empty())
                .map(str::to_string)
                .collect(),
        };
        let mut token: Option<String> = older.map(|c| c.0.clone());
        let mut next: Option<String> = None;
        let mut entries: Vec<TimelineEntry> = Vec::new();
        let mut hint: Option<u32> = None;
        for page in 0..PAGES_PER_WINDOW {
            let mut query = format!("$top={PAGE}&order=desc");
            if let Some(t) = &token {
                query.push_str(&format!("&continuationToken={}", enc(t)));
            }
            let url = self.conv_url(
                p,
                &format!("wit/workItems/{}/comments", key.number),
                &query,
                self.comments_version(),
            )?;
            let body = self
                .conv_json(self.get(url), "reading the comments of a work item")
                .await?;
            if page == 0 && older.is_none() {
                hint = body["totalCount"].as_u64().map(|n| n as u32);
            }
            for c in body["comments"].as_array().into_iter().flatten() {
                if c["isDeleted"].as_bool() != Some(true) {
                    entries.push(TimelineEntry::Comment(Self::wi_comment_of(c)));
                }
            }
            next = opt_text(&body, "continuationToken");
            match &next {
                Some(t) => token = Some(t.clone()),
                None => break,
            }
        }
        sort_by_time(&mut entries);
        Ok(TimelinePage {
            header,
            entries,
            older: next.map(PageCursor),
            total_hint: hint,
        })
    }

    /// The body as the host takes it: Markdown, or HTML on Server where
    /// work item comments have no Markdown format.
    fn wi_payload(&self, body: &str) -> (Value, &'static str) {
        if self.is_server() {
            (json!({ "text": markdown_to_html(body) }), "")
        } else {
            (json!({ "text": body }), "format=markdown")
        }
    }

    pub(super) async fn conv_post(
        &self,
        project: &str,
        key: &ItemKey,
        body: &str,
        internal: bool,
    ) -> Result<Comment> {
        if internal {
            return Err(invalid(
                "commenting",
                "Azure DevOps has no internal comments",
            ));
        }
        check_body("commenting", body)?;
        match key.kind {
            ItemKind::PullRequest => {
                let url = self.pr_threads_url(project, key.number, "", "").await?;
                let payload = json!({
                    "comments": [{ "parentCommentId": 0, "content": body, "commentType": 1 }],
                    "status": 1,
                });
                let thread = self
                    .conv_json(
                        self.post(url, &payload, None),
                        "commenting on an Azure DevOps pull request",
                    )
                    .await?;
                let id = thread["id"].as_u64().unwrap_or_default();
                let first = thread["comments"]
                    .get(0)
                    .ok_or_else(|| Error::Decode("the thread came back empty".to_string()))?;
                Ok(pr_comment_of(id, first))
            }
            ItemKind::Issue => {
                let (p, _) = split_project(project);
                let (payload, query) = self.wi_payload(body);
                let url = self.conv_url(
                    p,
                    &format!("wit/workItems/{}/comments", key.number),
                    query,
                    self.comments_version(),
                )?;
                let row = self
                    .conv_json(
                        self.post(url, &payload, None),
                        "commenting on an Azure DevOps work item",
                    )
                    .await?;
                Ok(Self::wi_comment_of(&row))
            }
        }
    }

    pub(super) async fn conv_reply(
        &self,
        project: &str,
        key: &ItemKey,
        discussion_id: &str,
        body: &str,
    ) -> Result<Comment> {
        if key.kind != ItemKind::PullRequest {
            return Err(Error::Unsupported(
                "replying in a thread of an Azure DevOps work item".to_string(),
            ));
        }
        check_body("replying", body)?;
        let thread: u64 = discussion_id
            .parse()
            .map_err(|_| invalid("replying", "not a thread of this host"))?;
        let url = self
            .pr_threads_url(project, key.number, &format!("/{thread}/comments"), "")
            .await?;
        // The first comment of a thread is comment 1.
        let payload = json!({ "parentCommentId": 1, "content": body, "commentType": 1 });
        let row = self
            .conv_json(
                self.post(url, &payload, None),
                "replying to an Azure DevOps thread",
            )
            .await?;
        Ok(pr_comment_of(thread, &row))
    }

    /// `"{thread}.{comment}"` as the two numbers.
    fn pr_comment_ids(id: &str) -> Result<(u64, u64)> {
        id.split_once('.')
            .and_then(|(t, c)| Some((t.parse().ok()?, c.parse().ok()?)))
            .ok_or_else(|| invalid("editing a comment", "not a comment of this host"))
    }

    pub(super) async fn conv_edit(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
        body: &str,
    ) -> Result<Comment> {
        check_body("editing a comment", body)?;
        match key.kind {
            ItemKind::PullRequest => {
                let (thread, comment) = Self::pr_comment_ids(comment_id)?;
                let url = self
                    .pr_threads_url(
                        project,
                        key.number,
                        &format!("/{thread}/comments/{comment}"),
                        "",
                    )
                    .await?;
                let row = self
                    .conv_json(
                        self.authed(HttpRequest::patch_json(url, &json!({ "content": body }))),
                        "editing an Azure DevOps comment",
                    )
                    .await?;
                Ok(pr_comment_of(thread, &row))
            }
            ItemKind::Issue => {
                let (p, _) = split_project(project);
                let (payload, query) = self.wi_payload(body);
                let url = self.conv_url(
                    p,
                    &format!("wit/workItems/{}/comments/{}", key.number, enc(comment_id)),
                    query,
                    self.comments_version(),
                )?;
                let row = self
                    .conv_json(
                        self.authed(HttpRequest::patch_json(url, &payload)),
                        "editing an Azure DevOps work item comment",
                    )
                    .await?;
                Ok(Self::wi_comment_of(&row))
            }
        }
    }

    pub(super) async fn conv_delete(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
    ) -> Result<()> {
        let (url, what) = match key.kind {
            ItemKind::PullRequest => {
                let (thread, comment) = Self::pr_comment_ids(comment_id)?;
                (
                    self.pr_threads_url(
                        project,
                        key.number,
                        &format!("/{thread}/comments/{comment}"),
                        "",
                    )
                    .await?,
                    "deleting an Azure DevOps comment",
                )
            }
            ItemKind::Issue => {
                let (p, _) = split_project(project);
                (
                    self.conv_url(
                        p,
                        &format!("wit/workItems/{}/comments/{}", key.number, enc(comment_id)),
                        "",
                        self.comments_version(),
                    )?,
                    "deleting an Azure DevOps work item comment",
                )
            }
        };
        self.send(self.authed(HttpRequest::delete(url)), what)
            .await?;
        Ok(())
    }

    /// The token's own account, from `connectionData`; unknown when the
    /// server does not say (then nothing is matched to "mine").
    pub(super) async fn conv_identity(&self, project: &str) -> Result<PostingIdentity> {
        let (source, _) = token_source(&self.scopes, &self.integration.id, project);
        let user = self.connection_user().await?;
        let mut identity = PostingIdentity::new(
            user.map(|u| u.login).filter(|l| !l.is_empty()),
            IdentityKind::PersonalToken,
            source,
        );
        if identity.login.is_none() {
            identity.kind = IdentityKind::Unknown;
            identity.display = "your Azure DevOps account".to_string();
        }
        Ok(identity)
    }
}