tirbofish/kitgit
main / src / webhooks.rs · 5182 bytes
src/webhooks.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Repository webhook delivery (JSON POST + optional HMAC-SHA256).
use crate::db::models::{Repository, User, Webhook};
use crate::db::queries;
use anyhow::Result;
use hmac::{Hmac, Mac};
use serde_json::{json, Value};
use sha2::Sha256;
use sqlx::PgPool;
use std::time::Instant;
use uuid::Uuid;
type HmacSha256 = Hmac<Sha256>;
pub const EVENT_PUSH: &str = "push";
pub const EVENT_ISSUES: &str = "issues";
pub const EVENT_PULL_REQUEST: &str = "pull_request";
pub const EVENT_RELEASE: &str = "release";
pub const ALL_EVENTS: &[&str] = &[EVENT_PUSH, EVENT_ISSUES, EVENT_PULL_REQUEST, EVENT_RELEASE];
pub fn normalize_events(raw: &[String]) -> Vec<String> {
let mut out = Vec::new();
for e in raw {
let e = e.trim();
if ALL_EVENTS.contains(&e) && !out.iter().any(|x| x == e) {
out.push(e.to_string());
}
}
out
}
fn sign_body(secret: &str, body: &[u8]) -> Option<String> {
if secret.is_empty() {
return None;
}
let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).ok()?;
mac.update(body);
Some(format!(
"sha256={}",
hex::encode(mac.finalize().into_bytes())
))
}
fn build_envelope(
event: &str,
action: &str,
delivery_id: Uuid,
repo: &Repository,
owner: &str,
sender: Option<&User>,
payload: Value,
) -> Value {
json!({
"event": event,
"action": action,
"delivery_id": delivery_id.to_string(),
"repository": {
"id": repo.id,
"name": repo.name,
"owner": owner,
"full_name": format!("{owner}/{}", repo.name),
"default_branch": repo.default_branch,
"visibility": repo.visibility,
"private": repo.visibility == "private",
},
"sender": sender.map(|u| json!({
"id": u.id,
"username": u.username,
"display_name": u.display_name,
})),
"payload": payload,
})
}
/// Fire matching active webhooks for a repo. Failures are logged; callers should ignore errors.
pub async fn dispatch(
pool: &PgPool,
event: &str,
action: &str,
repo: &Repository,
owner: &str,
sender: Option<&User>,
payload: Value,
) -> Result<()> {
let hooks = queries::list_active_webhooks_for_event(pool, repo.id, event).await?;
if hooks.is_empty() {
return Ok(());
}
let delivery_id = Uuid::new_v4();
let body_val = build_envelope(event, action, delivery_id, repo, owner, sender, payload);
let body = serde_json::to_vec(&body_val)?;
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(10))
.redirect(reqwest::redirect::Policy::none())
.user_agent("kitgit-webhooks/0.1")
.build()?;
for hook in hooks {
deliver_one(pool, &client, &hook, event, action, delivery_id, &body).await;
}
Ok(())
}
/// Spawn a background dispatch so request handlers stay fast.
pub fn spawn_dispatch(
pool: PgPool,
event: &'static str,
action: String,
repo: Repository,
owner: String,
sender: Option<User>,
payload: Value,
) {
tokio::spawn(async move {
if let Err(e) = dispatch(
&pool,
event,
&action,
&repo,
&owner,
sender.as_ref(),
payload,
)
.await
{
tracing::warn!("webhook dispatch failed: {e:#}");
}
});
}
async fn deliver_one(
pool: &PgPool,
client: &reqwest::Client,
hook: &Webhook,
event: &str,
action: &str,
delivery_id: Uuid,
body: &[u8],
) {
let started = Instant::now();
let mut req = client
.post(&hook.url)
.header("Content-Type", "application/json")
.header("X-Kitgit-Event", event)
.header("X-Kitgit-Delivery", delivery_id.to_string())
.header("X-Kitgit-Action", action)
.body(body.to_vec());
if let Some(sig) = sign_body(&hook.secret, body) {
req = req.header("X-Hub-Signature-256", sig);
}
let (success, status_code, error) = match req.send().await {
Ok(resp) => {
let code = resp.status().as_u16() as i32;
let ok = resp.status().is_success();
let err = if ok {
None
} else {
let text = resp.text().await.unwrap_or_default();
let truncated: String = text.chars().take(500).collect();
Some(if truncated.is_empty() {
format!("HTTP {code}")
} else {
format!("HTTP {code}: {truncated}")
})
};
(ok, Some(code), err)
}
Err(e) => (false, None, Some(e.to_string())),
};
let duration_ms = started.elapsed().as_millis().min(i32::MAX as u128) as i32;
if let Err(e) = queries::record_webhook_delivery(
pool,
hook.id,
event,
action,
success,
status_code,
error.as_deref(),
duration_ms,
)
.await
{
tracing::warn!("failed to record webhook delivery: {e:#}");
}
}