Skip to content

Commit

Permalink
feat(server): Support reply ping event
Browse files Browse the repository at this point in the history
Signed-off-by: KunoiSayami <[email protected]>
  • Loading branch information
KunoiSayami committed Dec 12, 2021
1 parent 44a460e commit 3d0669f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "github-webhook-notification"
version = "1.1.1"
version = "1.1.2"
edition = "2021"

[dependencies]
Expand All @@ -10,7 +10,7 @@ actix-web = "4.0.0-beta.3"
anyhow = "1"
clap = "2"
env_logger = "0.8"
hmac = "0.12.0"
hmac = "0.12"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1"
Expand Down
22 changes: 17 additions & 5 deletions src/datastructures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ use std::fmt::Formatter;
use std::ops::Index;

#[derive(Deserialize, Serialize, Debug)]
pub struct GitHubRequest {
pub struct GitHubPingEvent {
zen: String,
}

impl GitHubPingEvent {
pub fn zen(&self) -> &str {
&self.zen
}
}


#[derive(Deserialize, Serialize, Debug)]
pub struct GitHubPushEvent {
#[serde(rename = "ref")]
remote_ref: String,
after: String,
Expand All @@ -33,7 +45,7 @@ pub struct GitHubRequest {
repository: Repository,
}

impl GitHubRequest {
impl GitHubPushEvent {
pub fn remote_ref(&self) -> &str {
&self.remote_ref
}
Expand All @@ -54,7 +66,7 @@ impl GitHubRequest {
}
}

impl std::fmt::Display for GitHubRequest {
impl std::fmt::Display for GitHubPushEvent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let branch = self.remote_ref().rsplit_once("/").unwrap().1;
let git_ref = format!("{}:{}", self.repository(), branch);
Expand Down Expand Up @@ -161,11 +173,11 @@ impl Response {
Self::new(200)
}

pub fn reason(status: i64, reason: &str) -> Self {
pub fn reason<T: Into<String>>(status: i64, reason: T) -> Self {
Self {
version: env!("CARGO_PKG_VERSION").to_string(),
status,
reason: reason.to_string(),
reason: reason.into(),
}
}
}
Expand Down
46 changes: 34 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
** along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::datastructures::{GitHubRequest, Response};
use crate::datastructures::{GitHubPingEvent, GitHubPushEvent, Response};
use crate::Command::Text;
use actix_web::http::Method;
use actix_web::web::Data;
Expand Down Expand Up @@ -121,18 +121,40 @@ async fn route_post(
}
}

let request_body = serde_json::from_slice::<GitHubRequest>(&body)?;
if request_body.after().starts_with("000000000000")
|| request_body.before().starts_with("000000000000")
{
return Ok(HttpResponse::NoContent().finish());
if let Some(event) = request.headers().get("X-GitHub-Event") {
let event = event.to_str();
if let Err(ref e) = event {
error!("Parse X-GitHub-Event error: {:?}", e);
return Ok(HttpResponse::InternalServerError().finish())
}
let event = event.unwrap();
match event {
"ping" => {
let request_body = serde_json::from_slice::<GitHubPingEvent>(&body)?;
Ok(HttpResponse::Ok().json(Response::reason(200, request_body.zen())))
}
"push" => {
let request_body = serde_json::from_slice::<GitHubPushEvent>(&body)?;
if request_body.after().starts_with("000000000000")
|| request_body.before().starts_with("000000000000")
{
return Ok(HttpResponse::NoContent().finish());
}
sender
.bot_tx
.send(Text(request_body.to_string()))
.await
.unwrap();
Ok(HttpResponse::Ok().json(Response::new_ok()))
}
_ => {
Ok(HttpResponse::BadRequest().json(Response::reason(400, format!( "Unsupported event type {:?}", event))))
}
}
} else {
error!("Unknown request: {:?}", request);
Ok(HttpResponse::InternalServerError().finish())
}
sender
.bot_tx
.send(Text(request_body.to_string()))
.await
.unwrap();
Ok(HttpResponse::Ok().json(Response::new_ok()))
}

async fn async_main<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
Expand Down

0 comments on commit 3d0669f

Please sign in to comment.