Something went wrong. Try again.
forked from defelo/nixpkgs-review-gha
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128use serde::{Deserialize, Serialize};
pub mod oidc;
const USER_AGENT: &str = concat!( env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"), " (https://github.com/Defelo/nixpkgs-review-gha)");
#[derive(Debug, Deserialize)]pub struct PullRequest { pub user: User,}
pub async fn get_nixpkgs_pr(token: &str, pr: u64) -> anyhow::Result<PullRequest> { Ok(make_http_client() .get(format!( "https://api.github.com/repos/NixOS/nixpkgs/pulls/{pr}" )) .bearer_auth(token) .send() .await? .error_for_status()? .json() .await?)}
pub async fn post_nixpkgs_comment(token: &str, pr: u64, body: &str) -> anyhow::Result<String> { #[derive(Serialize)] struct Request<'a> { body: &'a str, }
#[derive(Deserialize)] struct Response { html_url: String, }
Ok(make_http_client() .post(format!( "https://api.github.com/repos/NixOS/nixpkgs/issues/{pr}/comments" )) .bearer_auth(token) .json(&Request { body }) .send() .await? .error_for_status()? .json::<Response>() .await? .html_url)}
pub async fn approve_nixpkgs_pr( token: &str, pr: u64, commit_id: &str, body: &str,) -> anyhow::Result<String> { #[derive(Serialize)] struct Request<'a> { commit_id: &'a str, body: &'a str, event: Event, }
#[derive(Serialize)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] enum Event { Approve, }
#[derive(Deserialize)] struct Response { html_url: String, }
Ok(make_http_client() .post(format!( "https://api.github.com/repos/NixOS/nixpkgs/pulls/{pr}/reviews" )) .bearer_auth(token) .json(&Request { commit_id, body, event: Event::Approve, }) .send() .await? .error_for_status()? .json::<Response>() .await? .html_url)}
#[derive(Debug, Deserialize)]pub struct User { pub login: String, pub id: u64,}
pub async fn get_self(token: &str) -> anyhow::Result<User> { Ok(make_http_client() .get("https://api.github.com/user") .bearer_auth(token) .send() .await? .error_for_status()? .json() .await?)}
fn make_http_client() -> reqwest::Client { reqwest::Client::builder() .user_agent(USER_AGENT) .build() .unwrap()}
#[cfg(test)]mod tests { #[test] fn make_http_client() { super::make_http_client(); }}