github_analytics/api/
mod.rs

1use eyre::{eyre, Result};
2
3// Graphql query that will fetch all the issues and pull requests for the specified repo.
4pub const QUERY: &str = r#"query ($name: String!, $owner: String!) {
5    repository(name: $name, owner: $owner) {
6      pullRequests(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
7        nodes {
8          createdAt
9          mergedAt
10          author {
11            login
12          }
13        }
14      }
15      issues(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
16        nodes {
17          author {
18            login
19          }
20          createdAt
21          closedAt
22        }
23      }
24    }
25  }"#;
26
27// GitHub API url.
28const URL: &str = "https://api.github.com/graphql";
29
30/// Returns the result of a graphql query to the github api.
31///
32/// # Arguments
33///
34/// * `client` - The reqwest client.
35/// * `query` - The graphql query.
36/// * `token` - The github token.
37/// * `owner` - The owner of the repo.
38/// * `repo` - The name of the repo.
39pub async fn gql_query(
40    client: &reqwest::Client,
41    query: &&str,
42    token: &String,
43    owner: &&str,
44    repo: &&str,
45) -> Result<serde_json::Value> {
46    client
47        .post(URL)
48        .bearer_auth(token)
49        .json(&serde_json::json!({
50            "query": query,
51            "variables": {
52                "owner": owner,
53                "name": repo,
54            }
55        }))
56        .send()
57        .await?
58        .json::<serde_json::Value>()
59        .await
60        .map_err(|e| eyre!(e))
61}