-
Notifications
You must be signed in to change notification settings - Fork 6
/
http_server.rs
158 lines (138 loc) · 4.18 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer, Responder};
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,
title: String,
message: String,
author: Author,
timestamp: String,
url: String,
}
#[derive(Debug, Deserialize)]
struct Author {
name: String,
email: 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 = &gitlab_event.project;
// replace - with \- to avoid error in telegram markdown
let project_name = &project.name;
let project_url = &project.homepage;
// create a paragprah with all commits, include committer name, commit message and commit url
let mut commit_paragraph = String::new();
for commit in &gitlab_event.commits {
log::info!("Commit: {}", commit.message);
log::info!("Commit url: {}", commit.url);
log::info!("Commit author: {}", commit.author.name);
let commit_url = &commit.url;
let commit_message = &commit.message.trim_end();
let commit_author_name = &commit.author.name;
// commit_paragraph.push_str(&format!("{}: [{}]({}) to [{}:{}]({})\n", commit_author_name, commit_message, commit_url, project_name, branch_name, project_url));
commit_paragraph.push_str(&format!(
"{}: <a href=\"{}\">{}</a> to <a href=\"{}\">{}:{}</a>\n",
commit_author_name, commit_url, commit_message, project_url, project_name, branch_name
));
}
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();
crate::telegram_bot::send_message(
chat.telegram_id
.parse::<i64>()
.expect("CHAT_ID must be an integer"),
commit_paragraph,
)
.await
.unwrap();
log::info!("bot sent message");
HttpResponse::Ok() // <- send response
}