Rust

VCS

Version control system adapters: GitHub, GitLab, Gitea, Forgejo, Gogs, Bitbucket and Origin: repositories, branches, tags, files, commits, pull requests, comments, statuses and check runs, webhook parsing and signature validation, app installation tokens, and shallow sparse clone commands.
Rust 9,648 lines (8,058 code)split
PartCodeCommentsBlankTotalFiles
Rust source8,0589226689,64814

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-vcs = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with VCS, each a complete program you can copy and run.

Getting started

utopia-vcs talks to Git providers (GitHub, GitLab, Gitea, Forgejo, Gogs, Bitbucket and Origin) through one contract: the Adapter and Git traits. An adapter reads and writes repositories, branches, files, commits, pull requests, comments, statuses and check runs; parses the provider's webhook deliveries into events and checks their signatures; and builds the shell command that clones one version of a repository. Payload-shaped results are php_std::Value arrays, key for key what the PHP library returns.

Read a webhook delivery

Check the delivery's signature with the secret the webhook was created with, then turn the payload into events. Neither step reaches the provider.

Rust
use std::sync::Arc;
use utopia_cache::{Cache, None as NoCache};
use utopia_vcs::{Adapter, Gitea};

let gitea = Gitea::new(Arc::new(Cache::from_dyn(Arc::new(NoCache))));
let payload = br#"{"ref":"refs/heads/main","after":"def456","created":false,"deleted":false,
    "repository":{"id":123,"name":"website","full_name":"acme/website","html_url":"https://gitea.example.com/acme/website","owner":{"login":"acme"}},
    "sender":{"login":"ada"},"head_commit":{"id":"def456","message":"Fix","url":"https://gitea.example.com/acme/website/commit/def456","author":{"name":"Ada","email":"ada@example.com"}},
    "commits":[{"id":"def456","added":["a.txt"],"removed":[],"modified":[]}]}"#;

// x-gitea-signature: the hex HMAC-SHA256 of the body
let signature = b"not-the-signature";
assert!(!gitea.validate_webhook_event(payload, signature, b"webhook-secret"));

let events = gitea.events("push", payload).expect("a push payload");
let event = php_std::json::encode(&events[0], php_std::json::Flags::NONE, 512).unwrap();
assert!(event.contains(r#""branch":"main""#));

Build a clone command

The command checks out one branch, tag (or tag glob) or commit, shallow and sparse: only rootDirectory lands on disk. It runs in a shell; every argument is quoted.

Rust
use std::sync::Arc;
use utopia_cache::{Cache, None as NoCache};
use utopia_vcs::{clone_type, Adapter, Gitea};

let mut gitea = Gitea::new(Arc::new(Cache::from_dyn(Arc::new(NoCache))));
gitea.set_endpoint("https://gitea.example.com");
gitea.initialize("", "", None, Some("token"), None).await?;

let command = gitea.clone_command("acme", "website", "v1.*", clone_type::TAG, "/tmp/build", "apps/web")?;
println!("{command}");

Authenticate a GitHub App

initialize signs an RS256 app JWT with the app's private key (PEM, base64 PEM or PEM with escaped newlines) and exchanges it for an installation token, cached under the installation id. Both runtimes read each other's cache entries.

Rust
use std::sync::Arc;
use utopia_cache::{Cache, Memory};
use utopia_vcs::{Adapter, Git, GitHub};

let mut github = GitHub::new(Arc::new(Cache::from_dyn(Arc::new(Memory::new()))));
github.initialize("12345678", private_key, Some("123456"), None, None).await?;

let owner = github.owner_name("12345678", None).await?;
let branches = github.list_branches(&owner, "website").await?;
let latest = github.latest_commit(&owner, "website", "main").await?;
Compiles in CI; needs a running service to execute.

Report a build as a check run

CheckRun carries createCheckRun()'s named parameters; CheckRun::new starts one queued.

Rust
use utopia_vcs::{Adapter, CheckRun, Git, GitHub};

let mut run = CheckRun::new("appwrite/build");
run.status = "in_progress".into();
let created = github.create_check_run("acme", "website", sha, &run).await?;

let mut done = CheckRun::new("appwrite/build");
done.status = "completed".into();
done.conclusion = "success".into();
done.title = "Build".into();
done.summary = "Deployed".into();
let id = php_std::json::encode(&created, php_std::json::Flags::NONE, 512).unwrap_or_default();
Compiles in CI; needs a running service to execute.

Reference#

Version control system adapters: GitHub, GitLab, Gitea, Forgejo, Gogs, Bitbucket and Origin behind one contract.

An adapter (Adapter, Git) reads and writes a provider's repositories, branches, tags, files, commits, pull requests, comments, commit statuses and check runs over its REST API, parses its webhook deliveries into events and validates their signatures, and builds the shallow, sparse clone command a build checks a version out with. Providers that authenticate apps (GitHub, Origin) exchange a signed app JWT for an installation token, cached in a utopia_cache::Cache.

Rust
use std::sync::Arc;
use utopia_cache::{Cache, None as NoCache};
use utopia_vcs::{Adapter, Gitea};

let mut gitea = Gitea::new(Arc::new(Cache::from_dyn(Arc::new(NoCache))));
gitea.set_endpoint("https://gitea.example.com");
gitea.initialize("", "", None, Some("token"), None).await?;
let branches = gitea.list_branches("acme", "website").await?;
Compiles in CI; needs a running service to execute.

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_vcs exports at its root.