|
| 1 | +use std::env; |
| 2 | + |
| 3 | +use chrono::{DateTime, FixedOffset}; |
| 4 | +use reqwest::blocking::Client; |
| 5 | +use serde_json::{Value, json}; |
| 6 | +use dotenv::dotenv; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + dotenv().ok(); |
| 10 | + let api_key = env::var("API_KEY").unwrap(); |
| 11 | + let server_url = env::var("URL").unwrap(); |
| 12 | + |
| 13 | + let repolist=get_repos(&server_url, "12core1", &api_key); |
| 14 | + println!("{:?}",repolist.len()); |
| 15 | + for i in get_recent_commits(&server_url, "12core1", &api_key){ |
| 16 | + println!("{}",i.time) |
| 17 | + } |
| 18 | +} |
| 19 | +fn get_repos(server_url: &str, user: &str, access_token: &str) -> Vec<String> { |
| 20 | + let client = Client::new(); |
| 21 | + |
| 22 | + let url = format!("{}/api/v1/users/{}/repos?access_token={}", server_url, user, access_token); |
| 23 | + // println!("{}",url); |
| 24 | + let responsept = client.get(&url).send().unwrap(); |
| 25 | + // println!("{:?}",responsept); |
| 26 | + let response = responsept.json::<Vec<Value>>().unwrap(); |
| 27 | + response.into_iter().map(|repo| repo["full_name"].as_str().unwrap().to_string()).collect() |
| 28 | +} |
| 29 | +struct commits{ |
| 30 | + additions:u64, |
| 31 | + deletions:u64, |
| 32 | + total:u64, |
| 33 | + message:String, |
| 34 | + time:DateTime<FixedOffset>, |
| 35 | + committer:String, |
| 36 | + commit:String, |
| 37 | +} |
| 38 | +fn get_commits(server_url: &str, repo: &str, access_token: &str) -> Vec<commits> { |
| 39 | + let client = Client::new(); |
| 40 | + let url = format!("{}/api/v1/repos/{}/commits?access_token={}", server_url, repo, access_token); |
| 41 | + // println!("{:?}",url); |
| 42 | + //TODO: skip empty repo |
| 43 | + let mut y:Vec<commits>=Vec::new(); |
| 44 | + match client.get(&url).send().unwrap().json::<Vec<Value>>(){ |
| 45 | + Ok(response) => { |
| 46 | + y=response.into_iter().map(|commit|{ |
| 47 | + commits{ |
| 48 | + |
| 49 | + additions:json!(commit["stats"])["additions"].as_u64().unwrap(), |
| 50 | + deletions:json!(commit["stats"])["deletions"].as_u64().unwrap(), |
| 51 | + total:json!(commit["stats"])["total"].as_u64().unwrap(), |
| 52 | + message:json!(commit["commit"])["message"].as_str().unwrap().to_string(), |
| 53 | + time:{ |
| 54 | + // format!("{}", |
| 55 | + match&(commit["created"]){ |
| 56 | + Value::String(date_string) => { |
| 57 | + let date_time = DateTime::parse_from_str(&date_string, "%Y-%m-%dT%H:%M:%S%z") |
| 58 | + .unwrap() |
| 59 | + .with_timezone(&FixedOffset::east_opt(5*3600+30*60).unwrap()); |
| 60 | + date_time |
| 61 | + }, |
| 62 | + _ => { |
| 63 | + DateTime::default() |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + // ) |
| 68 | + }, |
| 69 | + committer:format!("{}",json!(commit["committer"])["username"]), |
| 70 | + commit:commit["html_url"].as_str().unwrap().to_string(), |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + // print_key_value_pairs(&commit); |
| 75 | + // "".to_string() |
| 76 | + } |
| 77 | + ) |
| 78 | + // .take(2) |
| 79 | + .collect(); |
| 80 | + }, |
| 81 | + Err(err) => { |
| 82 | + eprintln!("Failed to deserialize JSON response: {}", err); |
| 83 | + }, |
| 84 | + } |
| 85 | + y |
| 86 | + |
| 87 | +} |
| 88 | +fn print_key_value_pairs(value: &Value) { |
| 89 | + if let Some(object) = value.as_object() { |
| 90 | + for (key, value) in object.iter() { |
| 91 | + println!("{}: {}", key, value); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +fn get_recent_commits(server_url: &str, user: &str, access_token: &str) -> Vec<commits> { |
| 96 | + let repos = get_repos(server_url, user, access_token); |
| 97 | + let mut commits = Vec::new(); |
| 98 | + for repo in repos { |
| 99 | + commits.extend(get_commits(server_url, &repo, access_token)); |
| 100 | + } |
| 101 | + commits.sort_by(|a, b| b.time.cmp(&a.time)); |
| 102 | + commits.into_iter().collect() |
| 103 | +} |
0 commit comments