github-readme-stats 0.2.0

Fetch GitHub user statistics as JSON
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
use anyhow::{Context, Result};
use chrono::{DateTime, FixedOffset, Utc};
use serde::Serialize;

use crate::api::http;
use crate::api::rest;
use crate::config::LanguageConfig;
use crate::models::{
    ContributionCalendar, ContributionDay, ContributionWeek, PinnedRepo, UserStats,
};

use super::models::{
    ContributionsCollectionNode, RepoResponse, RepositoriesNode, UserNode, UserResponse,
};
use super::retry::send_with_retry;

// Include GraphQL queries generated by build.rs from .graphql files
include!(concat!(env!("OUT_DIR"), "/queries.rs"));

/// Implements [[RFC-0002:C-GRAPHQL-CLIENT]]
#[derive(Clone)]
pub struct GraphQLClient {
    client: reqwest::Client,
    pinned_repos: Vec<(String, String)>, // (owner, name) pairs
    timezone_offset: FixedOffset,        // User's timezone for time distribution
    language_config: LanguageConfig,
}

const GRAPHQL_ENDPOINT: &str = "https://api.github.com/graphql";
const GRAPHQL_REPOS_PAGE_SIZE: usize = 100;

#[derive(Debug, Serialize)]
struct GraphQLRequest<'a> {
    query: &'a str,
    variables: serde_json::Value,
}

impl GraphQLClient {
    /// Create a new GraphQL client
    /// `pinned_repos_str` is a comma-separated list of "owner/repo" pairs
    /// `timezone_str` is an offset like "+08:00" or "-05:00" (default: "+00:00")
    pub fn new(
        token: String,
        pinned_repos_str: Option<String>,
        timezone_str: Option<String>,
    ) -> Self {
        let client = http::build_github_client(&token)
            .unwrap_or_else(|e| panic!("failed to build HTTP client: {e:#}"));
        let pinned_repos = pinned_repos_str
            .unwrap_or_default()
            .split(',')
            .filter_map(|s| {
                let s = s.trim();
                if s.is_empty() {
                    return None;
                }
                let parts: Vec<&str> = s.splitn(2, '/').collect();
                if parts.len() == 2 {
                    Some((parts[0].to_string(), parts[1].to_string()))
                } else {
                    eprintln!("warning: invalid pinned repo format: {s} (expected owner/repo)");
                    None
                }
            })
            .collect();

        // Parse timezone offset (e.g., "+08:00", "-05:00")
        let timezone_offset = parse_timezone_offset(timezone_str.as_deref());

        Self {
            client,
            pinned_repos,
            timezone_offset,
            language_config: LanguageConfig::default(),
        }
    }

    pub fn with_language_config(mut self, language_config: LanguageConfig) -> Self {
        self.language_config = language_config;
        self
    }

    /// Fetch all user stats via GraphQL
    /// Implements [[RFC-0002:C-GRAPHQL-CLIENT]]
    pub async fn fetch_stats(&self, username: &str) -> Result<UserStats> {
        let now = Utc::now();
        let one_year_ago = now - chrono::Duration::days(365);

        eprintln!("fetching user profile...");
        let user = self
            .fetch_user_with_repos(username, &one_year_ago, &now)
            .await?;

        let (total_stars, total_forks) = self.compute_repo_totals(&user.repositories.nodes);
        let (age_years, age_days) = self.compute_account_age(now, user.created_at);
        let calendar = self.build_calendar(&user.contributions_collection);
        let streaks = calendar.compute_streaks();

        eprintln!("fetching pinned repos ({})...", self.pinned_repos.len());
        let pinned_repos = self.fetch_pinned_repos(&one_year_ago, &user.id).await;

        eprintln!(
            "fetching commit sample (limit {})...",
            self.language_config.commits_limit
        );
        let commit_sample = match rest::fetch_commit_sample(
            &self.client,
            &user.login,
            self.language_config.commits_limit,
        )
        .await
        {
            Ok(commits) => Some(commits),
            Err(e) => {
                eprintln!("warning: failed to fetch commit sample: {e:#}");
                None
            }
        };

        eprintln!("computing time distribution...");
        let time_distribution = commit_sample
            .as_ref()
            .map(|commits| rest::compute_time_distribution(commits, self.timezone_offset));

        let commit_count = commit_sample.as_ref().map(|c| c.len()).unwrap_or(0);
        eprintln!("computing language usage ({commit_count} commits)...");
        let language_stats = if let Some(ref commits) = commit_sample {
            match rest::compute_language_usage(&self.client, commits, &self.language_config).await {
                Ok(result) if result.1 > 0 => Some(result),
                Ok(_) => None,
                Err(e) => {
                    eprintln!("warning: failed to compute language usage: {e:#}");
                    None
                }
            }
        } else {
            None
        };

        eprintln!("done.");
        let (first_issue, first_pr, first_repo) =
            self.first_contributions(&user.contributions_collection);

        Ok(UserStats {
            name: user.name,
            username: user.login,
            bio: user.bio,
            company: user.company,
            location: user.location,
            website_url: user.website_url,
            twitter_username: user.twitter_username,
            avatar_url: user.avatar_url,
            organizations: Some(user.organizations.total_count),
            repos: user.repositories.total_count,
            stars: total_stars,
            forks: total_forks,
            followers: user.followers.total_count,
            commits: user.contributions_collection.total_commit_contributions,
            prs: user
                .contributions_collection
                .total_pull_request_contributions,
            issues: user.contributions_collection.total_issue_contributions,
            total_repository_contributions: Some(
                user.contributions_collection.total_repository_contributions,
            ),
            restricted_contributions: Some(
                user.contributions_collection.restricted_contributions_count,
            ),
            contribution_years: Some(user.contributions_collection.contribution_years),
            first_issue_contribution: first_issue,
            first_pull_request_contribution: first_pr,
            first_repository_contribution: first_repo,
            account_age_years: age_years,
            account_age_days: age_days,
            contribution_calendar: Some(calendar),
            streaks: Some(streaks),
            pinned_repos,
            time_distribution,
            language_usage: language_stats.as_ref().map(|v| v.0.clone()),
            language_total_changes: language_stats.as_ref().map(|v| v.1),
            language_sampled_commits: language_stats.as_ref().map(|v| v.2),
        })
    }

    fn compute_repo_totals(&self, repos: &[super::models::RepoStatsNode]) -> (u64, u64) {
        let stars = repos.iter().map(|r| r.stargazer_count).sum();
        let forks = repos.iter().map(|r| r.fork_count).sum();
        (stars, forks)
    }

    fn compute_account_age(&self, now: DateTime<Utc>, created_at: DateTime<Utc>) -> (u64, u64) {
        let age = now.signed_duration_since(created_at);
        let age_days = age.num_days().max(0) as u64;
        let age_years = age_days / 365;
        (age_years, age_days)
    }

    fn build_calendar(&self, collection: &ContributionsCollectionNode) -> ContributionCalendar {
        ContributionCalendar {
            total_contributions: collection.contribution_calendar.total_contributions,
            weeks: collection
                .contribution_calendar
                .weeks
                .iter()
                .map(|w| ContributionWeek {
                    days: w
                        .contribution_days
                        .iter()
                        .map(|d| ContributionDay {
                            date: d.date.clone(),
                            contribution_count: d.contribution_count,
                            level: level_from_string(&d.contribution_level),
                        })
                        .collect(),
                })
                .collect(),
        }
    }

    fn first_contributions(
        &self,
        collection: &ContributionsCollectionNode,
    ) -> (Option<String>, Option<String>, Option<String>) {
        let first_issue = collection
            .first_issue_contribution
            .as_ref()
            .map(|c| c.occurred_at.format("%Y-%m-%d").to_string());
        let first_pr = collection
            .first_pull_request_contribution
            .as_ref()
            .map(|c| c.occurred_at.format("%Y-%m-%d").to_string());
        let first_repo = collection
            .first_repository_contribution
            .as_ref()
            .map(|c| c.occurred_at.format("%Y-%m-%d").to_string());
        (first_issue, first_pr, first_repo)
    }

    async fn fetch_user_with_repos(
        &self,
        username: &str,
        from: &DateTime<Utc>,
        to: &DateTime<Utc>,
    ) -> Result<UserNode> {
        let mut repo_after: Option<String> = None;
        let mut combined: Option<UserNode> = None;

        loop {
            let page = self
                .fetch_user_page(username, from, to, repo_after.as_deref())
                .await?;
            if let Some(ref mut user) = combined {
                merge_repository_page(&mut user.repositories, page.repositories);
            } else {
                combined = Some(page);
            }

            let page_info = combined
                .as_ref()
                .map(|u| u.repositories.page_info.clone())
                .unwrap();
            if !page_info.has_next_page {
                break;
            }
            repo_after = page_info.end_cursor;
            if repo_after.is_none() {
                break;
            }
        }

        combined.context("User not found")
    }

    async fn fetch_user_page(
        &self,
        username: &str,
        from: &DateTime<Utc>,
        to: &DateTime<Utc>,
        repo_after: Option<&str>,
    ) -> Result<UserNode> {
        let variables = serde_json::json!({
            "login": username,
            "from": from.to_rfc3339(),
            "to": to.to_rfc3339(),
            "repoAfter": repo_after,
            "repoFirst": GRAPHQL_REPOS_PAGE_SIZE,
        });

        let request = GraphQLRequest {
            query: USER_QUERY,
            variables,
        };

        let response = send_with_retry(
            || self.client.post(GRAPHQL_ENDPOINT).json(&request),
            "GraphQL user request",
        )
        .await?;

        let gql_response: UserResponse = response
            .json()
            .await
            .context("Failed to parse user GraphQL response")?;

        if let Some(errors) = gql_response.errors {
            let msgs: Vec<_> = errors.iter().map(|e| e.message.as_str()).collect();
            anyhow::bail!("GraphQL errors: {}", msgs.join(", "));
        }

        gql_response
            .data
            .and_then(|d| d.user)
            .context("User not found")
    }

    async fn fetch_pinned_repos(
        &self,
        since: &DateTime<Utc>,
        author_id: &str,
    ) -> Option<Vec<PinnedRepo>> {
        if self.pinned_repos.is_empty() {
            return None;
        }

        let mut join_set = tokio::task::JoinSet::new();
        for (index, (owner, name)) in self.pinned_repos.iter().enumerate() {
            let client = self.clone();
            let owner = owner.clone();
            let name = name.clone();
            let since = *since;
            let author_id = author_id.to_string();
            join_set.spawn(async move {
                client
                    .fetch_repo(&owner, &name, &since, &author_id)
                    .await
                    .map(|repo| (index, repo))
            });
        }

        let mut repos = Vec::new();
        while let Some(result) = join_set.join_next().await {
            match result {
                Ok(Ok((index, repo))) => repos.push((index, repo)),
                Ok(Err(e)) => eprintln!("warning: failed to fetch pinned repo: {e:#}"),
                Err(e) => eprintln!("warning: failed to join pinned repo task: {e:#}"),
            }
        }

        if repos.is_empty() {
            None
        } else {
            repos.sort_by_key(|(index, _)| *index);
            Some(repos.into_iter().map(|(_, repo)| repo).collect())
        }
    }

    async fn fetch_repo(
        &self,
        owner: &str,
        name: &str,
        since: &DateTime<Utc>,
        author_id: &str,
    ) -> Result<PinnedRepo> {
        let variables = serde_json::json!({
            "owner": owner,
            "name": name,
            "since": since.to_rfc3339(),
            "authorId": author_id,
        });

        let request = GraphQLRequest {
            query: REPO_QUERY,
            variables,
        };

        let response = send_with_retry(
            || self.client.post(GRAPHQL_ENDPOINT).json(&request),
            "GraphQL repo request",
        )
        .await?;

        let gql_response: RepoResponse = response
            .json()
            .await
            .context("Failed to parse repo GraphQL response")?;

        if let Some(errors) = gql_response.errors {
            let msgs: Vec<_> = errors.iter().map(|e| e.message.as_str()).collect();
            anyhow::bail!("GraphQL errors: {}", msgs.join(", "));
        }

        let repo = gql_response
            .data
            .and_then(|d| d.repository)
            .context("Repository not found")?;

        let (additions, deletions, commits, last_date) =
            if let Some(ref branch) = repo.default_branch_ref {
                let history = branch.target.history.as_ref();
                let adds: u64 = history
                    .map(|h| h.nodes.iter().map(|c| c.additions).sum())
                    .unwrap_or(0);
                let dels: u64 = history
                    .map(|h| h.nodes.iter().map(|c| c.deletions).sum())
                    .unwrap_or(0);
                let count = history.map(|h| h.total_count).unwrap_or(0);
                // Get user's last commit date from the first node (most recent)
                let date = history
                    .and_then(|h| h.nodes.first())
                    .map(|c| c.committed_date.format("%Y-%m-%d").to_string());
                (adds, dels, count, date)
            } else {
                (0, 0, 0, None)
            };

        let topics: Vec<String> = repo
            .repository_topics
            .nodes
            .iter()
            .map(|n| n.topic.name.clone())
            .collect();
        let topics = if topics.is_empty() {
            None
        } else {
            Some(topics)
        };

        Ok(PinnedRepo {
            name: repo.name,
            description: repo.description,
            stars: repo.stargazer_count,
            forks: repo.fork_count,
            watchers: repo.watchers.total_count,
            issues: repo.issues.total_count,
            pull_requests: repo.pull_requests.total_count,
            releases: repo.releases.total_count,
            license: repo.license_info.and_then(|l| l.spdx_id),
            topics,
            language: repo.primary_language.as_ref().map(|l| l.name.clone()),
            language_color: repo.primary_language.and_then(|l| l.color),
            is_archived: repo.is_archived,
            is_fork: repo.is_fork,
            is_template: repo.is_template,
            disk_usage: repo.disk_usage,
            default_branch: repo.default_branch_ref.as_ref().map(|b| b.name.clone()),
            recent_additions: additions,
            recent_deletions: deletions,
            recent_commits: commits,
            last_commit_date: last_date,
        })
    }
}

fn parse_timezone_offset(input: Option<&str>) -> FixedOffset {
    input
        .and_then(|s| s.parse::<FixedOffset>().ok())
        .unwrap_or_else(|| FixedOffset::east_opt(0).unwrap())
}

fn merge_repository_page(target: &mut RepositoriesNode, page: RepositoriesNode) {
    target.nodes.extend(page.nodes);
    target.total_count = page.total_count;
    target.page_info = page.page_info;
}

/// Convert GitHub's contribution level string to a numeric level (0-4)
/// Implements [[RFC-0002:C-CONTRIBUTION-CALENDAR]]
fn level_from_string(level: &str) -> u8 {
    match level {
        "NONE" => 0,
        "FIRST_QUARTILE" => 1,
        "SECOND_QUARTILE" => 2,
        "THIRD_QUARTILE" => 3,
        "FOURTH_QUARTILE" => 4,
        _ => 0,
    }
}

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

    #[test]
    fn parse_timezone_offset_defaults_to_utc() {
        let offset = parse_timezone_offset(Some("invalid"));
        assert_eq!(offset.local_minus_utc(), 0);
    }

    #[test]
    fn parse_timezone_offset_parses_valid() {
        let offset = parse_timezone_offset(Some("+08:00"));
        assert_eq!(offset.local_minus_utc(), 8 * 3600);
    }

    #[test]
    fn merge_repository_page_appends_nodes() {
        let mut target = RepositoriesNode {
            total_count: 1,
            nodes: vec![crate::api::graphql::models::RepoStatsNode {
                stargazer_count: 1,
                fork_count: 2,
            }],
            page_info: crate::api::graphql::models::PageInfoNode {
                has_next_page: true,
                end_cursor: Some("cursor".to_string()),
            },
        };

        let page = RepositoriesNode {
            total_count: 2,
            nodes: vec![crate::api::graphql::models::RepoStatsNode {
                stargazer_count: 3,
                fork_count: 4,
            }],
            page_info: crate::api::graphql::models::PageInfoNode {
                has_next_page: false,
                end_cursor: None,
            },
        };

        merge_repository_page(&mut target, page);
        assert_eq!(target.total_count, 2);
        assert_eq!(target.nodes.len(), 2);
        assert!(!target.page_info.has_next_page);
    }
}