jj-vine 0.1.0

Stacked pull requests for jj (jujutsu). Supports GitLab and bookmark-based flow.
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
use bon::bon;
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};

/// GitLab REST API client
pub struct GitLabClient {
    base_url: String,
    project_id: String,
    token: String,
    client: reqwest::Client,
}

#[bon]
impl GitLabClient {
    /// Create a new GitLab client
    ///
    /// # Arguments
    /// * `base_url` - GitLab instance URL (e.g., <https://gitlab.example.com>)
    /// * `project_id` - Project ID (e.g., "group/project" or "12345")
    /// * `token` - Personal Access Token
    /// * `ca_bundle` - Optional path to CA bundle for TLS verification
    /// * `accept_non_compliant_certs` - Accept non-compliant TLS certificates
    pub fn new(
        base_url: String,
        project_id: String,
        token: String,
        ca_bundle: Option<String>,
        accept_non_compliant_certs: bool,
    ) -> Result<Self> {
        let mut client_builder = reqwest::Client::builder();

        // Accept non-compliant certificates if configured
        if accept_non_compliant_certs {
            client_builder = client_builder.tls_danger_accept_invalid_certs(true);
        }

        // Add custom CA bundle if provided
        if let Some(ca_path) = ca_bundle {
            let ca_cert = std::fs::read(&ca_path).map_err(|e| Error::Config {
                message: format!("Failed to read CA bundle at {}: {}", ca_path, e),
            })?;

            let certs =
                reqwest::Certificate::from_pem_bundle(&ca_cert).map_err(|e| Error::Config {
                    message: format!("Failed to parse CA bundle: {}", e),
                })?;

            for cert in certs {
                client_builder = client_builder.add_root_certificate(cert);
            }
        }

        let client = client_builder.build().map_err(|e| Error::Config {
            message: format!("Failed to build HTTP client: {}", e),
        })?;

        Ok(Self {
            base_url,
            project_id,
            token,
            client,
        })
    }

    /// URL-encode the project ID for use in API paths
    fn encode_project_id(&self) -> String {
        urlencoding::encode(&self.project_id).to_string()
    }

    /// Get the current authenticated user
    pub async fn get_current_user(&self) -> Result<User> {
        let url = format!("{}/api/v4/user", self.base_url);

        let response = self
            .client
            .get(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to get current user: {} - {}", status, text),
            });
        }

        let user: User = response.json().await?;
        Ok(user)
    }

    /// Get user by username
    pub async fn get_user_by_username(&self, username: &str) -> Result<Option<User>> {
        let url = format!(
            "{}/api/v4/users?username={}",
            self.base_url,
            urlencoding::encode(username)
        );

        let response = self
            .client
            .get(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to get user by username: {} - {}", status, text),
            });
        }

        let users: Vec<User> = response.json().await?;
        Ok(users.into_iter().next())
    }

    /// Find merge request by source branch name
    ///
    /// Returns the first MR found with the given source branch, or None if not
    /// found
    pub async fn find_mr_by_source_branch(&self, branch: &str) -> Result<Option<MergeRequest>> {
        let url = format!(
            "{}/api/v4/projects/{}/merge_requests?source_branch={}&state=opened",
            self.base_url,
            self.encode_project_id(),
            urlencoding::encode(branch)
        );

        let response = self
            .client
            .get(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to query merge requests: {} - {}", status, text),
            });
        }

        let mrs: Vec<MergeRequest> = response.json().await?;
        Ok(mrs.into_iter().next())
    }

    /// Create a new merge request
    #[builder]
    pub async fn create_merge_request(
        &self,
        source_branch: &str,
        target_branch: &str,
        title: &str,
        description: Option<&str>,
        remove_source_branch: Option<bool>,
        squash: Option<bool>,
        assignee_ids: Option<&[u64]>,
        reviewer_ids: Option<&[u64]>,
    ) -> Result<MergeRequest> {
        let url = format!(
            "{}/api/v4/projects/{}/merge_requests",
            self.base_url,
            self.encode_project_id()
        );

        let mut payload = serde_json::json!({
            "source_branch": source_branch,
            "target_branch": target_branch,
            "title": title,
            "remove_source_branch": remove_source_branch.unwrap_or(true),
            "squash": squash.unwrap_or(false),
        });

        if let Some(desc) = description {
            payload["description"] = serde_json::json!(desc);
        }

        if let Some(assignees) = assignee_ids
            && !assignees.is_empty()
        {
            payload["assignee_ids"] = serde_json::json!(assignees);
        }

        if let Some(reviewers) = reviewer_ids
            && !reviewers.is_empty()
        {
            payload["reviewer_ids"] = serde_json::json!(reviewers);
        }

        let response = self
            .client
            .post(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .json(&payload)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to create merge request: {} - {}", status, text),
            });
        }

        let mr: MergeRequest = response.json().await?;
        Ok(mr)
    }

    /// Update the target branch (base) of an existing merge request
    pub async fn update_mr_base(
        &self,
        mr_iid: u64,
        new_target_branch: &str,
    ) -> Result<MergeRequest> {
        let url = format!(
            "{}/api/v4/projects/{}/merge_requests/{}",
            self.base_url,
            self.encode_project_id(),
            mr_iid
        );

        let payload = serde_json::json!({
            "target_branch": new_target_branch,
        });

        let response = self
            .client
            .put(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .json(&payload)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to update merge request: {} - {}", status, text),
            });
        }

        let mr: MergeRequest = response.json().await?;
        Ok(mr)
    }

    /// Update the description of an existing merge request
    pub async fn update_mr_description(
        &self,
        mr_iid: u64,
        new_description: &str,
    ) -> Result<MergeRequest> {
        let url = format!(
            "{}/api/v4/projects/{}/merge_requests/{}",
            self.base_url,
            self.encode_project_id(),
            mr_iid
        );

        let payload = serde_json::json!({
            "description": new_description,
        });

        let response = self
            .client
            .put(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .json(&payload)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to update MR description: {} - {}", status, text),
            });
        }

        let mr: MergeRequest = response.json().await?;
        Ok(mr)
    }

    /// Get a specific merge request by IID
    pub async fn get_merge_request(&self, mr_iid: u64) -> Result<MergeRequest> {
        let url = format!(
            "{}/api/v4/projects/{}/merge_requests/{}",
            self.base_url,
            self.encode_project_id(),
            mr_iid
        );

        let response = self
            .client
            .get(&url)
            .header("PRIVATE-TOKEN", &self.token)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await?;
            return Err(Error::GitLabApi {
                message: format!("Failed to get merge request: {} - {}", status, text),
            });
        }

        let mr: MergeRequest = response.json().await?;
        Ok(mr)
    }
}

/// GitLab User
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    /// User ID
    pub id: u64,

    /// Username
    pub username: String,

    /// User's name
    pub name: String,

    /// User's email
    pub email: Option<String>,
}

/// GitLab Merge Request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MergeRequest {
    /// MR internal ID (unique within project)
    pub iid: u64,

    /// MR global ID
    pub id: u64,

    /// MR title
    pub title: String,

    /// MR description
    pub description: Option<String>,

    /// Source branch name
    pub source_branch: String,

    /// Target branch name
    pub target_branch: String,

    /// MR state (opened, closed, merged, etc.)
    pub state: String,

    /// Web URL to view the MR
    pub web_url: String,
}

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

    #[test]
    fn test_gitlab_client_new() {
        let client = GitLabClient::new(
            "https://gitlab.example.com".to_string(),
            "group/project".to_string(),
            "token123".to_string(),
            None,
            false,
        )
        .expect("Failed to create client");

        assert_eq!(client.base_url, "https://gitlab.example.com");
        assert_eq!(client.project_id, "group/project");
        assert_eq!(client.token, "token123");
    }

    #[test]
    fn test_encode_project_id() {
        let client = GitLabClient::new(
            "https://gitlab.example.com".to_string(),
            "group/project".to_string(),
            "token123".to_string(),
            None,
            false,
        )
        .expect("Failed to create client");

        let encoded = client.encode_project_id();
        assert_eq!(encoded, "group%2Fproject");
    }

    #[test]
    fn test_user_struct() {
        let user = User {
            id: 123,
            username: "testuser".to_string(),
            name: "Test User".to_string(),
            email: Some("test@example.com".to_string()),
        };

        assert_eq!(user.id, 123);
        assert_eq!(user.username, "testuser");
        assert_eq!(user.name, "Test User");
        assert_eq!(user.email, Some("test@example.com".to_string()));
    }

    #[test]
    fn test_merge_request_struct() {
        let mr = MergeRequest {
            iid: 123,
            id: 456,
            title: "Test MR".to_string(),
            description: Some("Test description".to_string()),
            source_branch: "feature".to_string(),
            target_branch: "main".to_string(),
            state: "opened".to_string(),
            web_url: "https://gitlab.example.com/group/project/-/merge_requests/123".to_string(),
        };

        assert_eq!(mr.iid, 123);
        assert_eq!(mr.title, "Test MR");
        assert_eq!(mr.source_branch, "feature");
        assert_eq!(mr.target_branch, "main");
    }

    #[test]
    fn test_ca_bundle_with_multiple_certificates() {
        use std::io::Write;

        use tempfile::NamedTempFile;

        // Create a temporary file with multiple certificates
        let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");

        // Write two valid PEM certificates (generated with openssl x509 v3)
        let cert_bundle = "-----BEGIN CERTIFICATE-----
MIIDeTCCAmGgAwIBAgIUfO3nrSE5qNWMV+TDTa+tkCwUd04wDQYJKoZIhvcNAQEL
BQAwWTELMAkGA1UEBhMCVVMxDTALBgNVBAgMBFRlc3QxDTALBgNVBAcMBFRlc3Qx
DTALBgNVBAoMBFRlc3QxDTALBgNVBAsMBFRlc3QxDjAMBgNVBAMMBVRlc3QxMB4X
DTI2MDEwODAxMDQxNFoXDTI3MDEwODAxMDQxNFowWTELMAkGA1UEBhMCVVMxDTAL
BgNVBAgMBFRlc3QxDTALBgNVBAcMBFRlc3QxDTALBgNVBAoMBFRlc3QxDTALBgNV
BAsMBFRlc3QxDjAMBgNVBAMMBVRlc3QxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAo53dK+I1wLb2ck2zOGRDTAQXrXUazxJVPfCVdedJ+pOx4eIR1V8u
iffOsxjWG/hxoIlZpj0+OGj3GdL3wUi7KUqJUcpzVjqylAfYgBIGruQI9qLtmZSx
ZwKhLDRm++83SCRjkwe7daSAgvSlc/0cAWUQcczRPJG1WnG42+V2Tngy6z+FJck4
F8+3dPVGy0tQs0B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Vf6JBVzHtGY1Jrq1Yohcbbz2tRH13pmnsZ
ml6rnYw2BzJo8PuuLwVvI8yNOtX95XT1MoidW5Marh3MIr6AJ4zgIfNsoC4v32gZ
wfRTiU0E4Y0l6W/McA8DzN25gBUswkd9iPtosM+H5P/fF2xlXlH9TkMz/JxL9haI
wcJcFaLvmJLuO5j1byLplKefjTCVSCvMK+5Z9iP3FxVFkS/Dmjtw1aJwMBNIRXLL
+KDnRStmqqbMPwgNz28BKaif3QThGfa03lLrINQ2OOL3ZaULj5pllpOgf3SL3h54
zviV5VitLLTXowAJkpjgSjBjGHTS5MmW3wIDAQABozkwNzAJBgNVHRMEAjAAMAsG
A1UdDwQEAwIF4DAdBgNVHQ4EFgQUOoaNpsdD/j+YxvwbUDsGR/IjGfYwDQYJKoZI
hvcNAQELBQADggEBABmCPwOnbaTSbShJqFDscoRQo8nuPuSNP76pu+TB14O+vsJq
a8KIRiCTycs72zxaJbdB+5knZs+p3QnDRH3YXhDq8T6xJzDW+mDwrO/xcpdDfEkO
hkLenuLhRNuhwhqAkcdaBvrnZHI7wuI6FAx5EK6MnFaCVvNrFhF/XZRKWH0D022j
wNLLlmTiHEaSCWW/FNYfkwzF+oamHunxZ0TRfFFnVpE1ADMVt9CGe/K1eLoJ9ZLW
zAAjdQJFYiiLIdUrYat1Jz+NlrTCI5/KEIs3/+aS4HwRnM3h3w6taQKDg2q2Hiez
uYyBeUf6LmQswHqXfxOmAoy1HbXDtNvmClznsb0=
-----END CERTIFICATE-----";

        temp_file
            .write_all(cert_bundle.as_bytes())
            .expect("Failed to write to temp file");
        let path = temp_file.path().to_str().unwrap().to_string();

        // This should succeed with from_pem_bundle() but would fail with from_pem()
        GitLabClient::new(
            "https://gitlab.example.com".to_string(),
            "group/project".to_string(),
            "token123".to_string(),
            Some(path),
            false,
        )
        .expect("Failed to create client with multi-cert bundle");
    }

    // For real GitLab API integration tests, see
    // tests/gitlab_integration_tests.rs
}