aptu-core 0.4.0

Core library for Aptu - OSS issue triage with AI assistance
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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// SPDX-License-Identifier: Apache-2.0

//! GraphQL queries for GitHub API.
//!
//! Uses a single GraphQL query to fetch issues from multiple repositories
//! efficiently, avoiding multiple REST API calls.

use anyhow::{Context, Result};
use backon::Retryable;
use octocrab::Octocrab;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tracing::{debug, instrument};

use crate::ai::types::{IssueComment, RepoLabel, RepoMilestone};
use crate::error::{AptuError, ResourceType};
use crate::retry::retry_backoff;

/// Viewer permission level on a repository.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum ViewerPermission {
    /// Admin permission.
    Admin,
    /// Maintain permission.
    Maintain,
    /// Write permission.
    Write,
    /// Triage permission.
    Triage,
    /// Read permission.
    Read,
}

/// A GitHub issue from the GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueNode {
    /// Issue number.
    pub number: u64,
    /// Issue title.
    pub title: String,
    /// Creation timestamp (ISO 8601).
    #[serde(rename = "createdAt")]
    pub created_at: String,
    /// Issue labels.
    pub labels: Labels,
    /// Issue URL (used by triage command).
    pub url: String,
}

/// Labels container from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Labels {
    /// List of label nodes.
    pub nodes: Vec<LabelNode>,
}

/// A single label.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LabelNode {
    /// Label name.
    pub name: String,
}

/// Issues response for a single repository.
#[derive(Debug, Deserialize)]
pub struct RepoIssues {
    /// Repository name with owner (e.g., "block/goose").
    #[serde(rename = "nameWithOwner")]
    pub name_with_owner: String,
    /// Issues container.
    pub issues: IssuesConnection,
}

/// Issues connection from GraphQL.
#[derive(Debug, Deserialize)]
pub struct IssuesConnection {
    /// List of issue nodes.
    pub nodes: Vec<IssueNode>,
}

/// Builds a GraphQL query to fetch issues from multiple repositories.
///
/// Uses GraphQL aliases to query all repos in a single request.
fn build_issues_query<R: AsRef<str>>(repos: &[(R, R)]) -> Value {
    let fragments: Vec<String> = repos
        .iter()
        .enumerate()
        .map(|(i, (owner, name))| {
            format!(
                r#"repo{i}: repository(owner: "{owner}", name: "{name}") {{
                    nameWithOwner
                    issues(
                        first: 10
                        states: OPEN
                        labels: ["good first issue"]
                        filterBy: {{ assignee: null }}
                        orderBy: {{ field: CREATED_AT, direction: DESC }}
                    ) {{
                        nodes {{
                            number
                            title
                            createdAt
                            labels(first: 5) {{ nodes {{ name }} }}
                            url
                        }}
                    }}
                }}"#,
                i = i,
                owner = owner.as_ref(),
                name = name.as_ref()
            )
        })
        .collect();

    let query = format!("query {{ {} }}", fragments.join("\n"));
    debug!(query_length = query.len(), "Built GraphQL query");
    json!({ "query": query })
}

/// Fetches open "good first issue" issues from multiple repositories.
///
/// Accepts a slice of (owner, name) tuples.
/// Returns a vector of (`repo_name`, issues) tuples.
#[instrument(skip(client, repos), fields(repo_count = repos.len()))]
pub async fn fetch_issues<R: AsRef<str>>(
    client: &Octocrab,
    repos: &[(R, R)],
) -> Result<Vec<(String, Vec<IssueNode>)>> {
    if repos.is_empty() {
        return Ok(vec![]);
    }

    let query = build_issues_query(repos);
    debug!("Executing GraphQL query");

    // Execute the GraphQL query with retry logic
    let response: Value =
        (|| async { client.graphql(&query).await.map_err(|e| anyhow::anyhow!(e)) })
            .retry(retry_backoff())
            .notify(|err, dur| {
                tracing::warn!(
                    error = %err,
                    retry_after = ?dur,
                    "Retrying fetch_issues (GraphQL query)"
                );
            })
            .await
            .context("Failed to execute GraphQL query")?;

    // Check for GraphQL errors
    if let Some(errors) = response.get("errors") {
        let error_msg = serde_json::to_string_pretty(errors).unwrap_or_default();
        anyhow::bail!("GraphQL error: {error_msg}");
    }

    // Parse the response
    let data = response
        .get("data")
        .context("Missing 'data' field in GraphQL response")?;

    let mut results = Vec::with_capacity(repos.len());

    for i in 0..repos.len() {
        let key = format!("repo{i}");
        if let Some(repo_data) = data.get(&key) {
            // Repository might not exist or be private
            if repo_data.is_null() {
                debug!(repo = key, "Repository not found or inaccessible");
                continue;
            }

            let repo_issues: RepoIssues = serde_json::from_value(repo_data.clone())
                .with_context(|| format!("Failed to parse repository data for {key}"))?;

            let issue_count = repo_issues.issues.nodes.len();
            if issue_count > 0 {
                debug!(
                    repo = %repo_issues.name_with_owner,
                    issues = issue_count,
                    "Found issues"
                );
                results.push((repo_issues.name_with_owner, repo_issues.issues.nodes));
            }
        }
    }

    debug!(
        total_repos = results.len(),
        "Fetched issues from repositories"
    );
    Ok(results)
}

/// Repository label from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RepoLabelNode {
    /// Label name.
    pub name: String,
    /// Label description.
    pub description: Option<String>,
    /// Label color (hex code without #).
    pub color: String,
}

impl From<RepoLabelNode> for RepoLabel {
    fn from(node: RepoLabelNode) -> Self {
        RepoLabel {
            name: node.name,
            description: node.description.unwrap_or_default(),
            color: node.color,
        }
    }
}

/// Repository labels connection from GraphQL.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RepoLabelsConnection {
    /// List of label nodes.
    pub nodes: Vec<RepoLabelNode>,
}

/// Repository milestone from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RepoMilestoneNode {
    /// Milestone number.
    pub number: u64,
    /// Milestone title.
    pub title: String,
    /// Milestone description.
    pub description: Option<String>,
}

impl From<RepoMilestoneNode> for RepoMilestone {
    fn from(node: RepoMilestoneNode) -> Self {
        RepoMilestone {
            number: node.number,
            title: node.title,
            description: node.description.unwrap_or_default(),
        }
    }
}

/// Repository milestones connection from GraphQL.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RepoMilestonesConnection {
    /// List of milestone nodes.
    pub nodes: Vec<RepoMilestoneNode>,
}

/// Issue comment from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueCommentNode {
    /// Comment author login.
    pub author: Author,
    /// Comment body.
    pub body: String,
}

impl From<IssueCommentNode> for IssueComment {
    fn from(node: IssueCommentNode) -> Self {
        IssueComment {
            author: node.author.login,
            body: node.body,
        }
    }
}

/// Author information from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Author {
    /// Author login.
    pub login: String,
}

/// Comments connection from GraphQL.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CommentsConnection {
    /// Total count of comments.
    #[serde(rename = "totalCount")]
    pub total_count: u32,
    /// List of comment nodes.
    pub nodes: Vec<IssueCommentNode>,
}

/// Issue from GraphQL response for triage.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueNodeDetailed {
    /// Issue number.
    pub number: u64,
    /// Issue title.
    pub title: String,
    /// Issue body.
    pub body: Option<String>,
    /// Issue URL.
    pub url: String,
    /// Issue labels.
    pub labels: Labels,
    /// Issue comments.
    pub comments: CommentsConnection,
    /// Issue author.
    pub author: Option<Author>,
    /// Issue creation timestamp (ISO 8601).
    #[serde(rename = "createdAt")]
    pub created_at: String,
    /// Issue last update timestamp (ISO 8601).
    #[serde(rename = "updatedAt")]
    pub updated_at: String,
}

/// Repository data from GraphQL response for triage.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RepositoryData {
    /// Repository name with owner.
    #[serde(rename = "nameWithOwner")]
    pub name_with_owner: String,
    /// Repository labels.
    pub labels: RepoLabelsConnection,
    /// Repository milestones.
    pub milestones: RepoMilestonesConnection,
    /// Repository primary language.
    #[serde(rename = "primaryLanguage")]
    pub primary_language: Option<LanguageNode>,
    /// Viewer permission level on the repository.
    #[serde(rename = "viewerPermission")]
    pub viewer_permission: Option<ViewerPermission>,
}

/// Language information from GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LanguageNode {
    /// Language name.
    pub name: String,
}

/// Full response for issue with repo context.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueWithRepoContextResponse {
    /// The issue.
    pub issue: IssueNodeDetailed,
    /// The repository.
    pub repository: RepositoryData,
}

/// Builds a GraphQL query to fetch an issue with repository context.
fn build_issue_with_repo_context_query(owner: &str, repo: &str, number: u64) -> Value {
    let query = format!(
        r#"query {{
            issue: repository(owner: "{owner}", name: "{repo}") {{
                issue(number: {number}) {{
                    number
                    title
                    body
                    url
                    author {{
                        login
                    }}
                    createdAt
                    updatedAt
                    labels(first: 10) {{
                        nodes {{
                            name
                        }}
                    }}
                    comments(first: 5) {{
                        totalCount
                        nodes {{
                            author {{
                                login
                            }}
                            body
                        }}
                    }}
                }}
            }}
            repository(owner: "{owner}", name: "{repo}") {{
                nameWithOwner
                viewerPermission
                labels(first: 100) {{
                    nodes {{
                        name
                        description
                        color
                    }}
                }}
                milestones(first: 50, states: OPEN) {{
                    nodes {{
                        number
                        title
                        description
                    }}
                }}
                primaryLanguage {{
                    name
                }}
            }}
        }}"#
    );

    json!({ "query": query })
}

/// Checks if any error in the GraphQL errors array has type=`NOT_FOUND`.
fn is_not_found_error(errors: &Value) -> bool {
    if let Some(arr) = errors.as_array() {
        arr.iter().any(|err| {
            err.get("type")
                .and_then(|t| t.as_str())
                .is_some_and(|t| t == "NOT_FOUND")
        })
    } else {
        false
    }
}

/// Fetches an issue with repository context (labels, milestones) in a single GraphQL call.
///
/// # Errors
///
/// Returns an error if the GraphQL query fails or the issue is not found.
/// If the issue is not found but a PR with the same number exists, returns a `TypeMismatch` error.
#[instrument(skip(client), fields(owner = %owner, repo = %repo, number = number))]
pub async fn fetch_issue_with_repo_context(
    client: &Octocrab,
    owner: &str,
    repo: &str,
    number: u64,
) -> Result<(IssueNodeDetailed, RepositoryData)> {
    debug!("Fetching issue with repository context");

    let query = build_issue_with_repo_context_query(owner, repo, number);
    debug!("Executing GraphQL query for issue with repo context");

    let response: Value = client
        .graphql(&query)
        .await
        .context("Failed to execute GraphQL query")?;

    // Check for GraphQL errors
    if let Some(errors) = response.get("errors") {
        let error_msg = serde_json::to_string_pretty(errors).unwrap_or_default();

        // Only attempt fallback for NOT_FOUND errors to avoid unnecessary API calls
        if is_not_found_error(errors) {
            debug!("GraphQL NOT_FOUND error, checking if reference is a PR");

            // Try to fetch as a PR to provide a better error message
            if (client.pulls(owner, repo).get(number).await).is_ok() {
                return Err(AptuError::TypeMismatch {
                    number,
                    expected: ResourceType::Issue,
                    actual: ResourceType::PullRequest,
                }
                .into());
            }
        }

        // Not a PR or not a NOT_FOUND error, return the original GraphQL error
        anyhow::bail!("GraphQL error: {error_msg}");
    }

    let data = response
        .get("data")
        .context("Missing 'data' field in GraphQL response")?;

    // Extract issue from nested structure
    let issue_data = data.get("issue").and_then(|v| v.get("issue"));

    let Some(issue_val) = issue_data.filter(|v| !v.is_null()) else {
        debug!("Issue not found in GraphQL response, checking if reference is a PR");

        // Try to fetch as a PR to provide a better error message
        if (client.pulls(owner, repo).get(number).await).is_ok() {
            return Err(AptuError::TypeMismatch {
                number,
                expected: ResourceType::Issue,
                actual: ResourceType::PullRequest,
            }
            .into());
        }

        // Not a PR, return the original error
        anyhow::bail!("Issue not found in GraphQL response");
    };

    let issue: IssueNodeDetailed =
        serde_json::from_value(issue_val.clone()).context("Failed to parse issue data")?;

    let repo_data = data
        .get("repository")
        .context("Repository not found in GraphQL response")?;

    let repository: RepositoryData =
        serde_json::from_value(repo_data.clone()).context("Failed to parse repository data")?;

    debug!(
        issue_number = issue.number,
        labels_count = repository.labels.nodes.len(),
        milestones_count = repository.milestones.nodes.len(),
        "Fetched issue with repository context"
    );

    Ok((issue, repository))
}

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

    #[test]
    fn build_query_single_repo() {
        let repos = [("block", "goose")];

        let query = build_issues_query(&repos);
        let query_str = query["query"].as_str().unwrap();

        assert!(query_str.contains("repo0: repository(owner: \"block\", name: \"goose\")"));
        assert!(query_str.contains("labels: [\"good first issue\"]"));
        assert!(query_str.contains("states: OPEN"));
    }

    #[test]
    fn build_query_multiple_repos() {
        let repos = [("block", "goose"), ("astral-sh", "ruff")];

        let query = build_issues_query(&repos);
        let query_str = query["query"].as_str().unwrap();

        assert!(query_str.contains("repo0: repository(owner: \"block\", name: \"goose\")"));
        assert!(query_str.contains("repo1: repository(owner: \"astral-sh\", name: \"ruff\")"));
    }

    #[test]
    fn build_query_empty_repos() {
        let repos: [(&str, &str); 0] = [];
        let query = build_issues_query(&repos);
        let query_str = query["query"].as_str().unwrap();

        assert_eq!(query_str, "query {  }");
    }
}

/// Target of a reference (either a Tag or Commit).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum RefTarget {
    /// A tag object.
    Tag(TagTarget),
    /// A commit object.
    Commit(CommitTarget),
}

/// A tag object from the GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TagTarget {
    /// The commit that this tag points to.
    pub target: CommitTarget,
}

/// A commit object from the GraphQL response.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CommitTarget {
    /// The commit SHA.
    pub oid: String,
}

/// Build a GraphQL query to resolve a tag to its commit SHA.
///
/// Uses inline fragments to handle both Tag and Commit target types.
fn build_tag_resolution_query(owner: &str, repo: &str, ref_name: &str) -> Value {
    let query = format!(
        r#"query {{
  repository(owner: "{owner}", name: "{repo}") {{
    ref(qualifiedName: "refs/tags/{ref_name}") {{
      target {{
        ... on Tag {{
          target {{
            oid
          }}
        }}
        ... on Commit {{
          oid
        }}
      }}
    }}
  }}
}}"#
    );

    json!({
        "query": query,
    })
}

/// Resolve a tag to its commit SHA using GraphQL.
///
/// Handles both lightweight tags (which point directly to commits) and
/// annotated tags (which have a Tag object that points to a commit).
///
/// # Arguments
///
/// * `client` - Octocrab GitHub client
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `tag_name` - Tag name to resolve
///
/// # Returns
///
/// The commit SHA for the tag, or None if the tag doesn't exist.
#[instrument(skip(client))]
pub async fn resolve_tag_to_commit_sha(
    client: &Octocrab,
    owner: &str,
    repo: &str,
    tag_name: &str,
) -> Result<Option<String>> {
    let query = build_tag_resolution_query(owner, repo, tag_name);

    let response = (|| async {
        client
            .graphql::<serde_json::Value>(&query)
            .await
            .context("GraphQL query failed")
    })
    .retry(&retry_backoff())
    .await?;

    debug!("GraphQL response: {:?}", response);

    // Extract the target from the response
    let target = response
        .get("data")
        .and_then(|data| data.get("repository"))
        .and_then(|repo| repo.get("ref"))
        .and_then(|ref_obj| ref_obj.get("target"));

    match target {
        Some(target_value) => {
            // Try to deserialize as RefTarget to handle both Tag and Commit cases
            match serde_json::from_value::<RefTarget>(target_value.clone()) {
                Ok(RefTarget::Tag(tag)) => Ok(Some(tag.target.oid)),
                Ok(RefTarget::Commit(commit)) => Ok(Some(commit.oid)),
                Err(_) => Ok(None),
            }
        }
        None => Ok(None),
    }
}

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

    #[test]
    fn build_tag_resolution_query_correct_syntax() {
        let query = build_tag_resolution_query("owner", "repo", "v1.0.0");
        let query_str = query["query"].as_str().unwrap();

        assert!(query_str.contains("repository(owner: \"owner\", name: \"repo\")"));
        assert!(query_str.contains("ref(qualifiedName: \"refs/tags/v1.0.0\")"));
        assert!(query_str.contains("... on Tag"));
        assert!(query_str.contains("... on Commit"));
        assert!(query_str.contains("oid"));
    }
}