diff --git a/docs/self-host/hybrid.md b/docs/self-host/hybrid.md index 1f618cba9..e0df7b296 100644 --- a/docs/self-host/hybrid.md +++ b/docs/self-host/hybrid.md @@ -11,11 +11,11 @@ A **hybrid** install runs entirely in your AWS account, like any self-hosted Cod | **A CoderHelm address**, `yourname.coderhelm.com` | buying a domain and adding DNS records | a real address with HTTPS in a few minutes, set up end to end | | **CoderHelm's GitHub App**, through the event relay | creating and keeping your own GitHub App | install one app on your organization; events and tokens arrive at your install | | **CoderHelm's Jira app**, through the event relay | setting up your own Jira app | link your Jira site once; its events arrive at your install, without the app's secret | -| **CoderHelm's integration apps** (Notion, Confluence, Google Drive) | registering an app with each provider | sign in with one click, as on coderhelm.com | +| **CoderHelm's integration apps** (Notion, Confluence, Google Drive, Figma, and any CoderHelm adds later) | registering an app with each provider | sign in with one click, as on coderhelm.com | Each part is a separate choice, at install time (the [install guide](/self-host/install/)) or later in **Settings, System, Hybrid**. **Fully private** (your own domain or the AWS address, your own GitHub App and apps) sends nothing to CoderHelm and needs no registration. -On a hybrid install, every tool under **Other apps** (Jira, Notion, Confluence, Google Drive) uses CoderHelm's app unless you choose **Use my own app (fully private)** for it ([Other apps](#other-apps)). That only decides which app a sign-in goes through: nothing reaches your install or CoderHelm until someone connects the tool. +On a hybrid install, **Other apps** is one choice for the whole install: **Use Coderhelm's apps (easiest)**, the default, or **Fully private** ([Other apps](#other-apps)). That only decides which app a sign-in goes through: nothing reaches your install or CoderHelm until someone connects a tool. ## Trust model @@ -148,12 +148,20 @@ The Setup checks show the link as `hybrid_jira` (in Settings, Setup and the inst ## Other apps -**Settings, System, Hybrid, Other apps** lists each tool with **Use Coderhelm's app (easiest)** or **Use my own app (fully private)**, and one line on what passes through CoderHelm for it. The install guide offers the same choices under **What CoderHelm sets up for you**, all on, and applies them when the install registers. +**Settings, System, Hybrid, Other apps** is one choice for the whole install. The install guide offers the same choice under **What CoderHelm sets up for you** and applies it when the install registers. -- **CoderHelm's app is the default** for each tool nobody chose, on a new install and on an install that registered before these choices existed. A tool for which your install already has its own app set up (its own Notion, Atlassian or Google client in its secret) keeps using that until you pick CoderHelm's. -- **A choice is kept.** Picking **Use my own app** for a tool, in Settings or the guide, stays until you change it. An install whose admin had already turned some tools on under Other apps keeps the others on its own app. -- **Nothing starts by itself.** With CoderHelm's app chosen, **Connect** on that tool signs in through CoderHelm's app; until someone connects, no sign-in exists, no event is relayed and nothing is synced. Jira events come through only once CoderHelm's Jira app is installed on your site with your install id, or a site is linked. -- **A tool CoderHelm can't offer yet** (its app for that provider isn't set up on the hosted side) shows **Coderhelm's app isn't set up yet** and uses your own app until it is. +- **Use Coderhelm's apps (easiest)** is the default. Every tool CoderHelm has an app for uses it: today Jira, Notion, Confluence, Google Drive and Figma. A tool CoderHelm adds in a later release is on as soon as your install runs that release, with nothing more to choose. +- **Fully private** means no tool signs in or sends events through CoderHelm. Each tool uses your team's own app, which you set up on that tool's card in **Settings, Integrations**. + +GitHub is not part of this choice: it keeps its own, under **GitHub** on the same page. + +A few things hold either way: + +- **Your own app wins.** A tool for which your install has its own app set up (its own Notion, Atlassian, Google or Figma client in its secret) always uses that app, whichever option you chose. +- **Nothing starts by itself.** With CoderHelm's apps, **Connect** on a tool signs in through CoderHelm's app; until someone connects, no sign-in exists, no event is relayed and nothing is synced. Jira events come through only once CoderHelm's Jira app is installed on your site with your install id, or a site is linked. +- **A tool CoderHelm can't offer yet** (its app for that provider isn't set up on the hosted side) is shown as not ready and uses your own app until it is. + +An install that chose per tool in an earlier release moves to this choice once, on its first start after the update: if any tool was on CoderHelm's app, it gets **Use Coderhelm's apps**; if every tool chosen was on its own app, it gets **Fully private**. For one release, the settings API still takes the older `apps` (per tool) and `integrations` fields: any tool on CoderHelm's app means CoderHelm's apps, and only own apps means fully private. New callers send `apps_mode`, `coderhelm` or `private`. ## Integrations diff --git a/docs/self-host/integrations.md b/docs/self-host/integrations.md index 9e246cc05..228a658cb 100644 --- a/docs/self-host/integrations.md +++ b/docs/self-host/integrations.md @@ -102,18 +102,18 @@ New Relic, Confluence, OpenSearch, REST APIs, SQL databases, CloudWatch metrics Nothing. Servers that support OAuth dynamic client registration register your install by themselves the first time someone signs in. The redirect URI is your install's `https://coderhelm.example.com/settings/integrations/mcp-callback/`, and the add flow shows it for servers that need an app registered by hand. - **Figma**\ Figma's MCP server signs in only apps Figma approved, so CoderHelm runs Figma's tools against Figma's API with a sign-in (or a personal access token). Three ways, in Settings, Integrations, Figma: - - **Sign in with Figma** (CoderHelm's app): with hybrid, choose CoderHelm's app for Figma (Settings, Version and updates, Hybrid, or on the Figma card) and sign in. Nothing to create. CoderHelm keeps the sign-in (encrypted) and hands your install access tokens that expire within ten minutes; agents call Figma from your install. + - **Sign in with Figma** (CoderHelm's app): on a hybrid install that uses Coderhelm's apps (the default, in Settings, System, Hybrid, Other apps), sign in. Nothing to create. CoderHelm keeps the sign-in (encrypted) and hands your install access tokens that expire within ten minutes; agents call Figma from your install. - **Use my own Figma app**: create the app at https://www.figma.com/developers/apps under your Figma team or organization, add the redirect URI above, give it the `current_user:read` and `file_content:read` scopes, and publish it as a private app (no review from Figma; people in that team or organization can sign in). Paste its client id and secret there, then select Connect. - **For every team on the install**: put a Figma app's `figma_client_id` and `figma_client_secret` in the install's secret. The card's Sign in with Figma then uses it, with no hybrid. - **With hybrid**\ - MCP sign-ins go to your install directly, except Figma when CoderHelm's app is chosen for it (above). + MCP sign-ins go to your install directly, except Figma when the install uses Coderhelm's apps and has no Figma app of its own (above). ## Knowledge sources - **What you need**\ Sanity, GitHub and website sources work with nothing to set up. Notion and Confluence work with a token pasted in the add flow. One-click sign-in for Notion, Confluence and Google Drive, and instant updates from Notion, need OAuth apps of your own, registered with your install's redirect URI. See [Knowledge sources](./knowledge-sources.md). - **With hybrid**\ - CoderHelm's Notion, Confluence and Google Drive apps can sign you in instead. This only works on your install's CloudFront address or its `.coderhelm.com` address, not on a domain of your own yet. With CoderHelm's Notion app there are no instant updates: those sources sync every hour. + With Coderhelm's apps (the default under Settings, System, Hybrid, Other apps), CoderHelm's Notion, Confluence and Google Drive apps sign you in instead. This only works on your install's CloudFront address or its `.coderhelm.com` address, not on a domain of your own yet. With CoderHelm's Notion app there are no instant updates: those sources sync every hour. ## Plugins diff --git a/docs/self-host/setup-checks.md b/docs/self-host/setup-checks.md index af1fe8062..8298fd07f 100644 --- a/docs/self-host/setup-checks.md +++ b/docs/self-host/setup-checks.md @@ -25,7 +25,7 @@ The guide's checks come from the install's public `GET /api/setup/status`, the s - `domain`: when you chose your own domain, the guide shows a Domain step between AWS and GitHub, checks until the code is `ok`, then follows the install to the domain (so the GitHub App gets the domain's URLs). The records to add are in Settings, then System. You can skip it and add the domain later there. - `github_manifest_ready`: one click when true; the terminal command (`pull`, then `github-app`) when false. - `setup_open: false` (someone has signed up): the install is shown as ready. A hybrid install with **Link through CoderHelm** on still reports `hybrid_jira` then, because linking a Jira site needs an install admin: the guide's Jira step follows it until `ok`. -- `POST /api/setup/hybrid/settings` (with the setup key, while nobody has signed up) takes `github_mode`, `sharing`, `jira_link` and `apps` (per tool, `{"jira": "coderhelm" | "own", "notion": …, "confluence": …, "google_drive": …}`); the guide sends `jira_link: true` when **Use CoderHelm's Jira app** is on, so the install reports `hybrid_jira` from then on, and `apps` with each **Other apps** choice (all `coderhelm` unless turned off). +- `POST /api/setup/hybrid/settings` (with the setup key, while nobody has signed up) takes `github_mode`, `sharing`, `jira_link` and `apps_mode` (`coderhelm` or `private`, the **Other apps** choice). The guide sends `jira_link: true` when **Use CoderHelm's Jira app** is on, so the install reports `hybrid_jira` from then on. For one release it also sends the older per-tool `apps` with the same meaning, so an install on an earlier release takes the choice too. The answer carries no ids or URLs, so the guide makes its own links: the Bedrock console for the region it launched in, the models a new install calls (the release's defaults with the region's inference profile prefix), the SES console for that region, and `https://github.com/apps//installations/new` from the slug the manifest landing hands back. diff --git a/services/common/src/hybrid/integrations.rs b/services/common/src/hybrid/integrations.rs index 8229efe39..a47434440 100644 --- a/services/common/src/hybrid/integrations.rs +++ b/services/common/src/hybrid/integrations.rs @@ -135,9 +135,10 @@ pub fn hosted_ids() -> impl Iterator { .map(|e| e.id) } -/// The tools an install picks an app for, one by one (Settings, System, -/// Hybrid, Other apps, and the install guide): every hosted one but GitHub, -/// which has its own choice. +/// The tools the install-wide Other apps choice covers (Settings, System, +/// Hybrid, and the install guide): every hosted one but GitHub, which has its +/// own choice. An entry added here is on for every install that chose +/// CoderHelm's apps, with nothing more to choose. pub fn app_choice_ids() -> impl Iterator { hosted_ids().filter(|id| *id != "github") } diff --git a/services/gateway/src/routes/hybrid_install.rs b/services/gateway/src/routes/hybrid_install.rs index f07cd06c9..6aea8c3c7 100644 --- a/services/gateway/src/routes/hybrid_install.rs +++ b/services/gateway/src/routes/hybrid_install.rs @@ -76,17 +76,21 @@ pub struct Local { pub github_mode: String, /// Integrations using CoderHelm's apps (common::hybrid::integrations), as /// CoderHelm last accepted them. Worked out from `github_mode` and - /// `apps` ([`wanted_integrations`]), never set by hand. + /// `apps_mode` ([`wanted_integrations`]), never set by hand. #[serde(default)] pub integrations: Vec, - /// Other apps, per tool (`integrations::app_choice_ids`): `coderhelm` or - /// `own`, as someone chose it in Settings or the install guide. A tool - /// with no entry was never chosen and uses CoderHelm's app, unless this - /// install has its own app for it set up. + /// Other apps, for the whole install: `coderhelm` (CoderHelm's app for + /// every tool in `integrations::app_choice_ids`, including ones added in + /// later releases) or `private` (none). A tool with the install's own app + /// set up uses that app either way. Empty until [`migrate_apps`] runs. + #[serde(default)] + pub apps_mode: String, + /// The per-tool choices of earlier releases (`coderhelm` or `own`). Read + /// once by [`migrate_apps`]; kept so an older release still finds them. #[serde(default)] pub apps: BTreeMap, - /// 1 once `apps` holds the choices. A record from before (0) kept them - /// only in `integrations`; [`migrate_apps`] reads them from there once. + /// 2 once `apps_mode` holds the choice. 1: the choices were per tool, in + /// `apps`. 0: they were only in `integrations`. #[serde(default)] pub apps_v: u8, /// The `integrations` last asked of CoderHelm (it may accept fewer: @@ -378,7 +382,7 @@ pub(crate) async fn do_register(state: &AppState) -> Result, - /// Other apps, per tool: `{ "notion": "coderhelm" | "own", ... }`. Only - /// the tools named change. + /// Other apps, for the whole install: `coderhelm` or `private`. + #[serde(default)] + pub apps_mode: Option, + /// Earlier releases' per-tool choices, `{ "notion": "coderhelm" | "own" }`, + /// still taken for one release (older dashboards and install guides): + /// any `coderhelm` means CoderHelm's apps, only `own` means private. + /// Ignored when `apps_mode` is sent. #[serde(default)] pub apps: Option>, } @@ -621,21 +630,10 @@ pub fn apply_settings(l: &mut Local, b: &SettingsBody) -> Result<(), &'static st } } if let Some(list) = &b.integrations { - // The whole list (from a dashboard before `apps`): only the tools it - // switches on or off become a choice. let allowed: Vec<&str> = integrations::hosted_ids().collect(); if list.iter().any(|i| !allowed.contains(&i.as_str())) { return Err("an integration that can't use CoderHelm's app"); } - for id in integrations::app_choice_ids() { - let listed = list.iter().any(|i| i == id); - let on = l.integrations.iter().any(|i| i == id); - if listed && !on { - l.apps.insert(id.into(), "coderhelm".into()); - } else if !listed && on { - l.apps.insert(id.into(), "own".into()); - } - } } if let Some(apps) = &b.apps { for (id, v) in apps { @@ -646,9 +644,14 @@ pub fn apply_settings(l: &mut Local, b: &SettingsBody) -> Result<(), &'static st return Err("each app is coderhelm or own"); } } - for (id, v) in apps { - l.apps.insert(id.clone(), v.clone()); + } + if let Some(m) = &b.apps_mode { + match m.as_str() { + "coderhelm" | "private" => l.apps_mode = m.clone(), + _ => return Err("apps_mode is coderhelm or private"), } + } else if let Some(mode) = legacy_apps_mode(b) { + l.apps_mode = mode.into(); } if let Some(s) = &b.sharing { l.sharing = s.enabled; @@ -666,7 +669,7 @@ async fn do_settings( ) -> Result)> { let mut l = local(state).await; let was_linking = l.jira_link; - migrate_apps(&mut l, None); + migrate_apps(&mut l); apply_settings(&mut l, b).map_err(|m| fail(StatusCode::BAD_REQUEST, "bad_settings", m))?; if l.jira_link && creds_now().await.is_none() { return Err(not_registered()); @@ -706,22 +709,42 @@ fn strings(v: &Value) -> Option> { } /// Does this install have its own app set up for a tool (its own Notion, -/// Atlassian or Google client in its secret)? Then a tool nobody chose for -/// keeps using it. +/// Atlassian, Google or Figma client in its secret)? Then the tool uses it, +/// whichever Other apps choice the install made. fn own_app_set(state: &AppState, id: &str) -> bool { common::knowledge::sources::Kind::parse(id) .is_some_and(|k| super::knowledge_sources::app(state, k).is_set()) || super::mcp_servers::coderhelm_app(state, id).is_set() } -/// Does `id` use CoderHelm's app: as chosen, or by default (CoderHelm's, -/// unless the install has its own app for it set up). Pure; tested. +/// The Other apps choice, `coderhelm` unless someone chose `private`. +pub fn apps_mode(l: &Local) -> &'static str { + if l.apps_mode == "private" { + "private" + } else { + "coderhelm" + } +} + +/// Does `id` use CoderHelm's app: the install chose CoderHelm's apps and has +/// no app of its own for `id`. Pure; tested. pub fn uses_coderhelm(l: &Local, id: &str, own_app_set: impl Fn(&str) -> bool) -> bool { - match l.apps.get(id).map(String::as_str) { - Some("own") => false, - Some(_) => true, - None => !own_app_set(id), + apps_mode(l) == "coderhelm" && !own_app_set(id) +} + +/// The install-wide choice an earlier release's settings body means: the +/// per-tool `apps`, or the whole `integrations` list from before them. Any +/// tool on CoderHelm's app means CoderHelm's apps; only `own` (or a list +/// with none of the tools) means private, so an older page never makes an +/// install less private than it asked. `None` when neither is sent. +pub fn legacy_apps_mode(b: &SettingsBody) -> Option<&'static str> { + let pick = |coderhelm: bool| if coderhelm { "coderhelm" } else { "private" }; + if let Some(apps) = b.apps.as_ref().filter(|a| !a.is_empty()) { + return Some(pick(apps.values().any(|v| v == "coderhelm"))); } + b.integrations + .as_ref() + .map(|list| pick(integrations::app_choice_ids().any(|id| list.iter().any(|i| i == id)))) } /// The integrations to ask CoderHelm for: GitHub when its mode is hosted, @@ -739,31 +762,31 @@ pub fn wanted_integrations(l: &Local, own_app_set: impl Fn(&str) -> bool) -> Vec out } -/// A record from before per-tool choices kept them only as the list. If that -/// list names any Other apps tool, someone chose there: each tool on it is -/// CoderHelm's app, and each one CoderHelm offered but isn't on it is their -/// own. A list with none was never chosen, so every tool takes the default. -/// True when the record changed. Pure; tested. -pub fn migrate_apps(l: &mut Local, offered: Option<&[String]>) -> bool { - if l.apps_v >= 1 { +/// Turns an earlier release's per-tool choices into the install-wide one, +/// once. A tool was on CoderHelm's app when someone chose that for it, or +/// when nobody chose and CoderHelm holds it (a record from before per-tool +/// choices kept them only in that list). Any tool on CoderHelm's app means +/// CoderHelm's apps; tools all chosen as their own means private; nothing +/// chosen means the default, CoderHelm's apps. True when the record +/// changed. Pure; tested. +pub fn migrate_apps(l: &mut Local) -> bool { + if l.apps_v >= 2 { return false; } - l.apps_v = 1; - let on = |id: &str| l.integrations.iter().any(|i| i == id); - if integrations::app_choice_ids().any(on) { - let picks: Vec<(String, String)> = integrations::app_choice_ids() - .filter_map(|id| { - if on(id) { - Some((id.into(), "coderhelm".into())) - } else if offered.is_none_or(|o| o.iter().any(|x| x == id)) { - Some((id.into(), "own".into())) - } else { - None - } - }) - .collect(); - l.apps.extend(picks); + l.apps_v = 2; + if !l.apps_mode.is_empty() { + return true; } + let on = |id: &str| match l.apps.get(id).map(String::as_str) { + Some(v) => v == "coderhelm", + None => l.integrations.iter().any(|i| i == id), + }; + let any_own = l.apps.values().any(|v| v == "own"); + l.apps_mode = if integrations::app_choice_ids().any(on) || !any_own { + "coderhelm".into() + } else { + "private".into() + }; true } @@ -787,7 +810,7 @@ pub fn apps_out_of_date(l: &Local, want: &[String], hosted_now: Option<&[String] /// through changes: nothing is connected or synced here. async fn sync_apps(state: &AppState, l: &mut Local, hosted: Option<&Value>) { let offered = offered_by(hosted); - let mut changed = migrate_apps(l, offered.as_deref()); + let mut changed = migrate_apps(l); let mut want = wanted_integrations(l, |id| own_app_set(state, id)); if let Some(o) = &offered { want.retain(|i| o.contains(i)); @@ -1012,6 +1035,8 @@ async fn status_value(state: &AppState, deep: bool) -> Value { let offered = offered_by(hosted.as_ref()); if creds.is_some() { sync_apps(state, &mut l, hosted.as_ref()).await; + } else if migrate_apps(&mut l) { + let _ = save_local(state, &l).await; } let chosen: Vec = integrations::CATALOG .iter() @@ -1026,6 +1051,11 @@ async fn status_value(state: &AppState, deep: bool) -> Value { .any(|i| i == e.id) }; v["uses_coderhelm"] = json!(uses); + // The install's own app for this tool, which it uses whichever + // Other apps choice was made. + if integrations::app_choice_ids().any(|id| id == e.id) { + v["own_app"] = json!(own_app_set(state, e.id)); + } v["offered"] = json!(offered.as_ref().is_none_or(|o| o.iter().any(|i| i == e.id))); v }) @@ -1046,6 +1076,7 @@ async fn status_value(state: &AppState, deep: bool) -> Value { "links": hosted.as_ref().map(|h| h["github"]["links"].clone()).unwrap_or(json!([])), "app_slug": hosted.as_ref().map(|h| h["github"]["app_slug"].clone()).unwrap_or(Value::Null), }, + "apps_mode": apps_mode(&l), "integrations": chosen, "sharing": { "enabled": l.sharing, "payloads": l.share_payloads }, "jira": { @@ -1527,7 +1558,7 @@ async fn ensure_jira_synced(state: &AppState) -> Result<(), (StatusCode, &'stati .map_err(|_| (StatusCode::SERVICE_UNAVAILABLE, "jira_link_unsynced")) } -/// PUT /api/system/hybrid/settings `{github_mode?, integrations?, sharing?, jira_link?, apps?}` +/// PUT /api/system/hybrid/settings `{github_mode?, sharing?, jira_link?, apps_mode?}` (and, for one release, the older `integrations?` and `apps?`) pub async fn put_settings( State(state): State>, Extension(claims): Extension, @@ -1564,7 +1595,10 @@ pub struct GuideBody { /// install admin, after sign-up). #[serde(default)] jira_link: Option, - /// The guide's Other apps choices, per tool (`coderhelm` or `own`). + /// The guide's Other apps choice: `coderhelm` or `private`. + #[serde(default)] + apps_mode: Option, + /// An earlier guide's per-tool choices (see [`SettingsBody::apps`]). #[serde(default)] apps: Option>, } @@ -1649,7 +1683,7 @@ pub async fn setup_github_link( guide_reply(r) } -/// POST /api/setup/hybrid/settings `{setup_token, github_mode?, integrations?, sharing?, jira_link?, apps?}` +/// POST /api/setup/hybrid/settings `{setup_token, github_mode?, sharing?, jira_link?, apps_mode?}` (and, for one release, the older `integrations?` and `apps?`) pub async fn setup_settings( State(state): State>, body: Bytes, @@ -1662,6 +1696,7 @@ pub async fn setup_settings( integrations: b.integrations.clone(), sharing: b.sharing.clone(), jira_link: b.jira_link, + apps_mode: b.apps_mode.clone(), apps: b.apps.clone(), }; do_settings(&state, &s).await?; @@ -2278,77 +2313,134 @@ mod tests { } #[test] - fn every_other_app_uses_coderhelms_unless_someone_chose_their_own() { - // A new install: nobody chose, so every tool is CoderHelm's app. + fn every_hosted_app_is_on_with_coderhelms_apps_and_none_when_private() { + // A new install: nothing chosen, so CoderHelm's apps, for every tool. let mut l = Local::default(); - assert!(migrate_apps(&mut l, None) && l.apps.is_empty()); + assert!(migrate_apps(&mut l)); + assert_eq!(apps_mode(&l), "coderhelm"); assert_eq!( wanted_integrations(&l, no_own_apps), - vec!["jira", "notion", "confluence", "google_drive", "figma"] + integrations::app_choice_ids().collect::>() ); - // An explicit "my own app" is kept; the rest stay on the default. - l.apps.insert("notion".into(), "own".into()); - assert!(!uses_coderhelm(&l, "notion", no_own_apps)); - assert!(uses_coderhelm(&l, "confluence", no_own_apps)); - // A tool nobody chose, with the install's own app set up, keeps it. - let own_drive = |id: &str| id == "google_drive" || id == "figma"; - // An install with its own Figma app in its secret keeps it too. - assert!(!uses_coderhelm(&l, "figma", own_drive)); - assert!(!uses_coderhelm(&l, "google_drive", own_drive)); - // Chosen CoderHelm's app explicitly: wins over an own app set up. - l.apps.insert("google_drive".into(), "coderhelm".into()); - assert!(uses_coderhelm(&l, "google_drive", own_drive)); + // A tool with the install's own app set up uses it either way. + let own_figma = |id: &str| id == "figma"; + assert!(!uses_coderhelm(&l, "figma", own_figma)); + assert!(uses_coderhelm(&l, "notion", own_figma)); l.github_mode = "hosted".into(); assert_eq!( - wanted_integrations(&l, no_own_apps), - vec!["github", "jira", "confluence", "google_drive", "figma"] + wanted_integrations(&l, own_figma), + vec!["github", "jira", "notion", "confluence", "google_drive"] ); - // Unrecorded: the serde default of a record from before is unset. - let old: Local = serde_json::from_str(r#"{"integrations":["github"]}"#).unwrap(); - assert!(old.apps.is_empty() && old.apps_v == 0); + // Fully private: no app through CoderHelm; GitHub keeps its own choice. + l.apps_mode = "private".into(); + assert_eq!(wanted_integrations(&l, no_own_apps), vec!["github"]); + l.github_mode = "own".into(); + assert!(wanted_integrations(&l, no_own_apps).is_empty()); + } + + #[test] + fn a_new_hosted_entry_is_on_with_no_choice_to_make() { + // Whatever the catalog holds, every hosted entry but GitHub is wanted + // by an install on CoderHelm's apps: nothing names the tools one by one. + let l = Local { + apps_mode: "coderhelm".into(), + apps_v: 2, + ..Default::default() + }; + let hosted: Vec<&str> = integrations::CATALOG + .iter() + .filter(|e| e.mode == integrations::Mode::Hosted && e.id != "github") + .map(|e| e.id) + .collect(); + assert_eq!(wanted_integrations(&l, no_own_apps), hosted); + // A record migrated by an earlier release, before an entry was added + // (Figma here), gets the entry too: per-tool picks are no longer read. + let mut l = Local { + apps: [("jira", "coderhelm"), ("notion", "own")] + .map(|(a, b)| (a.to_string(), b.to_string())) + .into(), + apps_v: 1, + ..Default::default() + }; + assert!(migrate_apps(&mut l)); + assert!(wanted_integrations(&l, no_own_apps).contains(&"figma".to_string())); + assert!(wanted_integrations(&l, no_own_apps).contains(&"notion".to_string())); } #[test] - fn a_record_from_before_keeps_its_explicit_own_choices() { - // Never chose anything in Other apps (only GitHub's app): defaults. + fn earlier_per_tool_choices_migrate_once() { + let picks = |p: &[(&str, &str)]| -> BTreeMap { + p.iter() + .map(|(a, b)| (a.to_string(), b.to_string())) + .collect() + }; + // Any tool on CoderHelm's app: CoderHelm's apps. + let mut l = Local { + apps: picks(&[ + ("jira", "own"), + ("notion", "coderhelm"), + ("confluence", "own"), + ]), + apps_v: 1, + ..Default::default() + }; + assert!(migrate_apps(&mut l)); + assert_eq!(l.apps_mode, "coderhelm"); + assert!(!migrate_apps(&mut l), "only once"); + // Every tool chosen as their own: private. + let mut l = Local { + apps: picks(&[ + ("jira", "own"), + ("notion", "own"), + ("confluence", "own"), + ("google_drive", "own"), + ]), + apps_v: 1, + ..Default::default() + }; + assert!(migrate_apps(&mut l)); + assert_eq!(l.apps_mode, "private"); + assert!(wanted_integrations(&l, no_own_apps).is_empty()); + // Some own, the rest never chosen but held by CoderHelm (the old + // default): those were on CoderHelm's app, so CoderHelm's apps. + let mut l = Local { + apps: picks(&[("notion", "own")]), + integrations: vec!["jira".into(), "confluence".into()], + apps_v: 1, + ..Default::default() + }; + migrate_apps(&mut l); + assert_eq!(l.apps_mode, "coderhelm"); + // Some own and nothing else on CoderHelm's app: private. let mut l = Local { - github_mode: "hosted".into(), + apps: picks(&[("notion", "own"), ("confluence", "own")]), integrations: vec!["github".into()], + apps_v: 1, ..Default::default() }; - assert!(migrate_apps(&mut l, None)); - assert!(l.apps.is_empty()); - assert_eq!( - wanted_integrations(&l, no_own_apps), - vec![ - "github", - "jira", - "notion", - "confluence", - "google_drive", - "figma" - ] - ); - assert!(!migrate_apps(&mut l, None), "only once"); - // Chose Notion only, while Google Drive wasn't offered: Jira and - // Confluence were left on their own app on purpose, Drive wasn't. + migrate_apps(&mut l); + assert_eq!(l.apps_mode, "private"); + // A record from before per-tool choices (only the list): any tool on + // it means CoderHelm's apps; none means never chosen, the default. + for list in [vec!["github", "notion"], vec!["github"], vec![]] { + let mut l = Local { + integrations: list.iter().map(|s| s.to_string()).collect(), + ..Default::default() + }; + assert!(migrate_apps(&mut l)); + assert_eq!(l.apps_mode, "coderhelm", "{list:?}"); + } + // A record from before has none of the new fields. + let old: Local = serde_json::from_str(r#"{"integrations":["github"]}"#).unwrap(); + assert!(old.apps.is_empty() && old.apps_v == 0 && old.apps_mode.is_empty()); + // A choice made already is never overwritten. let mut l = Local { + apps_mode: "private".into(), integrations: vec!["notion".into()], ..Default::default() }; - let offered: Vec = ["github", "jira", "notion", "confluence"] - .map(String::from) - .to_vec(); - assert!(migrate_apps(&mut l, Some(&offered))); - assert_eq!(l.apps.get("notion").map(String::as_str), Some("coderhelm")); - assert_eq!(l.apps.get("jira").map(String::as_str), Some("own")); - assert_eq!(l.apps.get("confluence").map(String::as_str), Some("own")); - assert!(!l.apps.contains_key("google_drive")); - assert_eq!( - wanted_integrations(&l, no_own_apps), - vec!["notion", "google_drive", "figma"] - ); - assert!(offered_by(Some(&serde_json::json!({}))).is_none()); + assert!(migrate_apps(&mut l)); + assert_eq!(l.apps_mode, "private"); } #[test] @@ -2362,36 +2454,51 @@ mod tests { assert!(!apps_out_of_date(&l, &want, Some(&l.integrations.clone()))); // CoderHelm holds something else: send again. assert!(apps_out_of_date(&l, &want, Some(&["jira".to_string()]))); - l.apps.insert("jira".into(), "own".into()); + l.apps_mode = "private".into(); let want = wanted_integrations(&l, no_own_apps); assert!(apps_out_of_date(&l, &want, None)); } #[test] - fn the_guide_and_settings_record_each_tool_they_name() { + fn the_guide_and_settings_take_one_choice_and_older_bodies_for_now() { let mut l = Local::default(); - let b = SettingsBody { + let mode = |m: &str| SettingsBody { + apps_mode: Some(m.into()), + ..Default::default() + }; + apply_settings(&mut l, &mode("private")).unwrap(); + assert_eq!(apps_mode(&l), "private"); + apply_settings(&mut l, &mode("coderhelm")).unwrap(); + assert_eq!(apps_mode(&l), "coderhelm"); + assert!(apply_settings(&mut l, &mode("own")).is_err()); + // An older dashboard's per-tool body: any CoderHelm's app means + // CoderHelm's apps, only "own" means private. + let apps = |p: &[(&str, &str)]| SettingsBody { apps: Some( - [("notion", "own"), ("jira", "coderhelm")] + p.iter() .map(|(a, b)| (a.to_string(), b.to_string())) - .into(), + .collect(), ), ..Default::default() }; - apply_settings(&mut l, &b).unwrap(); - assert_eq!( - wanted_integrations(&l, no_own_apps), - vec!["jira", "confluence", "google_drive", "figma"] - ); - let bad = |id: &str, v: &str| SettingsBody { - apps: Some([(id.to_string(), v.to_string())].into()), - ..Default::default() + apply_settings(&mut l, &apps(&[("notion", "own")])).unwrap(); + assert_eq!(apps_mode(&l), "private"); + apply_settings(&mut l, &apps(&[("notion", "own"), ("jira", "coderhelm")])).unwrap(); + assert_eq!(apps_mode(&l), "coderhelm"); + // Both sent (the guide, for older installs): the new field decides. + let both = SettingsBody { + apps_mode: Some("private".into()), + ..apps(&[("jira", "coderhelm")]) }; - assert!(apply_settings(&mut l, &bad("github", "own")).is_err()); - assert!(apply_settings(&mut l, &bad("slack_commands", "coderhelm")).is_err()); - assert!(apply_settings(&mut l, &bad("notion", "both")).is_err()); - // The guide's body takes the same field. - let g = guide_body(br#"{"setup_token":"t","apps":{"confluence":"own"}}"#); + apply_settings(&mut l, &both).unwrap(); + assert_eq!(apps_mode(&l), "private"); + assert!(apply_settings(&mut l, &apps(&[("github", "own")])).is_err()); + assert!(apply_settings(&mut l, &apps(&[("slack_commands", "coderhelm")])).is_err()); + assert!(apply_settings(&mut l, &apps(&[("notion", "both")])).is_err()); + // The guide's body takes the same fields. + let g = + guide_body(br#"{"setup_token":"t","apps_mode":"private","apps":{"confluence":"own"}}"#); + assert_eq!(g.apps_mode.as_deref(), Some("private")); assert_eq!( g.apps.unwrap().get("confluence").map(String::as_str), Some("own") @@ -2678,23 +2785,23 @@ mod tests { payloads: true, }), jira_link: None, + apps_mode: None, apps: None, }; apply_settings(&mut l, &b).unwrap(); - // The whole list from an older dashboard: Notion switched on. - assert_eq!(l.apps.get("notion").map(String::as_str), Some("coderhelm")); + // The whole list from an older dashboard: a tool on it, CoderHelm's apps. + assert_eq!(apps_mode(&l), "coderhelm"); assert_eq!(wanted_integrations(&l, no_own_apps)[0], "github"); assert!(!l.share_payloads, "payloads only with sharing on"); - // Switched off in that list: an explicit own choice. - l.integrations = vec!["github".into(), "notion".into()]; + // None of the tools on that list: private. let b = SettingsBody { github_mode: Some("own".into()), integrations: Some(vec![]), ..Default::default() }; apply_settings(&mut l, &b).unwrap(); - assert_eq!(l.apps.get("notion").map(String::as_str), Some("own")); - assert!(!wanted_integrations(&l, no_own_apps).contains(&"github".to_string())); + assert_eq!(apps_mode(&l), "private"); + assert!(wanted_integrations(&l, no_own_apps).is_empty()); let bad = SettingsBody { integrations: Some(vec!["slack_commands".into()]), ..Default::default()