-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_pr_comment.rs
More file actions
632 lines (571 loc) · 21.4 KB
/
add_pr_comment.rs
File metadata and controls
632 lines (571 loc) · 21.4 KB
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
//! Add PR comment safe output tool
use log::{debug, info};
use percent_encoding::utf8_percent_encode;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::PATH_SEGMENT;
use ado_aw_derive::SanitizeConfig;
use crate::sanitize::{SanitizeContent, sanitize as sanitize_text};
use crate::tool_result;
use crate::safeoutputs::{ExecutionContext, ExecutionResult, Executor, Validate};
use anyhow::{Context, ensure};
/// Parameters for adding a comment thread on a pull request
#[derive(Deserialize, JsonSchema)]
pub struct AddPrCommentParams {
/// The pull request ID to comment on
pub pull_request_id: i32,
/// Comment text in markdown format. Ensure adequate content > 10 characters.
pub content: String,
/// Repository alias: "self" for pipeline repo, or an alias from the checkout list.
/// Defaults to "self" if omitted.
#[serde(default = "default_repository")]
pub repository: String,
/// File path for an inline comment. When set, the comment is anchored to this file.
#[serde(default)]
pub file_path: Option<String>,
/// Starting line number for a multi-line inline comment. Requires `file_path` and `line`.
/// When set, the comment spans from `start_line` to `line`. Must be strictly less than
/// `line` (use `line` alone for single-line comments — do not pass `start_line == line`).
#[serde(default)]
pub start_line: Option<i32>,
/// Line number for an inline comment. Requires `file_path` to be set.
#[serde(default)]
pub line: Option<i32>,
/// Thread status: "active" (default), "fixed", "wont-fix", "closed", or "by-design".
/// CamelCase forms ("Active", "WontFix", etc.) are also accepted for backwards compatibility.
#[serde(default = "default_status")]
pub status: String,
}
fn default_repository() -> String {
"self".to_string()
}
fn default_status() -> String {
"active".to_string()
}
impl Validate for AddPrCommentParams {
fn validate(&self) -> anyhow::Result<()> {
ensure!(self.pull_request_id > 0, "pull_request_id must be positive");
ensure!(
self.content.len() >= 10,
"content must be at least 10 characters"
);
ensure!(
status_to_int(&self.status).is_some(),
"status must be one of: {}",
VALID_STATUSES.join(", ")
);
if self.line.is_some() {
ensure!(
self.file_path.is_some(),
"line requires file_path to be set"
);
}
if self.start_line.is_some() {
ensure!(
self.line.is_some(),
"start_line requires line to be set"
);
if let (Some(start), Some(end)) = (self.start_line, self.line) {
ensure!(
start < end,
"start_line ({}) must be less than line ({})",
start,
end
);
}
}
if let Some(fp) = &self.file_path {
validate_file_path(fp)?;
}
Ok(())
}
}
tool_result! {
name = "add-pr-comment",
write = true,
params = AddPrCommentParams,
/// Result of adding a comment thread on a pull request
pub struct AddPrCommentResult {
pull_request_id: i32,
content: String,
repository: String,
file_path: Option<String>,
start_line: Option<i32>,
line: Option<i32>,
status: String,
}
}
impl SanitizeContent for AddPrCommentResult {
fn sanitize_content_fields(&mut self) {
self.content = sanitize_text(&self.content);
// Strip control characters from structural fields for defense-in-depth
self.repository = self.repository.chars().filter(|c| !c.is_control()).collect();
self.status = self.status.chars().filter(|c| !c.is_control()).collect();
self.file_path = self.file_path.as_ref().map(|fp| {
fp.chars().filter(|c| !c.is_control()).collect()
});
}
}
/// Configuration for the add-pr-comment tool (specified in front matter)
///
/// Example front matter:
/// ```yaml
/// safe-outputs:
/// add-pr-comment:
/// comment-prefix: "[Agent Review] "
/// allowed-repositories:
/// - self
/// - other-repo
/// allowed-statuses:
/// - Active
/// - Closed
/// ```
#[derive(Debug, Clone, SanitizeConfig, Serialize, Deserialize)]
pub struct AddPrCommentConfig {
/// Prefix prepended to all comments (e.g., "[Agent Review] ")
#[serde(default, rename = "comment-prefix")]
pub comment_prefix: Option<String>,
/// Restrict which repositories the agent can comment on.
/// If empty, all repositories in the checkout list (plus "self") are allowed.
#[serde(default, rename = "allowed-repositories")]
pub allowed_repositories: Vec<String>,
/// Restrict which thread statuses can be set.
/// If empty, all valid statuses are allowed.
#[serde(default, rename = "allowed-statuses")]
pub allowed_statuses: Vec<String>,
/// Whether to include agent execution stats in the output (default: true).
#[serde(default = "crate::agent_stats::default_include_stats", rename = "include-stats")]
pub include_stats: bool,
}
impl Default for AddPrCommentConfig {
fn default() -> Self {
Self {
comment_prefix: None,
allowed_repositories: Vec::new(),
allowed_statuses: Vec::new(),
include_stats: true,
}
}
}
/// Map a thread status string to the ADO API integer value.
/// Accepts both kebab-case (preferred) and CamelCase for backwards compatibility.
fn status_to_int(status: &str) -> Option<i32> {
match status {
"active" | "Active" => Some(1),
"fixed" | "Fixed" => Some(2),
"wont-fix" | "WontFix" => Some(3),
"closed" | "Closed" => Some(4),
"by-design" | "ByDesign" => Some(5),
_ => None,
}
}
/// All valid thread status strings (kebab-case canonical form)
const VALID_STATUSES: &[&str] = &["active", "fixed", "wont-fix", "closed", "by-design"];
/// Validate a file path for inline comments: no `..` path traversal, not absolute
fn validate_file_path(path: &str) -> anyhow::Result<()> {
ensure!(!path.is_empty(), "file_path must not be empty");
ensure!(
!path.split(['/', '\\']).any(|component| component == ".."),
"file_path must not contain a '..' path component"
);
ensure!(
!path.starts_with('/') && !path.starts_with('\\'),
"file_path must not be absolute"
);
Ok(())
}
#[async_trait::async_trait]
impl Executor for AddPrCommentResult {
fn dry_run_summary(&self) -> String {
format!("add comment to PR #{}", self.pull_request_id)
}
async fn execute_impl(&self, ctx: &ExecutionContext) -> anyhow::Result<ExecutionResult> {
info!(
"Adding comment to PR #{}: {} chars",
self.pull_request_id,
self.content.len()
);
debug!(
"add-pr-comment: pr_id={}, content length={}",
self.pull_request_id,
self.content.len()
);
let org_url = ctx
.ado_org_url
.as_ref()
.context("AZURE_DEVOPS_ORG_URL not set")?;
let project = ctx
.ado_project
.as_ref()
.context("SYSTEM_TEAMPROJECT not set")?;
let token = ctx
.access_token
.as_ref()
.context("No access token available (SYSTEM_ACCESSTOKEN or AZURE_DEVOPS_EXT_PAT)")?;
debug!("ADO org: {}, project: {}", org_url, project);
let config: AddPrCommentConfig = ctx.get_tool_config("add-pr-comment");
debug!("Config: {:?}", config);
// Validate repository against allowed-repositories config
if !config.allowed_repositories.is_empty()
&& !config.allowed_repositories.contains(&self.repository)
{
return Ok(ExecutionResult::failure(format!(
"Repository '{}' is not in the allowed-repositories list",
self.repository
)));
}
// Validate status against allowed-statuses config (case-insensitive)
if !config.allowed_statuses.is_empty()
&& !config
.allowed_statuses
.iter()
.any(|s| s.eq_ignore_ascii_case(&self.status))
{
return Ok(ExecutionResult::failure(format!(
"Status '{}' is not in the allowed-statuses list",
self.status
)));
}
// Validate status is a known value
let status_int = match status_to_int(&self.status) {
Some(v) => v,
None => {
return Ok(ExecutionResult::failure(format!(
"Invalid status '{}'. Valid statuses: {}",
self.status,
VALID_STATUSES.join(", ")
)));
}
};
// Validate file_path if present
if let Some(ref fp) = self.file_path {
if let Err(e) = validate_file_path(fp) {
return Ok(ExecutionResult::failure(format!(
"Invalid file_path: {}",
e
)));
}
}
// Determine the repository name for the API call
let repo_name = if self.repository == "self" || self.repository.is_empty() {
ctx.repository_name
.as_ref()
.context("BUILD_REPOSITORY_NAME not set and repository is 'self'")?
.clone()
} else {
match ctx.allowed_repositories.get(&self.repository) {
Some(name) => name.clone(),
None => {
return Ok(ExecutionResult::failure(format!(
"Repository alias '{}' not found in allowed repositories",
self.repository
)));
}
}
};
// Build comment content with optional prefix
let comment_body = match &config.comment_prefix {
Some(prefix) => format!("{}{}", prefix, self.content),
None => self.content.clone(),
};
let comment_body = crate::agent_stats::append_stats_to_body(
&comment_body,
ctx,
config.include_stats,
);
// Build the API URL
let url = format!(
"{}/{}/_apis/git/repositories/{}/pullRequests/{}/threads?api-version=7.1",
org_url.trim_end_matches('/'),
utf8_percent_encode(project, PATH_SEGMENT),
utf8_percent_encode(&repo_name, PATH_SEGMENT),
self.pull_request_id,
);
debug!("API URL: {}", url);
// Build the request body
let comment_obj = serde_json::json!({
"parentCommentId": 0,
"content": comment_body,
"commentType": 1
});
let mut thread_body = serde_json::json!({
"comments": [comment_obj],
"status": status_int
});
// Add thread context for inline comments
if let Some(ref fp) = self.file_path {
let end_line = self.line.unwrap_or(1);
let start_line = self.start_line.unwrap_or(end_line);
thread_body["threadContext"] = serde_json::json!({
"filePath": format!("/{}", fp),
"rightFileStart": { "line": start_line, "offset": 1 },
"rightFileEnd": { "line": end_line, "offset": 1 }
});
}
let client = reqwest::Client::new();
info!("Sending comment thread to PR #{}", self.pull_request_id);
let response = client
.post(&url)
.header("Content-Type", "application/json")
.basic_auth("", Some(token))
.json(&thread_body)
.send()
.await
.context("Failed to send request to Azure DevOps")?;
if response.status().is_success() {
let body: serde_json::Value = response
.json()
.await
.context("Failed to parse response JSON")?;
let thread_id = body.get("id").and_then(|v| v.as_i64()).unwrap_or(0);
info!(
"Comment thread added to PR #{}: thread #{}",
self.pull_request_id, thread_id
);
Ok(ExecutionResult::success_with_data(
format!(
"Added comment thread #{} to PR #{}",
thread_id, self.pull_request_id
),
serde_json::json!({
"thread_id": thread_id,
"pull_request_id": self.pull_request_id,
"repository": repo_name,
"project": project,
"status": self.status,
}),
))
} else {
let status = response.status();
let error_body = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
Ok(ExecutionResult::failure(format!(
"Failed to add comment to PR #{} (HTTP {}): {}",
self.pull_request_id, status, error_body
)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::safeoutputs::ToolResult;
#[test]
fn test_result_has_correct_name() {
assert_eq!(AddPrCommentResult::NAME, "add-pr-comment");
}
#[test]
fn test_params_deserializes() {
let json = r#"{"pull_request_id": 42, "content": "This is a review comment on the PR."}"#;
let params: AddPrCommentParams = serde_json::from_str(json).unwrap();
assert_eq!(params.pull_request_id, 42);
assert!(params.content.contains("review comment"));
assert_eq!(params.repository, "self");
assert!(params.file_path.is_none());
assert!(params.line.is_none());
assert_eq!(params.status, "active");
}
#[test]
fn test_params_converts_to_result() {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "This is a test comment with enough characters.".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: None,
status: "active".to_string(),
};
let result: AddPrCommentResult = params.try_into().unwrap();
assert_eq!(result.name, "add-pr-comment");
assert_eq!(result.pull_request_id, 42);
assert!(result.content.contains("test comment"));
}
#[test]
fn test_validation_rejects_zero_pr_id() {
let params = AddPrCommentParams {
pull_request_id: 0,
content: "This is a valid comment body text.".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: None,
status: "active".to_string(),
};
let result: Result<AddPrCommentResult, _> = params.try_into();
assert!(result.is_err());
}
#[test]
fn test_validation_rejects_short_content() {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "Too short".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: None,
status: "active".to_string(),
};
let result: Result<AddPrCommentResult, _> = params.try_into();
assert!(result.is_err());
}
#[test]
fn test_validation_rejects_line_without_file_path() {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "This is a valid comment body text.".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: Some(10),
status: "active".to_string(),
};
let result: Result<AddPrCommentResult, _> = params.try_into();
assert!(result.is_err());
}
#[test]
fn test_result_serializes_correctly() {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "A comment body that is definitely longer than ten characters.".to_string(),
repository: "self".to_string(),
file_path: Some("src/main.rs".to_string()),
start_line: None,
line: Some(10),
status: "active".to_string(),
};
let result: AddPrCommentResult = params.try_into().unwrap();
let json = serde_json::to_string(&result).unwrap();
assert!(json.contains(r#""name":"add-pr-comment""#));
assert!(json.contains(r#""pull_request_id":42"#));
}
#[test]
fn test_config_defaults() {
let config = AddPrCommentConfig::default();
assert!(config.comment_prefix.is_none());
assert!(config.allowed_repositories.is_empty());
assert!(config.allowed_statuses.is_empty());
}
#[test]
fn test_config_deserializes_from_yaml() {
let yaml = r#"
comment-prefix: "[Agent Review] "
allowed-repositories:
- self
- other-repo
allowed-statuses:
- Active
- Closed
"#;
let config: AddPrCommentConfig = serde_yaml::from_str(yaml).unwrap();
assert_eq!(config.comment_prefix, Some("[Agent Review] ".to_string()));
assert_eq!(config.allowed_repositories, vec!["self", "other-repo"]);
assert_eq!(config.allowed_statuses, vec!["Active", "Closed"]);
}
#[test]
fn test_status_to_int_mapping() {
// Kebab-case (canonical)
assert_eq!(status_to_int("active"), Some(1));
assert_eq!(status_to_int("fixed"), Some(2));
assert_eq!(status_to_int("wont-fix"), Some(3));
assert_eq!(status_to_int("closed"), Some(4));
assert_eq!(status_to_int("by-design"), Some(5));
// CamelCase (backwards compat)
assert_eq!(status_to_int("Active"), Some(1));
assert_eq!(status_to_int("WontFix"), Some(3));
assert_eq!(status_to_int("ByDesign"), Some(5));
// Invalid
assert_eq!(status_to_int("Invalid"), None);
}
#[test]
fn test_validate_file_path_rejects_traversal() {
assert!(validate_file_path("../etc/passwd").is_err());
assert!(validate_file_path("src/../secret").is_err());
}
#[test]
fn test_validate_file_path_rejects_absolute() {
assert!(validate_file_path("/etc/passwd").is_err());
assert!(validate_file_path("\\windows\\system32").is_err());
}
#[test]
fn test_validate_file_path_accepts_valid() {
assert!(validate_file_path("src/main.rs").is_ok());
assert!(validate_file_path("path/to/file.txt").is_ok());
// ".." within a component name is not a traversal — must be accepted
assert!(validate_file_path("releases..notes/v1.md").is_ok());
assert!(validate_file_path("v2..beta/file.txt").is_ok());
}
#[test]
fn test_validation_rejects_invalid_status() {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "This is a valid comment body text.".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: None,
status: "unknown".to_string(),
};
let result: Result<AddPrCommentResult, _> = params.try_into();
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("status must be one of"));
}
#[test]
fn test_validation_accepts_valid_statuses() {
for s in &["active", "fixed", "wont-fix", "closed", "by-design", "Active", "WontFix"] {
let params = AddPrCommentParams {
pull_request_id: 42,
content: "This is a valid comment body text.".to_string(),
repository: "self".to_string(),
file_path: None,
start_line: None,
line: None,
status: s.to_string(),
};
let result: Result<AddPrCommentResult, _> = params.try_into();
assert!(result.is_ok(), "Expected status '{}' to be valid", s);
}
}
#[test]
fn test_allowed_statuses_case_insensitive_match() {
// Config has "Active" but agent sends "active" (canonical lowercase) — should be allowed
let config = AddPrCommentConfig {
comment_prefix: None,
allowed_repositories: Vec::new(),
allowed_statuses: vec!["Active".to_string(), "Closed".to_string()],
include_stats: true,
};
// Test the exact comparison logic extracted from execute_impl
let status = "active";
let matched = config
.allowed_statuses
.iter()
.any(|s| s.eq_ignore_ascii_case(status));
assert!(
matched,
"lowercase 'active' should match config value 'Active'"
);
}
#[test]
fn test_allowed_statuses_case_insensitive_reverse() {
// Config has "active" but agent sends "Active" — should be allowed
let config = AddPrCommentConfig {
comment_prefix: None,
allowed_repositories: Vec::new(),
allowed_statuses: vec!["active".to_string()],
include_stats: true,
};
let status = "Active";
let matched = config
.allowed_statuses
.iter()
.any(|s| s.eq_ignore_ascii_case(status));
assert!(
matched,
"uppercase 'Active' should match config value 'active'"
);
}
}