Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds multiple selection criteria, version output #14

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build and test

on:
pull_request:
workflow_dispatch:

jobs:
build_test:
strategy:
matrix:
os: [ubuntu-latest, ubuntu-20.04, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3

- name: setup rust stable
run: curl https://sh.rustup.rs -sSf | sh -s -- -y

- name: unit tests
run: |
cp tests/*.json .
cargo test --all --release
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pulse"
version = "0.0.3"
version = "0.0.4"
authors = ["Jerboa"]

edition="2021"
Expand Down Expand Up @@ -33,6 +33,7 @@ serde = {version="1.0", features=["derive"]}
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] }
regex = "1.10.2"
semver = "1.0.20"

[profile.dev]
opt-level = 0
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use semver::{BuildMetadata, Prerelease, Version};

pub mod web;
pub mod server;
pub mod stats;
pub mod util;

const MAJOR: &str = env!("CARGO_PKG_VERSION_MAJOR");
const MINOR: &str = env!("CARGO_PKG_VERSION_MINOR");
const PATCH: &str = env!("CARGO_PKG_VERSION_PATCH");

const DEBUG: bool = true;

pub fn debug(msg: String, context: Option<String>)
Expand All @@ -15,4 +21,16 @@ pub fn debug(msg: String, context: Option<String>)
None => println!("[DEBUG] {msg}")
}

}

pub fn program_version() -> Version
{
Version
{
major: MAJOR.parse().unwrap(),
minor: MINOR.parse().unwrap(),
patch: PATCH.parse().unwrap(),
pre: Prerelease::EMPTY,
build: BuildMetadata::EMPTY
}
}
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use pulse::server::https::Server;
use pulse::program_version;

#[tokio::main]
async fn main() {

let args: Vec<String> = std::env::args().collect();

if args.iter().any(|x| x == "-v")
{
println!("Version: {}", program_version());
std::process::exit(0);
}

let server = Server::new(0,0,0,0);

server.serve().await;
Expand Down
11 changes: 10 additions & 1 deletion src/main_http.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#[cfg(feature = "http")]

use pulse::{server::http::ServerHttp, stats};
use pulse::server::http::ServerHttp;
use pulse::program_version;

#[cfg(feature = "http")]
#[tokio::main]
async fn main() {

let args: Vec<String> = std::env::args().collect();

if args.iter().any(|x| x == "-v")
{
println!("Version: {}", program_version());
std::process::exit(0);
}

let server = ServerHttp::new(0,0,0,0);

server.serve().await;
Expand Down
94 changes: 69 additions & 25 deletions src/web/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,32 @@ pub const CONFIG_PATH: &str = "event_config.json";
const TEMPLATE_REPLACE_REGEX: &str = "<[^<>]+>";

#[derive(Clone, Serialize, Deserialize)]
pub struct Template
pub struct Criterion
{
#[serde(default)]
check_value_path: String,
#[serde(default)]
check_value: String,
check_value_in: Vec<String>,
#[serde(default)]
check_value_not_in: Vec<String>
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Template
{
#[serde(default="default_criteria")]
criteria: Vec<Criterion>,
#[serde(default)]
body: String
}

fn default_criteria() -> Vec<Criterion> { Vec::<Criterion>::new() }

impl Template
{
pub fn new() -> Template
{
Template {check_value_path: String::new(), check_value: String::new(), body: String::new()}
Template {criteria: Vec::<Criterion>::new(), body: String::new()}
}
}

Expand Down Expand Up @@ -171,40 +182,73 @@ pub fn expand_template(template: String, data: HashMap<String, serde_json::Value
Some(formatted)
}

pub fn select_template(templates: Vec<Template>, data: HashMap<String, serde_json::Value>) -> String
pub fn satisfied(criterion: Criterion, data: &HashMap<String, serde_json::Value>) -> bool
{
if templates.is_empty()
{
return "".to_string()
}
else if templates.len() == 1

if criterion.check_value_path == ""
{
return templates[0].body.clone()
return true
}

for template in templates
{
let path: Vec<&str> = template.check_value_path.split("/").collect();
let path: Vec<&str> = criterion.check_value_path.split("/").collect();

let extracted_value= match path.len()
let extracted_value= match path.len()
{
0 => None,
1 =>
{
if data.contains_key(path[0])
{
Some(&data[path[0]])
}
else
{
None
}
},
_ =>
{
0 => None,
1 => Some(&data[path[0]]),
_ =>
let p = ["/", &path[1..path.len()].join("/")].join("");
if data.contains_key(path[0])
{
let p = ["/", &path[1..path.len()].join("/")].join("");
data[path[0]].pointer(&p)
}
};

if extracted_value.is_none()
{
continue
else
{
None
}

}
};

if extracted_value.is_none()
{
return false
}

let string_value = extracted_value.unwrap().to_string().replace("\"", "");

if (criterion.check_value_in.is_empty() || criterion.check_value_in.contains(&string_value)) &&
(criterion.check_value_not_in.is_empty() || !criterion.check_value_not_in.contains(&string_value))
{
return true
}
else
{
return false
}
}

let string_value = extracted_value.unwrap().to_string().replace("\"", "");
pub fn select_template(templates: Vec<Template>, data: HashMap<String, serde_json::Value>) -> String
{
if templates.is_empty()
{
return "".to_string()
}

if string_value == template.check_value
for template in templates
{
if template.criteria.into_iter().all(|c| satisfied(c, &data))
{
return template.body
}
Expand Down
32 changes: 24 additions & 8 deletions tests/event_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
"templates":
[
{
"check_value_path": "",
"check_value": "",
"criteria":
[
{
"check_value_path": "",
"check_value_in": [],
"check_value_not_in": []
}
],
"body": "New release!"
}
],
Expand All @@ -19,13 +25,25 @@
"templates":
[
{
"check_value_path": "action",
"check_value": "created",
"criteria":
[
{
"check_value_path": "action",
"check_value_in": ["created"],
"check_value_not_in": []
}
],
"body": "<repository/name> just got a new star! That makes <repository/stargazers_count>"
},
{
"check_value_path": "action",
"check_value": "deleted",
"criteria":
[
{
"check_value_path": "action",
"check_value_in": ["deleted"],
"check_value_not_in": []
}
],
"body": "<repository/name> just lost a star :cry: That makes <repository/stargazers_count>"
}
],
Expand All @@ -45,8 +63,6 @@
"templates":
[
{
"check_value_path": "",
"check_value": "",
"body": "Ping!"
}
],
Expand Down
Loading
Loading