Skip to content

feat: add GitLab uploader for self-hosted instances (AI生成) - #105

Merged
pluveto merged 2 commits into
mainfrom
feat/issue-79-gitlab-uploader
Sep 1, 2026
Merged

pluveto merged 2 commits into
mainfrom
feat/issue-79-gitlab-uploader

Conversation

@pluveto

@pluveto pluveto commented Sep 1, 2026

Copy link
Copy Markdown
Owner

Closes #79

First-class GitlabUploader for self-hosted / private GitLab. Not an HTTP recipe. Printed URLs default to {instance}/{project}/-/raw/{branch}/{path} and accept public_base / host so private projects are not a GitLab raw 404.

Config

[uploaders.gitlab]
url = "https://gitlab.example.com"   # instance; alias: host when url is omitted
project = "group/name"
token = "…"                          # PRIVATE-TOKEN; api or write_repository
branch = "main"                      # default main
# public_base = "https://cdn.example.com/"
# host = "https://cdn.example.com/"  # printed URL only when url is also set

Private GitLab raw links 404. Set public_base (or host next to url) to a CDN, reverse proxy, or public raw prefix.

Three giants (deliberate)

Eric Evans:
GitLab is its own bounded context, not GitHub with a different origin. The ubiquitous language is instance (url), project (group/name), token, branch, and public_base for the printed Locator — not GitHub’s pat / username / repo, and not a generic “git forge.” The Uploader message stays “put this artifact at this key.” Config is a birth record for that object (install.rs → GitlabConfig), not a recipe document. Bottom line: do not fold GitLab into GithubUploader or into HTTP JSONC; do not keep two vocabularies (PAT vs token) for one host.

John Ousterhout:
Design twice. (A) HTTP recipe: cannot hide POST-then-PUT, %2F encoding, or “API URL vs printed URL” without leaking those into every caller. Shallow. (B) First-class object: one upload / explain surface, thick internals. Done means: catalog lists gitlab after github; install_into has a gitlab arm and inference (id == "gitlab" or token+project+(url|host) before the HTTP token heuristic); explain never dumps GitLab JSON; locator is tested with and without public_base; UnknownKind lists gitlab. Do not invent a shared GitHost trait — GitHub’s SHA retry and GitLab’s create/update are different, and a common type would be a shallow leak.

Alan Kay:
GitlabUploader is an autonomous object. It owns instance, token, project encoding, the create-vs-update conversation, and how a Locator is named. Callers send upload; they do not choose POST vs PUT or encode group/name. host meaning both “GitLab server” and “CDN prefix” is a birth-parameter alias, normalized in new / gitlab_from_profile so the object’s rest of life has one instance URL and one optional public base. Do not let install.rs know about PRIVATE-TOKEN or %2F.

三个人的讨论

Evans rejected a GitHub-shaped config (username/repo/pat) as a false shared language; Kay agreed the object should speak GitLab’s words. Ousterhout rejected a GitHost superclass as a shallow module that would push SHA-vs-PUT into callers; Kay agreed two objects, one message (upload). Evans wanted host to mean one thing; Ousterhout called dual host an interface bug; Kay allowed aliases at construction only. Convergence: canonical fields are url + public_base; host is instance when url is absent, printed override when url is already set. Recipe JSONC is rejected (cannot own the 400-already-exists retry without leaking). WebDAV/SFTP/FTP (#26) stay out of this change. No Go 0.2 forward-compat.

收敛结论

状态:已收敛。

推荐路径:GitlabUploader in crates/upgit-uploaders/src/gitlab.rs, wired through HostCatalog and AppConfig::install_into. Repository Files API: POST create, PUT on HTTP 400 “already exists”; PRIVATE-TOKEN; percent-encoded project and file path; locator {url}/{project}/-/raw/{branch}/{key} unless public_base/host is set.

被否决:HTTP recipe; extending GithubUploader; a shared Git-forge trait; treating GitLab raw as always public.

残留风险:GitLab installed under a relative URL path is supported only if url includes that path; empty repositories / missing branches surface as canned 400s.

决策与后续

已决定:first-class object; config fields above; explain maps 401/403/404/429 like GitHub and never dumps JSON; tests cover locator, encoding, explain(401), install inference, and POST-then-PUT against a mock server.

待验证:a real private GitLab with public_base pointing at a reachable CDN.

How to verify

cargo test --workspace --locked
cargo clippy --workspace --locked --all-targets -- -D warnings

Copy [uploaders.gitlab] from config.sample.toml, set default = "gitlab", fill url / project / token. For a private project set public_base. Then upgit FILE. upgit uploaders should list gitlab after github.

First-class GitlabUploader talks to the Repository Files API
(POST create, PUT when the file already exists). Printed URLs
default to instance raw and accept public_base/host so private
projects are not a GitLab raw 404.
Comment thread crates/upgit-uploaders/src/gitlab.rs Outdated
Comment on lines +10 to +68
#[derive(Serialize)]
struct FileBody<'a> {
branch: &'a str,
content: String,
encoding: &'a str,
commit_message: String,
}

fn encode_file_body(branch: &str, file_name: &str, data: &[u8]) -> Result<String, UploadError> {
serde_json::to_string(&FileBody {
branch,
content: STANDARD.encode(data),
encoding: "base64",
commit_message: format!("upload {file_name} via upgit"),
})
.map_err(|e| UploadError::message(e.to_string()))
}

fn file_already_exists(status: u16, body: &str) -> bool {
status == 400 && body.to_ascii_lowercase().contains("already exists")
}

/// RFC 3986 unreserved stay literal; everything else is `%HH` (uppercase).
/// GitLab needs `/` in project and file_path as `%2F`, not as extra path segments.
fn percent_encode(s: &str) -> String {
let mut out = String::new();
for &b in s.as_bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char)
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}

fn with_scheme(url: &str) -> String {
let t = url.trim();
if t.starts_with("http://") || t.starts_with("https://") {
t.to_string()
} else {
format!("https://{t}")
}
}

fn normalize_instance(url: &str) -> String {
with_scheme(url).trim_end_matches('/').to_string()
}

fn normalize_public_base(base: &str) -> Option<String> {
let t = with_scheme(base).trim_end_matches('/').to_string();
if t.is_empty() || t == "https:" || t == "http:" {
None
} else {
Some(t)
}
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些显然并非 gitlab.rs‎ 的职责。打回重写。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten in 008d218.

URL encoding and origin normalization live in util.rs (percent_encode, http_origin). gitlab.rs is the Repository Files object: upload, explain, locator_for. encode_file_body / file_already_exists are methods on GitlabUploader.

}
}

fn resolved_kind(id: &str, profile: &UploaderProfile) -> Result<String, InstallError> {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

槽点太多。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten in 008d218.

Kind is only type = \"gitlab\" or table id gitlab — no token+project+url/host inference. url is the instance; host is solely an alias of url; public_base is the printed prefix. gitlab_from_profile is a short mapping like GitHub's.

Comment thread crates/upgit-uploaders/tests/gitlab.rs Outdated

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仔细一看,全是低价值测试,不如全删了

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 008d218: deleted crates/upgit-uploaders/tests/gitlab.rs and the install tests that only asserted field-alias soup. No replacement tests.

Move URL encoding and origin normalization into util. GitLab
install infers only from type or table id; host aliases url
only. Drop the low-value GitLab tests.
@pluveto
pluveto merged commit 3bf66d3 into main Sep 1, 2026
3 checks passed
@pluveto
pluveto deleted the feat/issue-79-gitlab-uploader branch September 1, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

希望支持私有gitlab

1 participant