Skip to content

Commit

Permalink
feat: config file extend for uses
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 7, 2024
1 parent 19b2384 commit 3d8f3ea
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
Expand Down
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
anyhow = "1.0"
toml = "0.8.8"
5 changes: 5 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config = "github"
token = "ghp_4"
reviews = ["baerwang"]
owners = [{ name = "baerwang", repos = ["pika"] }]
orgs = { }
18 changes: 18 additions & 0 deletions src/conf/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::collections::HashMap;

use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct ConfigData {
pub config: String,
pub token: String,
pub reviews: Vec<String>,
pub owners: Vec<Owner>,
pub orgs: HashMap<String, Vec<String>>,
}

#[derive(Deserialize, Debug)]
pub struct Owner {
pub name: String,
pub repos: Vec<String>,
}
18 changes: 18 additions & 0 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub mod config;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
* limitations under the License.
*/

pub mod conf;
pub mod plugins;
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,33 @@
* limitations under the License.
*/

use std::env;
use std::fs;

use flexible::conf::config::ConfigData;
use flexible::plugins::get_api;

fn main() {
let api = get_api("github", "baerwang".to_string());
_ = api.execute(env::var("token").unwrap().as_str());
let toml_data = match fs::read_to_string("config.toml") {
Ok(content) => content,
Err(err) => {
panic!("Failed to read TOML file: {}", err);
}
};

let config: ConfigData = match toml::from_str(&toml_data) {
Ok(parsed) => parsed,
Err(err) => {
panic!("Failed to parse TOML data: {}", err);
}
};

let token = config.token.as_str();
let plugin = config.config.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());
})
});
}
7 changes: 3 additions & 4 deletions src/plugins/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
use reqwest::header::HeaderMap;

pub trait Api {
fn domain(&self) -> &str {
"https://api.github.com"
}
fn execute(&self, token: &str) -> Result<(), anyhow::Error>;
fn domain(&self) -> &str;
fn execute(&self, token: &str, repo: &str) -> Result<(), anyhow::Error>;
fn headers(&self, token: &str) -> HeaderMap;
fn repo(&self, repo: &str) -> String;
fn repos(&self) -> String;
fn pull_requests(&self, repo: &str) -> String;
fn issues(&self, repo: &str) -> String;
Expand Down
32 changes: 15 additions & 17 deletions src/plugins/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,19 @@ impl GitHub {
}

impl Api for GitHub {
fn execute(&self, token: &str) -> Result<(), anyhow::Error> {
let resp = client(self.repos(), self.headers(token))?;
let parsed_json: Value = serde_json::from_str(&resp)?;
fn domain(&self) -> &str {
"https://api.github.com"
}

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(name) = element.get("name").and_then(|name| name.as_str()) {
let rsp = client(self.pull_requests(name), 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(name, number), self.headers(token))?;
// todo
println!("{}", rsp)
}
}
}
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)
}
}
}
Expand All @@ -61,6 +55,10 @@ impl Api for GitHub {
headers
}

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

fn repos(&self) -> String {
match self.owner {
Some(ref s) => format!("{}/users/{}/repos?page=1&per_page=100", self.domain(), s),
Expand Down

0 comments on commit 3d8f3ea

Please sign in to comment.