guts-migrate 0.1.0

Migration tools for importing repositories from GitHub, GitLab, and Bitbucket to Guts
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
//! Common types for migration operations.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for a migration operation.
#[derive(Debug, Clone)]
pub struct MigrationConfig {
    /// Source repository identifier (e.g., "owner/repo").
    pub source_repo: String,

    /// Guts node API URL.
    pub guts_url: String,

    /// Guts API token for authentication.
    pub guts_token: Option<String>,

    /// Target repository name on Guts (defaults to source name).
    pub target_name: Option<String>,

    /// Target owner on Guts (defaults to authenticated user).
    pub target_owner: Option<String>,
}

impl MigrationConfig {
    /// Create a new migration configuration.
    pub fn new(source_repo: impl Into<String>, guts_url: impl Into<String>) -> Self {
        Self {
            source_repo: source_repo.into(),
            guts_url: guts_url.into(),
            guts_token: None,
            target_name: None,
            target_owner: None,
        }
    }

    /// Set the Guts API token.
    pub fn with_token(mut self, token: impl Into<String>) -> Self {
        self.guts_token = Some(token.into());
        self
    }

    /// Set the target repository name.
    pub fn with_target_name(mut self, name: impl Into<String>) -> Self {
        self.target_name = Some(name.into());
        self
    }

    /// Set the target owner.
    pub fn with_target_owner(mut self, owner: impl Into<String>) -> Self {
        self.target_owner = Some(owner.into());
        self
    }
}

/// Options for controlling what gets migrated.
#[derive(Debug, Clone)]
pub struct MigrationOptions {
    /// Migrate issues.
    pub migrate_issues: bool,

    /// Migrate pull requests / merge requests.
    pub migrate_pull_requests: bool,

    /// Migrate releases and assets.
    pub migrate_releases: bool,

    /// Migrate wiki.
    pub migrate_wiki: bool,

    /// Migrate labels.
    pub migrate_labels: bool,

    /// Migrate milestones.
    pub migrate_milestones: bool,

    /// Include closed issues/PRs.
    pub include_closed: bool,

    /// Rewrite content links to point to Guts.
    pub rewrite_links: bool,

    /// Map of source usernames to Guts usernames.
    pub user_mapping: HashMap<String, String>,
}

impl Default for MigrationOptions {
    fn default() -> Self {
        Self {
            migrate_issues: true,
            migrate_pull_requests: true,
            migrate_releases: true,
            migrate_wiki: true,
            migrate_labels: true,
            migrate_milestones: true,
            include_closed: true,
            rewrite_links: true,
            user_mapping: HashMap::new(),
        }
    }
}

impl MigrationOptions {
    /// Enable or disable issue migration.
    pub fn with_issues(mut self, migrate: bool) -> Self {
        self.migrate_issues = migrate;
        self
    }

    /// Enable or disable pull request migration.
    pub fn with_pull_requests(mut self, migrate: bool) -> Self {
        self.migrate_pull_requests = migrate;
        self
    }

    /// Enable or disable release migration.
    pub fn with_releases(mut self, migrate: bool) -> Self {
        self.migrate_releases = migrate;
        self
    }

    /// Enable or disable wiki migration.
    pub fn with_wiki(mut self, migrate: bool) -> Self {
        self.migrate_wiki = migrate;
        self
    }

    /// Enable or disable label migration.
    pub fn with_labels(mut self, migrate: bool) -> Self {
        self.migrate_labels = migrate;
        self
    }

    /// Enable or disable milestone migration.
    pub fn with_milestones(mut self, migrate: bool) -> Self {
        self.migrate_milestones = migrate;
        self
    }

    /// Enable or disable including closed items.
    pub fn with_closed(mut self, include: bool) -> Self {
        self.include_closed = include;
        self
    }

    /// Enable or disable link rewriting.
    pub fn with_link_rewriting(mut self, rewrite: bool) -> Self {
        self.rewrite_links = rewrite;
        self
    }

    /// Add a user mapping.
    pub fn with_user_mapping(
        mut self,
        source_user: impl Into<String>,
        guts_user: impl Into<String>,
    ) -> Self {
        self.user_mapping
            .insert(source_user.into(), guts_user.into());
        self
    }
}

/// Report of a completed migration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MigrationReport {
    /// Whether the repository was created on Guts.
    pub repo_created: bool,

    /// Whether git data was mirrored successfully.
    pub git_mirrored: bool,

    /// Number of branches migrated.
    pub branches_migrated: usize,

    /// Number of tags migrated.
    pub tags_migrated: usize,

    /// Number of issues migrated.
    pub issues_migrated: usize,

    /// Number of pull requests migrated.
    pub prs_migrated: usize,

    /// Number of releases migrated.
    pub releases_migrated: usize,

    /// Number of release assets migrated.
    pub assets_migrated: usize,

    /// Whether wiki was migrated.
    pub wiki_migrated: bool,

    /// Number of labels migrated.
    pub labels_migrated: usize,

    /// Number of milestones migrated.
    pub milestones_migrated: usize,

    /// Errors encountered during migration.
    pub errors: Vec<MigrationErrorInfo>,

    /// Warnings generated during migration.
    pub warnings: Vec<String>,

    /// Start time of migration.
    pub started_at: Option<DateTime<Utc>>,

    /// End time of migration.
    pub completed_at: Option<DateTime<Utc>>,

    /// URL of the migrated repository on Guts.
    pub guts_repo_url: Option<String>,
}

impl MigrationReport {
    /// Create a new empty report.
    pub fn new() -> Self {
        Self {
            started_at: Some(Utc::now()),
            ..Default::default()
        }
    }

    /// Mark the migration as complete.
    pub fn complete(&mut self) {
        self.completed_at = Some(Utc::now());
    }

    /// Check if the migration was successful (no critical errors).
    pub fn is_successful(&self) -> bool {
        self.repo_created && self.git_mirrored && self.errors.iter().all(|e| !e.is_critical)
    }

    /// Get the total number of items migrated.
    pub fn total_items_migrated(&self) -> usize {
        self.issues_migrated
            + self.prs_migrated
            + self.releases_migrated
            + self.labels_migrated
            + self.milestones_migrated
    }

    /// Add an error to the report.
    pub fn add_error(&mut self, category: &str, message: &str, is_critical: bool) {
        self.errors.push(MigrationErrorInfo {
            category: category.to_string(),
            message: message.to_string(),
            is_critical,
        });
    }

    /// Add a warning to the report.
    pub fn add_warning(&mut self, message: impl Into<String>) {
        self.warnings.push(message.into());
    }

    /// Get the duration of the migration.
    pub fn duration(&self) -> Option<chrono::Duration> {
        match (self.started_at, self.completed_at) {
            (Some(start), Some(end)) => Some(end - start),
            _ => None,
        }
    }

    /// Print a summary of the migration.
    pub fn print_summary(&self) {
        println!("\n=== Migration Summary ===\n");
        println!(
            "Repository created: {}",
            if self.repo_created { "" } else { "" }
        );
        println!(
            "Git data mirrored:  {}",
            if self.git_mirrored { "" } else { "" }
        );

        if self.branches_migrated > 0 || self.tags_migrated > 0 {
            println!("Branches migrated:  {}", self.branches_migrated);
            println!("Tags migrated:      {}", self.tags_migrated);
        }

        println!("Issues migrated:    {}", self.issues_migrated);
        println!("PRs migrated:       {}", self.prs_migrated);
        println!("Releases migrated:  {}", self.releases_migrated);
        println!("Assets migrated:    {}", self.assets_migrated);
        println!(
            "Wiki migrated:      {}",
            if self.wiki_migrated { "" } else { "N/A" }
        );
        println!("Labels migrated:    {}", self.labels_migrated);
        println!("Milestones:         {}", self.milestones_migrated);

        if let Some(url) = &self.guts_repo_url {
            println!("\nRepository URL: {url}");
        }

        if let Some(duration) = self.duration() {
            println!("\nCompleted in {} seconds", duration.num_seconds());
        }

        if !self.errors.is_empty() {
            println!("\nErrors ({}):", self.errors.len());
            for error in &self.errors {
                let severity = if error.is_critical {
                    "CRITICAL"
                } else {
                    "WARNING"
                };
                println!("  [{severity}] {}: {}", error.category, error.message);
            }
        }

        if !self.warnings.is_empty() {
            println!("\nWarnings ({}):", self.warnings.len());
            for warning in &self.warnings {
                println!("  - {warning}");
            }
        }

        let status = if self.is_successful() {
            "SUCCESS"
        } else {
            "FAILED"
        };
        println!("\nOverall Status: {status}");
    }
}

/// Information about an error that occurred during migration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationErrorInfo {
    /// Category of the error (e.g., "issues", "git", "api").
    pub category: String,

    /// Error message.
    pub message: String,

    /// Whether this error is critical (blocks migration success).
    pub is_critical: bool,
}

/// Source platform types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SourcePlatform {
    /// GitHub.
    GitHub,
    /// GitLab.
    GitLab,
    /// Bitbucket.
    Bitbucket,
}

impl std::fmt::Display for SourcePlatform {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::GitHub => write!(f, "GitHub"),
            Self::GitLab => write!(f, "GitLab"),
            Self::Bitbucket => write!(f, "Bitbucket"),
        }
    }
}

/// Issue state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IssueState {
    Open,
    Closed,
}

/// Pull request state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PullRequestState {
    Open,
    Closed,
    Merged,
}

/// Migrated issue representation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigratedIssue {
    pub source_number: u64,
    pub guts_number: u64,
    pub title: String,
    pub state: IssueState,
}

/// Migrated pull request representation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigratedPullRequest {
    pub source_number: u64,
    pub guts_number: u64,
    pub title: String,
    pub state: PullRequestState,
}

/// Migrated release representation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigratedRelease {
    pub tag_name: String,
    pub name: String,
    pub assets_count: usize,
}