Skip to content

Commit

Permalink
Merge pull request #15 from JerboaBurrow/add_fork_issue_pr
Browse files Browse the repository at this point in the history
adds fork event
  • Loading branch information
Jerboa-app authored Feb 4, 2024
2 parents 1e3cded + ec0ecbd commit 0195ec9
Show file tree
Hide file tree
Showing 9 changed files with 1,134 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pulse"
version = "0.0.4"
version = "0.0.5"
authors = ["Jerboa"]

edition="2021"
Expand Down
15 changes: 14 additions & 1 deletion src/web/github/response/github_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ response::

use crate::web::is_authentic;

use super::github_ping;
use super::{github_forked, github_issue, github_ping, github_pull_request};

/// Middleware to detect, verify, and respond to a github POST request from a
/// Github webhook
Expand Down Expand Up @@ -130,6 +130,7 @@ where B: axum::body::HttpBody<Data = Bytes>
{
true =>
{
crate::debug(format!("x-github-event: {:?}", headers["x-github-event"]), None);
match std::str::from_utf8(headers["x-github-event"].as_bytes()).unwrap()
{
github_pushed::X_GTIHUB_EVENT =>
Expand All @@ -147,6 +148,18 @@ where B: axum::body::HttpBody<Data = Bytes>
github_ping::X_GTIHUB_EVENT =>
{
Box::new(github_ping::GithubPing::new())
},
github_forked::X_GTIHUB_EVENT =>
{
Box::new(github_forked::GithubForked::new())
},
github_pull_request::X_GTIHUB_EVENT =>
{
Box::new(github_pull_request::GithubPullRequest::new())
},
github_issue::X_GTIHUB_EVENT =>
{
Box::new(github_issue::GithubIssue::new())
}
_ => return Ok(StatusCode::CONTINUE.into_response())
}
Expand Down
76 changes: 76 additions & 0 deletions src/web/github/response/github_forked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::collections::HashMap;

use axum::http::{HeaderMap, StatusCode};
use axum::body::Bytes;

use crate::web::discord;
use crate::web::event::{EventConfig, read_config, select_template, expand_template};
use crate::web::event::Event;

use super::github_filter::github_request_is_authentic;

pub const X_GTIHUB_EVENT: &str = "fork";

pub struct GithubForked
{
config: EventConfig
}

impl GithubForked
{
pub fn new() -> GithubForked
{
GithubForked { config: EventConfig::new() }
}
}

impl Event for GithubForked
{
fn get_token(&self) -> String
{
self.config.get_token()
}

fn get_end_point(&self) -> discord::request::model::Webhook
{
self.config.get_end_point()
}

fn load_config(&mut self)
{
self.config = read_config("github_forked");
}

fn is_authentic(&self, headers: HeaderMap, body: Bytes) -> StatusCode
{
return github_request_is_authentic(self.get_token(), headers, body);
}

fn into_response(&self, data: HashMap<String, serde_json::Value>) -> (Option<String>, StatusCode)
{

let template = select_template(self.config.get_templates(), data.clone());

let template = if data["repository"]["name"].is_string()
{
match data["repository"]["name"] == "Pulse"
{
true => template.replacen("<repository/name>", "Pulse (that's me!)", 1),
false => {template}
}
}
else
{
return (None, StatusCode::INTERNAL_SERVER_ERROR)
};

crate::debug(format!("Fork from {:?}", data["sender"]["login"]), None);

if self.config.silent_on_private_repos() && data["repository"]["private"].as_bool().is_some_and(|x|x)
{
return (None, StatusCode::OK);
}

(expand_template(template, data), StatusCode::OK)
}
}
76 changes: 76 additions & 0 deletions src/web/github/response/github_issue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::collections::HashMap;

use axum::http::{HeaderMap, StatusCode};
use axum::body::Bytes;

use crate::web::discord;
use crate::web::event::{EventConfig, read_config, select_template, expand_template};
use crate::web::event::Event;

use super::github_filter::github_request_is_authentic;

pub const X_GTIHUB_EVENT: &str = "issues";

pub struct GithubIssue
{
config: EventConfig
}

impl GithubIssue
{
pub fn new() -> GithubIssue
{
GithubIssue { config: EventConfig::new() }
}
}

impl Event for GithubIssue
{
fn get_token(&self) -> String
{
self.config.get_token()
}

fn get_end_point(&self) -> discord::request::model::Webhook
{
self.config.get_end_point()
}

fn load_config(&mut self)
{
self.config = read_config("github_issue");
}

fn is_authentic(&self, headers: HeaderMap, body: Bytes) -> StatusCode
{
return github_request_is_authentic(self.get_token(), headers, body);
}

fn into_response(&self, data: HashMap<String, serde_json::Value>) -> (Option<String>, StatusCode)
{

let template = select_template(self.config.get_templates(), data.clone());

let template = if data["repository"]["name"].is_string()
{
match data["repository"]["name"] == "Pulse"
{
true => template.replacen("<repository/name>", "Pulse (that's me!)", 1),
false => {template}
}
}
else
{
return (None, StatusCode::INTERNAL_SERVER_ERROR)
};

crate::debug(format!("issue from {:?}", data["sender"]["login"]), None);

if self.config.silent_on_private_repos() && data["repository"]["private"].as_bool().is_some_and(|x|x)
{
return (None, StatusCode::OK);
}

(expand_template(template, data), StatusCode::OK)
}
}
76 changes: 76 additions & 0 deletions src/web/github/response/github_pull_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::collections::HashMap;

use axum::http::{HeaderMap, StatusCode};
use axum::body::Bytes;

use crate::web::discord;
use crate::web::event::{EventConfig, read_config, select_template, expand_template};
use crate::web::event::Event;

use super::github_filter::github_request_is_authentic;

pub const X_GTIHUB_EVENT: &str = "pull_request";

pub struct GithubPullRequest
{
config: EventConfig
}

impl GithubPullRequest
{
pub fn new() -> GithubPullRequest
{
GithubPullRequest { config: EventConfig::new() }
}
}

impl Event for GithubPullRequest
{
fn get_token(&self) -> String
{
self.config.get_token()
}

fn get_end_point(&self) -> discord::request::model::Webhook
{
self.config.get_end_point()
}

fn load_config(&mut self)
{
self.config = read_config("github_pull_request");
}

fn is_authentic(&self, headers: HeaderMap, body: Bytes) -> StatusCode
{
return github_request_is_authentic(self.get_token(), headers, body);
}

fn into_response(&self, data: HashMap<String, serde_json::Value>) -> (Option<String>, StatusCode)
{

let template = select_template(self.config.get_templates(), data.clone());

let template = if data["repository"]["name"].is_string()
{
match data["repository"]["name"] == "Pulse"
{
true => template.replacen("<repository/name>", "Pulse (that's me!)", 1),
false => {template}
}
}
else
{
return (None, StatusCode::INTERNAL_SERVER_ERROR)
};

crate::debug(format!("pull_request from {:?}", data["sender"]["login"]), None);

if self.config.silent_on_private_repos() && data["repository"]["private"].as_bool().is_some_and(|x|x)
{
return (None, StatusCode::OK);
}

(expand_template(template, data), StatusCode::OK)
}
}
3 changes: 3 additions & 0 deletions src/web/github/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ pub mod github_release;
pub mod github_starred;
pub mod github_pushed;
pub mod github_ping;
pub mod github_forked;
pub mod github_pull_request;
pub mod github_issue;
pub mod model;
Loading

0 comments on commit 0195ec9

Please sign in to comment.