git-parsec 0.3.0

Git worktree lifecycle manager — ticket to PR in one command. Parallel AI agent workflows with Jira & GitHub Issues integration.
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
use anyhow::{bail, Context, Result};
use reqwest::Client;
use serde::{Deserialize, Serialize};

use crate::config::ParsecConfig;

/// Result of PR creation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrResult {
    pub url: String,
    pub number: u64,
}

/// Parsed GitHub remote info including the host (for Enterprise support).
#[derive(Debug, Clone)]
pub struct GitHubRemote {
    pub host: String,
    pub owner: String,
    pub repo: String,
}

impl GitHubRemote {
    /// Return the API base URL for this remote.
    /// - `github.com` → `https://api.github.com`
    /// - Enterprise (e.g. `github.daumkakao.com`) → `https://{host}/api/v3`
    pub fn api_base(&self) -> String {
        if self.host == "github.com" {
            "https://api.github.com".to_string()
        } else {
            format!("https://{}/api/v3", self.host)
        }
    }

    /// Return the browse URL for this remote.
    #[allow(dead_code)]
    pub fn browse_url(&self, path: &str) -> String {
        format!(
            "https://{}/{}/{}/{}",
            self.host, self.owner, self.repo, path
        )
    }
}

/// Parse any GitHub remote URL (github.com or Enterprise) into GitHubRemote.
///
/// Supports:
/// - SSH: `git@github.com:owner/repo.git`, `git@github.enterprise.com:owner/repo.git`
/// - HTTPS: `https://github.com/owner/repo.git`, `https://github.enterprise.com/owner/repo.git`
pub fn parse_github_remote(url: &str) -> Option<GitHubRemote> {
    // SSH form: git@<host>:owner/repo.git
    if url.starts_with("git@") {
        let rest = url.strip_prefix("git@")?;
        let (host, path) = rest.split_once(':')?;
        let path = path.trim_end_matches(".git");
        let mut parts = path.splitn(2, '/');
        let owner = parts.next()?.to_owned();
        let repo = parts.next()?.to_owned();
        return Some(GitHubRemote {
            host: host.to_owned(),
            owner,
            repo,
        });
    }

    // HTTPS form: https://<host>/owner/repo.git
    let rest = url
        .strip_prefix("https://")
        .or_else(|| url.strip_prefix("http://"))?;
    let (host, path) = rest.split_once('/')?;
    let path = path.trim_end_matches(".git");
    let mut parts = path.splitn(2, '/');
    let owner = parts.next()?.to_owned();
    let repo = parts.next()?.to_owned();
    Some(GitHubRemote {
        host: host.to_owned(),
        owner,
        repo,
    })
}

/// Resolve a GitHub token for the given host.
///
/// Resolution priority:
/// 1. `config.github.<host>.token` — host-specific config
/// 2. `PARSEC_GITHUB_TOKEN` env var — explicit override
/// 3. `GITHUB_TOKEN` / `GH_TOKEN` — generic fallback
pub fn resolve_github_token(host: &str, config: &ParsecConfig) -> Option<String> {
    // 1. Host-specific config token
    if let Some(host_cfg) = config.github.get(host) {
        if let Some(ref token) = host_cfg.token {
            if !token.is_empty() {
                return Some(token.clone());
            }
        }
    }

    // 2 & 3. Environment variables (PARSEC_GITHUB_TOKEN > GITHUB_TOKEN > GH_TOKEN)
    if let Some(token) = crate::env::github_token() {
        return Some(token);
    }

    None
}

/// PR status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrStatus {
    pub number: u64,
    pub title: String,
    pub state: String,
    pub mergeable: Option<bool>,
    pub ci_status: String,
    pub review_status: String,
    pub url: String,
}

/// Fetch the status of a GitHub PR by number.
pub async fn get_pr_status(
    remote_url: &str,
    pr_number: u64,
    config: &ParsecConfig,
) -> Result<Option<PrStatus>> {
    let remote = parse_github_remote(remote_url).ok_or_else(|| {
        anyhow::anyhow!("could not parse owner/repo from remote URL: {}", remote_url)
    })?;

    let token = match resolve_github_token(&remote.host, config) {
        Some(t) => t,
        None => return Ok(None),
    };

    let api_base = remote.api_base();
    let client = Client::new();

    // Fetch PR details
    let pr_url = format!(
        "{}/repos/{}/{}/pulls/{}",
        api_base, remote.owner, remote.repo, pr_number
    );
    let pr_resp: serde_json::Value = client
        .get(&pr_url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .send()
        .await?
        .json()
        .await?;

    let title = pr_resp["title"].as_str().unwrap_or("").to_string();
    let state = pr_resp["state"].as_str().unwrap_or("unknown").to_string();
    let mergeable = pr_resp["mergeable"].as_bool();
    let html_url = pr_resp["html_url"].as_str().unwrap_or("").to_string();
    let head_sha = pr_resp["head"]["sha"].as_str().unwrap_or("");

    // Fetch combined commit status
    let ci_status = if !head_sha.is_empty() {
        let status_url = format!(
            "{}/repos/{}/{}/commits/{}/status",
            api_base, remote.owner, remote.repo, head_sha
        );
        let status_resp: serde_json::Value = client
            .get(&status_url)
            .header("Accept", "application/vnd.github+json")
            .header("X-GitHub-Api-Version", "2022-11-28")
            .header("User-Agent", "git-parsec")
            .bearer_auth(&token)
            .send()
            .await?
            .json()
            .await?;
        status_resp["state"]
            .as_str()
            .unwrap_or("unknown")
            .to_string()
    } else {
        "unknown".to_string()
    };

    // Fetch reviews
    let reviews_url = format!(
        "{}/repos/{}/{}/pulls/{}/reviews",
        api_base, remote.owner, remote.repo, pr_number
    );
    let reviews_resp: Vec<serde_json::Value> = client
        .get(&reviews_url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .send()
        .await?
        .json()
        .await?;

    let review_status = if reviews_resp.iter().any(|r| {
        r["state"]
            .as_str()
            .is_some_and(|s| s == "CHANGES_REQUESTED")
    }) {
        "changes_requested".to_string()
    } else if reviews_resp
        .iter()
        .any(|r| r["state"].as_str().is_some_and(|s| s == "APPROVED"))
    {
        "approved".to_string()
    } else if reviews_resp.is_empty() {
        "no reviews".to_string()
    } else {
        "pending".to_string()
    };

    Ok(Some(PrStatus {
        number: pr_number,
        title,
        state,
        mergeable,
        ci_status,
        review_status,
        url: html_url,
    }))
}

/// A single CI check run
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckRun {
    pub name: String,
    pub status: String,
    pub conclusion: Option<String>,
    pub started_at: Option<String>,
    pub completed_at: Option<String>,
    pub html_url: Option<String>,
}

/// Aggregated CI status for a PR
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CiStatus {
    pub pr_number: u64,
    pub head_sha: String,
    pub overall: String,
    pub checks: Vec<CheckRun>,
}

/// Fetch check runs for a PR by number.
/// Returns None if no GitHub token is available.
pub async fn get_check_runs(
    remote_url: &str,
    pr_number: u64,
    config: &ParsecConfig,
) -> Result<Option<CiStatus>> {
    let remote = parse_github_remote(remote_url).ok_or_else(|| {
        anyhow::anyhow!("could not parse owner/repo from remote URL: {}", remote_url)
    })?;

    let token = match resolve_github_token(&remote.host, config) {
        Some(t) => t,
        None => return Ok(None),
    };

    let api_base = remote.api_base();
    let client = Client::new();

    // Fetch PR to get head SHA
    let pr_url = format!(
        "{}/repos/{}/{}/pulls/{}",
        api_base, remote.owner, remote.repo, pr_number
    );
    let pr_resp: serde_json::Value = client
        .get(&pr_url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .send()
        .await?
        .json()
        .await?;

    let head_sha = pr_resp["head"]["sha"].as_str().unwrap_or("").to_string();

    if head_sha.is_empty() {
        bail!("could not determine head SHA for PR #{}", pr_number);
    }

    // Fetch check runs for the head SHA
    let checks_url = format!(
        "{}/repos/{}/{}/commits/{}/check-runs",
        api_base, remote.owner, remote.repo, head_sha
    );
    let checks_resp: serde_json::Value = client
        .get(&checks_url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .send()
        .await?
        .json()
        .await?;

    let checks: Vec<CheckRun> = checks_resp["check_runs"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .map(|c| CheckRun {
            name: c["name"].as_str().unwrap_or("").to_string(),
            status: c["status"].as_str().unwrap_or("").to_string(),
            conclusion: c["conclusion"].as_str().map(|s| s.to_string()),
            started_at: c["started_at"].as_str().map(|s| s.to_string()),
            completed_at: c["completed_at"].as_str().map(|s| s.to_string()),
            html_url: c["html_url"].as_str().map(|s| s.to_string()),
        })
        .collect();

    // Derive overall status
    let overall = if checks.is_empty() {
        "no checks".to_string()
    } else if checks
        .iter()
        .any(|c| c.conclusion.as_deref() == Some("failure"))
    {
        "failing".to_string()
    } else if checks.iter().all(|c| {
        c.conclusion.as_deref() == Some("success") || c.conclusion.as_deref() == Some("skipped")
    }) {
        "passing".to_string()
    } else {
        "pending".to_string()
    };

    Ok(Some(CiStatus {
        pr_number,
        head_sha,
        overall,
        checks,
    }))
}

/// Find an open PR by branch name.
/// Returns the PR number if found, None if no token or no matching PR.
pub async fn find_pr_by_branch(
    remote_url: &str,
    branch: &str,
    config: &ParsecConfig,
) -> Result<Option<u64>> {
    let remote = parse_github_remote(remote_url).ok_or_else(|| {
        anyhow::anyhow!("could not parse owner/repo from remote URL: {}", remote_url)
    })?;

    let token = match resolve_github_token(&remote.host, config) {
        Some(t) => t,
        None => return Ok(None),
    };

    let api_base = remote.api_base();
    let client = Client::new();

    let url = format!(
        "{}/repos/{}/{}/pulls?head={}:{}&state=open",
        api_base, remote.owner, remote.repo, remote.owner, branch
    );
    let resp: Vec<serde_json::Value> = client
        .get(&url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .send()
        .await?
        .json()
        .await?;

    Ok(resp.first().and_then(|pr| pr["number"].as_u64()))
}

/// Result of merging a PR
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MergeResult {
    pub sha: String,
    pub message: String,
    pub merged: bool,
}

/// Merge a GitHub PR.
/// `method` should be "squash", "rebase", or "merge".
/// Returns None if no token, Some(MergeResult) on success.
pub async fn merge_pr(
    remote_url: &str,
    pr_number: u64,
    method: &str,
    delete_branch: bool,
    config: &ParsecConfig,
) -> Result<Option<MergeResult>> {
    let remote = parse_github_remote(remote_url).ok_or_else(|| {
        anyhow::anyhow!("could not parse owner/repo from remote URL: {}", remote_url)
    })?;

    let token = match resolve_github_token(&remote.host, config) {
        Some(t) => t,
        None => return Ok(None),
    };

    let api_base = remote.api_base();
    let client = Client::new();

    // Merge the PR
    let url = format!(
        "{}/repos/{}/{}/pulls/{}/merge",
        api_base, remote.owner, remote.repo, pr_number
    );
    let payload = serde_json::json!({
        "merge_method": method,
    });

    let response = client
        .put(&url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .json(&payload)
        .send()
        .await
        .context("Failed to send merge request to GitHub")?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        bail!("GitHub merge API returned {}: {}", status, body);
    }

    let resp: serde_json::Value = response.json().await?;
    let sha = resp["sha"].as_str().unwrap_or("").to_string();
    let message = resp["message"].as_str().unwrap_or("").to_string();

    // Delete remote branch if requested
    if delete_branch {
        let branch_url = format!(
            "{}/repos/{}/{}/pulls/{}",
            api_base, remote.owner, remote.repo, pr_number
        );
        let pr_resp: serde_json::Value = client
            .get(&branch_url)
            .header("Accept", "application/vnd.github+json")
            .header("X-GitHub-Api-Version", "2022-11-28")
            .header("User-Agent", "git-parsec")
            .bearer_auth(&token)
            .send()
            .await?
            .json()
            .await?;

        if let Some(branch_name) = pr_resp["head"]["ref"].as_str() {
            let del_url = format!(
                "{}/repos/{}/{}/git/refs/heads/{}",
                api_base, remote.owner, remote.repo, branch_name
            );
            match client
                .delete(&del_url)
                .header("Accept", "application/vnd.github+json")
                .header("X-GitHub-Api-Version", "2022-11-28")
                .header("User-Agent", "git-parsec")
                .bearer_auth(&token)
                .send()
                .await
            {
                Ok(resp) if !resp.status().is_success() => {
                    let status = resp.status();
                    let body = resp.text().await.unwrap_or_default();
                    eprintln!(
                        "warning: failed to delete remote branch '{}': {} {}",
                        branch_name, status, body
                    );
                }
                Err(e) => {
                    eprintln!(
                        "warning: failed to delete remote branch '{}': {}",
                        branch_name, e
                    );
                }
                _ => {} // success
            }
        }
    }

    Ok(Some(MergeResult {
        sha,
        message,
        merged: true,
    }))
}

/// Create a GitHub pull request.
/// Returns None if no GitHub token is available.
pub async fn create_pr(
    remote_url: &str,
    branch: &str,
    base: &str,
    title: &str,
    body: &str,
    draft: bool,
    config: &ParsecConfig,
) -> Result<Option<PrResult>> {
    let remote = parse_github_remote(remote_url).ok_or_else(|| {
        anyhow::anyhow!("could not parse owner/repo from remote URL: {}", remote_url)
    })?;

    let token = match resolve_github_token(&remote.host, config) {
        Some(t) => t,
        None => return Ok(None),
    };

    let api_url = format!(
        "{}/repos/{}/{}/pulls",
        remote.api_base(),
        remote.owner,
        remote.repo
    );

    let payload = serde_json::json!({
        "title": title,
        "head": branch,
        "base": base,
        "body": body,
        "draft": draft,
    });

    let client = Client::new();
    let response = client
        .post(&api_url)
        .header("Accept", "application/vnd.github+json")
        .header("X-GitHub-Api-Version", "2022-11-28")
        .header("User-Agent", "git-parsec")
        .bearer_auth(&token)
        .json(&payload)
        .send()
        .await
        .context("Failed to send PR creation request to GitHub")?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        bail!("GitHub API returned {}: {}", status, body);
    }

    let resp: serde_json::Value = response
        .json()
        .await
        .context("Failed to parse GitHub API response")?;

    let html_url = resp["html_url"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("GitHub response missing html_url"))?
        .to_owned();

    let number = resp["number"].as_u64().unwrap_or(0);

    Ok(Some(PrResult {
        url: html_url,
        number,
    }))
}