Skip to content

Commit fa0f952

Browse files
committed
app/: create_organization, invite_member, create_project_token and delete_project_token on #70's functions (create_org, invite_to, make_project_token, remove_project_token), with today's role checks and messages; the personal-token test's settings goldens gain comment_when_unchanged
1 parent 5b8ff02 commit fa0f952

6 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎app/src/mcp/mod.rs‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,10 @@ impl VisualCi {
542542
}
543543
}
544544

545-
/// A subdomains function a tool needs that isn't on main yet: the tool's error until it lands.
546-
fn not_on_main_yet(function: &str) -> CallToolResult {
547-
tool_error(format!(
548-
"this tool isn't available yet: it needs {function} (subdomains' organization functions, sc-8900)"
549-
))
545+
/// A subdomains function's refusal (`abort_with(422 | 409 | 429, message)`) as the tool's error, with its message;
546+
/// anything else is an internal error.
547+
fn refusal(e: laravel::Error) -> Result<String, ErrorData> {
548+
if e.status().is_client_error() { Ok(e.to_string()) } else { Err(internal(e)) }
550549
}
551550

552551
/// The OAuth client's display name, when the request came through one (for "Approved by Claude Code for Ada").
@@ -777,16 +776,22 @@ impl VisualCi {
777776
Parameters(args): Parameters<InviteArgs>,
778777
rc: RequestContext<RoleServer>,
779778
) -> Result<CallToolResult, ErrorData> {
780-
let (_org, role) = need!(Self::org(&rc, &args.org));
781-
let Some(Principal::User { .. }) = principal_of(&rc) else {
779+
let (org, role) = need!(Self::org(&rc, &args.org));
780+
let Some(Principal::User { user, .. }) = principal_of(&rc) else {
782781
return Ok(tool_error("invitations need a personal access token"));
783782
};
784783
if role != "admin" {
785784
return Ok(tool_error(format!("only an organization's admins can invite people; you're {role}")));
786785
}
787-
let _ = (&args.invitee, crate::auth::Role::from(args.role));
788-
// TODO(#70): app::auth::invitations::invite_to(org.id, &user, invitee, role) once it's on main.
789-
Ok(not_on_main_yet("app::auth::invitations::invite_to"))
786+
let role = crate::auth::Role::from(args.role).as_str();
787+
let invitation = match crate::auth::invitations::invite_to(org.id, &user, &args.invitee, role).await {
788+
Ok(v) => v,
789+
Err(e) => return Ok(tool_error(refusal(e)?)),
790+
};
791+
Ok(structured(json!({
792+
"invitation": invitation,
793+
"members": crate::auth::invitations::members_json(org.id, true).await.map_err(internal)?,
794+
})))
790795
}
791796

792797
#[tool(
@@ -803,12 +808,23 @@ impl VisualCi {
803808
Parameters(args): Parameters<CreateOrgArgs>,
804809
rc: RequestContext<RoleServer>,
805810
) -> Result<CallToolResult, ErrorData> {
806-
let Some(Principal::User { .. }) = principal_of(&rc) else {
811+
let Some(Principal::User { user, .. }) = principal_of(&rc) else {
807812
return Ok(tool_error("creating an organization needs a personal access token"));
808813
};
809-
let _ = (&args.slug, &args.name);
810-
// TODO(#70): orgs::create_org(&user, slug, name) once it's on main.
811-
Ok(not_on_main_yet("orgs::create_org"))
814+
let slug = match crate::auth::orgs::create_org(&user, &args.slug, &args.name).await {
815+
Ok(slug) => slug,
816+
// Today's words for a slug that's taken.
817+
Err(e) if e.status().as_u16() == 409 => return Ok(tool_error(format!("the slug {} is taken", args.slug))),
818+
Err(e) => return Ok(tool_error(refusal(e)?)),
819+
};
820+
let org = Organization::query()
821+
.filter(Organization::slug.eq(slug.as_str()))
822+
.first()
823+
.await
824+
.map_err(internal)?
825+
.ok_or_else(|| internal("the new organization wasn't saved"))?;
826+
let plan = domain::credits::plan_by_id(&org.plan);
827+
Ok(structured(json!({ "slug": org.slug, "name": org.name, "plan": plan.name })))
812828
}
813829

814830
#[tool(
@@ -945,11 +961,18 @@ impl VisualCi {
945961
if let Err(e) = Self::need_role(&rc, &project, Role::Admin, "create project tokens").await? {
946962
return Ok(tool_error(e));
947963
}
948-
if args.name.trim().is_empty() {
964+
let Some(Principal::User { user, .. }) = principal_of(&rc) else {
965+
return Ok(tool_error("creating a project token needs a personal access token"));
966+
};
967+
let name = args.name.trim();
968+
if name.is_empty() {
949969
return Ok(tool_error("give the token a name"));
950970
}
951-
// TODO(#70): app::auth::tokens::make_project_token(&user, project.id, name) once it's on main.
952-
Ok(not_on_main_yet("app::auth::tokens::make_project_token"))
971+
let (token, id) = match crate::auth::tokens::make_project_token(&user, project.id, name).await {
972+
Ok(made) => made,
973+
Err(e) => return Ok(tool_error(refusal(e)?)),
974+
};
975+
Ok(structured(json!({ "id": id, "name": name, "token": token })))
953976
}
954977

955978
#[tool(
@@ -970,9 +993,10 @@ impl VisualCi {
970993
if let Err(e) = Self::need_role(&rc, &project, Role::Admin, "delete project tokens").await? {
971994
return Ok(tool_error(e));
972995
}
973-
let _ = args.token_id;
974-
// TODO(#70): app::auth::tokens::remove_project_token(project.id, id) once it's on main.
975-
Ok(not_on_main_yet("app::auth::tokens::remove_project_token"))
996+
if !crate::auth::tokens::remove_project_token(project.id, args.token_id).await.map_err(internal)? {
997+
return Ok(tool_error(format!("no token {} in this project", args.token_id)));
998+
}
999+
Ok(structured(json!({ "deleted": args.token_id })))
9761000
}
9771001

9781002
#[tool(

‎app/tests/golden/mcp/a_personal_token_manages_organizations_projects_and_tokens/create_project.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"result": {
88
"content": [
99
{
10-
"text": "{\"project\":\"<org:1>-b/docs\",\"settings\":{\"ci_mode\":\"review\",\"default_branch\":\"main\",\"dpi\":140,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
10+
"text": "{\"project\":\"<org:1>-b/docs\",\"settings\":{\"ci_mode\":\"review\",\"comment_when_unchanged\":false,\"default_branch\":\"main\",\"dpi\":140,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
1111
"type": "text"
1212
}
1313
],
@@ -16,6 +16,7 @@
1616
"project": "<org:1>-b/docs",
1717
"settings": {
1818
"ci_mode": "review",
19+
"comment_when_unchanged": false,
1920
"default_branch": "main",
2021
"dpi": 140,
2122
"forge": null,

‎app/tests/golden/mcp/a_personal_token_manages_organizations_projects_and_tokens/get_project.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"result": {
66
"content": [
77
{
8-
"text": "{\"gallery\":{\"enabled\":true,\"items\":[],\"manifest_url\":null},\"project\":\"<org:1>-b/docs\",\"repository_seen_in_ci\":null,\"settings\":{\"ci_mode\":\"review\",\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[{\"json_paths\":[\"$.generated_at\"],\"snapshots\":\"api/**\",\"text_patterns\":[\"\\\\d{4}-\\\\d{2}-\\\\d{2}\"]}],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true},\"tokens\":[{\"created_at\":\"<time:1>\",\"id\":3,\"last_used_at\":null,\"name\":\"CircleCI\",\"prefix\":\"<token:1>\"}],\"url\":\"https://app.visualci.test/<org:1>-b/docs\"}",
8+
"text": "{\"gallery\":{\"enabled\":true,\"items\":[],\"manifest_url\":null},\"project\":\"<org:1>-b/docs\",\"repository_seen_in_ci\":null,\"settings\":{\"ci_mode\":\"review\",\"comment_when_unchanged\":false,\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[{\"json_paths\":[\"$.generated_at\"],\"snapshots\":\"api/**\",\"text_patterns\":[\"\\\\d{4}-\\\\d{2}-\\\\d{2}\"]}],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true},\"tokens\":[{\"created_at\":\"<time:1>\",\"id\":3,\"last_used_at\":null,\"name\":\"CircleCI\",\"prefix\":\"<token:1>\"}],\"url\":\"https://app.visualci.test/<org:1>-b/docs\"}",
99
"type": "text"
1010
}
1111
],
@@ -20,6 +20,7 @@
2020
"repository_seen_in_ci": null,
2121
"settings": {
2222
"ci_mode": "review",
23+
"comment_when_unchanged": false,
2324
"default_branch": "main",
2425
"dpi": 150,
2526
"forge": null,

‎app/tests/golden/mcp/a_personal_token_manages_organizations_projects_and_tokens/update_project-4.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
"result": {
1818
"content": [
1919
{
20-
"text": "{\"settings\":{\"ci_mode\":\"review\",\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[{\"json_paths\":[\"$.generated_at\"],\"snapshots\":\"api/**\",\"text_patterns\":[\"\\\\d{4}-\\\\d{2}-\\\\d{2}\"]}],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
20+
"text": "{\"settings\":{\"ci_mode\":\"review\",\"comment_when_unchanged\":false,\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[{\"json_paths\":[\"$.generated_at\"],\"snapshots\":\"api/**\",\"text_patterns\":[\"\\\\d{4}-\\\\d{2}-\\\\d{2}\"]}],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
2121
"type": "text"
2222
}
2323
],
2424
"isError": false,
2525
"structuredContent": {
2626
"settings": {
2727
"ci_mode": "review",
28+
"comment_when_unchanged": false,
2829
"default_branch": "main",
2930
"dpi": 150,
3031
"forge": null,

‎app/tests/golden/mcp/a_personal_token_manages_organizations_projects_and_tokens/update_project-8.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"result": {
1313
"content": [
1414
{
15-
"text": "failed to deserialize parameters: missing field `snapshots`\n\nupdate_project takes:\n- ci_mode (string): `review` (CI passes, the check waits for review), `strict` (CI fails on unreviewed changes) or `wait`.\n- default_branch (string): The branch whose builds become the baseline automatically.\n- dismiss_notice (boolean): `true` dismisses the project's notice.\n- dpi (integer): Render resolution for documents, 36 to 600.\n- forge_token (string): A project on your organization's own GitLab instance: an access token for it (a project or group access token with the `api` scope and the Developer role, or Maintainer to let VisualCI add its webhook), sent with `repository_url`. Stored encrypted; never shown again.\n- gallery_enabled (boolean): `false` takes the public gallery down (visualci.yml's `gallery` decides what's in it); `true` puts it back.\n- ignore_anti_aliasing (boolean): Ignore anti-aliasing differences in images.\n- ignore_rules (array): Values in text snapshots that change on every run, ignored when comparing. Replaces the project's rules. Each rule names the snapshots it applies to with a glob (`api/invoices/**`; `**` for every snapshot). A build's `visualci.yml` replaces these rules when it has `ignore`.\n- move_link (boolean): With `repository_url`: a repository belongs to one organization, so when another organization has it linked, `true` moves the link here (you must maintain the repository; that organization's admins get an email).\n- name (string): The project's display name.\n- project (string): `org/project`. Optional with a project token.\n- public (boolean): `true` lets anyone, signed in or not, read the project's builds and diffs (for open source projects); settings, tokens and reviewing stay with its members. Only a project linked to a public repository can be public. `false` makes it private again.\n- renderer (string): The PDF renderer: `pdfbox` or `mutool`.\n- repository_url (string): The repository this project tests, by URL (`https://github.com/acme/web`); an empty string unlinks it. Linking needs the forge's VisualCI app installed on the repository and you to be an admin or maintainer of it there. Its pull requests then get VisualCI's check, and people's access to this organization follows their permission on it.\n- slug (string): A new slug, renaming the project (`acme/old` → `acme/new`): lowercase letters, digits and dashes. Its builds, baselines and tokens stay. The old address and `acme/old` stop working.\n- threshold (number): The diff threshold, 0 to 1: 0 compares exactly; above 0 it's pixelmatch's colour threshold (how different a pixel's colour must be to count), and anti-aliased pixels are ignored.\n- tokens_can_approve (boolean): `false`: project tokens can't approve changes (people do); `true` lets CI's agents approve again.\nExample: {}",
15+
"text": "failed to deserialize parameters: missing field `snapshots`\n\nupdate_project takes:\n- ci_mode (string): `review` (CI passes, the check waits for review), `strict` (CI fails on unreviewed changes) or `wait`.\n- comment_when_unchanged (boolean): `true`: comment on pull requests even when nothing changed. `false` (the default): a pull request whose builds are all unchanged gets no comment, only the passing check; a comment already there keeps being updated.\n- default_branch (string): The branch whose builds become the baseline automatically.\n- dismiss_notice (boolean): `true` dismisses the project's notice.\n- dpi (integer): Render resolution for documents, 36 to 600.\n- forge_token (string): A project on your organization's own GitLab instance: an access token for it (a project or group access token with the `api` scope and the Developer role, or Maintainer to let VisualCI add its webhook), sent with `repository_url`. Stored encrypted; never shown again.\n- gallery_enabled (boolean): `false` takes the public gallery down (visualci.yml's `gallery` decides what's in it); `true` puts it back.\n- ignore_anti_aliasing (boolean): Ignore anti-aliasing differences in images.\n- ignore_rules (array): Values in text snapshots that change on every run, ignored when comparing. Replaces the project's rules. Each rule names the snapshots it applies to with a glob (`api/invoices/**`; `**` for every snapshot). A build's `visualci.yml` replaces these rules when it has `ignore`.\n- move_link (boolean): With `repository_url`: a repository belongs to one organization, so when another organization has it linked, `true` moves the link here (you must maintain the repository; that organization's admins get an email).\n- name (string): The project's display name.\n- project (string): `org/project`. Optional with a project token.\n- public (boolean): `true` lets anyone, signed in or not, read the project's builds and diffs (for open source projects); settings, tokens and reviewing stay with its members. Only a project linked to a public repository can be public. `false` makes it private again.\n- renderer (string): The PDF renderer: `pdfbox` or `mutool`.\n- repository_url (string): The repository this project tests, by URL (`https://github.com/acme/web`); an empty string unlinks it. Linking needs the forge's VisualCI app installed on the repository and you to be an admin or maintainer of it there. Its pull requests then get VisualCI's check, and people's access to this organization follows their permission on it.\n- slug (string): A new slug, renaming the project (`acme/old` → `acme/new`): lowercase letters, digits and dashes. Its builds, baselines and tokens stay. The old address and `acme/old` stop working.\n- threshold (number): The diff threshold, 0 to 1: 0 compares exactly; above 0 it's pixelmatch's colour threshold (how different a pixel's colour must be to count), and anti-aliased pixels are ignored.\n- tokens_can_approve (boolean): `false`: project tokens can't approve changes (people do); `true` lets CI's agents approve again.\nExample: {}",
1616
"type": "text"
1717
}
1818
],

‎app/tests/golden/mcp/a_personal_token_manages_organizations_projects_and_tokens/update_project.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"result": {
77
"content": [
88
{
9-
"text": "{\"settings\":{\"ci_mode\":\"review\",\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
9+
"text": "{\"settings\":{\"ci_mode\":\"review\",\"comment_when_unchanged\":false,\"default_branch\":\"main\",\"dpi\":150,\"forge\":null,\"forge_app_installed\":false,\"forge_webhook_at\":null,\"gallery_enabled\":true,\"ignore_anti_aliasing\":false,\"ignore_rules\":[],\"name\":\"Docs\",\"notice\":null,\"open_source\":false,\"open_source_note\":null,\"open_source_pool\":false,\"open_source_withdrawn\":false,\"public\":false,\"renderer\":\"pdfbox\",\"repository_public\":false,\"repository_url\":null,\"slug\":\"docs\",\"threshold\":0.0,\"tokens_can_approve\":true}}",
1010
"type": "text"
1111
}
1212
],
1313
"isError": false,
1414
"structuredContent": {
1515
"settings": {
1616
"ci_mode": "review",
17+
"comment_when_unchanged": false,
1718
"default_branch": "main",
1819
"dpi": 150,
1920
"forge": null,

0 commit comments

Comments
 (0)