Repository navigation
feat: add GitLab uploader for self-hosted instances (AI生成) - #105
Conversation
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.
| #[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) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
这些显然并非 gitlab.rs 的职责。打回重写。
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Closes #79
First-class
GitlabUploaderfor self-hosted / private GitLab. Not an HTTP recipe. Printed URLs default to{instance}/{project}/-/raw/{branch}/{path}and acceptpublic_base/hostso private projects are not a GitLab raw 404.Config
Private GitLab raw links 404. Set
public_base(orhostnext tourl) 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, andpublic_basefor the printed Locator — not GitHub’spat/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 intoGithubUploaderor 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,
%2Fencoding, or “API URL vs printed URL” without leaking those into every caller. Shallow. (B) First-class object: oneupload/explainsurface, thick internals. Done means: catalog listsgitlabaftergithub;install_intohas agitlabarm and inference (id == "gitlab"ortoken+project+(url|host)before the HTTPtokenheuristic);explainnever dumps GitLab JSON; locator is tested with and withoutpublic_base;UnknownKindlistsgitlab. Do not invent a sharedGitHosttrait — GitHub’s SHA retry and GitLab’s create/update are different, and a common type would be a shallow leak.Alan Kay:
GitlabUploaderis an autonomous object. It owns instance, token, project encoding, the create-vs-update conversation, and how a Locator is named. Callers sendupload; they do not choose POST vs PUT or encodegroup/name.hostmeaning both “GitLab server” and “CDN prefix” is a birth-parameter alias, normalized innew/gitlab_from_profileso the object’s rest of life has one instance URL and one optional public base. Do not letinstall.rsknow aboutPRIVATE-TOKENor%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 aGitHostsuperclass as a shallow module that would push SHA-vs-PUT into callers; Kay agreed two objects, one message (upload). Evans wantedhostto mean one thing; Ousterhout called dualhostan interface bug; Kay allowed aliases at construction only. Convergence: canonical fields areurl+public_base;hostis instance whenurlis absent, printed override whenurlis 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.收敛结论
状态:已收敛。
推荐路径:
GitlabUploaderincrates/upgit-uploaders/src/gitlab.rs, wired throughHostCatalogandAppConfig::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}unlesspublic_base/hostis 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
urlincludes that path; empty repositories / missing branches surface as canned 400s.决策与后续
已决定:first-class object; config fields above;
explainmaps 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_basepointing at a reachable CDN.How to verify
cargo test --workspace --locked cargo clippy --workspace --locked --all-targets -- -D warningsCopy
[uploaders.gitlab]fromconfig.sample.toml, setdefault = "gitlab", fillurl/project/token. For a private project setpublic_base. Thenupgit FILE.upgit uploadersshould listgitlabaftergithub.