File: graphql_issues.rs

package info (click to toggle)
rust-octocrab 0.43.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,532 kB
  • sloc: makefile: 2
file content (90 lines) | stat: -rw-r--r-- 2,439 bytes parent folder | download
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
//! Run this to update `github_schema.graphql`:
//!
//! ```sh
//! curl -L https://docs.github.com/public/schema.docs.graphql -o examples/github_schema.graphql
//! ```
use graphql_client::GraphQLQuery;

#[allow(clippy::upper_case_acronyms)]
type URI = String;

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "examples/github_schema.graphql",
    query_path = "examples/issues_query.graphql",
    variables_derives = "Clone, Debug",
    response_derives = "Clone, Debug"
)]
pub struct IssuesQuery;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let octocrab = octocrab::Octocrab::builder()
        .personal_token(std::env::var("GITHUB_TOKEN").unwrap())
        .build()?;

    let mut variables = issues_query::Variables {
        owner: "XAMPPRocky".to_string(),
        name: "octocrab".to_string(),
        page_size: 5,
        before: None,
    };

    let pages_to_show = 3;
    let mut page = 1;
    loop {
        if page > pages_to_show {
            break;
        }

        let response: octocrab::Result<graphql_client::Response<issues_query::ResponseData>> =
            octocrab
                .graphql(&IssuesQuery::build_query(variables.clone()))
                .await;

        match response {
            Ok(response) => {
                println!("Page {page}:");
                let issues = &response
                    .data
                    .as_ref()
                    .unwrap()
                    .repository
                    .as_ref()
                    .unwrap()
                    .issues;
                print_issues(issues);
                if !update_page_info(&mut variables, issues) {
                    break;
                }
            }
            Err(error) => {
                println!("{error:#?}");
                break;
            }
        }

        page += 1;
    }

    Ok(())
}

fn print_issues(issues: &issues_query::IssuesQueryRepositoryIssues) {
    for issue in issues.nodes.as_ref().unwrap().iter().flatten() {
        println!("{} {}", issue.url, issue.title);
    }
}

fn update_page_info(
    variables: &mut issues_query::Variables,
    issues: &issues_query::IssuesQueryRepositoryIssues,
) -> bool {
    let page_info = &issues.page_info;
    if page_info.has_previous_page {
        variables.before = Some(page_info.start_cursor.as_ref().unwrap().clone());
        true
    } else {
        false
    }
}