feat(service-account): Add the create, update and delete commands

Description

Second of three merge requests implementing glab service-account for #8279 (closed). Follows !4292 (merged) (parent, list, get) and adds create, update and delete, for both project and group service accounts. The token subcommands come next.

Command surface

glab service-account create                    [--name <name>] [--username <username>] [--email <email>]
                                               [-g, --group <group>] [-F, --output text|json] [--jq <expr>] [-R, --repo <repo>]
glab service-account update <id | username>    [--name <name>] [--username <username>] [--email <email>]
                                               [-g, --group <group>] [-F, --output text|json] [--jq <expr>] [-R, --repo <repo>]
glab service-account delete <id | username>    [-y, --yes] [-g, --group <group>] [-R, --repo <repo>]

Notes for review

  • create sends only the flags that were given. Every field is optional in the API, and an unset one gets GitLab's default (name "Service account user", a generated username and noreply email). Same pattern as deploy-token create.
  • update needs at least one of --name, --username, --email (MarkFlagsOneRequired), and changes only those fields.
  • delete is a soft delete. hard_delete is not exposed, as agreed on the issue. The prompt and the result name the resolved scope, so a GITLAB_GROUP redirect is visible before anything is deleted. --yes skips the prompt and is required when not running interactively.
  • delete reports the account as scheduled, not deleted. GitLab removes service accounts in the background (delete_async in Namespaces::ServiceAccounts::BaseDeleteService), so the account can still show up in list for a moment. The help says so.
  • update and delete accept a username, through ResolveID, which comes back here with its first callers. A numeric argument is always an ID and makes no lookup request.
  • The details table is shared. get, create and update print the same table from serviceaccountutils.PrintDetails. It gains an Unconfirmed Email row, shown only while a custom email address waits for confirmation - right after create or update --email that is where the new address is.
  • Help text covers the subgroup note on create (an account created in a subgroup can be invited only to that subgroup and below, GitLab 18.11 or later), email confirmation on create and update, and the composite identity username limit on update.

Commits

  • refactor(service-account): Share the account details table

    create and update print the same table as get, so it moves next to the other shared helpers. A custom email waits for confirmation, so the table also shows the pending address.

  • feat(service-account): Add service-account create

    Service accounts can only be created with glab api today. Unset flags are not sent, so GitLab still picks its own name, username and email.

  • feat(service-account): Add service-account update

    Renaming a service account or changing its email address needs glab api today. ResolveID comes back with this first caller, so update and delete accept a username as get does.

  • feat(service-account): Add service-account delete

    Removing a service account needs glab api today. GitLab deletes it in the background, so the command reports it as scheduled, and the prompt names the scope so a GITLAB_GROUP redirect is visible first.

  • test(service-account): Cover the shared details table directly

Related to #8279 (closed).

Not Resolves: the token subcommands are still to come in the third merge request.

How has this been tested?

Unit tests use cmdtest.SetupCmdForTest with client-go mocks.

  • create: no flags sends no fields, only the given flags are sent, all flags, group scope, the unconfirmed email row, --output json, an API error, positional arguments rejected, --repo with --group rejected, both sides of the base repository requirement, and GITLAB_GROUP.
  • update: by ID without a lookup, by username (list, then update, in order), only the changed fields sent, all fields, group scope, --output json, no field flag, unknown username, invalid ID, an API error, and the same scope cases.
  • delete: project and group with --yes and no hard_delete, by username, --yes required when not interactive, unknown username, invalid ID, an API error, and the scope cases. The prompt is driven with ugh: confirming deletes, declining deletes nothing, a username is resolved before the prompt so it names the ID, and a group from GITLAB_GROUP is named in the advisory.
  • serviceaccountutils: ResolveID with a numeric ID (no API call), a username found in a group and in a project, not found, and an invalid ID. PrintDetails with and without a pending email.

Coverage: create 93.5%, update 94.1%, delete 93.5%, get 92.7%, serviceaccountutils 80.5% (List is covered from the list package).

Against a real instance: not run end to end. I have no group or project where I can hold service accounts.

lefthook run pre-push passes in full, including comment-overlap, comment-ratio, check-generated, check-args, markdownlint, vale and lychee.

Screenshots (if appropriate):

N/A - terminal output only.

Merge request reports

Loading
Loading