rustploy 0.1.35

Rustploy PaaS daemon — lightweight self-hosted deployment platform
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Rotas HTTP **públicas** (sem Bearer): o webhook de deploy e o callback OAuth
//! do Gitea. Não há listener próprio aqui — desde a unificação das portas
//! (`docs/plano-unificacao-webhook-api.md`) as duas rotas são servidas pelo
//! listener da API (`http/mod.rs`), que as roteia para cá **antes** do gate de
//! token, porque cada uma tem autenticação própria: o webhook valida o token de
//! 192 bits que vem na URL; o callback valida o `state` CSRF emitido no início
//! do fluxo OAuth.

use bytes::Bytes;
use http_body_util::Full;
use hyper::{Method, Request, Response, StatusCode, body::Incoming};
use tracing::{error, info, warn};

use crate::bootstrap::state::AppState;
use crate::store::webhook_tokens;

/// `POST /webhook/{service_id}/{token}` — valida o token e dispara um deploy,
/// a menos que o serviço seja git-sourced com uma branch configurada e o
/// payload identifique um push pra outra branch (ver `extract_push_branch`).
/// A autenticação é inteiramente o token na URL; método e path chegam já
/// roteados pelo `http`.
///
/// Todo método é roteado para cá (não só POST) para que um GET — a URL colada no
/// navegador, o "ping" de um provedor — receba o `405` honesto do webhook, e não
/// o `401 unauthorized` do gate de Bearer da API, que faria parecer que a URL
/// está errada.
pub async fn webhook(req: Request<Incoming>, state: AppState) -> Response<Full<Bytes>> {
    let method = req.method().clone();
    let path = req.uri().path().to_owned();
    if method != Method::POST {
        return resp(StatusCode::METHOD_NOT_ALLOWED, "method not allowed");
    }

    let parts: Vec<&str> = path.trim_start_matches('/').splitn(3, '/').collect();
    if parts.len() != 3 || parts[0] != "webhook" {
        return resp(StatusCode::NOT_FOUND, "not found");
    }

    let service_id = parts[1].to_string();
    let provided_token = parts[2].to_string();

    let stored = match webhook_tokens::get(&state.db, &state.secrets, &service_id).await {
        Ok(Some(t)) => t,
        Ok(None) => return resp(StatusCode::UNAUTHORIZED, "invalid token"),
        Err(e) => {
            error!(service_id = %service_id, error = %e, "webhook: db error");
            return resp(StatusCode::INTERNAL_SERVER_ERROR, "internal error");
        }
    };

    // Comparação em tempo constante: o token é o único segredo do endpoint.
    if !constant_time_eq(stored.as_bytes(), provided_token.as_bytes()) {
        return resp(StatusCode::UNAUTHORIZED, "invalid token");
    }

    // Filtro de branch: GitHub/Gitea/Gogs mandam `"ref": "refs/heads/<branch>"`
    // no payload de push (mesmo formato nos três). Só filtra quando o serviço é
    // git-sourced com uma branch configurada E o payload trouxe um ref
    // reconhecível — qualquer outro caso (Docker Hub, curl manual sem body,
    // provider não suportado) dispara o deploy do jeito de sempre; não dá pra
    // bloquear silenciosamente um trigger que a gente não sabe interpretar.
    let content_type = req
        .headers()
        .get(hyper::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_owned();
    let is_ping = req
        .headers()
        .get("x-github-event")
        .and_then(|v| v.to_str().ok())
        .is_some_and(|e| e.eq_ignore_ascii_case("ping"));
    let raw_body = match http_body_util::BodyExt::collect(req.into_body()).await {
        Ok(c) => c.to_bytes(),
        Err(_) => Bytes::new(),
    };
    // O "ping" que o GitHub manda ao criar o webhook não é um push: não deploya.
    if is_ping {
        info!(service_id = %service_id, "webhook: ping do GitHub, nada a fazer");
        return resp(StatusCode::OK, "pong");
    }
    // `application/x-www-form-urlencoded` (padrão da UI do GitHub) traz o JSON em `payload=`.
    let body = json_body(&content_type, raw_body);
    let service = crate::store::services::get(&state.db, &service_id)
        .await
        .ok()
        .flatten();
    if let Some(service) = &service
        && let shared::ServiceSource::Git(git) = &service.spec.source
    {
        if let Some(payload_branch) = extract_push_branch(&body)
            && !git.branch.is_empty()
            && git.branch != payload_branch
        {
            info!(
                service_id = %service_id,
                configured = %git.branch,
                received = %payload_branch,
                "webhook: branch do payload não confere com a configurada, deploy ignorado"
            );
            return resp(StatusCode::OK, "skipped: branch mismatch");
        }

        // Filtro por caminhos: com `watch_paths` preenchido, só dispara se o push mexeu em algum
        // deles. Qualquer dúvida (ver `extract_changed_files`) dispara o deploy.
        if skip_by_watch_paths(&git.watch_paths, &body) {
            info!(
                service_id = %service_id,
                watch_paths = ?git.watch_paths,
                "webhook: nenhum arquivo do push casa com watch_paths, deploy ignorado"
            );
            return resp(StatusCode::OK, "skipped: no watched path changed");
        }
    }

    info!(service_id = %service_id, "webhook: disparando deploy");
    tokio::spawn(async move {
        crate::features::deploy::start::handle(state.deploy_ctx(), service_id).await;
    });

    resp(StatusCode::OK, "deploy triggered")
}

/// Devolve o corpo como JSON: `application/json` (e qualquer outro tipo) passa como veio; com
/// `application/x-www-form-urlencoded` o JSON está no campo `payload` (percent-decoding). Form sem
/// `payload` vira corpo vazio, que dispara o deploy sem filtros (como um `curl` manual).
fn json_body(content_type: &str, body: Bytes) -> Bytes {
    let is_form = content_type.split(';').next().is_some_and(|t| {
        t.trim()
            .eq_ignore_ascii_case("application/x-www-form-urlencoded")
    });
    if !is_form {
        return body;
    }
    let text = String::from_utf8_lossy(&body);
    url_decode_pairs(&text)
        .into_iter()
        .find(|(k, _)| k == "payload")
        .map(|(_, v)| Bytes::from(v))
        .unwrap_or_default()
}

/// Extrai o nome curto da branch de um payload de push GitHub/Gitea/Gogs
/// (`{"ref": "refs/heads/main", ...}`). `None` pra qualquer coisa que não seja
/// JSON com esse formato — tags (`refs/tags/...`) também caem em `None`, já
/// que não têm branch pra comparar.
fn extract_push_branch(body: &[u8]) -> Option<String> {
    let v: serde_json::Value = serde_json::from_slice(body).ok()?;
    let r = v.get("ref")?.as_str()?;
    r.strip_prefix("refs/heads/").map(str::to_string)
}

/// A partir de quantos commits no payload a lista de arquivos pode estar truncada: o GitHub manda
/// no máximo 20 commits e o Gitea 10 por padrão. Daí em diante não dá para afirmar que "nada
/// casou", então o deploy é feito.
const MAX_COMMITS_CONFIAVEIS: usize = 10;

/// Arquivos alterados por um push (GitHub/Gitea/Gogs/GitLab: `commits[].added|modified|removed`),
/// sem repetição. `None` quando **não dá para saber** — nesse caso o chamador faz o deploy:
/// - corpo que não é JSON, ou sem `commits` (Docker Hub, `curl` manual);
/// - `commits` vazio (branch nova, force push sem commits novos);
/// - algum commit sem uma das três listas;
/// - `commits` com [`MAX_COMMITS_CONFIAVEIS`] itens ou mais (a lista pode estar truncada);
/// - nenhuma lista com arquivo (ex.: só um commit de merge): sem dado, não se descarta o push.
fn extract_changed_files(body: &[u8]) -> Option<Vec<String>> {
    let v: serde_json::Value = serde_json::from_slice(body).ok()?;
    let commits = v.get("commits")?.as_array()?;
    if commits.is_empty() || commits.len() >= MAX_COMMITS_CONFIAVEIS {
        return None;
    }
    let mut files: Vec<String> = Vec::new();
    for commit in commits {
        for chave in ["added", "modified", "removed"] {
            for f in commit.get(chave)?.as_array()? {
                let f = f.as_str()?;
                if !files.iter().any(|x| x == f) {
                    files.push(f.to_string());
                }
            }
        }
    }
    if files.is_empty() { None } else { Some(files) }
}

/// `true` quando o deploy deve ser **ignorado**: há `watch_paths`, sabemos os arquivos do push e
/// nenhum casa com nenhum deles.
fn skip_by_watch_paths(watch_paths: &[String], body: &[u8]) -> bool {
    if watch_paths.is_empty() {
        return false;
    }
    match extract_changed_files(body) {
        Some(files) => !files
            .iter()
            .any(|f| watch_paths.iter().any(|w| path_matches(w, f))),
        None => false,
    }
}

/// Uma entrada de `watch_paths` casa um arquivo (caminho relativo à raiz do repositório) quando
/// é o próprio arquivo, um diretório que o contém (`src` casa `src/a/b.rs`) ou, se tiver `*`,
/// um glob: `*` não atravessa `/`, `**` atravessa; o glob também vale para qualquer diretório
/// pai do arquivo (`crates/*` casa `crates/api/src/main.rs`). `.` casa o repositório inteiro.
fn path_matches(pattern: &str, file: &str) -> bool {
    let pat = pattern.trim().trim_start_matches("./").trim_matches('/');
    let file = file.trim_start_matches('/');
    if pat.is_empty() || pat == "." {
        return true;
    }
    if !pat.contains('*') {
        return file == pat || file.strip_prefix(pat).is_some_and(|r| r.starts_with('/'));
    }
    let mut fim = 0;
    loop {
        let fim_prox = file[fim..].find('/').map(|i| fim + i);
        let candidato = &file[..fim_prox.unwrap_or(file.len())];
        if glob_match(pat.as_bytes(), candidato.as_bytes()) {
            return true;
        }
        match fim_prox {
            Some(i) => fim = i + 1,
            None => return false,
        }
    }
}

/// Glob mínimo: `*` = qualquer coisa menos `/`, `**` = qualquer coisa.
fn glob_match(pat: &[u8], txt: &[u8]) -> bool {
    match pat.split_first() {
        None => txt.is_empty(),
        Some((b'*', resto)) => {
            let (livre, resto) = match resto.split_first() {
                Some((b'*', r)) => (true, r),
                _ => (false, resto),
            };
            for i in 0..=txt.len() {
                if glob_match(resto, &txt[i..]) {
                    return true;
                }
                if i < txt.len() && !livre && txt[i] == b'/' {
                    break;
                }
            }
            false
        }
        Some((c, resto)) => txt.first() == Some(c) && glob_match(resto, &txt[1..]),
    }
}

fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
}

fn resp(status: StatusCode, body: &'static str) -> Response<Full<Bytes>> {
    Response::builder()
        .status(status)
        .body(Full::new(Bytes::from(body)))
        .unwrap()
}

fn html(status: StatusCode, title: &str, body: &str) -> Response<Full<Bytes>> {
    let page = format!(
        "<!doctype html><html><head><meta charset=\"utf-8\"><title>{title}</title>\
         <style>body{{font-family:system-ui,sans-serif;background:#1b1d22;color:#e6eaf0;\
         display:flex;align-items:center;justify-content:center;height:100vh;margin:0}}\
         .card{{text-align:center;padding:32px 48px;border-radius:12px;background:#23262d}}\
         h1{{color:#4ccfe6;font-size:20px}}p{{color:#9aa0a8}}</style></head>\
         <body><div class=\"card\"><h1>{title}</h1><p>{body}</p></div></body></html>"
    );
    Response::builder()
        .status(status)
        .header("Content-Type", "text/html; charset=utf-8")
        .body(Full::new(Bytes::from(page)))
        .unwrap()
}

/// `GET /oauth/{gitea,github}/callback` — completes an OAuth2 authorization-code
/// flow: validates the CSRF `state`, exchanges the `code` for tokens, records
/// the connected account. The provider kind comes from the stored row (keyed by
/// the CSRF `state`), so a single handler serves every provider.
pub async fn oauth_callback(req: Request<Incoming>, state: AppState) -> Response<Full<Bytes>> {
    use crate::store::git_providers;

    let query = req.uri().query().unwrap_or("");
    let mut code = None;
    let mut csrf = None;
    for (k, v) in url_decode_pairs(query) {
        match k.as_str() {
            "code" => code = Some(v),
            "state" => csrf = Some(v),
            _ => {}
        }
    }
    let (Some(code), Some(csrf)) = (code, csrf) else {
        return html(
            StatusCode::BAD_REQUEST,
            "Erro",
            "Parâmetros OAuth ausentes.",
        );
    };

    // Consome o state (CSRF) e recupera o provider associado.
    let provider_id = match state.oauth_states.lock().unwrap().remove(&csrf) {
        Some(id) => id,
        None => {
            return html(
                StatusCode::BAD_REQUEST,
                "Erro",
                "State OAuth inválido ou expirado.",
            );
        }
    };

    let provider = match git_providers::get(&state.db, &provider_id).await {
        Ok(Some(p)) => p,
        _ => return html(StatusCode::NOT_FOUND, "Erro", "Provider não encontrado."),
    };
    let kind =
        shared::GitProviderKind::from_str(&provider.kind).unwrap_or(shared::GitProviderKind::Gitea);

    let client_id = provider.oauth_client_id.clone().unwrap_or_default();
    let client_secret = match &provider.oauth_client_secret_enc {
        Some(enc) => match state.secrets.decrypt(enc) {
            Ok(s) => s,
            Err(e) => {
                error!(error = %e, "oauth: falha ao decifrar client_secret");
                return html(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "Erro",
                    "Falha ao ler client secret.",
                );
            }
        },
        None => {
            return html(
                StatusCode::BAD_REQUEST,
                "Erro",
                "Provider sem client secret.",
            );
        }
    };

    let redirect_uri = match callback_redirect_uri(&state, kind) {
        Some(u) => u,
        None => {
            return html(
                StatusCode::BAD_REQUEST,
                "Erro",
                "Não foi possível determinar a URL pública do daemon (configure [api] domain).",
            );
        }
    };

    let tokens = match crate::features::git_providers::exchange_code(
        kind,
        &provider.base_url,
        &client_id,
        &client_secret,
        &code,
        &redirect_uri,
    )
    .await
    {
        Ok(t) => t,
        Err(e) => {
            error!(error = %e, "oauth: troca de code falhou");
            return html(
                StatusCode::BAD_GATEWAY,
                "Erro",
                "Falha ao trocar o código por token.",
            );
        }
    };

    // Auto-registra a redirect URI atual no app OAuth2 do provider para que
    // futuras trocas funcionem mesmo após mudança do domínio/porta da API
    // (no-op no GitHub — callback URL não é editável via API).
    {
        let base_url = provider.base_url.clone();
        let token = tokens.access_token.clone();
        let cid = client_id.clone();
        let ru = redirect_uri.clone();
        tokio::spawn(async move {
            if let Err(e) = crate::features::git_providers::ensure_redirect_uri(
                kind, &base_url, &token, &cid, &ru,
            )
            .await
            {
                warn!(error = %e, "oauth: falha ao sincronizar redirect URI no provider (não crítico)");
            }
        });
    }

    let account = match crate::features::git_providers::current_user(
        kind,
        &provider.base_url,
        &tokens.access_token,
    )
    .await
    {
        Ok(a) => a,
        Err(e) => {
            error!(error = %e, "oauth: current_user falhou");
            return html(
                StatusCode::BAD_GATEWAY,
                "Erro",
                "Token obtido, mas falha ao ler a conta.",
            );
        }
    };

    let access_enc = state.secrets.encrypt(&tokens.access_token).ok();
    let refresh_enc = tokens
        .refresh_token
        .as_deref()
        .and_then(|r| state.secrets.encrypt(r).ok());

    if let Err(e) = git_providers::set_tokens(
        &state.db,
        &provider_id,
        access_enc.as_deref(),
        refresh_enc.as_deref(),
        &account.login,
        account.avatar_url.as_deref(),
    )
    .await
    {
        error!(error = %e, "oauth: falha ao persistir tokens");
        return html(
            StatusCode::INTERNAL_SERVER_ERROR,
            "Erro",
            "Falha ao salvar a conexão.",
        );
    }

    let provider_label = match kind {
        shared::GitProviderKind::Github => "GitHub",
        shared::GitProviderKind::Gitea => "Gitea",
    };
    info!(provider_id = %provider_id, kind = %provider.kind, login = %account.login, "oauth: conta conectada");
    html(
        StatusCode::OK,
        "Conta conectada",
        &format!(
            "{} @{} conectado. Você já pode fechar esta aba.",
            provider_label, account.login
        ),
    )
}

/// Builds `{public_base_url}/oauth/{gitea,github}/callback` — a base sai de
/// `[api]` (domínio/porta do listener que atende este callback), não de uma
/// setting no banco; o segmento do path vem do kind do provider. `Option`
/// mantido porque o chamador ainda trata o caso "sem base utilizável"; hoje ela
/// é sempre derivável.
pub fn callback_redirect_uri(state: &AppState, kind: shared::GitProviderKind) -> Option<String> {
    let seg = crate::features::git_providers::callback_path_segment(kind);
    Some(format!("{}/oauth/{seg}/callback", state.public_base_url()))
}

/// Tiny `application/x-www-form-urlencoded` query parser (percent-decoding).
fn url_decode_pairs(query: &str) -> Vec<(String, String)> {
    query
        .split('&')
        .filter(|s| !s.is_empty())
        .map(|pair| {
            let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
            (percent_decode(k), percent_decode(v))
        })
        .collect()
}

fn percent_decode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'%' if i + 2 < bytes.len() => {
                let hi = (bytes[i + 1] as char).to_digit(16);
                let lo = (bytes[i + 2] as char).to_digit(16);
                if let (Some(hi), Some(lo)) = (hi, lo) {
                    out.push((hi * 16 + lo) as u8);
                    i += 3;
                    continue;
                }
                out.push(b'%');
                i += 1;
            }
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            c => {
                out.push(c);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

#[cfg(test)]
mod tests {
    use super::{
        extract_changed_files, extract_push_branch, json_body, path_matches, skip_by_watch_paths,
    };
    use bytes::Bytes;

    #[test]
    fn form_urlencoded_entrega_o_json_do_payload() {
        let json = r#"{"ref":"refs/heads/dev","commits":[{"added":[],"modified":["a b.rs"],"removed":[]}]}"#;
        let enc: String = json
            .bytes()
            .map(|b| match b {
                b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => (b as char).to_string(),
                _ => format!("%{b:02X}"),
            })
            .collect();
        let body = Bytes::from(format!("payload={enc}"));
        let out = json_body("application/x-www-form-urlencoded; charset=utf-8", body);
        assert_eq!(extract_push_branch(&out), Some("dev".to_string()));
        assert_eq!(
            extract_changed_files(&out),
            Some(vec!["a b.rs".to_string()])
        );
    }

    #[test]
    fn json_e_outros_tipos_passam_intactos() {
        let b = Bytes::from_static(br#"{"ref":"refs/heads/main"}"#);
        assert_eq!(json_body("application/json", b.clone()), b);
        assert_eq!(json_body("", b.clone()), b);
    }

    #[test]
    fn form_sem_payload_vira_corpo_vazio() {
        let out = json_body(
            "application/x-www-form-urlencoded",
            Bytes::from_static(b"foo=bar"),
        );
        assert!(out.is_empty());
    }

    #[test]
    fn extrai_branch_de_payload_github_gitea() {
        let body = br#"{"ref":"refs/heads/main","repository":{}}"#;
        assert_eq!(extract_push_branch(body), Some("main".to_string()));
    }

    #[test]
    fn none_pra_ref_de_tag() {
        let body = br#"{"ref":"refs/tags/v1.0.0"}"#;
        assert_eq!(extract_push_branch(body), None);
    }

    #[test]
    fn none_pra_corpo_vazio_ou_nao_json() {
        assert_eq!(extract_push_branch(b""), None);
        assert_eq!(extract_push_branch(b"not json"), None);
    }

    #[test]
    fn none_pra_payload_sem_ref_ex_docker_hub() {
        let body = br#"{"push_data":{"tag":"latest"},"repository":{"repo_name":"user/img"}}"#;
        assert_eq!(extract_push_branch(body), None);
    }

    #[test]
    fn branch_com_barra_no_nome() {
        let body = br#"{"ref":"refs/heads/feature/login"}"#;
        assert_eq!(extract_push_branch(body), Some("feature/login".to_string()));
    }

    fn push(commits: &str) -> Vec<u8> {
        format!(r#"{{"ref":"refs/heads/main","commits":[{commits}]}}"#).into_bytes()
    }
    fn commit(added: &str, modified: &str, removed: &str) -> String {
        format!(r#"{{"added":[{added}],"modified":[{modified}],"removed":[{removed}]}}"#)
    }
    fn w(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn arquivos_de_varios_commits_sem_repeticao() {
        let body = push(&format!(
            "{},{}",
            commit(r#""a.rs""#, r#""b.rs""#, ""),
            commit("", r#""b.rs""#, r#""c.rs""#)
        ));
        assert_eq!(
            extract_changed_files(&body),
            Some(w(&["a.rs", "b.rs", "c.rs"]))
        );
    }

    #[test]
    fn sem_dado_confiavel_nao_se_sabe_os_arquivos() {
        // sem `commits`, não-JSON, commits vazio
        assert_eq!(extract_changed_files(br#"{"ref":"refs/heads/main"}"#), None);
        assert_eq!(extract_changed_files(b"not json"), None);
        assert_eq!(extract_changed_files(&push("")), None);
        // commit sem uma das listas
        assert_eq!(
            extract_changed_files(&push(r#"{"added":["a"],"modified":["b"]}"#)),
            None
        );
        // só um merge, sem arquivo nenhum
        assert_eq!(extract_changed_files(&push(&commit("", "", ""))), None);
    }

    #[test]
    fn muitos_commits_pode_estar_truncado() {
        let um = commit("", r#""docs/x.md""#, "");
        let dez = [um.as_str(); 10].join(",");
        assert_eq!(extract_changed_files(&push(&dez)), None);
        let nove = [um.as_str(); 9].join(",");
        assert!(extract_changed_files(&push(&nove)).is_some());
    }

    #[test]
    fn casamento_de_caminhos() {
        assert!(path_matches("src", "src/a/b.rs"));
        assert!(path_matches("src/", "src/a/b.rs"));
        assert!(path_matches("./src", "src/a.rs"));
        assert!(path_matches("Cargo.toml", "Cargo.toml"));
        assert!(
            !path_matches("src", "srcs/a.rs"),
            "prefixo de texto não é diretório"
        );
        assert!(!path_matches("Cargo.toml", "crates/x/Cargo.toml"));
        assert!(path_matches(".", "qualquer/coisa"));
        // glob
        assert!(path_matches("crates/**", "crates/api/src/main.rs"));
        assert!(
            path_matches("crates/*", "crates/api/src/main.rs"),
            "glob vale para o diretório pai"
        );
        assert!(!path_matches("crates/*", "other/api/main.rs"));
        assert!(path_matches("**/Cargo.toml", "crates/x/Cargo.toml"));
        assert!(path_matches(
            "dockerfiles/*.dockerfile",
            "dockerfiles/api.dockerfile"
        ));
        assert!(!path_matches(
            "dockerfiles/*.dockerfile",
            "dockerfiles/sub/api.dockerfile"
        ));
    }

    #[test]
    fn filtro_por_caminhos_so_ignora_quando_tem_certeza() {
        let docs = push(&commit("", r#""docs/a.md""#, ""));
        let src = push(&commit("", r#""crates/api/src/main.rs""#, ""));
        let watch = w(&["crates/api", "Cargo.lock"]);
        assert!(skip_by_watch_paths(&watch, &docs), "só docs mudou: ignora");
        assert!(
            !skip_by_watch_paths(&watch, &src),
            "mexeu em crates/api: faz deploy"
        );
        assert!(
            !skip_by_watch_paths(&[], &docs),
            "sem watch_paths: comportamento de sempre"
        );
        assert!(
            !skip_by_watch_paths(&watch, b"not json"),
            "sem dado: faz deploy"
        );
        assert!(!skip_by_watch_paths(
            &watch,
            br#"{"ref":"refs/heads/main"}"#
        ));
        // um dos arquivos casa entre vários
        let misto = push(&commit(r#""docs/a.md""#, r#""Cargo.lock""#, ""));
        assert!(!skip_by_watch_paths(&watch, &misto));
    }
}