gitgrip 1.0.0

Multi-repo workflow tool - manage multiple git repositories as one
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
//! Hosting platform trait definition

use async_trait::async_trait;
use thiserror::Error;

use super::types::*;
use crate::core::manifest::PlatformType;

/// Errors that can occur during platform operations
#[derive(Error, Debug)]
pub enum PlatformError {
    #[error("Authentication failed: {0}")]
    AuthError(String),

    #[error("API error: {0}")]
    ApiError(String),

    #[error("Not found: {0}")]
    NotFound(String),

    #[error("Rate limited")]
    RateLimited,

    #[error("Network error: {0}")]
    NetworkError(String),

    #[error("Parse error: {0}")]
    ParseError(String),

    #[error("Branch is behind base: {0}")]
    BranchBehind(String),

    #[error("Branch protection prevents merge: {0}")]
    BranchProtected(String),
}

/// Linked PR reference for cross-repo tracking
#[derive(Debug, Clone)]
pub struct LinkedPRRef {
    pub repo_name: String,
    pub number: u64,
}

/// Interface for hosting platform adapters
/// Each platform (GitHub, GitLab, Azure DevOps) implements this trait
#[async_trait]
#[allow(clippy::too_many_arguments)]
pub trait HostingPlatform: Send + Sync {
    /// Platform type identifier
    fn platform_type(&self) -> PlatformType;

    /// Get authentication token for API calls
    async fn get_token(&self) -> Result<String, PlatformError>;

    /// Create a pull request
    async fn create_pull_request(
        &self,
        owner: &str,
        repo: &str,
        head: &str,
        base: &str,
        title: &str,
        body: Option<&str>,
        draft: bool,
    ) -> Result<PRCreateResult, PlatformError>;

    /// Get pull request details
    async fn get_pull_request(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
    ) -> Result<PullRequest, PlatformError>;

    /// Update pull request body
    async fn update_pull_request_body(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
        body: &str,
    ) -> Result<(), PlatformError>;

    /// Update pull request title and/or body
    ///
    /// At least one of `title` or `body` must be `Some`.
    async fn update_pull_request(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
        title: Option<&str>,
        body: Option<&str>,
    ) -> Result<(), PlatformError> {
        // Default: delegate to update_pull_request_body if only body is provided.
        // Platforms that support title updates should override this.
        // Check title first to avoid partial updates (body mutated, then title errors).
        if title.is_some() {
            return Err(PlatformError::ApiError(
                "Updating PR title is not supported on this platform".to_string(),
            ));
        }
        if let Some(body_text) = body {
            self.update_pull_request_body(owner, repo, pull_number, body_text)
                .await?;
        }
        Ok(())
    }

    /// Merge a pull request
    async fn merge_pull_request(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
        method: Option<MergeMethod>,
        delete_branch: bool,
    ) -> Result<bool, PlatformError>;

    /// Find an open PR by branch name
    async fn find_pr_by_branch(
        &self,
        owner: &str,
        repo: &str,
        branch: &str,
    ) -> Result<Option<PRCreateResult>, PlatformError>;

    /// List pull requests for a repository
    async fn list_pull_requests(
        &self,
        _owner: &str,
        _repo: &str,
        _filter: &PRListFilter,
    ) -> Result<Vec<PullRequest>, PlatformError> {
        Err(PlatformError::ApiError(
            "Listing pull requests is not supported on this platform".to_string(),
        ))
    }

    /// Check if PR is approved
    async fn is_pull_request_approved(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
    ) -> Result<bool, PlatformError>;

    /// Get reviews for a PR
    async fn get_pull_request_reviews(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
    ) -> Result<Vec<PRReview>, PlatformError>;

    /// Create a review on a PR
    async fn create_pull_request_review(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
        event: ReviewEvent,
        body: Option<&str>,
    ) -> Result<(), PlatformError>;

    /// Get CI/CD status checks for a commit
    async fn get_status_checks(
        &self,
        owner: &str,
        repo: &str,
        ref_name: &str,
    ) -> Result<StatusCheckResult, PlatformError>;

    /// Get allowed merge methods for a repository
    async fn get_allowed_merge_methods(
        &self,
        owner: &str,
        repo: &str,
    ) -> Result<AllowedMergeMethods, PlatformError>;

    /// Get the diff for a pull request
    async fn get_pull_request_diff(
        &self,
        owner: &str,
        repo: &str,
        pull_number: u64,
    ) -> Result<String, PlatformError>;

    /// Parse a git URL to extract owner/repo information
    fn parse_repo_url(&self, url: &str) -> Option<ParsedRepoInfo>;

    /// Check if a URL belongs to this platform
    fn matches_url(&self, url: &str) -> bool;

    /// Create a new repository on the platform
    ///
    /// This is an optional operation - platforms may not support it or the user
    /// may not have permission. Returns the clone URL of the created repository.
    async fn create_repository(
        &self,
        owner: &str,
        name: &str,
        description: Option<&str>,
        private: bool,
    ) -> Result<String, PlatformError> {
        // Default implementation returns an error
        let _ = (owner, name, description, private);
        Err(PlatformError::ApiError(
            "Repository creation not supported on this platform".to_string(),
        ))
    }

    /// Delete a repository from the platform
    ///
    /// This is a destructive operation and should be used with caution.
    /// Mainly useful for testing cleanup.
    async fn delete_repository(&self, owner: &str, name: &str) -> Result<(), PlatformError> {
        // Default implementation returns an error
        let _ = (owner, name);
        Err(PlatformError::ApiError(
            "Repository deletion not supported on this platform".to_string(),
        ))
    }

    /// Update a pull request branch (merge base into head)
    ///
    /// Returns Ok(true) if the branch was updated, Ok(false) if already up to date.
    /// Returns Err if the update failed (e.g., conflicts).
    async fn update_branch(
        &self,
        _owner: &str,
        _repo: &str,
        _pull_number: u64,
    ) -> Result<bool, PlatformError> {
        Err(PlatformError::ApiError(
            "Branch update not supported on this platform".to_string(),
        ))
    }

    /// Enable auto-merge for a pull request
    ///
    /// The PR will be automatically merged when all required checks pass.
    /// Returns Ok(true) if auto-merge was enabled.
    async fn enable_auto_merge(
        &self,
        _owner: &str,
        _repo: &str,
        _pull_number: u64,
        _method: Option<MergeMethod>,
    ) -> Result<bool, PlatformError> {
        Err(PlatformError::ApiError(
            "Auto-merge not supported on this platform".to_string(),
        ))
    }

    /// Create a release on the platform with a tag
    ///
    /// Creates a git tag and a release (e.g., GitHub Release) on the repository.
    /// Returns the release URL and metadata.
    async fn create_release(
        &self,
        _owner: &str,
        _repo: &str,
        _tag: &str,
        _name: &str,
        _body: Option<&str>,
        _target_commitish: &str,
        _draft: bool,
        _prerelease: bool,
    ) -> Result<ReleaseResult, PlatformError> {
        Err(PlatformError::ApiError(
            "Release creation not supported on this platform".to_string(),
        ))
    }

    /// List issues for a repository
    async fn list_issues(
        &self,
        _owner: &str,
        _repo: &str,
        _filter: &IssueListFilter,
    ) -> Result<Vec<Issue>, PlatformError> {
        Err(PlatformError::ApiError(
            "Issue listing not supported on this platform".to_string(),
        ))
    }

    /// Create an issue on a repository
    async fn create_issue(
        &self,
        _owner: &str,
        _repo: &str,
        _options: &IssueCreateOptions,
    ) -> Result<IssueCreateResult, PlatformError> {
        Err(PlatformError::ApiError(
            "Issue creation not supported on this platform".to_string(),
        ))
    }

    /// Get a single issue by number
    async fn get_issue(
        &self,
        _owner: &str,
        _repo: &str,
        _issue_number: u64,
    ) -> Result<Issue, PlatformError> {
        Err(PlatformError::ApiError(
            "Issue retrieval not supported on this platform".to_string(),
        ))
    }

    /// Close an issue
    async fn close_issue(
        &self,
        _owner: &str,
        _repo: &str,
        _issue_number: u64,
    ) -> Result<(), PlatformError> {
        Err(PlatformError::ApiError(
            "Issue closing not supported on this platform".to_string(),
        ))
    }

    /// Reopen an issue
    async fn reopen_issue(
        &self,
        _owner: &str,
        _repo: &str,
        _issue_number: u64,
    ) -> Result<(), PlatformError> {
        Err(PlatformError::ApiError(
            "Issue reopening not supported on this platform".to_string(),
        ))
    }

    /// Generate HTML comment for linked PR tracking
    fn generate_linked_pr_comment(&self, links: &[LinkedPRRef]) -> String {
        if links.is_empty() {
            return String::new();
        }

        let mut comment = String::from("<!-- gitgrip-linked-prs\n");
        for link in links {
            comment.push_str(&format!("{}:{}\n", link.repo_name, link.number));
        }
        comment.push_str("-->");
        comment
    }

    /// Parse linked PR references from PR body
    fn parse_linked_pr_comment(&self, body: &str) -> Vec<LinkedPRRef> {
        let start_marker = "<!-- gitgrip-linked-prs";
        let end_marker = "-->";

        let Some(start) = body.find(start_marker) else {
            return Vec::new();
        };

        let content_start = start + start_marker.len();
        let Some(end) = body[content_start..].find(end_marker) else {
            return Vec::new();
        };

        let content = &body[content_start..content_start + end];

        content
            .lines()
            .filter_map(|line| {
                let line = line.trim();
                if line.is_empty() {
                    return None;
                }

                let parts: Vec<&str> = line.splitn(2, ':').collect();
                if parts.len() != 2 {
                    return None;
                }

                let number = parts[1].parse().ok()?;
                Some(LinkedPRRef {
                    repo_name: parts[0].to_string(),
                    number,
                })
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct MockPlatform;

    #[async_trait]
    impl HostingPlatform for MockPlatform {
        fn platform_type(&self) -> PlatformType {
            PlatformType::GitHub
        }

        async fn get_token(&self) -> Result<String, PlatformError> {
            Ok("mock-token".to_string())
        }

        async fn create_pull_request(
            &self,
            _owner: &str,
            _repo: &str,
            _head: &str,
            _base: &str,
            _title: &str,
            _body: Option<&str>,
            _draft: bool,
        ) -> Result<PRCreateResult, PlatformError> {
            unimplemented!()
        }

        async fn get_pull_request(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
        ) -> Result<PullRequest, PlatformError> {
            unimplemented!()
        }

        async fn update_pull_request_body(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
            _body: &str,
        ) -> Result<(), PlatformError> {
            unimplemented!()
        }

        async fn merge_pull_request(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
            _method: Option<MergeMethod>,
            _delete_branch: bool,
        ) -> Result<bool, PlatformError> {
            unimplemented!()
        }

        async fn find_pr_by_branch(
            &self,
            _owner: &str,
            _repo: &str,
            _branch: &str,
        ) -> Result<Option<PRCreateResult>, PlatformError> {
            unimplemented!()
        }

        async fn is_pull_request_approved(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
        ) -> Result<bool, PlatformError> {
            unimplemented!()
        }

        async fn get_pull_request_reviews(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
        ) -> Result<Vec<PRReview>, PlatformError> {
            unimplemented!()
        }

        async fn create_pull_request_review(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
            _event: ReviewEvent,
            _body: Option<&str>,
        ) -> Result<(), PlatformError> {
            unimplemented!()
        }

        async fn get_status_checks(
            &self,
            _owner: &str,
            _repo: &str,
            _ref_name: &str,
        ) -> Result<StatusCheckResult, PlatformError> {
            unimplemented!()
        }

        async fn get_allowed_merge_methods(
            &self,
            _owner: &str,
            _repo: &str,
        ) -> Result<AllowedMergeMethods, PlatformError> {
            unimplemented!()
        }

        async fn get_pull_request_diff(
            &self,
            _owner: &str,
            _repo: &str,
            _pull_number: u64,
        ) -> Result<String, PlatformError> {
            unimplemented!()
        }

        fn parse_repo_url(&self, _url: &str) -> Option<ParsedRepoInfo> {
            None
        }

        fn matches_url(&self, _url: &str) -> bool {
            false
        }
    }

    #[test]
    fn test_generate_linked_pr_comment() {
        let platform = MockPlatform;
        let links = vec![
            LinkedPRRef {
                repo_name: "app".to_string(),
                number: 123,
            },
            LinkedPRRef {
                repo_name: "lib".to_string(),
                number: 456,
            },
        ];

        let comment = platform.generate_linked_pr_comment(&links);
        assert!(comment.contains("app:123"));
        assert!(comment.contains("lib:456"));
    }

    #[test]
    fn test_parse_linked_pr_comment() {
        let platform = MockPlatform;
        let body = r#"
Some PR description

<!-- gitgrip-linked-prs
app:123
lib:456
-->

More content
"#;

        let links = platform.parse_linked_pr_comment(body);
        assert_eq!(links.len(), 2);
        assert_eq!(links[0].repo_name, "app");
        assert_eq!(links[0].number, 123);
        assert_eq!(links[1].repo_name, "lib");
        assert_eq!(links[1].number, 456);
    }

    #[test]
    fn test_parse_empty_comment() {
        let platform = MockPlatform;
        let links = platform.parse_linked_pr_comment("No linked PRs here");
        assert!(links.is_empty());
    }

    #[test]
    fn test_generate_linked_pr_comment_empty() {
        let platform = MockPlatform;
        assert_eq!(platform.generate_linked_pr_comment(&[]), "");
    }

    #[test]
    fn test_parse_linked_pr_comment_unterminated() {
        let platform = MockPlatform;
        let body = "<!-- gitgrip-linked-prs\napp:42\n";
        assert!(platform.parse_linked_pr_comment(body).is_empty());
    }

    #[test]
    fn test_parse_linked_pr_comment_malformed_lines() {
        let platform = MockPlatform;
        let body = "<!-- gitgrip-linked-prs\nno-colon-here\napp:notanumber\nvalid:99\n-->";
        let links = platform.parse_linked_pr_comment(body);
        assert_eq!(links.len(), 1);
        assert_eq!(links[0].repo_name, "valid");
        assert_eq!(links[0].number, 99);
    }

    #[test]
    fn test_parse_linked_pr_comment_with_surrounding_text() {
        let platform = MockPlatform;
        let body = "PR description here.\n\n<!-- gitgrip-linked-prs\nrepo:1\n-->\n\nMore text.";
        let links = platform.parse_linked_pr_comment(body);
        assert_eq!(links.len(), 1);
        assert_eq!(links[0].repo_name, "repo");
    }

    #[test]
    fn test_generate_linked_pr_comment_format() {
        let platform = MockPlatform;
        let links = vec![LinkedPRRef {
            repo_name: "myapp".to_string(),
            number: 7,
        }];
        let comment = platform.generate_linked_pr_comment(&links);
        assert_eq!(comment, "<!-- gitgrip-linked-prs\nmyapp:7\n-->");
    }
}