vgi-cli 0.4.15

The `vgi` command: `vgi repo init` turns VGI commit trust on for a repository in a manual or personal-account git namespace, acting as you through `gh` (GitHub) or your own token (Forgejo) — the same bootstrap the community's bridge runs.
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
//! Forgejo (and Gitea), with the person's own token.
//!
//! The plan, the merge-settings body and the branch-protection body — and
//! the tests for "already done" — are the Forgejo adapter's own; this module
//! only carries them over HTTP as the repository's admin instead of the
//! community's bot.

use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use reqwest::{Method, StatusCode, redirect};
use serde_json::{Value, json};
use url::Url;
use vgi_forge::{
    BootstrapStep, MergeMethod, ProtectionSpec, RepoSettings, RepoSpec, StepAction, VgiConfig,
};
use vgi_forge_forgejo::plan::{MergePlan, PlanOptions, default_status_context, forgejo_plan};
use vgi_forge_forgejo::{
    DEFAULT_ACTIONS_BASE, DEFAULT_CHECKOUT_ACTION, InstanceInfo, managed_protection_rule,
    protection_body, protection_satisfies, settings_body, settings_satisfied,
};

use crate::report::{Change, Report};

/// The environment variable holding the person's token.
pub const TOKEN_ENV: &str = "FORGEJO_TOKEN";

/// A Forgejo API client acting as the token's owner.
pub struct Client {
    http: reqwest::Client,
    api: Url,
    token: String,
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("api", &self.api.as_str())
            .field("token", &"<redacted>")
            .finish()
    }
}

/// `https://<host>/`, or `base` checked the way the adapter checks its
/// instance URL: `https`, or `http` only on loopback (the token travels on
/// every request). `base` must be on `host`, the repository's own host (any
/// port), so the token never goes to a server the resource does not name.
pub fn base_url(host: &str, base: Option<&Url>) -> Result<Url> {
    let mut url = match base {
        Some(u) => u.clone(),
        None => Url::parse(&format!("https://{host}/")).context("instance URL")?,
    };
    let h = url.host_str().unwrap_or("").to_ascii_lowercase();
    if !h.eq_ignore_ascii_case(host) {
        bail!(
            "--forgejo-url `{url}` is on `{h}`, but the repository is on `{host}`. {TOKEN_ENV} \
             is sent to --forgejo-url, so it must be the repository's own host; give --resource \
             as <instance-host>/<owner>/<repo>"
        );
    }
    let loopback = matches!(h.as_str(), "localhost" | "127.0.0.1" | "[::1]");
    match url.scheme() {
        "https" => {}
        "http" if loopback => {}
        s => bail!("instance URL `{url}`: `{s}` is not allowed (https, or http on loopback)"),
    }
    if !url.path().ends_with('/') {
        let p = format!("{}/", url.path());
        url.set_path(&p);
    }
    url.set_query(None);
    url.set_fragment(None);
    Ok(url)
}

impl Client {
    /// Connect to the instance at `base` (see [`base_url`]): ask it for
    /// `GET /api/v1/version` *without* the token, and hand back a client
    /// carrying the token only if the answer is Forgejo's or Gitea's (JSON
    /// with a `version` string). `auto`: the forge was guessed from the
    /// host (`--forge auto`), so a failure suggests `--forge`.
    pub async fn connect(base: &Url, token: String, auto: bool) -> Result<(Self, InstanceInfo)> {
        let c = Client::new(base, token)?;
        match c.version().await {
            Ok(info) => Ok((c, info)),
            Err(why) if auto => bail!(
                "{base} did not answer GET /api/v1/version as Forgejo or Gitea ({why:#}), so \
                 {TOKEN_ENV} was not sent to it. `--forge auto` takes any host other than \
                 github.com for Forgejo: pass --forge github for GitHub Enterprise Server, or \
                 --forge forgejo with --forgejo-url if the instance lives under a sub-path"
            ),
            Err(why) => bail!(
                "{base} did not answer GET /api/v1/version as Forgejo or Gitea ({why:#}), so \
                 {TOKEN_ENV} was not sent to it; check --forgejo-url"
            ),
        }
    }

    /// `GET /api/v1/version`, unauthenticated.
    async fn version(&self) -> Result<InstanceInfo> {
        let url = self.url(&["version"]);
        let resp = self
            .http
            .get(url)
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| anyhow!("{}", e.without_url()))?;
        let status = resp.status();
        if !status.is_success() {
            bail!("HTTP {status}");
        }
        let bytes = resp.bytes().await.context("reading the reply")?;
        let v: Value = serde_json::from_slice(&bytes).map_err(|_| anyhow!("not JSON"))?;
        let version = v
            .get("version")
            .and_then(Value::as_str)
            .filter(|s| !s.trim().is_empty())
            .ok_or_else(|| anyhow!("no `version` string"))?;
        Ok(InstanceInfo::from_version(version))
    }

    fn new(base: &Url, token: String) -> Result<Self> {
        if token.trim().is_empty() {
            bail!("{TOKEN_ENV} is empty");
        }
        let http = reqwest::Client::builder()
            .user_agent(concat!("vgi/", env!("CARGO_PKG_VERSION")))
            .redirect(redirect::Policy::none())
            .timeout(Duration::from_secs(30))
            .build()
            .context("HTTP client")?;
        Ok(Client {
            http,
            api: base.join("api/v1/").context("API URL")?,
            token,
        })
    }

    fn url(&self, segments: &[&str]) -> Url {
        let mut u = self.api.clone();
        u.path_segments_mut()
            .expect("an http(s) URL has path segments")
            .pop_if_empty()
            .extend(segments);
        u
    }

    async fn call(
        &self,
        method: Method,
        url: Url,
        body: Option<&Value>,
    ) -> Result<(StatusCode, Value)> {
        let mut req = self
            .http
            .request(method.clone(), url.clone())
            .header("Authorization", format!("token {}", self.token))
            .header("Accept", "application/json");
        if let Some(b) = body {
            req = req.json(b);
        }
        let what = format!("{method} {}", url.path());
        let resp = req
            .send()
            .await
            .map_err(|e| anyhow!("{what}: {}", e.without_url()))?;
        let status = resp.status();
        let bytes = resp.bytes().await.with_context(|| what.clone())?;
        let value = if bytes.iter().all(u8::is_ascii_whitespace) {
            Value::Null
        } else {
            serde_json::from_slice(&bytes)
                .unwrap_or_else(|_| json!(String::from_utf8_lossy(&bytes)))
        };
        Ok((status, value))
    }

    async fn get(&self, segments: &[&str]) -> Result<Option<Value>> {
        let url = self.url(segments);
        let (status, v) = self.call(Method::GET, url.clone(), None).await?;
        match status.as_u16() {
            200..=299 => Ok(Some(v)),
            404 => Ok(None),
            _ => bail!("GET {}: HTTP {status}: {}", url.path(), message(&v)),
        }
    }

    async fn send(&self, method: Method, segments: &[&str], body: &Value) -> Result<Value> {
        let url = self.url(segments);
        let (status, v) = self.call(method.clone(), url.clone(), Some(body)).await?;
        if !status.is_success() {
            bail!("{method} {}: HTTP {status}: {}", url.path(), message(&v));
        }
        Ok(v)
    }
}

fn message(v: &Value) -> String {
    v.get("message")
        .and_then(Value::as_str)
        .map(str::to_string)
        .unwrap_or_else(|| v.to_string())
}

/// The token owner's login.
pub async fn whoami(c: &Client) -> Result<String> {
    let me = c
        .get(&["user"])
        .await?
        .ok_or_else(|| anyhow!("{TOKEN_ENV} is not accepted (GET /user)"))?;
    let login = me
        .get("login")
        .and_then(Value::as_str)
        .ok_or_else(|| anyhow!("GET /user: no login"))?
        .to_string();
    Ok(login)
}

/// The adapter's plan, with the adapter's defaults: fast-forward-only
/// merges, DIDs written into the workflow.
pub fn plan(spec: &RepoSpec, cfg: &VgiConfig, runs_on: &str) -> Result<Vec<BootstrapStep>> {
    let actions_base = Url::parse(DEFAULT_ACTIONS_BASE).expect("static URL");
    let opts = PlanOptions {
        checkout_action: DEFAULT_CHECKOUT_ACTION,
        actions_base: &actions_base,
        runs_on,
        status_context: default_status_context(&cfg.required_check),
        inline_variables: true,
        merges: MergePlan::FastForwardOnly,
    };
    Ok(forgejo_plan(spec, cfg, &opts)?)
}

/// Run `steps` in order, check-then-apply, stopping at the first failure.
pub async fn apply(
    c: &Client,
    owner: &str,
    name: &str,
    me: &str,
    info: &InstanceInfo,
    steps: &[BootstrapStep],
    report: &mut Report<'_>,
) -> Result<()> {
    let repo = c.get(&["repos", owner, name]).await?.ok_or_else(|| {
        anyhow!("{owner}/{name} does not exist, or {TOKEN_ENV} cannot see it; create it first")
    })?;
    if repo.pointer("/permissions/admin").and_then(Value::as_bool) != Some(true) {
        bail!(
            "{TOKEN_ENV} does not show admin on {owner}/{name}; branch protection needs admin \
             (the account holder, or an organisation owner, runs this)"
        );
    }
    for step in steps {
        let id = step.id.as_str();
        match &step.action {
            StepAction::ConfigureRepo(s) => {
                let (change, body) = configure(c, owner, name, info, s, report.dry_run()).await?;
                report.step(
                    id,
                    "merge settings",
                    change,
                    Some(&serde_json::to_string_pretty(&body)?),
                );
            }
            StepAction::WriteFile {
                path,
                contents,
                message,
            } => {
                let change =
                    write_file(c, owner, name, path, contents, message, report.dry_run()).await?;
                report.step(id, path, change, Some(&String::from_utf8_lossy(contents)));
            }
            StepAction::ProtectDefaultBranch(spec) => {
                let (change, body) = protect(c, owner, name, me, spec, report.dry_run()).await?;
                report.step(
                    id,
                    "default branch protection",
                    change,
                    Some(&serde_json::to_string_pretty(&body)?),
                );
            }
            other => bail!("step `{id}` ({other:?}) is not one `vgi repo init` runs on Forgejo"),
        }
    }
    Ok(())
}

async fn configure(
    c: &Client,
    owner: &str,
    name: &str,
    info: &InstanceInfo,
    s: &RepoSettings,
    dry_run: bool,
) -> Result<(Change, Value)> {
    let repo = c
        .get(&["repos", owner, name])
        .await?
        .ok_or_else(|| anyhow!("{owner}/{name} vanished"))?;
    let ff_wanted = s.merge_methods.contains(&MergeMethod::FastForward);
    let ff_available =
        info.features.fast_forward_only && repo.get("allow_fast_forward_only_merge").is_some();
    if ff_wanted && !ff_available {
        bail!(
            "this instance ({}) cannot restrict merges to fast-forward only, and every web merge \
             would land a commit the check never saw. Upgrade to Forgejo 7 or Gitea 1.22",
            info.version
        );
    }
    let body = settings_body(&repo, s)?;
    if settings_satisfied(&repo, s)? {
        return Ok((Change::Unchanged, body));
    }
    if !dry_run {
        let after = c
            .send(Method::PATCH, &["repos", owner, name], &body)
            .await?;
        if !settings_satisfied(&after, s)? {
            bail!(
                "{owner}/{name}: the instance accepted the settings but did not apply them all \
                 (are Actions or pull requests disabled instance-wide?)"
            );
        }
    }
    Ok((Change::Update, body))
}

async fn write_file(
    c: &Client,
    owner: &str,
    name: &str,
    file: &str,
    contents: &[u8],
    message: &str,
    dry_run: bool,
) -> Result<Change> {
    vgi_forge::validate_repo_path(file)?;
    let mut segs = vec!["repos", owner, name, "contents"];
    segs.extend(file.split('/'));
    let existing = match c.get(&segs).await? {
        None => None,
        Some(v) => {
            if v.is_array() || v.get("type").and_then(Value::as_str) != Some("file") {
                bail!("`{file}` exists in {owner}/{name} and is not a file");
            }
            let Some(b64) = v.get("content").and_then(Value::as_str) else {
                bail!(
                    "`{file}` in {owner}/{name} is too large for the instance to return inline; \
                     it was not written by the bootstrap — remove or rename it"
                );
            };
            let compact: String = b64.chars().filter(|c| !c.is_whitespace()).collect();
            let bytes = STANDARD
                .decode(compact)
                .with_context(|| format!("`{file}`: content is not base64"))?;
            let sha = v
                .get("sha")
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_string();
            Some((sha, bytes))
        }
    };
    let (change, method) = match &existing {
        Some((_, current)) if current == contents => return Ok(Change::Unchanged),
        Some(_) => (Change::Update, Method::PUT),
        None => (Change::Create, Method::POST),
    };
    if dry_run {
        return Ok(change);
    }
    let mut body = json!({ "message": message, "content": STANDARD.encode(contents) });
    if let Some((sha, _)) = &existing {
        body["sha"] = json!(sha);
    }
    c.send(method, &segs, &body).await.map_err(|e| {
        e.context(format!(
            "writing `{file}` — once the default branch is protected, no one can push to it and \
             the workflow directories are protected file patterns. To change the workflow (a new \
             --verify-trust-version, say), an admin temporarily lifts that protection (deletes \
             the default branch's protection rule, or allows pushes and clears its protected \
             file patterns) and re-runs `vgi repo init`, which writes the workflow and puts the \
             protection back; where a community bridge manages the repository, it makes the \
             change instead"
        ))
    })?;
    Ok(change)
}

/// Admin collaborators, plus the person running this: the rule applies to
/// admins, and its merge allow-list is who may merge at all, so the account
/// holder (never a collaborator of their own repository) must be on it or
/// they lock themselves out. The bridge seeds only collaborators, because
/// there a namespace's owner is not the one running the plan.
async fn admins(c: &Client, owner: &str, name: &str, me: &str) -> Result<Vec<String>> {
    let mut out = vec![me.to_string()];
    let mut page = 1u32;
    loop {
        let mut url = c.url(&["repos", owner, name, "collaborators"]);
        url.query_pairs_mut()
            .append_pair("page", &page.to_string())
            .append_pair("limit", "50");
        let (status, v) = c.call(Method::GET, url, None).await?;
        if !status.is_success() {
            bail!("listing collaborators: HTTP {status}: {}", message(&v));
        }
        let users = v.as_array().cloned().unwrap_or_default();
        if users.is_empty() {
            break;
        }
        for u in &users {
            let Some(login) = u.get("login").and_then(Value::as_str) else {
                continue;
            };
            let perm = c
                .get(&["repos", owner, name, "collaborators", login, "permission"])
                .await?;
            let is_admin = perm
                .as_ref()
                .and_then(|p| p.get("permission"))
                .and_then(Value::as_str)
                == Some("admin");
            if is_admin && !out.iter().any(|l| l.eq_ignore_ascii_case(login)) {
                out.push(login.to_string());
            }
        }
        if users.len() < 50 {
            break;
        }
        page += 1;
    }
    Ok(out)
}

async fn protect(
    c: &Client,
    owner: &str,
    name: &str,
    me: &str,
    spec: &ProtectionSpec,
    dry_run: bool,
) -> Result<(Change, Value)> {
    let repo = c
        .get(&["repos", owner, name])
        .await?
        .ok_or_else(|| anyhow!("{owner}/{name} vanished"))?;
    let empty = repo.get("empty").and_then(Value::as_bool) == Some(true);
    let branch = repo
        .get("default_branch")
        .and_then(Value::as_str)
        .filter(|b| !b.is_empty() && !empty)
        .map(str::to_string);
    let Some(branch) = branch else {
        if dry_run {
            // The workflow step would have made the first commit.
            let mut body = protection_body(None, &[me.to_string()], spec)?;
            body["rule_name"] = json!("<default branch>");
            return Ok((Change::Create, body));
        }
        bail!("{owner}/{name} is empty: there is no default branch to protect yet");
    };
    if branch.contains(['*', '?', '[', ']', '{', '}', '\\']) {
        bail!(
            "the default branch `{branch}` contains glob characters, so Forgejo would read a rule \
             for it as a pattern; rename the branch"
        );
    }
    let rules = c
        .get(&["repos", owner, name, "branch_protections"])
        .await?
        .unwrap_or(Value::Array(Vec::new()));
    let (existing, shadowing) = managed_protection_rule(&rules, &branch)?;
    if !shadowing.is_empty() {
        bail!(
            "{owner}/{name}: branch protection rule(s) {} also match `{branch}` (Forgejo compares \
             rule names case-insensitively and applies the oldest), so the managed rule may \
             never apply; remove them and re-run",
            shadowing.join(", ")
        );
    }
    if let Some(rule) = &existing
        && protection_satisfies(rule, spec)?
    {
        return Ok((Change::Unchanged, rule.clone()));
    }
    let admins = admins(c, owner, name, me).await?;
    let mut body = protection_body(existing.as_ref(), &admins, spec)?;
    let (change, method, segs): (Change, Method, Vec<String>) = match &existing {
        Some(rule) => {
            let rule_name = rule
                .get("rule_name")
                .and_then(Value::as_str)
                .filter(|n| !n.is_empty())
                .or_else(|| rule.get("branch_name").and_then(Value::as_str))
                .unwrap_or(&branch)
                .to_string();
            (
                Change::Update,
                Method::PATCH,
                vec![
                    "repos".into(),
                    owner.into(),
                    name.into(),
                    "branch_protections".into(),
                    rule_name,
                ],
            )
        }
        None => {
            body["rule_name"] = json!(branch);
            // Pre-1.22 instances know only `branch_name`.
            body["branch_name"] = json!(branch);
            (
                Change::Create,
                Method::POST,
                vec![
                    "repos".into(),
                    owner.into(),
                    name.into(),
                    "branch_protections".into(),
                ],
            )
        }
    };
    if !dry_run {
        let segs: Vec<&str> = segs.iter().map(String::as_str).collect();
        let after = c.send(method, &segs, &body).await?;
        if !protection_satisfies(&after, spec)? {
            bail!(
                "{owner}/{name}: the instance accepted the branch protection but it does not read \
                 back as requested"
            );
        }
    }
    Ok((change, body))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn instance_urls_are_checked() {
        assert_eq!(
            base_url("codeberg.org", None).unwrap().as_str(),
            "https://codeberg.org/"
        );
        let sub = Url::parse("https://example.org/git").unwrap();
        assert_eq!(
            base_url("example.org", Some(&sub)).unwrap().as_str(),
            "https://example.org/git/"
        );
        let lo = Url::parse("http://127.0.0.1:3000").unwrap();
        assert!(base_url("127.0.0.1", Some(&lo)).is_ok());
        let plain = Url::parse("http://git.example.org/").unwrap();
        assert!(base_url("git.example.org", Some(&plain)).is_err());
    }

    #[test]
    fn the_instance_url_must_be_on_the_repositorys_host() {
        let sub = Url::parse("https://Example.org:8443/git").unwrap();
        assert_eq!(
            base_url("example.org", Some(&sub)).unwrap().as_str(),
            "https://example.org:8443/git/"
        );
        let err = base_url("codeberg.org", Some(&sub))
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("codeberg.org") && err.contains(TOKEN_ENV),
            "{err}"
        );
        let lo = Url::parse("http://127.0.0.1:3000").unwrap();
        assert!(base_url("codeberg.org", Some(&lo)).is_err());
    }

    #[test]
    fn path_segments_are_encoded() {
        let c = Client::new(&Url::parse("https://h.example/sub/").unwrap(), "t".into()).unwrap();
        assert_eq!(
            c.url(&["repos", "a", "b", "contents", ".forgejo", "x y?"])
                .as_str(),
            "https://h.example/sub/api/v1/repos/a/b/contents/.forgejo/x%20y%3F"
        );
    }
}