garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

cartopolis-agent · 2026-11-27T17:44:53Z

fix(hosted): --namespace, not --owner, says whose a new project is; an e2e of a hosted project

--owner is every command's session (MIDAS_OWNER), so a project made from a
session was put under the session's name. tests/e2e/hosted.sh makes a project
on a scratch hub, clones it over public HTTP, pushes a branch and lands main
through the hooks, and reads its pages and archive; a private one 404s.
src/cli.rs          |  4 +--
 src/hosted.rs       |  3 ++-
 src/main.rs         |  5 ++--
 tests/e2e/hosted.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/src/cli.rs b/src/cli.rs
index 405325b..6156456 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -859,9 +859,9 @@ pub fn project_act(a: crate::ProjectCmd, owner: &str) -> Result<i32> {
         Ok(r.trim_end_matches(".git").to_string())
     };
     let v = match a {
-        P::Create { name, description, public, gate, gate_nodes, owner: repo_owner } => client::post("/v1/projects/create", &json!({
+        P::Create { name, description, public, gate, gate_nodes, namespace } => client::post("/v1/projects/create", &json!({
             "owner": owner, "name": name, "description": description, "public": public,
-            "gate": gate, "gate_nodes": gate_nodes, "repo_owner": repo_owner,
+            "gate": gate, "gate_nodes": gate_nodes, "repo_owner": namespace,
         }))?,
         P::Set { repo, gate, gate_nodes, pre_land, description, public, private } => {
             let mut b = json!({"owner": owner, "repo": repo_path(&repo)?});
diff --git a/src/hosted.rs b/src/hosted.rs
index 565dda2..73381f1 100644
--- a/src/hosted.rs
+++ b/src/hosted.rs
@@ -163,7 +163,8 @@ fn conf_path() -> String {
 
 /// The hooks every hosted repository runs: they hand over to this binary.
 fn install_hooks(dir: &Path) -> Result<()> {
-    let bin = std::env::current_exe().ok().filter(|p| p.starts_with("/usr/local/bin"))
+    // The hub's own binary (/usr/local/bin/midas on the hub; a scratch hub's own in tests/e2e).
+    let bin = std::env::current_exe().ok()
         .map_or_else(|| crate::hubsetup::BIN.to_string(), |p| p.display().to_string());
     for hook in ["pre-receive", "post-receive"] {
         let f = dir.join("hooks").join(hook);
diff --git a/src/main.rs b/src/main.rs
index 61cc542..d90c482 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1238,9 +1238,10 @@ pub enum ProjectCmd {
         gate: Option<String>,
         #[arg(long)]
         gate_nodes: Option<String>,
-        /// Whose namespace (default: the workspace's first project's owner).
+        /// Whose namespace (default: the workspace's first project's owner). Not
+        /// `--owner`, which is every command's session.
         #[arg(long)]
-        owner: Option<String>,
+        namespace: Option<String>,
     },
     /// Change a hosted project's landing or description (`--public` is the person's).
     Set {
diff --git a/tests/e2e/hosted.sh b/tests/e2e/hosted.sh
new file mode 100755
index 0000000..b62e866
--- /dev/null
+++ b/tests/e2e/hosted.sh
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+# A project the hub hosts, end to end on a scratch hub (src/hosted.rs): made
+# through the API, cloned over public HTTP, pushed through its hooks with an
+# agent's role — a branch yes, main only from the landing with a pass — read
+# on its pages and as an archive, and a private one not found.
+#
+#   cargo build && tests/e2e/hosted.sh [target/debug/midas]
+#
+# The ssh login (`midas hub git --key`) only sets MIDAS_GIT_ROLE and runs
+# git receive-pack; here a local push sets the role itself.
+set -euo pipefail
+midas=$(realpath "${1:-target/debug/midas}")
+e2e=$(mktemp -d)
+trap 'kill "${hub_pid:-}" 2>/dev/null; rm -rf "$e2e"' EXIT
+fail() { echo "hosted e2e: $*" >&2; [ -f "$e2e/hub.log" ] && tail -20 "$e2e/hub.log" >&2; exit 1; }
+port=$((40000 + RANDOM % 20000))
+hub=http://127.0.0.1:$port
+key=$(head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n')
+export MIDAS_STATE=$e2e/state MIDAS_HUB_CONF=$e2e/hub.conf
+mkdir -p "$MIDAS_STATE"
+printf '{"listen": "127.0.0.1:%s", "forgejo_ssh": "", "forgejo_api": "", "repos": []}\n' "$port" > "$MIDAS_HUB_CONF"
+printf '{"e2e": {"key": "%s"}}\n' "$key" > "$e2e/callers.json"
+MIDAS_CALLERS=$e2e/callers.json "$midas" hub serve --listen "127.0.0.1:$port" > "$e2e/hub.log" 2>&1 &
+hub_pid=$!
+for _ in $(seq 50); do curl -fs "$hub/v1/health" >/dev/null 2>&1 && break; sleep 0.2; done
+curl -fs "$hub/v1/health" >/dev/null || fail "the hub did not come up"
+git_id=(-c user.name=e2e -c user.email=e2e@e2e)
+
+echo "== made through the API"
+MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" project create demo --public --gate true \
+    --description "An e2e project" > "$e2e/made.json" || fail "project create"
+repo=$(sed -n 's/.*"repo": "\(.*\)".*/\1/p' "$e2e/made.json")
+[ "$repo" = midas/demo ] || fail "not in the workspace's namespace: $(cat "$e2e/made.json")"
+MIDAS_HUB=$hub MIDAS_HUB_KEY=$key HOME=$e2e "$midas" project create hidden --description "private" >/dev/null || fail "private create"
+bare=$MIDAS_STATE/git/$repo.git
+echo "   $repo at $bare"
+
+echo "== cloned over public HTTP, without a login"
+git clone -q "$hub/git/$repo.git" "$e2e/clone" || fail "public clone"
+grep -q "An e2e project" "$e2e/clone/README.md" || fail "README not cloned"
+if git clone -q "$hub/git/${repo%/*}/hidden.git" "$e2e/nope" 2>/dev/null; then fail "a private repository cloned"; fi
+
+echo "== an agent pushes a branch"
+cd "$e2e/clone"
+echo "more" >> README.md
+git "${git_id[@]}" commit -qam "more"
+head=$(git rev-parse HEAD)
+export MIDAS_GIT_ROLE=agent MIDAS_GIT_KEY=e2e
+git push -q "$bare" HEAD:refs/heads/session/s-e2e || fail "branch push refused"
+
+echo "== main: refused without a landing, a turn and a pass"
+if git push -q "$bare" HEAD:refs/heads/main 2> "$e2e/err"; then fail "main moved without a landing"; fi
+grep -q "moved by a landing" "$e2e/err" || fail "unexpected refusal: $(cat "$e2e/err")"
+MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" land take --purpose e2e >/dev/null 2>&1 || fail "land take"
+if git push -q -o midas-owner=s-e2e "$bare" HEAD:refs/heads/main 2> "$e2e/err"; then fail "main moved with no pass"; fi
+grep -q "no gate pass" "$e2e/err" || fail "unexpected refusal: $(cat "$e2e/err")"
+MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" verified --sha "$head" --node e2e >/dev/null 2>&1 || fail "verified"
+git push -q -o midas-owner=s-e2e "$bare" HEAD:refs/heads/main || fail "the landing's push refused"
+[ "$(git -C "$bare" rev-parse main)" = "$head" ] || fail "main is not the landed head"
+
+echo "== read on its pages and as an archive"
+curl -fs "$hub/git/$repo" | grep -q "An e2e project" || fail "home page"
+curl -fs "$hub/git/$repo/log" | grep -q "more" || fail "log page"
+curl -fs "$hub/git/$repo/commit/$head" | grep -q "+more" || fail "commit page"
+curl -fs "$hub/git/$repo/archive/main.tar.gz" | tar tz | grep -q "demo/README.md" || fail "archive"
+code=$(curl -s -o /dev/null -w '%{http_code}' "$hub/git/${repo%/*}/hidden")
+[ "$code" = 404 ] || fail "a private repository's page answered $code"
+MIDAS_HUB=$hub MIDAS_HUB_KEY=$key HOME=$e2e "$midas" project log "$repo" | grep -q "more" || fail "project log"
+
+echo "hosted e2e: all passed"