Skip to content

Commit

Permalink
feat: find out if the current approval has you own
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 7, 2024
1 parent 3d8f3ea commit 10e9860
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config = "github"
plugin = "github"
token = "ghp_4"
reviews = ["baerwang"]
owners = [{ name = "baerwang", repos = ["pika"] }]
Expand Down
2 changes: 1 addition & 1 deletion src/conf/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct ConfigData {
pub config: String,
pub plugin: String,
pub token: String,
pub reviews: Vec<String>,
pub owners: Vec<Owner>,
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ fn main() {
};

let token = config.token.as_str();
let plugin = config.config.as_str();
let plugin = config.plugin.as_str();

config.owners.iter().for_each(|owner| {
let api = get_api(plugin, owner.name.clone());
owner.repos.iter().for_each(|repo| {
_ = api.execute(token, repo.as_str());
})
});

for (org, repos) in &config.orgs {
let api = get_api(plugin, org.clone());
for repo in repos {
_ = api.execute(token, repo.as_str());
}
}
}
1 change: 1 addition & 0 deletions src/plugins/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub trait Api {
fn headers(&self, token: &str) -> HeaderMap;
fn repo(&self, repo: &str) -> String;
fn repos(&self) -> String;
fn org_repos(&self) -> String;
fn pull_requests(&self, repo: &str) -> String;
fn issues(&self, repo: &str) -> String;
fn reviews(&self, repo: &str, number: i64) -> String;
Expand Down
78 changes: 44 additions & 34 deletions src/plugins/github.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use std::string::String;

use reqwest::header::{HeaderMap, ACCEPT, AUTHORIZATION, USER_AGENT};
use serde_json::Value;
use serde::Deserialize;

use crate::plugins::api::Api;
use crate::plugins::client;

pub struct GitHub {
pub owner: Option<String>,
pub org: Option<String>,
pub owner: String,
}

impl GitHub {
pub fn new(owner: Option<String>, org: Option<String>) -> Self {
GitHub { owner, org }
pub fn new(owner: String) -> Self {
GitHub { owner }
}
}

fn name(&self) -> String {
match self.owner {
Some(ref s) => s.clone(),
None => match self.org {
Some(ref s) => s.clone(),
None => panic!("No owner or org specified"),
},
}
}
#[derive(Debug, Deserialize)]
struct User {
login: String,
}

#[derive(Debug, Deserialize)]
struct Reviews {
users: Vec<User>,
}

#[derive(Debug, Deserialize)]
struct PullRequest {
number: i64,
}

impl Api for GitHub {
Expand All @@ -34,15 +38,15 @@ impl Api for GitHub {

fn execute(&self, token: &str, repo: &str) -> Result<(), anyhow::Error> {
let rsp = client(self.pull_requests(repo), self.headers(token))?;
let parsed_json: Value = serde_json::from_str(&rsp)?;
if let Some(array) = parsed_json.as_array() {
for element in array {
if let Some(number) = element.get("number").and_then(|number| number.as_i64()) {
let rsp = client(self.reviews(repo, number), self.headers(token))?;
// todo
println!("{}", rsp)
let prs: Vec<PullRequest> = serde_json::from_str(&rsp)?;
for pr in prs {
let reviews_data = client(self.reviews(repo, pr.number), self.headers(token))?;
let reviews: Reviews = serde_json::from_str(&reviews_data)?;
reviews.users.iter().for_each(|user| {
if user.login == self.owner {
println!("{}: {}", repo, pr.number);
}
}
});
}
Ok(())
}
Expand All @@ -55,33 +59,39 @@ impl Api for GitHub {
headers
}

fn repo(&self, name: &str) -> String {
format!("{}/users/{}/repos?page=1&per_page=100", self.domain(), name)
fn repo(&self, repo: &str) -> String {
format!("{}/users/{repo}/repos?page=1&per_page=100", self.domain())
}

fn repos(&self) -> String {
match self.owner {
Some(ref s) => format!("{}/users/{}/repos?page=1&per_page=100", self.domain(), s),
None => match self.org {
Some(ref s) => format!("{}/orgs/{}/repos?page=1&per_page=100", self.domain(), s),
None => panic!("No owner or org specified"),
},
}
format!(
"{}/users/{}/repos?page=1&per_page=100",
self.domain(),
self.owner
)
}

fn org_repos(&self) -> String {
format!(
"{}/orgs/{}/repos?page=1&per_page=100",
self.domain(),
self.owner
)
}

fn pull_requests(&self, repo: &str) -> String {
format!("{}/repos/{}/{repo}/pulls", self.domain(), self.name())
format!("{}/repos/{}/{repo}/pulls", self.domain(), self.owner)
}

fn issues(&self, repo: &str) -> String {
format!("{}/repos/{}/{repo}/issues", self.domain(), self.name())
format!("{}/repos/{}/{repo}/issues", self.domain(), self.owner)
}

fn reviews(&self, repo: &str, number: i64) -> String {
format!(
"{}/repos/{}/{repo}/pulls/{number}/requested_reviewers",
self.domain(),
self.name()
self.owner
)
}
}
2 changes: 1 addition & 1 deletion src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod github;

pub fn get_api(api: &str, owner: String) -> Box<dyn Api> {
match api {
"github" => Box::new(github::GitHub::new(Some(owner), None)),
"github" => Box::new(github::GitHub::new(owner)),
_ => panic!("Unsupported"),
}
}
Expand Down

0 comments on commit 10e9860

Please sign in to comment.