ninox-core 0.11.0

Engine core for the Ninox native app: session lifecycle, config, and storage.
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
use anyhow::{Context, Result};
use async_trait::async_trait;
use reqwest::{header, Client};
use serde::Deserialize;

// ---------------------------------------------------------------------------
// Public data types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct PrStatus {
    pub merged:    bool,
    pub state:     String,   // "open" | "closed"
    pub mergeable: Option<bool>,
    pub title:     String,
    pub number:    u64,
    pub head_sha:  String,
}

/// A PR found by searching for an existing head branch (`find_open_pr_for_branch`),
/// independent of any metadata the `gh` wrapper hook may or may not have recorded.
#[derive(Debug, Clone, PartialEq)]
pub struct PrRef {
    pub number: u64,
    pub url:    String,
}

#[derive(Debug, Clone)]
pub struct CheckRun {
    pub name:        String,
    pub status:      String,      // "queued" | "in_progress" | "completed"
    pub conclusion:  Option<String>, // "success" | "failure" | "neutral" | ...
}

#[derive(Debug, Clone)]
pub struct ReviewThread {
    pub id:     i64,
    pub author: String,
    pub body:   String,
    pub path:   Option<String>,
    pub line:   Option<u32>,
    pub state:  String,  // "APPROVED" | "CHANGES_REQUESTED" | "COMMENTED"
}

// ---------------------------------------------------------------------------
// Internal API response shapes
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
struct GhPrHead {
    sha: String,
}

#[derive(Deserialize)]
struct GhPr {
    number:    u64,
    title:     String,
    state:     String,
    merged:    bool,
    mergeable: Option<bool>,
    head:      GhPrHead,
}

#[derive(Deserialize)]
struct GhCheckRunsResponse {
    check_runs: Vec<GhCheckRun>,
}

#[derive(Deserialize)]
struct GhCheckRun {
    name:       String,
    status:     String,
    conclusion: Option<String>,
}

#[derive(Deserialize)]
struct GhReview {
    id:   i64,
    user: GhUser,
    body: String,
    state: String,
}

#[derive(Deserialize)]
struct GhReviewComment {
    id:   i64,
    user: GhUser,
    body: String,
    path: Option<String>,
    line: Option<u32>,
}

#[derive(Deserialize)]
struct GhUser { login: String }

/// Shape returned by the PR *list* endpoint (`GET .../pulls?head=...`) — much
/// thinner than the single-PR shape (`GhPr`): no `merged`/`mergeable`, so it
/// cannot reuse `GhPr`.
#[derive(Deserialize)]
struct GhPrListItem {
    number:   u64,
    html_url: String,
}

// ---------------------------------------------------------------------------
// Trait — allows the poller to be driven by a fake in tests, without any
// network access.
// ---------------------------------------------------------------------------

#[async_trait]
pub trait GithubApi: Send + Sync {
    async fn get_pr_status(&self, owner: &str, repo: &str, pr_number: u64) -> Result<PrStatus>;
    async fn get_ci_checks(&self, owner: &str, repo: &str, head_sha: &str) -> Result<Vec<CheckRun>>;
    async fn get_review_threads(&self, owner: &str, repo: &str, pr_number: u64) -> Result<Vec<ReviewThread>>;
    /// Find an open PR whose head branch is `branch`, independent of any
    /// metadata the `gh` wrapper hook may or may not have recorded — the
    /// active fallback for PRs created outside the wrapped `gh pr create`.
    async fn find_open_pr_for_branch(&self, owner: &str, repo: &str, branch: &str) -> Result<Option<PrRef>>;
}

// ---------------------------------------------------------------------------
// Client
// ---------------------------------------------------------------------------

#[derive(Clone)]
pub struct GitHubClient {
    http:  Client,
    token: String,
}

impl GitHubClient {
    pub fn new(token: String) -> Result<Self> {
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::ACCEPT,
            header::HeaderValue::from_static("application/vnd.github+json"),
        );
        headers.insert(
            "X-GitHub-Api-Version",
            header::HeaderValue::from_static("2022-11-28"),
        );
        let http = Client::builder()
            .user_agent("ninox/0.1")
            .default_headers(headers)
            .build()
            .context("failed to build HTTP client")?;
        Ok(Self { http, token })
    }

    fn auth(&self) -> String {
        format!("Bearer {}", self.token)
    }
}

#[async_trait]
impl GithubApi for GitHubClient {
    async fn get_pr_status(
        &self,
        owner: &str,
        repo: &str,
        pr_number: u64,
    ) -> Result<PrStatus> {
        let url = format!(
            "https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
        );
        let gh: GhPr = self
            .http
            .get(&url)
            .header(header::AUTHORIZATION, self.auth())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;
        Ok(PrStatus {
            merged:    gh.merged,
            state:     gh.state,
            mergeable: gh.mergeable,
            title:     gh.title,
            number:    gh.number,
            head_sha:  gh.head.sha,
        })
    }

    async fn get_ci_checks(
        &self,
        owner: &str,
        repo: &str,
        head_sha: &str,
    ) -> Result<Vec<CheckRun>> {
        let url = format!(
            "https://api.github.com/repos/{owner}/{repo}/commits/{head_sha}/check-runs?per_page=100"
        );
        let resp: GhCheckRunsResponse = self
            .http
            .get(&url)
            .header(header::AUTHORIZATION, self.auth())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;
        Ok(resp.check_runs.into_iter().map(|r| CheckRun {
            name:       r.name,
            status:     r.status,
            conclusion: r.conclusion,
        }).collect())
    }

    async fn get_review_threads(
        &self,
        owner: &str,
        repo: &str,
        pr_number: u64,
    ) -> Result<Vec<ReviewThread>> {
        let url = format!(
            "https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}/reviews?per_page=100"
        );
        let reviews: Vec<GhReview> = self
            .http
            .get(&url)
            .header(header::AUTHORIZATION, self.auth())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        // Also fetch inline review comments
        let comments_url = format!(
            "https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}/comments?per_page=100"
        );
        let comments: Vec<GhReviewComment> = self
            .http
            .get(&comments_url)
            .header(header::AUTHORIZATION, self.auth())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        let mut threads: Vec<ReviewThread> = reviews.into_iter().map(|r| ReviewThread {
            id:     r.id,
            author: r.user.login,
            body:   r.body,
            path:   None,
            line:   None,
            state:  r.state,
        }).collect();

        for c in comments {
            threads.push(ReviewThread {
                id:     c.id,
                author: c.user.login,
                body:   c.body,
                path:   c.path,
                line:   c.line,
                state:  "COMMENTED".to_string(),
            });
        }

        Ok(threads)
    }

    async fn find_open_pr_for_branch(
        &self,
        owner: &str,
        repo: &str,
        branch: &str,
    ) -> Result<Option<PrRef>> {
        let url = format!(
            "https://api.github.com/repos/{owner}/{repo}/pulls?head={owner}:{branch}&state=open&per_page=5"
        );
        let items: Vec<GhPrListItem> = self
            .http
            .get(&url)
            .header(header::AUTHORIZATION, self.auth())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;
        Ok(items.into_iter().next().map(|p| PrRef { number: p.number, url: p.html_url }))
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Parse a git remote URL or bare slug into (owner, repo). Handles
/// "owner/repo", "https://github.com/owner/repo(.git)",
/// "ssh://[user@]host/owner/repo(.git)", and scp-like SSH syntax
/// "[user@]host:owner/repo(.git)" — including a *custom* ssh config host
/// alias (e.g. `git@github.com-work:owner/repo.git`), which multi-remote
/// setups (a personal `origin` alongside an internal mirror reached via an
/// aliased SSH host) rely on and which a fixed `github.com` prefix can't
/// recognize.
pub fn split_repo(s: &str) -> Option<(String, String)> {
    let s = s.trim();
    let tail = if let Some(rest) = s.strip_prefix("https://").or_else(|| s.strip_prefix("http://")) {
        rest.trim_start_matches("github.com/")
    } else if let Some(rest) = s.strip_prefix("ssh://") {
        rest.split_once('/').map(|(_host, r)| r).unwrap_or(rest)
    } else if let Some((_host, rest)) = s.split_once(':') {
        // scp-like syntax: the host is whatever precedes the colon —
        // deliberately not matched against a literal `github.com`.
        rest
    } else {
        s.trim_start_matches("github.com/")
    };
    let mut parts = tail.trim_start_matches('/').splitn(2, '/');
    let owner = parts.next()?.to_string();
    let repo = parts.next()?.trim_end_matches(".git").to_string();
    if owner.is_empty() || repo.is_empty() { return None; }
    Some((owner, repo))
}

/// Every configured git remote's GitHub repo slug (`owner/repo`) for
/// `workspace`, `origin` first (the common case, tried with no extra
/// requests) then any other remote in `git remote` order. Repos here
/// routinely carry a personal `origin` alongside an internal mirror remote —
/// PR detection must not assume a PR always lives against `origin`.
/// Returns an empty `Vec` if `workspace` isn't a git repo or has no remotes.
pub fn candidate_repos(workspace: &str) -> Vec<String> {
    let Ok(output) = std::process::Command::new("git")
        .args(["-C", workspace, "remote"])
        .output()
    else {
        return Vec::new();
    };
    if !output.status.success() {
        return Vec::new();
    }
    let mut names: Vec<String> = String::from_utf8_lossy(&output.stdout)
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();
    names.sort_by_key(|n| if n == "origin" { 0 } else { 1 });

    let mut repos = Vec::new();
    for name in names {
        let Ok(out) = std::process::Command::new("git")
            .args(["-C", workspace, "remote", "get-url", &name])
            .output()
        else {
            continue;
        };
        if !out.status.success() {
            continue;
        }
        let url = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if let Some((owner, repo)) = split_repo(&url) {
            let slug = format!("{owner}/{repo}");
            if !repos.contains(&slug) {
                repos.push(slug);
            }
        }
    }
    repos
}

/// The branch currently checked out in `workspace`. `None` on detached HEAD
/// or if `workspace` isn't a git repo.
pub fn current_branch(workspace: &str) -> Option<String> {
    let output = std::process::Command::new("git")
        .args(["-C", workspace, "rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if branch.is_empty() || branch == "HEAD" {
        return None;
    }
    Some(branch)
}

/// Resolve GitHub token: config value → GITHUB_TOKEN env → `gh auth token`.
pub fn resolve_token(config_token: Option<String>) -> Option<String> {
    config_token
        .or_else(|| std::env::var("GITHUB_TOKEN").ok())
        .or_else(|| {
            std::process::Command::new("gh")
                .args(["auth", "token"])
                .output()
                .ok()
                .filter(|o| o.status.success())
                .and_then(|o| String::from_utf8(o.stdout).ok())
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
        })
}

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

    #[test]
    fn parse_repo_owner_from_url() {
        let (owner, repo) = split_repo("Made-by-Moonlight/Athene").unwrap();
        assert_eq!(owner, "Made-by-Moonlight");
        assert_eq!(repo, "Athene");
    }

    #[test]
    fn parse_repo_owner_strips_github_prefix() {
        let (owner, repo) = split_repo("github.com/Made-by-Moonlight/Athene").unwrap();
        assert_eq!(owner, "Made-by-Moonlight");
        assert_eq!(repo, "Athene");
    }

    #[test]
    fn invalid_repo_returns_none() {
        assert!(split_repo("notarepo").is_none());
    }

    #[test]
    fn parse_repo_owner_from_ssh_scp_syntax() {
        let (owner, repo) = split_repo("git@github.com:Made-by-Moonlight/Athene.git").unwrap();
        assert_eq!(owner, "Made-by-Moonlight");
        assert_eq!(repo, "Athene");
    }

    /// A custom ssh config host alias (e.g. `Host github.com-work` pointing
    /// at a different account/key) is exactly how this environment's
    /// internal mirror remote is configured — the host must be recognized
    /// structurally (anything before the `:`), not by matching a literal
    /// `github.com`.
    #[test]
    fn parse_repo_owner_from_ssh_scp_syntax_with_custom_host_alias() {
        let (owner, repo) = split_repo("git@github.com-synthesia:Synthesia-Technologies/ninox.git").unwrap();
        assert_eq!(owner, "Synthesia-Technologies");
        assert_eq!(repo, "ninox");
    }

    #[test]
    fn parse_repo_owner_from_ssh_url_syntax() {
        let (owner, repo) = split_repo("ssh://git@github.com/Made-by-Moonlight/Athene.git").unwrap();
        assert_eq!(owner, "Made-by-Moonlight");
        assert_eq!(repo, "Athene");
    }

    #[test]
    fn parse_repo_owner_strips_git_suffix_from_https_url() {
        let (owner, repo) = split_repo("https://github.com/Made-by-Moonlight/Athene.git").unwrap();
        assert_eq!(owner, "Made-by-Moonlight");
        assert_eq!(repo, "Athene");
    }

    #[test]
    fn resolve_token_prefers_config_over_env() {
        let token = resolve_token(Some("config-token".to_string()));
        assert_eq!(token, Some("config-token".to_string()));
    }

    fn init_repo(dir: &std::path::Path) {
        let run = |args: &[&str]| {
            let status = std::process::Command::new("git")
                .args(["-C", &dir.to_string_lossy()])
                .args(args)
                .status()
                .unwrap();
            assert!(status.success(), "git {args:?} failed");
        };
        run(&["init", "-q"]);
        run(&["config", "user.email", "test@example.com"]);
        run(&["config", "user.name", "Test"]);
        run(&["commit", "--allow-empty", "-q", "-m", "init"]);
    }

    #[test]
    fn candidate_repos_returns_empty_outside_a_git_repo() {
        let dir = tempfile::tempdir().unwrap();
        assert!(candidate_repos(&dir.path().to_string_lossy()).is_empty());
    }

    /// The environment this bug was filed for has both a personal `origin`
    /// (HTTPS) remote and an internal mirror reached over SSH through a
    /// custom host alias — `origin` must come first (no extra requests in
    /// the common case), but the mirror must still be found.
    #[test]
    fn candidate_repos_lists_origin_first_then_other_remotes() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        let workspace = dir.path().to_string_lossy().to_string();
        std::process::Command::new("git")
            .args(["-C", &workspace, "remote", "add", "internal", "git@github.com-synthesia:Synthesia-Technologies/ninox.git"])
            .status().unwrap();
        std::process::Command::new("git")
            .args(["-C", &workspace, "remote", "add", "origin", "https://github.com/Made-by-Moonlight/ninox.git"])
            .status().unwrap();

        let repos = candidate_repos(&workspace);
        assert_eq!(repos, vec!["Made-by-Moonlight/ninox".to_string(), "Synthesia-Technologies/ninox".to_string()]);
    }

    #[test]
    fn current_branch_reads_checked_out_branch() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        let workspace = dir.path().to_string_lossy().to_string();
        std::process::Command::new("git")
            .args(["-C", &workspace, "checkout", "-q", "-b", "feat/my-fix"])
            .status().unwrap();
        assert_eq!(current_branch(&workspace).as_deref(), Some("feat/my-fix"));
    }

    #[test]
    fn current_branch_none_outside_a_git_repo() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(current_branch(&dir.path().to_string_lossy()), None);
    }
}