-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_pr.rs
More file actions
1216 lines (1097 loc) · 42.4 KB
/
create_pr.rs
File metadata and controls
1216 lines (1097 loc) · 42.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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Create pull request safe output tool
use log::{debug, info, warn};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tokio::process::Command;
use crate::tools::{ExecutionContext, ExecutionResult, Executor, ToolResult, Validate};
use crate::sanitize::{Sanitize, sanitize as sanitize_text};
use anyhow::{Context, ensure};
/// Maximum allowed patch file size (5 MB)
const MAX_PATCH_SIZE_BYTES: u64 = 5 * 1024 * 1024;
/// Resolve a reviewer identifier (email, display name, or ID) to an Azure DevOps identity ID.
///
/// If the input is already a GUID, returns it directly. Otherwise, uses the Azure DevOps
/// Identity Picker API to resolve the email or display name to an identity ID.
async fn resolve_reviewer_identity(
client: &reqwest::Client,
organization: &str,
token: &str,
reviewer: &str,
) -> Option<String> {
// Check if already a GUID (36 chars with 4 hyphens)
if reviewer.len() == 36 && reviewer.chars().filter(|c| *c == '-').count() == 4 {
if reviewer.chars().all(|c| c.is_ascii_hexdigit() || c == '-') {
debug!("Reviewer '{}' is already a GUID", reviewer);
return Some(reviewer.to_string());
}
}
// Use Identity Picker API on vssps.dev.azure.com to resolve email or display name
let identity_url = format!(
"https://vssps.dev.azure.com/{}/_apis/identitypicker/identities?api-version=7.1-preview.1",
organization
);
debug!("Identity lookup URL: {}", identity_url);
let query_body = serde_json::json!({
"query": reviewer,
"identityTypes": ["user"],
"operationScopes": ["ims", "source"],
"properties": ["DisplayName", "Mail", "SubjectDescriptor"],
"filterByAncestorEntityIds": [],
"filterByEntityIds": [],
"options": {
"MinResults": 1,
"MaxResults": 5
}
});
match client
.post(&identity_url)
.basic_auth("", Some(token))
.json(&query_body)
.send()
.await
{
Ok(resp) if resp.status().is_success() => {
match resp.json::<serde_json::Value>().await {
Ok(data) => {
// Navigate the response: results[0].identities[0].localId
if let Some(results) = data.get("results").and_then(|r| r.as_array()) {
if let Some(first_result) = results.first() {
if let Some(identities) =
first_result.get("identities").and_then(|i| i.as_array())
{
// Try to find exact match first (by email or display name)
let reviewer_lower = reviewer.to_lowercase();
for identity in identities {
let display_name = identity
.get("displayName")
.and_then(|d| d.as_str())
.unwrap_or_default()
.to_lowercase();
let mail = identity
.get("mail")
.and_then(|m| m.as_str())
.unwrap_or_default()
.to_lowercase();
if display_name == reviewer_lower || mail == reviewer_lower {
if let Some(local_id) =
identity.get("localId").and_then(|id| id.as_str())
{
debug!(
"Resolved reviewer '{}' to ID '{}'",
reviewer, local_id
);
return Some(local_id.to_string());
}
}
}
// Fall back to first result if no exact match
if let Some(first_identity) = identities.first() {
if let Some(local_id) =
first_identity.get("localId").and_then(|id| id.as_str())
{
debug!(
"Resolved reviewer '{}' to first match ID '{}'",
reviewer, local_id
);
return Some(local_id.to_string());
}
}
}
}
}
warn!("No identity found for reviewer '{}'", reviewer);
None
}
Err(e) => {
warn!(
"Failed to parse identity response for '{}': {}",
reviewer, e
);
None
}
}
}
Ok(resp) => {
warn!(
"Identity lookup failed for '{}': {}",
reviewer,
resp.status()
);
None
}
Err(e) => {
warn!("Identity lookup request failed for '{}': {}", reviewer, e);
None
}
}
}
/// Parameters for creating a pull request
#[derive(Deserialize, JsonSchema)]
pub struct CreatePrParams {
/// Title for the pull request; should be concise and descriptive
pub title: String,
/// Description of the changes in the pull request. Use markdown formatting.
/// Explain what changes were made and why.
pub description: String,
/// Repository to create the PR in. Use "self" for the pipeline's own repository,
/// or a repository alias from the checkout list for other repositories.
/// Required when multiple repositories are checked out.
#[serde(default)]
pub repository: Option<String>,
}
impl Validate for CreatePrParams {
fn validate(&self) -> anyhow::Result<()> {
ensure!(
self.title.len() >= 5,
"PR title must be at least 5 characters"
);
ensure!(
self.title.len() <= 200,
"PR title must be at most 200 characters"
);
ensure!(
self.description.len() >= 10,
"PR description must be at least 10 characters"
);
Ok(())
}
}
/// Result of creating a pull request - stored as safe output
#[derive(Debug, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct CreatePrResult {
/// Tool identifier
pub name: String,
/// Title for the pull request
pub title: String,
/// Description/body of the pull request (markdown)
pub description: String,
/// Source branch name (generated or provided)
pub source_branch: String,
/// Path to the patch file in the safe outputs directory
pub patch_file: String,
/// Repository alias ("self" or alias from checkout list)
pub repository: String,
}
impl crate::tools::ToolResult for CreatePrResult {
const NAME: &'static str = "create-pull-request";
}
impl Sanitize for CreatePrResult {
fn sanitize_fields(&mut self) {
self.title = sanitize_text(&self.title);
self.description = sanitize_text(&self.description);
}
}
impl CreatePrResult {
/// Create a new CreatePrResult with all fields
pub fn new(
title: String,
description: String,
source_branch: String,
patch_file: String,
repository: String,
) -> Self {
Self {
name: Self::NAME.to_string(),
title,
description,
source_branch,
patch_file,
repository,
}
}
}
/// Configuration for the create_pr tool (specified in front matter)
///
/// Example front matter:
/// ```yaml
/// safe-outputs:
/// create_pr:
/// auto_complete: true
/// delete_source_branch: true
/// squash_merge: true
/// reviewers:
/// - "user@example.com"
/// labels:
/// - "automated"
/// - "agent-created"
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatePrConfig {
/// Target branch to merge into (default: "main")
#[serde(default = "default_target_branch", rename = "target-branch")]
pub target_branch: String,
/// Whether to set auto-complete on the PR (default: false)
#[serde(default, rename = "auto-complete")]
pub auto_complete: bool,
/// Whether to delete source branch after merge (default: true)
#[serde(default = "default_true", rename = "delete-source-branch")]
pub delete_source_branch: bool,
/// Whether to squash commits on merge (default: true)
#[serde(default = "default_true", rename = "squash-merge")]
pub squash_merge: bool,
/// Reviewers to add to the PR (email addresses or user IDs)
#[serde(default)]
pub reviewers: Vec<String>,
/// Labels to add to the PR
#[serde(default)]
pub labels: Vec<String>,
/// Work item IDs to link to the PR
#[serde(default, rename = "work-items")]
pub work_items: Vec<i32>,
}
fn default_target_branch() -> String {
"main".to_string()
}
fn default_true() -> bool {
true
}
impl Default for CreatePrConfig {
fn default() -> Self {
Self {
target_branch: default_target_branch(),
auto_complete: false,
delete_source_branch: true,
squash_merge: true,
reviewers: Vec::new(),
labels: Vec::new(),
work_items: Vec::new(),
}
}
}
/// Guard to ensure git worktree is cleaned up on drop
struct WorktreeGuard {
repo_dir: std::path::PathBuf,
worktree_path: std::path::PathBuf,
}
impl Drop for WorktreeGuard {
fn drop(&mut self) {
// Best effort cleanup - ignore errors
let _ = std::process::Command::new("git")
.args([
"worktree",
"remove",
"--force",
&self.worktree_path.to_string_lossy(),
])
.current_dir(&self.repo_dir)
.output();
}
}
#[async_trait::async_trait]
impl Executor for CreatePrResult {
async fn execute_impl(&self, ctx: &ExecutionContext) -> anyhow::Result<ExecutionResult> {
info!(
"Creating PR: '{}' in repository '{}'",
self.title, self.repository
);
debug!("PR description length: {} chars", self.description.len());
debug!("Source branch: {}", self.source_branch);
debug!("Patch file: {}", self.patch_file);
let config: CreatePrConfig = ctx.get_tool_config("create-pull-request");
debug!("Target branch from config: {}", config.target_branch);
debug!("Auto-complete: {}", config.auto_complete);
debug!("Squash merge: {}", config.squash_merge);
// Validate repository against allowed list
debug!(
"Validating repository '{}' against allowed list",
self.repository
);
let repo_id = if self.repository == "self" {
// "self" uses the pipeline's own repository
debug!("Using 'self' repository");
ctx.repository_id
.as_ref()
.or(ctx.repository_name.as_ref())
.context("Repository ID not configured for 'self'")?
.clone()
} else if let Some(ado_repo_name) = ctx.allowed_repositories.get(&self.repository) {
// Alias found in allowed list - use the mapped ADO repo name
debug!(
"Repository alias '{}' maps to '{}'",
self.repository, ado_repo_name
);
ado_repo_name.clone()
} else if ctx.allowed_repositories.is_empty() {
// No allowed_repositories configured - fall back to default repo (backward compat)
debug!("No allowed_repositories configured, using default repo");
ctx.repository_id
.as_ref()
.or(ctx.repository_name.as_ref())
.context("Repository ID not configured")?
.clone()
} else {
// Repository not in allowed list
warn!(
"Repository '{}' not in allowed list: {:?}",
self.repository,
ctx.allowed_repositories.keys().collect::<Vec<_>>()
);
return Ok(ExecutionResult::failure(format!(
"Repository '{}' is not in the allowed list. Allowed: self, {}",
self.repository,
ctx.allowed_repositories
.keys()
.cloned()
.collect::<Vec<_>>()
.join(", ")
)));
};
debug!("Resolved repository ID: {}", repo_id);
// Get ADO configuration
let org_url = ctx
.ado_org_url
.as_ref()
.context("Azure DevOps organization URL not configured")?;
let organization = ctx
.ado_organization
.as_ref()
.context("Azure DevOps organization name not configured")?;
let project = ctx
.ado_project
.as_ref()
.context("Azure DevOps project not configured")?;
let token = ctx
.access_token
.as_ref()
.context("Access token not configured")?;
debug!(
"ADO org: {}, organization: {}, project: {}",
org_url, organization, project
);
// Validate and read the patch file
let patch_path = ctx.working_directory.join(&self.patch_file);
if !patch_path.exists() {
return Ok(ExecutionResult::failure(format!(
"Patch file not found: {}",
self.patch_file
)));
}
// Security: Enforce patch file size limit
let metadata = tokio::fs::metadata(&patch_path)
.await
.context("Failed to get patch file metadata")?;
if metadata.len() > MAX_PATCH_SIZE_BYTES {
return Ok(ExecutionResult::failure(format!(
"Patch file exceeds maximum size of {} bytes (got {} bytes)",
MAX_PATCH_SIZE_BYTES,
metadata.len()
)));
}
// Read patch content for validation
debug!("Reading patch file content");
let patch_content = tokio::fs::read_to_string(&patch_path)
.await
.context("Failed to read patch file")?;
debug!("Patch content size: {} bytes", patch_content.len());
// Security: Validate patch paths before applying
debug!("Validating patch paths for security");
if let Err(e) = validate_patch_paths(&patch_content) {
warn!("Patch path validation failed: {}", e);
return Ok(ExecutionResult::failure(format!(
"Patch validation failed: {}",
e
)));
}
debug!("Patch path validation passed");
// Use target branch from config
let target_branch = &config.target_branch;
let source_ref = format!("refs/heads/{}", self.source_branch);
let target_ref = format!("refs/heads/{}", target_branch);
debug!("Source ref: {}, Target ref: {}", source_ref, target_ref);
// Determine the git repository directory from the source checkout
// For "self", use the source directory; for other repos, use the subdirectory
let repo_git_dir = if self.repository == "self" {
ctx.source_directory.clone()
} else {
ctx.source_directory.join(&self.repository)
};
debug!("Git repository directory: {}", repo_git_dir.display());
// Verify this is a git repository
debug!("Verifying git repository");
let git_check = Command::new("git")
.args(["rev-parse", "--git-dir"])
.current_dir(&repo_git_dir)
.output()
.await
.context("Failed to verify git repository")?;
if !git_check.status.success() {
warn!("Not a git repository: {}", repo_git_dir.display());
return Ok(ExecutionResult::failure(format!(
"Not a git repository: {}",
repo_git_dir.display()
)));
}
debug!("Git repository verified");
// Create a temporary directory for the worktree
let temp_dir = tempfile::tempdir().context("Failed to create temp directory")?;
let worktree_path = temp_dir.path().join("worktree");
debug!("Creating worktree at: {}", worktree_path.display());
// Create a worktree at the target branch
let worktree_output = Command::new("git")
.args([
"worktree",
"add",
&worktree_path.to_string_lossy(),
&format!("origin/{}", target_branch),
])
.current_dir(&repo_git_dir)
.output()
.await
.context("Failed to create git worktree")?;
if !worktree_output.status.success() {
debug!(
"Worktree creation with origin/ prefix failed, trying without: {}",
String::from_utf8_lossy(&worktree_output.stderr)
);
// Try with just the branch name if origin/ prefix fails
let worktree_output = Command::new("git")
.args([
"worktree",
"add",
&worktree_path.to_string_lossy(),
target_branch,
])
.current_dir(&repo_git_dir)
.output()
.await
.context("Failed to create git worktree")?;
if !worktree_output.status.success() {
warn!(
"Failed to create worktree: {}",
String::from_utf8_lossy(&worktree_output.stderr)
);
return Ok(ExecutionResult::failure(format!(
"Failed to create worktree: {}",
String::from_utf8_lossy(&worktree_output.stderr)
)));
}
}
debug!("Worktree created successfully");
// Ensure worktree cleanup on exit
let _worktree_guard = WorktreeGuard {
repo_dir: repo_git_dir.clone(),
worktree_path: worktree_path.clone(),
};
// Create and checkout the source branch in the worktree
debug!("Creating source branch: {}", self.source_branch);
let checkout_output = Command::new("git")
.args(["checkout", "-b", &self.source_branch])
.current_dir(&worktree_path)
.output()
.await
.context("Failed to create source branch")?;
if !checkout_output.status.success() {
warn!(
"Failed to create source branch: {}",
String::from_utf8_lossy(&checkout_output.stderr)
);
return Ok(ExecutionResult::failure(format!(
"Failed to create source branch: {}",
String::from_utf8_lossy(&checkout_output.stderr)
)));
}
debug!("Source branch created");
// Security: Validate patch with git apply --check first (dry run)
debug!("Running git apply --check (dry run)");
let check_output = Command::new("git")
.args(["apply", "--check", &patch_path.to_string_lossy()])
.current_dir(&worktree_path)
.output()
.await
.context("Failed to run git apply --check")?;
if !check_output.status.success() {
warn!(
"Patch dry-run failed: {}",
String::from_utf8_lossy(&check_output.stderr)
);
return Ok(ExecutionResult::failure(format!(
"Patch validation failed (git apply --check): {}",
String::from_utf8_lossy(&check_output.stderr)
)));
}
debug!("Patch dry-run passed");
// Apply the patch
debug!("Applying patch");
let apply_output = Command::new("git")
.args(["apply", &patch_path.to_string_lossy()])
.current_dir(&worktree_path)
.output()
.await
.context("Failed to run git apply")?;
if !apply_output.status.success() {
warn!(
"Failed to apply patch: {}",
String::from_utf8_lossy(&apply_output.stderr)
);
return Ok(ExecutionResult::failure(format!(
"Failed to apply patch: {}",
String::from_utf8_lossy(&apply_output.stderr)
)));
}
debug!("Patch applied successfully");
// Get list of changed files using git status
debug!("Getting list of changed files");
let status_output = Command::new("git")
.args(["status", "--porcelain"])
.current_dir(&worktree_path)
.output()
.await
.context("Failed to run git status")?;
if !status_output.status.success() {
warn!(
"Failed to get git status: {}",
String::from_utf8_lossy(&status_output.stderr)
);
return Ok(ExecutionResult::failure(format!(
"Failed to get git status: {}",
String::from_utf8_lossy(&status_output.stderr)
)));
}
// Parse changed files and build ADO push payload
let status_str = String::from_utf8_lossy(&status_output.stdout);
debug!("Git status output:\n{}", status_str);
let changes = collect_changes_from_worktree(&worktree_path, &status_str).await?;
debug!("Collected {} file changes for push", changes.len());
if changes.is_empty() {
warn!("No changes detected after applying patch");
return Ok(ExecutionResult::failure(
"No changes detected after applying patch".to_string(),
));
}
// Use ADO REST API to create branch and push changes
let client = reqwest::Client::new();
// Get the target branch ref to find the base commit
debug!("Getting target branch ref from ADO");
let refs_url = format!(
"{}{}/_apis/git/repositories/{}/refs?filter=heads/{}&api-version=7.1",
org_url, project, repo_id, target_branch
);
debug!("Refs URL: {}", refs_url);
let refs_response = client
.get(&refs_url)
.basic_auth("", Some(token))
.send()
.await
.context("Failed to get target branch ref")?;
if !refs_response.status().is_success() {
let status = refs_response.status();
let body = refs_response.text().await.unwrap_or_default();
warn!("Failed to get target branch ref: {} - {}", status, body);
return Ok(ExecutionResult::failure(format!(
"Failed to get target branch ref: {} - {}",
status, body
)));
}
let refs_data: serde_json::Value = refs_response.json().await?;
let base_commit = refs_data["value"][0]["objectId"]
.as_str()
.context("Could not find target branch commit")?;
debug!("Base commit: {}", base_commit);
info!(
"Base commit for target branch '{}': {}",
target_branch, base_commit
);
// Push changes via ADO API (this creates the branch and commits in one call)
info!("Pushing changes to ADO");
let push_url = format!(
"{}{}/_apis/git/repositories/{}/pushes?api-version=7.1",
org_url, project, repo_id
);
debug!("Push URL: {}", push_url);
// For creating a new branch with a commit:
// - refUpdates.oldObjectId = zeros (new ref)
// - commits[0].parents = [base_commit] (parent of new commit)
// ADO will compute the newObjectId from the commit
let push_body = serde_json::json!({
"refUpdates": [{
"name": source_ref,
"oldObjectId": "0000000000000000000000000000000000000000"
}],
"commits": [{
"comment": self.title,
"changes": changes,
"parents": [base_commit]
}]
});
debug!(
"Push request body: {}",
serde_json::to_string_pretty(&push_body).unwrap_or_default()
);
let push_response = client
.post(&push_url)
.basic_auth("", Some(token))
.json(&push_body)
.send()
.await
.context("Failed to push changes")?;
if !push_response.status().is_success() {
let status = push_response.status();
let body = push_response.text().await.unwrap_or_default();
warn!("Failed to push changes: {} - {}", status, body);
return Ok(ExecutionResult::failure(format!(
"Failed to push changes: {} - {}",
status, body
)));
}
debug!("Changes pushed successfully");
// Create the pull request via REST API
info!("Creating pull request");
let pr_url = format!(
"{}{}/_apis/git/repositories/{}/pullrequests?api-version=7.1",
org_url, project, repo_id
);
debug!("PR URL: {}", pr_url);
let mut pr_body = serde_json::json!({
"sourceRefName": source_ref,
"targetRefName": target_ref,
"title": self.title,
"description": self.description,
});
// Add work item links if configured
if !config.work_items.is_empty() {
debug!("Linking {} work items", config.work_items.len());
pr_body["workItemRefs"] = serde_json::json!(
config
.work_items
.iter()
.map(|id| serde_json::json!({"id": id}))
.collect::<Vec<_>>()
);
}
// Add labels if configured
if !config.labels.is_empty() {
debug!("Adding {} labels", config.labels.len());
pr_body["labels"] = serde_json::json!(
config
.labels
.iter()
.map(|l| serde_json::json!({"name": l}))
.collect::<Vec<_>>()
);
}
let pr_response = client
.post(&pr_url)
.basic_auth("", Some(token))
.json(&pr_body)
.send()
.await
.context("Failed to create pull request")?;
if !pr_response.status().is_success() {
let status = pr_response.status();
let body = pr_response.text().await.unwrap_or_default();
warn!("Failed to create pull request: {} - {}", status, body);
return Ok(ExecutionResult::failure(format!(
"Failed to create pull request: {} - {}",
status, body
)));
}
let pr_data: serde_json::Value = pr_response.json().await?;
let pr_id = pr_data["pullRequestId"].as_i64().unwrap_or(0);
let pr_web_url = pr_data["url"].as_str().unwrap_or("");
info!("Pull request created: #{} - {}", pr_id, pr_web_url);
// Set completion options (delete source branch, squash merge) and optionally auto-complete
// completionOptions apply when the PR is completed by anyone, auto_complete makes it complete automatically
{
debug!(
"Setting PR completion options: delete_source_branch={}, squash_merge={}, auto_complete={}",
config.delete_source_branch, config.squash_merge, config.auto_complete
);
let pr_update_url = format!(
"{}{}/_apis/git/repositories/{}/pullrequests/{}?api-version=7.1",
org_url, project, repo_id, pr_id
);
let mut update_body = serde_json::json!({
"completionOptions": {
"deleteSourceBranch": config.delete_source_branch,
"squashMerge": config.squash_merge
}
});
// Only set autoCompleteSetBy if auto_complete is enabled
if config.auto_complete {
update_body["autoCompleteSetBy"] = serde_json::json!({
"id": pr_data["createdBy"]["id"]
});
}
match client
.patch(&pr_update_url)
.basic_auth("", Some(token))
.json(&update_body)
.send()
.await
{
Ok(resp) if resp.status().is_success() => {
debug!("PR completion options set successfully");
}
Ok(resp) => {
warn!("Failed to set PR completion options: {}", resp.status());
}
Err(e) => {
warn!("Failed to set PR completion options: {}", e);
}
}
}
// Add reviewers if configured
if !config.reviewers.is_empty() {
debug!("Adding {} reviewers", config.reviewers.len());
for reviewer in &config.reviewers {
debug!("Adding reviewer: {}", reviewer);
// Resolve reviewer identity (email/name -> ID)
let reviewer_id =
match resolve_reviewer_identity(&client, organization, token, reviewer).await {
Some(id) => id,
None => {
warn!(
"Could not resolve reviewer '{}' to an identity ID, skipping",
reviewer
);
continue;
}
};
let reviewer_url = format!(
"{}{}/_apis/git/repositories/{}/pullrequests/{}/reviewers/{}?api-version=7.1",
org_url, project, repo_id, pr_id, reviewer_id
);
let reviewer_body = serde_json::json!({
"vote": 0,
"isRequired": false
});
match client
.put(&reviewer_url)
.basic_auth("", Some(token))
.json(&reviewer_body)
.send()
.await
{
Ok(resp) if resp.status().is_success() => {
debug!(
"Reviewer '{}' (ID: {}) added successfully",
reviewer, reviewer_id
);
}
Ok(resp) => {
warn!(
"Failed to add reviewer '{}' (ID: {}): {}",
reviewer,
reviewer_id,
resp.status()
);
}
Err(e) => {
warn!("Failed to add reviewer '{}': {}", reviewer, e);
}
}
}
}
info!(
"PR #{} created successfully: {} -> {}",
pr_id, self.source_branch, target_branch
);
Ok(ExecutionResult::success_with_data(
format!("Created pull request #{}: {}", pr_id, self.title),
serde_json::json!({
"pull_request_id": pr_id,
"url": pr_web_url,
"source_branch": self.source_branch,
"target_branch": target_branch
}),
))
}
}
/// Collect file changes from a worktree based on git status output
///
/// Parses git status --porcelain output and reads file contents to build
/// ADO Push API change objects with full file content.
async fn collect_changes_from_worktree(
worktree_path: &std::path::Path,
status_output: &str,
) -> anyhow::Result<Vec<serde_json::Value>> {
let mut changes = Vec::new();
for line in status_output.lines() {
if line.len() < 3 {
continue;
}
let status_code = &line[0..2];
let file_path = line[3..].trim();
// Skip empty paths
if file_path.is_empty() {
continue;
}
// Validate path for security
validate_single_path(file_path)?;
let full_path = worktree_path.join(file_path);
match status_code {
// Deleted files
" D" | "D " | "DD" => {
changes.push(serde_json::json!({
"changeType": "delete",
"item": {
"path": format!("/{}", file_path)
}
}));
}
// New/untracked files
"??" | "A " | " A" | "AM" => {
if full_path.is_file() {
let content = tokio::fs::read_to_string(&full_path)
.await
.with_context(|| format!("Failed to read new file: {}", file_path))?;
changes.push(serde_json::json!({
"changeType": "add",
"item": {
"path": format!("/{}", file_path)
},
"newContent": {
"content": content,
"contentType": "rawtext"
}
}));
}
}
// Modified files
" M" | "M " | "MM" => {
if full_path.is_file() {
let content = tokio::fs::read_to_string(&full_path)
.await
.with_context(|| format!("Failed to read modified file: {}", file_path))?;
changes.push(serde_json::json!({
"changeType": "edit",
"item": {
"path": format!("/{}", file_path)
},
"newContent": {
"content": content,
"contentType": "rawtext"
}
}));
}
}
// Renamed files - format is "R old_path -> new_path"
"R " | " R" | "RM" => {
if let Some((old_path, new_path)) = file_path.split_once(" -> ") {
validate_single_path(old_path.trim())?;
validate_single_path(new_path.trim())?;
changes.push(serde_json::json!({
"changeType": "rename",
"sourceServerItem": format!("/{}", old_path.trim()),
"item": {
"path": format!("/{}", new_path.trim())
}
}));
}
}
// Other statuses - try to handle as edit if file exists
_ => {
if full_path.is_file() {
let content = tokio::fs::read_to_string(&full_path)
.await
.with_context(|| format!("Failed to read file: {}", file_path))?;
changes.push(serde_json::json!({
"changeType": "edit",
"item": {
"path": format!("/{}", file_path)
},
"newContent": {
"content": content,
"contentType": "rawtext"
}
}));
}
}
}
}
Ok(changes)
}