Skip to main content

libverify_github/
release_api.rs

1use anyhow::{Context, Result};
2
3use crate::client::GitHubClient;
4use crate::types::{
5    CompareCommit, CompareResponse, PullRequestSummary, Release, ReleaseAsset, Tag,
6};
7
8/// Fetch repository tags (reverse chronological).
9pub fn get_tags(client: &GitHubClient, owner: &str, repo: &str) -> Result<Vec<Tag>> {
10    client.paginate(&format!("/repos/{owner}/{repo}/tags?per_page=100"))
11}
12
13/// Compare two refs and return commits between them.
14pub fn compare_refs(
15    client: &GitHubClient,
16    owner: &str,
17    repo: &str,
18    base: &str,
19    head: &str,
20) -> Result<Vec<CompareCommit>> {
21    let path = format!("/repos/{owner}/{repo}/compare/{base}...{head}");
22    let body = client.get(&path)?;
23    let response: CompareResponse =
24        serde_json::from_str(&body).context("failed to parse compare response")?;
25    Ok(response.commits)
26}
27
28/// Fetch release assets for a given tag.
29///
30/// Returns an empty vec if the tag has no associated release (e.g. lightweight tag).
31pub fn get_release_assets(
32    client: &GitHubClient,
33    owner: &str,
34    repo: &str,
35    tag: &str,
36) -> Result<Vec<ReleaseAsset>> {
37    let path = format!("/repos/{owner}/{repo}/releases/tags/{tag}");
38    match client.get(&path) {
39        Ok(body) => {
40            let release: Release =
41                serde_json::from_str(&body).context("failed to parse release response")?;
42            Ok(release.assets)
43        }
44        Err(_) => {
45            // Tag may exist without a release object (lightweight tag)
46            Ok(vec![])
47        }
48    }
49}
50
51/// Fetch PRs associated with a commit.
52pub fn get_commit_pulls(
53    client: &GitHubClient,
54    owner: &str,
55    repo: &str,
56    sha: &str,
57) -> Result<Vec<PullRequestSummary>> {
58    let path = format!("/repos/{owner}/{repo}/commits/{sha}/pulls");
59    let body = client.get(&path)?;
60    serde_json::from_str(&body).context("failed to parse commit pulls")
61}