-
Notifications
You must be signed in to change notification settings - Fork 6
/
http_server.rs
123 lines (106 loc) · 3.2 KB
/
http_server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use actix_web::{get, post, App, HttpResponse, HttpServer, middleware, Responder, web};
use serde::{Deserialize};
use telegram_gitlab::{find_chat_by_id, find_webhook_by_webhook_url};
#[derive(Debug, Deserialize)]
struct GitlabEvent {
object_kind: String,
event_name: String,
before: String,
after: String,
r#ref: String,
checkout_sha: String,
message: Option<String>,
user_id: u32,
user_name: String,
user_username: String,
user_email: String,
user_avatar: String,
project_id: u32,
project: Project,
repository: Repository,
commits: Vec<Commit>,
total_commits_count: u32,
}
#[derive(Debug, Deserialize)]
struct Project {
id: u32,
name: String,
description: String,
web_url: String,
avatar_url: Option<String>,
git_ssh_url: String,
git_http_url: String,
namespace: String,
visibility_level: u32,
path_with_namespace: String,
default_branch: String,
ci_config_path: Option<String>,
homepage: String,
url: String,
ssh_url: String,
http_url: String,
}
#[derive(Debug, Deserialize)]
struct Repository {
name: String,
url: String,
description: String,
homepage: String,
}
#[derive(Debug, Deserialize)]
struct Commit {
id: String,
message: String,
timestamp: String,
url: String,
}
pub async fn run_http_server() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.service(health)
.service(handle_gitlab_webhook)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
#[get("/health")]
async fn health() -> impl Responder {
log::info!("Health check");
"I'm ok"
}
#[derive(Deserialize)]
struct GitlabWebhook {
webhook_url: String,
}
#[post("/gitlab/{webhook_url}")]
async fn handle_gitlab_webhook(gitlab_webhook: web::Path<GitlabWebhook>, gitlab_event: web::Json<GitlabEvent>) -> impl Responder {
let branch_ref = &gitlab_event.r#ref;
let branch_name = branch_ref.split('/').last().unwrap();
let project_name = &gitlab_event.project.name;
let commit_message = &gitlab_event.commits[0].message;
let commit_url = &gitlab_event.commits[0].url;
let webhook_url = &gitlab_webhook.webhook_url;
log::info!("webhook_url: {}", webhook_url);
let webhook = find_webhook_by_webhook_url(webhook_url);
if webhook.is_none() {
log::error!("Webhook not found");
return HttpResponse::NotFound();
}
let webhook = webhook.unwrap();
// log chat_id
log::info!("Webhook: {}", webhook.webhook_url);
let chat_id = webhook.chat_id.expect("Chat id must be set");
log::info!("Chat id: {}", chat_id);
let chat = find_chat_by_id(webhook.chat_id.expect("Chat id must be set"));
if chat.is_none() {
log::error!("Chat not found");
return HttpResponse::NotFound();
}
let chat = chat.unwrap();
let message = format!("{}: {} - {} - {}", project_name, branch_name, commit_message, commit_url);
crate::telegram_bot::send_message(chat.telegram_id.parse::<i64>().expect("CHAT_ID must be an integer"), message).await.unwrap();
log::info!("bot sent message");
HttpResponse::Ok() // <- send response
}