-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use anyhow::{anyhow, Result}; | ||
use chrono::{DateTime, Utc}; | ||
use reqwest::Client; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct UpstreamVersion { | ||
pub tag_name: String, | ||
pub target_commitish: Option<String>, | ||
pub published_at: DateTime<Utc>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct RepoCommit { | ||
pub commit: Commit, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Commit { | ||
pub author: CommitAuthor, | ||
pub message: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct CommitAuthor { | ||
pub name: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct ErrorResponse { | ||
pub message: String, | ||
pub documentation_url: String, | ||
} | ||
|
||
pub async fn get_upstream_nightly(client: &Client) -> Result<UpstreamVersion> { | ||
let response = client | ||
.get("https://api.github.com/repos/neovim/neovim/releases/tags/nightly") | ||
.header("user-agent", "bob") | ||
.header("Accept", "application/vnd.github.v3+json") | ||
.send() | ||
.await? | ||
.text() | ||
.await?; | ||
|
||
deserialize_response(response) | ||
} | ||
|
||
pub async fn get_commits_for_nightly( | ||
client: &Client, | ||
since: &DateTime<Utc>, | ||
until: &DateTime<Utc>, | ||
) -> Result<Vec<RepoCommit>> { | ||
let response = client | ||
.get(format!( | ||
"https://api.github.com/repos/neovim/neovim/commits?since={since}&until={until}&per_page=100")) | ||
.header("user-agent", "bob") | ||
.header("Accept", "application/vnd.github.v3+json") | ||
.send() | ||
.await? | ||
.text() | ||
.await?; | ||
|
||
deserialize_response(response) | ||
} | ||
|
||
pub fn deserialize_response<T: DeserializeOwned>(response: String) -> Result<T> { | ||
let value: serde_json::Value = serde_json::from_str(&response)?; | ||
|
||
if let Some(_) = value.get("message") { | ||
Check failure on line 69 in src/github_requests.rs GitHub Actions / linux
Check failure on line 69 in src/github_requests.rs GitHub Actions / linux
Check failure on line 69 in src/github_requests.rs GitHub Actions / macos
Check failure on line 69 in src/github_requests.rs GitHub Actions / macos-m1
Check failure on line 69 in src/github_requests.rs GitHub Actions / macos
Check failure on line 69 in src/github_requests.rs GitHub Actions / linux-arm
Check failure on line 69 in src/github_requests.rs GitHub Actions / windows
|
||
let result: ErrorResponse = serde_json::from_value(value)?; | ||
|
||
if result.documentation_url.contains("rate-limiting") { | ||
return Err(anyhow!("Github API rate limit has been reach, either wait an hour or checkout https://github.com/MordechaiHadad/bob#increasing-github-rate-limit")); | ||
} | ||
|
||
return Err(anyhow!(result.message)); | ||
} | ||
|
||
Ok(serde_json::from_value(value)?) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ mod cli; | |
mod config; | ||
mod handlers; | ||
mod helpers; | ||
pub mod github_requests; | ||
|
||
extern crate core; | ||
|
||
|