rototo 0.1.0-alpha.6

Control plane for runtime configuration of your application.
Documentation
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
681
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use serde::Deserialize;
use serde_json::{Value as JsonValue, json};

use super::error::{GitHubApiError, GitHubError, GitHubResult};
use super::source::{enc, encode_repo_path, manifest_package_path, package_git_source};
use super::{GITHUB_API, GITHUB_USER_AGENT};

/// GitHub viewer identity returned by `/user`.
///
/// Auth code converts this into `ActorIdentity` and session/user records. The
/// DTO itself is transient and exists only while handling token validation or
/// sign-in.
#[derive(Clone, Debug, Deserialize)]
pub struct GitHubUser {
    pub id: i64,
    pub login: String,
    pub name: Option<String>,
    pub avatar_url: Option<String>,
}

/// Repository metadata returned by GitHub.
///
/// Registration and permission checks read this DTO, then persist only the
/// rototo console repo fields needed for future package discovery.
#[derive(Clone, Debug, Deserialize)]
pub struct GitHubRepo {
    pub name: String,
    pub owner: GitHubRepoOwner,
    pub default_branch: String,
    pub permissions: Option<GitHubRepoPermissions>,
}

/// Owner object nested inside GitHub repository responses.
///
/// It exists because GitHub returns owner metadata as an object while rototo's
/// store only needs the login string.
#[derive(Clone, Debug, Deserialize)]
pub struct GitHubRepoOwner {
    pub login: String,
}

/// GitHub permission flags used to decide whether a credential can write.
///
/// The client reads this during a permission check and discards it after
/// choosing whether a branch mutation may continue.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
pub struct GitHubRepoPermissions {
    #[serde(default)]
    pub admin: bool,
    #[serde(default)]
    pub push: bool,
}

/// Rototo package found in a GitHub repository tree.
///
/// Discovery creates these from `rototo-package.toml` blobs. Store code
/// upserts them into durable `packages` rows for the registering principal.
#[derive(Clone, Debug)]
pub struct DiscoveredPackage {
    pub path: String,
    pub git_ref: String,
    pub source: String,
}

/// File content plus blob SHA returned by GitHub contents API.
///
/// Branch save/delete paths need both pieces: content to diff or edit, and SHA
/// to perform GitHub's optimistic update. The value lives only for one file
/// operation.
#[derive(Clone, Debug)]
pub struct GitHubContentFile {
    pub sha: String,
    pub content: String,
}

/// Pull request metadata returned by GitHub.
///
/// Publish and sync routes copy these fields into a branch row so the
/// UI can show PR state without requiring a GitHub request on every render.
#[derive(Clone, Debug, Deserialize)]
pub struct GitHubPullRequest {
    pub html_url: String,
    pub number: i64,
    pub state: Option<String>,
    pub merged_at: Option<String>,
}

/// One entry from a GitHub recursive tree response.
///
/// Discovery, entity creation conflict checks, and branch scans use these to
/// find blobs. They are transient results of GitHub API calls.
#[derive(Clone, Debug, Deserialize)]
pub struct GitHubTreeEntry {
    pub path: String,
    #[serde(rename = "type")]
    pub entry_type: String,
}

/// Branch comparison summary returned by GitHub.
///
/// Branch-candidate scans use it to decide whether a branch changes only files
/// in the package path. It is filtered into a UI summary and not persisted.
#[derive(Clone, Debug)]
pub struct RefComparison {
    pub ahead_by: i64,
    pub files: Vec<String>,
}

/// Small async GitHub REST client used by the console server.
///
/// The client owns a reusable `reqwest::Client` and lives in `ConsoleState` for
/// the process lifetime. It does not store credentials; every call receives the
/// current user's token explicitly.
#[derive(Clone)]
pub struct GitHubClient {
    http: reqwest::Client,
}

impl GitHubClient {
    pub fn new() -> Self {
        Self {
            http: reqwest::Client::new(),
        }
    }

    pub async fn viewer(&self, token: &str) -> GitHubResult<GitHubUser> {
        self.get(token, "/user").await
    }

    pub async fn repo(&self, token: &str, owner: &str, name: &str) -> GitHubResult<GitHubRepo> {
        self.get(token, &format!("/repos/{}/{}", enc(owner), enc(name)))
            .await
    }

    pub async fn assert_repo_write_access(
        &self,
        token: &str,
        owner: &str,
        name: &str,
    ) -> GitHubResult<()> {
        let repo = self.repo(token, owner, name).await?;
        if let Some(permissions) = repo.permissions
            && !permissions.push
            && !permissions.admin
        {
            return Err(GitHubError::Other(format!(
                "Your GitHub credential can read {owner}/{name}, but cannot push to it. Grant \
                 repository write access before editing this package."
            )));
        }
        Ok(())
    }

    pub async fn discover_packages(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        git_ref: &str,
    ) -> GitHubResult<Vec<DiscoveredPackage>> {
        let tree = self.tree(token, owner, name, git_ref).await?;
        let mut packages: Vec<DiscoveredPackage> = tree
            .into_iter()
            .filter(|entry| {
                entry.entry_type == "blob" && entry.path.ends_with("rototo-package.toml")
            })
            .map(|entry| {
                let path = manifest_package_path(&entry.path);
                DiscoveredPackage {
                    source: package_git_source(owner, name, git_ref, &path),
                    path,
                    git_ref: git_ref.to_owned(),
                }
            })
            .collect();
        packages.sort_by(|left, right| left.path.cmp(&right.path));
        Ok(packages)
    }

    pub async fn branch_head_sha(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        branch: &str,
    ) -> GitHubResult<String> {
        /// Git ref lookup response from GitHub.
        ///
        /// The method validates that the ref points at a commit and returns
        /// only the SHA needed for branch creation or write checks.
        #[derive(Deserialize)]
        struct RefResponse {
            object: RefObject,
        }
        /// Nested git object in a GitHub ref response.
        ///
        /// It lives only long enough to reject refs that do not resolve to a
        /// commit object.
        #[derive(Deserialize)]
        struct RefObject {
            sha: String,
            #[serde(rename = "type")]
            object_type: String,
        }
        let reference: RefResponse = self
            .get(
                token,
                &format!(
                    "/repos/{}/{}/git/ref/{}",
                    enc(owner),
                    enc(name),
                    enc(&format!("heads/{branch}"))
                ),
            )
            .await?;
        if reference.object.object_type != "commit" {
            return Err(GitHubError::Other(format!(
                "GitHub ref {branch} does not point to a commit"
            )));
        }
        Ok(reference.object.sha)
    }

    pub async fn list_branches(
        &self,
        token: &str,
        owner: &str,
        name: &str,
    ) -> GitHubResult<Vec<String>> {
        /// Branch list item returned by GitHub.
        ///
        /// The client keeps only branch names for branch-candidate scanning.
        #[derive(Deserialize)]
        struct Branch {
            name: String,
        }
        let branches: Vec<Branch> = self
            .get(
                token,
                &format!("/repos/{}/{}/branches?per_page=100", enc(owner), enc(name)),
            )
            .await?;
        Ok(branches.into_iter().map(|branch| branch.name).collect())
    }

    pub async fn compare_refs(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        base: &str,
        head: &str,
    ) -> GitHubResult<RefComparison> {
        /// GitHub compare response subset used by branch candidate scans.
        ///
        /// It is converted immediately into `RefComparison`.
        #[derive(Deserialize)]
        struct Comparison {
            ahead_by: i64,
            #[serde(default)]
            files: Vec<ComparisonFile>,
        }
        /// Changed-file item nested in a GitHub compare response.
        ///
        /// Only the filename is needed to decide whether a branch touches the
        /// package path.
        #[derive(Deserialize)]
        struct ComparisonFile {
            filename: String,
        }
        let comparison: Comparison = self
            .get(
                token,
                &format!(
                    "/repos/{}/{}/compare/{}...{}",
                    enc(owner),
                    enc(name),
                    enc(base),
                    enc(head)
                ),
            )
            .await?;
        Ok(RefComparison {
            ahead_by: comparison.ahead_by,
            files: comparison
                .files
                .into_iter()
                .map(|file| file.filename)
                .collect(),
        })
    }

    pub async fn create_branch(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        branch: &str,
        sha: &str,
    ) -> GitHubResult<()> {
        let _: JsonValue = self
            .send(
                token,
                reqwest::Method::POST,
                &format!("/repos/{}/{}/git/refs", enc(owner), enc(name)),
                Some(json!({ "ref": format!("refs/heads/{branch}"), "sha": sha })),
            )
            .await?;
        Ok(())
    }

    pub async fn rename_branch(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        branch: &str,
        new_name: &str,
    ) -> GitHubResult<String> {
        /// GitHub branch-rename response body.
        ///
        /// The route persists the returned name on the branch row and discards
        /// the raw response shape.
        #[derive(Deserialize)]
        struct Renamed {
            name: String,
        }
        let renamed: Renamed = self
            .send(
                token,
                reqwest::Method::POST,
                &format!(
                    "/repos/{}/{}/branches/{}/rename",
                    enc(owner),
                    enc(name),
                    enc(branch)
                ),
                Some(json!({ "new_name": new_name })),
            )
            .await?;
        Ok(renamed.name)
    }

    pub async fn file(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        path: &str,
        git_ref: &str,
    ) -> GitHubResult<GitHubContentFile> {
        /// GitHub contents response for a file.
        ///
        /// The client validates it is a base64 file blob, decodes the content,
        /// and returns `GitHubContentFile` for one branch operation.
        #[derive(Deserialize)]
        struct Content {
            #[serde(rename = "type")]
            content_type: String,
            sha: String,
            encoding: Option<String>,
            content: Option<String>,
        }
        let file: Content = self
            .get(
                token,
                &format!(
                    "/repos/{}/{}/contents/{}?ref={}",
                    enc(owner),
                    enc(name),
                    encode_repo_path(path),
                    enc(git_ref)
                ),
            )
            .await?;
        let (Some(encoding), Some(content)) = (file.encoding, file.content) else {
            return Err(GitHubError::Other(format!(
                "GitHub path is not a readable file: {path}"
            )));
        };
        if file.content_type != "file" || encoding != "base64" {
            return Err(GitHubError::Other(format!(
                "GitHub path is not a readable file: {path}"
            )));
        }
        let bytes = BASE64
            .decode(content.replace('\n', ""))
            .map_err(GitHubError::other)?;
        Ok(GitHubContentFile {
            sha: file.sha,
            content: String::from_utf8(bytes).map_err(GitHubError::other)?,
        })
    }

    #[expect(clippy::too_many_arguments)]
    pub async fn update_file(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        path: &str,
        branch: &str,
        sha: &str,
        content: &str,
        message: &str,
    ) -> GitHubResult<()> {
        let _: JsonValue = self
            .send(
                token,
                reqwest::Method::PUT,
                &format!(
                    "/repos/{}/{}/contents/{}",
                    enc(owner),
                    enc(name),
                    encode_repo_path(path)
                ),
                Some(json!({
                    "message": message,
                    "content": BASE64.encode(content.as_bytes()),
                    "sha": sha,
                    "branch": branch,
                })),
            )
            .await?;
        Ok(())
    }

    #[expect(clippy::too_many_arguments)]
    pub async fn create_file(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        path: &str,
        branch: &str,
        content: &str,
        message: &str,
    ) -> GitHubResult<()> {
        let _: JsonValue = self
            .send(
                token,
                reqwest::Method::PUT,
                &format!(
                    "/repos/{}/{}/contents/{}",
                    enc(owner),
                    enc(name),
                    encode_repo_path(path)
                ),
                Some(json!({
                    "message": message,
                    "content": BASE64.encode(content.as_bytes()),
                    "branch": branch,
                })),
            )
            .await?;
        Ok(())
    }

    #[expect(clippy::too_many_arguments)]
    pub async fn delete_file(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        path: &str,
        branch: &str,
        sha: &str,
        message: &str,
    ) -> GitHubResult<()> {
        let _: JsonValue = self
            .send(
                token,
                reqwest::Method::DELETE,
                &format!(
                    "/repos/{}/{}/contents/{}",
                    enc(owner),
                    enc(name),
                    encode_repo_path(path)
                ),
                Some(json!({ "message": message, "sha": sha, "branch": branch })),
            )
            .await?;
        Ok(())
    }

    pub async fn tree(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        git_ref: &str,
    ) -> GitHubResult<Vec<GitHubTreeEntry>> {
        /// GitHub recursive tree response.
        ///
        /// The client rejects truncated trees because discovery and conflict
        /// checks need a complete file list for the requested ref.
        #[derive(Deserialize)]
        struct TreeResponse {
            truncated: bool,
            tree: Vec<GitHubTreeEntry>,
        }
        let tree: TreeResponse = self
            .get(
                token,
                &format!(
                    "/repos/{}/{}/git/trees/{}?recursive=1",
                    enc(owner),
                    enc(name),
                    enc(git_ref)
                ),
            )
            .await?;
        if tree.truncated {
            return Err(GitHubError::Other(
                "GitHub tree response was truncated".to_owned(),
            ));
        }
        Ok(tree.tree)
    }

    #[expect(clippy::too_many_arguments)]
    pub async fn create_pull_request(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        title: &str,
        body: &str,
        head: &str,
        base: &str,
    ) -> GitHubResult<GitHubPullRequest> {
        self.send(
            token,
            reqwest::Method::POST,
            &format!("/repos/{}/{}/pulls", enc(owner), enc(name)),
            Some(json!({
                "title": title,
                "body": body,
                "head": head,
                "base": base,
                "maintainer_can_modify": true,
            })),
        )
        .await
    }

    pub async fn pull_request(
        &self,
        token: &str,
        owner: &str,
        name: &str,
        number: i64,
    ) -> GitHubResult<GitHubPullRequest> {
        self.get(
            token,
            &format!("/repos/{}/{}/pulls/{number}", enc(owner), enc(name)),
        )
        .await
    }

    async fn get<T: serde::de::DeserializeOwned>(
        &self,
        token: &str,
        path: &str,
    ) -> GitHubResult<T> {
        self.send(token, reqwest::Method::GET, path, None).await
    }

    async fn send<T: serde::de::DeserializeOwned>(
        &self,
        token: &str,
        method: reqwest::Method,
        path: &str,
        body: Option<JsonValue>,
    ) -> GitHubResult<T> {
        let started = std::time::Instant::now();
        let method_label = method.as_str().to_owned();
        let path_pattern = github_path_pattern(path);
        tracing::debug!(
            operation = "github.rest",
            method = %method_label,
            path = %path_pattern,
            "console outbound GitHub REST call started"
        );
        let mut request = self
            .http
            .request(method, format!("{GITHUB_API}{path}"))
            .header("Accept", "application/vnd.github+json")
            .header("Authorization", format!("Bearer {token}"))
            .header("Content-Type", "application/json")
            .header("User-Agent", GITHUB_USER_AGENT)
            .header("X-GitHub-Api-Version", "2022-11-28");
        if let Some(body) = body {
            request = request.json(&body);
        }
        let response = match request.send().await {
            Ok(response) => response,
            Err(err) => {
                tracing::warn!(
                    operation = "github.rest",
                    method = %method_label,
                    path = %path_pattern,
                    error = %err,
                    latency_ms = started.elapsed().as_millis(),
                    "console outbound GitHub REST call failed before response"
                );
                return Err(GitHubError::other(err));
            }
        };
        let status = response.status();
        let text = match response.text().await {
            Ok(text) => text,
            Err(err) => {
                tracing::warn!(
                    operation = "github.rest",
                    method = %method_label,
                    path = %path_pattern,
                    status = status.as_u16(),
                    error = %err,
                    latency_ms = started.elapsed().as_millis(),
                    "console outbound GitHub REST response read failed"
                );
                return Err(GitHubError::other(err));
            }
        };
        if status.is_success() {
            tracing::info!(
                operation = "github.rest",
                method = %method_label,
                path = %path_pattern,
                status = status.as_u16(),
                latency_ms = started.elapsed().as_millis(),
                "console outbound GitHub REST call completed"
            );
        } else {
            tracing::warn!(
                operation = "github.rest",
                method = %method_label,
                path = %path_pattern,
                status = status.as_u16(),
                latency_ms = started.elapsed().as_millis(),
                "console outbound GitHub REST call returned error status"
            );
        }
        if !status.is_success() {
            return Err(GitHubError::Api(GitHubApiError {
                status: status.as_u16(),
                response_text: text,
            }));
        }
        serde_json::from_str(&text).map_err(|err| {
            tracing::warn!(
                operation = "github.rest",
                method = %method_label,
                path = %path_pattern,
                status = status.as_u16(),
                error = %err,
                latency_ms = started.elapsed().as_millis(),
                "console outbound GitHub REST response decode failed"
            );
            GitHubError::other(err)
        })
    }
}

fn github_path_pattern(path: &str) -> String {
    path.split('/')
        .map(|segment| {
            if segment.chars().all(|ch| ch.is_ascii_digit()) {
                ":number"
            } else if segment.len() >= 32 && segment.chars().all(|ch| ch.is_ascii_hexdigit()) {
                ":sha"
            } else {
                segment
            }
        })
        .collect::<Vec<_>>()
        .join("/")
}