lgtm-cli 0.3.1

Run LGTM code reviews with a bundled web UI and gRPC server.
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
//! Comments command arguments and RPC callbacks.
use super::{client_for_actor, get_conversation, get_review, get_scope, key, print_result};
use crate::{
    models::{ActorArgs, ContextArgs, MutationArgs},
    rpc_client::Client,
    store::Store,
};
use anyhow::{Context as _, Result, bail};
use clap::{Args, Subcommand};
use serde_json::{Value, json};

// Types -----------------------------------------------------------------------

#[derive(Subcommand)]
pub enum CommentsCommand {
    /// Synchronize or recover a locally saved PR draft without publishing its review.
    Sync {
        comment: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
    },
    List {
        #[command(flatten)]
        context: ContextArgs,
    },
    Add {
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[arg(long)]
        body: String,
        #[arg(long)]
        file: Option<String>,
        #[arg(long, requires = "file")]
        line: Option<i64>,
        #[arg(long, requires = "line")]
        end_line: Option<i64>,
        #[arg(long,requires="line",value_parser=["old","new"])]
        side: Option<String>,
        #[command(flatten)]
        guard: MutationArgs,
    },
    Reply {
        thread: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[arg(long)]
        body: String,
        #[command(flatten)]
        guard: MutationArgs,
    },
    Resolve {
        thread: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[command(flatten)]
        guard: MutationArgs,
    },
    Edit {
        comment: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[arg(long)]
        body: String,
        #[command(flatten)]
        guard: MutationArgs,
    },
    Delete {
        comment: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[command(flatten)]
        guard: MutationArgs,
    },
    Promote {
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[arg(long)]
        target_session: String,
        #[arg(long, required = true)]
        comment: Vec<String>,
        #[arg(long)]
        request_key: Option<String>,
        /// Explicit target file anchor; omit with --general for a review-wide thread.
        #[arg(long, conflicts_with = "general")]
        file: Option<String>,
        #[arg(long)]
        general: bool,
    },
    Reopen {
        thread: String,
        #[command(flatten)]
        context: ContextArgs,
        #[command(flatten)]
        actor: ActorArgs,
        #[command(flatten)]
        guard: MutationArgs,
    },
}

// Command ---------------------------------------------------------------------

#[derive(Args)]
pub struct CommentsArgs {
    #[command(subcommand)]
    pub command: CommentsCommand,
}

// Callbacks -------------------------------------------------------------------

pub async fn run(args: CommentsArgs, store: &Store, json_output: bool) -> Result<()> {
    let result = match args.command {
        CommentsCommand::List { context } => {
            let client = Client::new(store)?;
            let scope = get_scope(&client, &context, None, false).await?;
            get_conversation(&client, &scope).await?
        }
        CommentsCommand::Add {
            context,
            actor,
            body,
            file,
            line,
            end_line,
            side,
            guard,
        } => {
            let client = client_for_actor(store, &actor).await?;
            let scope = get_scope(&client, &context, file.as_deref(), true).await?;
            let conversation = get_conversation(&client, &scope).await?;
            let anchor = capture_anchor(
                &client,
                &scope,
                file.as_deref(),
                line,
                end_line,
                side.as_deref(),
            )
            .await?;
            client.call("AddComment",json!({"scope":scope,"expectedVersion":guard.expected_version.map(Value::String).unwrap_or_else(||conversation["version"].clone()),"requestKey":key(guard.request_key),"body":body,"anchor":anchor})).await?
        }
        CommentsCommand::Sync {
            comment,
            context,
            actor,
        } => {
            let client = client_for_actor(store, &actor).await?;
            let review = get_review(&client, &context).await?;
            client
                .call(
                    "SyncDraftComment",
                    json!({"reviewId":review["id"],"commentId":comment}),
                )
                .await?
        }
        CommentsCommand::Reply {
            thread,
            context,
            actor,
            body,
            guard,
        } => {
            let (client, scope, version) =
                object_guard(store, &context, &actor, &guard, "threads", &thread, true).await?;
            client.call("Reply",json!({"scope":scope,"expectedVersion":version,"requestKey":key(guard.request_key),"threadId":thread,"body":body})).await?
        }
        CommentsCommand::Resolve {
            thread,
            context,
            actor,
            guard,
        } => {
            let (client, scope, version) =
                object_guard(store, &context, &actor, &guard, "threads", &thread, false).await?;
            client.call("ResolveThread",json!({"scope":scope,"expectedVersion":version,"requestKey":key(guard.request_key),"threadId":thread,"resolved":true})).await?
        }
        CommentsCommand::Reopen {
            thread,
            context,
            actor,
            guard,
        } => {
            let (client, scope, version) =
                object_guard(store, &context, &actor, &guard, "threads", &thread, false).await?;
            client.call("ResolveThread",json!({"scope":scope,"expectedVersion":version,"requestKey":key(guard.request_key),"threadId":thread,"resolved":false})).await?
        }
        CommentsCommand::Edit {
            comment,
            context,
            actor,
            body,
            guard,
        } => {
            let (client, scope, version) =
                object_guard(store, &context, &actor, &guard, "comments", &comment, false).await?;
            client.call("EditComment",json!({"scope":scope,"expectedVersion":version,"requestKey":key(guard.request_key),"commentId":comment,"body":body})).await?
        }
        CommentsCommand::Delete {
            comment,
            context,
            actor,
            guard,
        } => {
            let (client, scope, version) =
                object_guard(store, &context, &actor, &guard, "comments", &comment, false).await?;
            client.call("DeleteComment",json!({"scope":scope,"expectedVersion":version,"requestKey":key(guard.request_key),"commentId":comment})).await?
        }
        CommentsCommand::Promote {
            context,
            actor,
            target_session,
            comment,
            request_key,
            file,
            general,
        } => {
            if file.is_none() && !general {
                bail!("Promotion requires explicit --file PATH or --general target mapping");
            }
            let client = client_for_actor(store, &actor).await?;
            let mut source = get_scope(&client, &context, None, false).await?;
            if !source["reviewId"].is_null() {
                let review = client
                    .call("GetReview", json!({"reviewId":source["reviewId"]}))
                    .await?;
                let path = review["review"]["target"]["directory"]["path"]
                    .as_str()
                    .context("Promotion source must be local notes")?;
                source = json!({"notes":{"path":path}});
            }
            let source_conversation = get_conversation(&client, &source).await?;
            let target_scope = json!({"reviewId":target_session});
            let target_conversation = get_conversation(&client, &target_scope).await?;
            let anchor =
                capture_anchor(&client, &target_scope, file.as_deref(), None, None, None).await?;
            let anchors: Vec<_> = comment.iter().map(|_| anchor.clone()).collect();
            client.call("PromoteNotes",json!({"source":source,"targetReviewId":target_session,"expectedSourceVersion":source_conversation["version"],"expectedReviewVersion":target_conversation["version"],"commentIds":comment,"anchors":anchors,"requestKey":key(request_key)})).await?
        }
    };
    print_result(&result, json_output)
}

// Private ---------------------------------------------------------------------

async fn object_guard(
    store: &Store,
    args: &ContextArgs,
    actor: &ActorArgs,
    guard: &MutationArgs,
    collection: &str,
    id: &str,
    starts_draft: bool,
) -> Result<(Client, Value, Value)> {
    let client = client_for_actor(store, actor).await?;
    let scope = get_scope(&client, args, None, starts_draft).await?;
    if let Some(version) = &guard.expected_version {
        return Ok((client, scope, json!(version)));
    }
    let conversation = get_conversation(&client, &scope).await?;
    let version = conversation[collection]
        .as_array()
        .and_then(|objects| objects.iter().find(|object| object["id"] == id))
        .context("Object is outside the selected scope")?["version"]
        .clone();
    Ok((client, scope, version))
}

async fn capture_anchor(
    client: &Client,
    scope: &Value,
    file: Option<&str>,
    first: Option<i64>,
    last: Option<i64>,
    side: Option<&str>,
) -> Result<Value> {
    use crate::backend::{
        database::types::SourceKind,
        models::{AnchorEvidence, AnchorSource},
        services::workspace,
    };
    let Some(file) = file else {
        return Ok(json!({"kind":"general"}));
    };
    let side = side.unwrap_or("new");
    let mut path = file.to_owned();
    let (source, bytes) = if let Some(notes) = scope["notes"]["path"].as_str() {
        let selected = std::path::Path::new(notes);
        let root = selected.parent().context("Notes file has no parent")?;
        path = selected
            .file_name()
            .context("Notes file has no name")?
            .to_string_lossy()
            .into_owned();
        workspace::get_source(root, &path, SourceKind::LocalFile, None)?
    } else {
        let review = client
            .call("GetReview", json!({"reviewId":scope["reviewId"]}))
            .await?["review"]
            .clone();
        let target = &review["target"];
        if let Some(root) = target["directory"]["path"].as_str() {
            workspace::get_source(
                std::path::Path::new(root),
                &path,
                SourceKind::LocalFile,
                None,
            )?
        } else if let Some(root) = target["localGit"]["path"].as_str() {
            let comparison = &review["comparison"];
            let (kind, commit) = if side == "old" {
                (
                    SourceKind::Commit,
                    comparison
                        .as_object()
                        .and_then(|c| c.values().next())
                        .and_then(|c| c["baseCommit"].as_str()),
                )
            } else if comparison["staged"].is_object() {
                (SourceKind::IndexFile, None)
            } else if comparison["working"].is_object() {
                (SourceKind::WorkingFile, None)
            } else {
                (
                    SourceKind::Commit,
                    comparison["branch"]["headCommit"].as_str(),
                )
            };
            let mut selected =
                workspace::get_source(std::path::Path::new(root), &path, kind, commit)?;
            if kind == SourceKind::WorkingFile
                && let Ok(indexed) = workspace::get_source(
                    std::path::Path::new(root),
                    &path,
                    SourceKind::IndexFile,
                    None,
                )
                && selected.1 == indexed.1
            {
                selected = indexed;
            }
            selected
        } else {
            let diff = client
                .call(
                    "GetDiff",
                    json!({"target":target,"comparison":review["comparison"]}),
                )
                .await?["diff"]
                .clone();
            let selected = client
                .call("GetFile", json!({"diff":diff,"path":path}))
                .await?;
            let value = &selected[if side == "old" { "oldSide" } else { "newSide" }];
            path = value["path"]
                .as_str()
                .context("File does not exist on the selected side")?
                .into();
            let wire = &value["source"]["commit"];
            (
                AnchorSource::Commit {
                    sha: wire["sha"]
                        .as_str()
                        .context("Missing selected commit")?
                        .into(),
                    content_hash: wire["contentHash"]
                        .as_str()
                        .context("Missing selected content hash")?
                        .into(),
                },
                value["content"].as_str().unwrap_or("").as_bytes().to_vec(),
            )
        }
    };
    let evidence = match first {
        Some(first) => {
            AnchorEvidence::capture(source.clone(), &bytes, first, last.unwrap_or(first))?
        }
        None => AnchorEvidence::capture_file(source.clone(), &bytes)?,
    };
    let source = match source {
        AnchorSource::Commit { sha, content_hash } => {
            json!({"commit":{"sha":sha,"contentHash":content_hash}})
        }
        AnchorSource::IndexFile {
            blob_oid,
            content_hash,
        } => json!({"indexFile":{"blobOid":blob_oid,"contentHash":content_hash}}),
        AnchorSource::WorkingFile { content_hash } => {
            json!({"workingFile":{"contentHash":content_hash}})
        }
        AnchorSource::LocalFile { content_hash } => {
            json!({"localFile":{"contentHash":content_hash}})
        }
    };
    let mut anchor = json!({"kind":if first.is_some(){"line"}else{"file"},"path":path,"source":source,"evidence":{"selectedText":evidence.selected_text,"contextBefore":evidence.context_before,"contextAfter":evidence.context_after}});
    if let Some(first) = first {
        anchor["start"] = json!(first.to_string());
        anchor["end"] = json!(last.unwrap_or(first).to_string());
        anchor["side"] = json!(side);
    }
    Ok(anchor)
}