pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#![cfg_attr(coverage_nightly, coverage(off))]
//! GitHub API client for unified workflow (Issue #75)
//!
//! This module provides GitHub API integration using octocrab for:
//! - Fetching issue details from GitHub
//! - Creating new issues
//! - Updating issue status and labels
//! - Syncing with local roadmap.yaml
//!
//! Note: This module requires the `github-api` feature to be enabled.
//! When disabled, use the `gh` CLI fallback in work_handlers.rs instead.

#![cfg(feature = "github-api")]

use anyhow::{Context, Result};
use octocrab::models::issues::Issue;
use octocrab::models::IssueState;
use octocrab::Octocrab;
use std::env;

/// GitHub API client for repository operations
#[derive(Debug)]
pub struct GitHubClient {
    octocrab: Octocrab,
    repo_owner: String,
    repo_name: String,
}

impl GitHubClient {
    /// Create a new GitHub client from repo string (e.g., "paiml/pmat")
    ///
    /// Requires GITHUB_TOKEN environment variable to be set.
    ///
    /// # Arguments
    /// * `repo` - Repository in "owner/name" format
    ///
    /// # Errors
    /// Returns error if:
    /// - GITHUB_TOKEN is not set
    /// - Repo format is invalid (not "owner/name")
    /// - Failed to initialize octocrab client
    pub fn new(repo: &str) -> Result<Self> {
        let parts: Vec<&str> = repo.split('/').collect();
        if parts.len() != 2 {
            anyhow::bail!("Invalid repo format: '{}'. Expected 'owner/name'", repo);
        }

        let token = env::var("GITHUB_TOKEN").context(
            "GITHUB_TOKEN environment variable not set. Please set it to use GitHub API.",
        )?;

        let octocrab = Octocrab::builder()
            .personal_token(token)
            .build()
            .context("Failed to initialize GitHub client")?;

        Ok(Self {
            octocrab,
            repo_owner: parts[0].to_string(),
            repo_name: parts[1].to_string(),
        })
    }

    /// Create a new GitHub client without authentication (read-only, rate-limited)
    ///
    /// This is useful for public repositories when GITHUB_TOKEN is not available.
    /// Note: Rate limits are much lower without authentication (60 req/hour vs 5000).
    ///
    /// # Arguments
    /// * `repo` - Repository in "owner/name" format
    pub fn new_unauthenticated(repo: &str) -> Result<Self> {
        let parts: Vec<&str> = repo.split('/').collect();
        if parts.len() != 2 {
            anyhow::bail!("Invalid repo format: '{}'. Expected 'owner/name'", repo);
        }

        let octocrab = Octocrab::builder()
            .build()
            .context("Failed to initialize GitHub client")?;

        Ok(Self {
            octocrab,
            repo_owner: parts[0].to_string(),
            repo_name: parts[1].to_string(),
        })
    }

    /// Fetch issue details from GitHub
    ///
    /// # Arguments
    /// * `issue_num` - GitHub issue number
    ///
    /// # Returns
    /// Issue details including title, labels, body, and state
    pub async fn fetch_issue(&self, issue_num: u64) -> Result<Issue> {
        let issue = self
            .octocrab
            .issues(&self.repo_owner, &self.repo_name)
            .get(issue_num)
            .await
            .context(format!("Failed to fetch issue #{}", issue_num))?;

        Ok(issue)
    }

    /// Create a new GitHub issue
    ///
    /// # Arguments
    /// * `title` - Issue title
    /// * `body` - Issue description (markdown)
    /// * `labels` - Optional labels to apply
    ///
    /// # Returns
    /// Created issue with GitHub-assigned number
    pub async fn create_issue(
        &self,
        title: &str,
        body: &str,
        labels: Option<Vec<String>>,
    ) -> Result<Issue> {
        let issues_handler = self.octocrab.issues(&self.repo_owner, &self.repo_name);
        let mut issue_builder = issues_handler.create(title).body(body);

        if let Some(label_list) = labels {
            issue_builder = issue_builder.labels(label_list);
        }

        let issue = issue_builder
            .send()
            .await
            .context("Failed to create GitHub issue")?;

        Ok(issue)
    }

    /// Update an existing GitHub issue
    ///
    /// # Arguments
    /// * `issue_num` - GitHub issue number
    /// * `title` - Optional new title
    /// * `body` - Optional new body
    /// * `state` - Optional new state ("open" or "closed")
    /// * `labels` - Optional new labels (replaces existing)
    pub async fn update_issue(
        &self,
        issue_num: u64,
        title: Option<&str>,
        body: Option<&str>,
        state: Option<&str>,
        labels: Option<Vec<String>>,
    ) -> Result<Issue> {
        let issues_handler = self.octocrab.issues(&self.repo_owner, &self.repo_name);
        let mut update_builder = issues_handler.update(issue_num);

        if let Some(t) = title {
            update_builder = update_builder.title(t);
        }

        if let Some(b) = body {
            update_builder = update_builder.body(b);
        }

        if let Some(s) = state {
            let state_enum = match s {
                "open" => IssueState::Open,
                "closed" => IssueState::Closed,
                _ => anyhow::bail!("Invalid state: '{}'. Must be 'open' or 'closed'", s),
            };
            update_builder = update_builder.state(state_enum);
        }

        // Clone labels to ensure they live long enough
        let labels_owned = labels;
        if let Some(ref label_list) = labels_owned {
            update_builder = update_builder.labels(label_list);
        }

        let issue = update_builder
            .send()
            .await
            .context(format!("Failed to update issue #{}", issue_num))?;

        Ok(issue)
    }

    /// Close a GitHub issue
    ///
    /// # Arguments
    /// * `issue_num` - GitHub issue number to close
    pub async fn close_issue(&self, issue_num: u64) -> Result<Issue> {
        self.update_issue(issue_num, None, None, Some("closed"), None)
            .await
    }

    /// Reopen a GitHub issue
    ///
    /// # Arguments
    /// * `issue_num` - GitHub issue number to reopen
    pub async fn reopen_issue(&self, issue_num: u64) -> Result<Issue> {
        self.update_issue(issue_num, None, None, Some("open"), None)
            .await
    }

    /// List all open issues for the repository
    ///
    /// # Returns
    /// Vector of open issues (max 100, paginated)
    pub async fn list_open_issues(&self) -> Result<Vec<Issue>> {
        let issues = self
            .octocrab
            .issues(&self.repo_owner, &self.repo_name)
            .list()
            .state(octocrab::params::State::Open)
            .per_page(100)
            .send()
            .await
            .context("Failed to list open issues")?;

        Ok(issues.items)
    }

    /// Get repository full name (owner/name)
    pub fn repo_full_name(&self) -> String {
        format!("{}/{}", self.repo_owner, self.repo_name)
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_github_client_creation_requires_valid_repo_format() {
        // This will fail because GITHUB_TOKEN is not set in tests,
        // but it validates the repo format parsing
        let result = GitHubClient::new("invalid");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Invalid repo format"));

        let result = GitHubClient::new("owner/repo/extra");
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Invalid repo format"));
    }

    #[tokio::test]
    async fn test_unauthenticated_client_creation() {
        let result = GitHubClient::new_unauthenticated("paiml/pmat");
        assert!(result.is_ok());
        let client = result.unwrap();
        assert_eq!(client.repo_owner, "paiml");
        assert_eq!(client.repo_name, "pmat");
        assert_eq!(client.repo_full_name(), "paiml/pmat");
    }

    #[tokio::test]
    async fn test_repo_full_name() {
        let client = GitHubClient::new_unauthenticated("paiml/pmat").unwrap();
        assert_eq!(client.repo_full_name(), "paiml/pmat");
    }

    // === Repo format validation tests ===

    #[test]
    fn test_invalid_repo_format_empty() {
        let result = GitHubClient::new("");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Invalid repo format"));
    }

    #[test]
    fn test_invalid_repo_format_no_slash() {
        let result = GitHubClient::new("ownerrepo");
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_repo_format_trailing_slash() {
        let result = GitHubClient::new("owner/");
        // This parses as ["owner", ""] which is 2 parts but one is empty
        // The code doesn't validate empty parts, so this may succeed
        // Let's verify it doesn't crash
        if result.is_ok() {
            let client = result.unwrap();
            assert_eq!(client.repo_owner, "owner");
            assert_eq!(client.repo_name, "");
        }
    }

    #[test]
    fn test_invalid_repo_format_leading_slash() {
        let result = GitHubClient::new("/repo");
        if result.is_ok() {
            let client = result.unwrap();
            assert_eq!(client.repo_owner, "");
            assert_eq!(client.repo_name, "repo");
        }
    }

    #[tokio::test]
    async fn test_valid_repo_format_unauthenticated() {
        let result = GitHubClient::new_unauthenticated("octocat/hello-world");
        assert!(result.is_ok());
        let client = result.unwrap();
        assert_eq!(client.repo_owner, "octocat");
        assert_eq!(client.repo_name, "hello-world");
    }

    #[tokio::test]
    async fn test_unauthenticated_invalid_format() {
        let result = GitHubClient::new_unauthenticated("invalid");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_unauthenticated_too_many_parts() {
        let result = GitHubClient::new_unauthenticated("a/b/c");
        assert!(result.is_err());
    }

    // === repo_full_name tests ===

    #[tokio::test]
    async fn test_repo_full_name_various() {
        let client = GitHubClient::new_unauthenticated("microsoft/vscode").unwrap();
        assert_eq!(client.repo_full_name(), "microsoft/vscode");

        let client = GitHubClient::new_unauthenticated("rust-lang/rust").unwrap();
        assert_eq!(client.repo_full_name(), "rust-lang/rust");
    }

    // === Debug trait test ===

    #[tokio::test]
    async fn test_github_client_debug() {
        let client = GitHubClient::new_unauthenticated("test/repo").unwrap();
        let debug_str = format!("{:?}", client);
        assert!(debug_str.contains("GitHubClient"));
        assert!(debug_str.contains("test"));
        assert!(debug_str.contains("repo"));
    }

    // === Client reuse tests ===

    #[tokio::test]
    async fn test_client_can_be_reused() {
        let client = GitHubClient::new_unauthenticated("owner/repo").unwrap();

        // Multiple calls to repo_full_name should work
        assert_eq!(client.repo_full_name(), "owner/repo");
        assert_eq!(client.repo_full_name(), "owner/repo");
    }

    // === GITHUB_TOKEN environment tests ===

    #[test]
    fn test_new_without_token() {
        // Save current token if exists
        let saved = env::var("GITHUB_TOKEN").ok();

        // Remove token
        env::remove_var("GITHUB_TOKEN");

        let result = GitHubClient::new("owner/repo");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("GITHUB_TOKEN"));

        // Restore token if it existed
        if let Some(token) = saved {
            env::set_var("GITHUB_TOKEN", token);
        }
    }

    // === Edge cases ===

    #[tokio::test]
    async fn test_repo_with_dots() {
        let client = GitHubClient::new_unauthenticated("owner/repo.name").unwrap();
        assert_eq!(client.repo_name, "repo.name");
    }

    #[tokio::test]
    async fn test_repo_with_underscores() {
        let client = GitHubClient::new_unauthenticated("owner/repo_name").unwrap();
        assert_eq!(client.repo_name, "repo_name");
    }

    #[tokio::test]
    async fn test_repo_with_numbers() {
        let client = GitHubClient::new_unauthenticated("owner123/repo456").unwrap();
        assert_eq!(client.repo_owner, "owner123");
        assert_eq!(client.repo_name, "repo456");
    }

    #[tokio::test]
    async fn test_repo_with_mixed_case() {
        let client = GitHubClient::new_unauthenticated("Owner/RepoName").unwrap();
        assert_eq!(client.repo_owner, "Owner");
        assert_eq!(client.repo_name, "RepoName");
    }
}