feat(service-account): Add the list and get commands

Description

First of three merge requests implementing glab service-account for #8279 (closed). This one adds the parent command with list and get, for both project and group service accounts.

create, update and delete follow, then the token subcommands.

Command surface

glab service-account list                  [-g, --group <group>] [-F, --output text|json] [--jq <expr>] [-R, --repo <repo>]
glab service-account get <id | username>   [-g, --group <group>] [-F, --output text|json] [--jq <expr>] [-R, --repo <repo>]

Notes for review

  • Scope selection mirrors deploy-token. With neither --repo nor --group, the commands act on the current project, or on the group in GITLAB_GROUP if that is set. This is the one design point I'd like confirmed here, since the later merge requests build on it.
  • Instance-level service accounts are out of scope, as agreed on the issue.
  • Shared setup lives in serviceaccountutils. Scope resolution, the client, the account list and the <id | username> lookup are written once, instead of being copied into every subcommand.
  • A username is resolved from the list. The list endpoints have no username filter, so get <username> pages through the scope's accounts and matches exactly. A numeric argument is always an ID and skips the lookup.
  • Text output uses the fields common to both API types. Group and project accounts are different client-go types (UserName vs Username, and only the group one has public_email). Text output maps both to ID, username, name and email. JSON output prints the API response unchanged.
  • Required roles and the GitLab version are in the parent's help. Group: Owner, or an administrator on Self-Managed and Dedicated. Project: Maintainer or Owner, or an administrator, on GitLab 18.11 or later.

Commits

  • feat(service-account): Add the list command

    Service accounts can only be managed through glab api today. This adds the command group with project and group scope, starting with list.

  • feat(service-account): Add the get command

    Accounts are referred to by ID or username. The API has no username filter, so a username is resolved from the scope's account list.

Related to #8279 (closed).

Not Resolves: this is the first of three merge requests, and the issue should stay open until the token subcommands land.

How has this been tested?

Unit tests use cmdtest.SetupCmdForTest with client-go mocks from gitlab.com/gitlab-org/api/client-go/v3/testing.

  • list and get cover project scope, group scope, --output json (unmarshalled into the client-go type, including the group-only public_email), the empty list, an API error, --repo with --group being rejected, both sides of the base repository requirement, and GITLAB_GROUP from the environment.
  • get also covers lookup by username, an unknown username, and a missing argument.
  • serviceaccountutils covers the lookup on its own: a username found in a group and in a project, a username not found, and a numeric ID, which makes no API call.

Coverage: list 94.3%, get 93.3%, serviceaccountutils 95.7%.

Against a real instance: not run end to end, since I don't have a group or project where I can hold service accounts. A read-only call against a group without the required role shows the permission error:

$ glab service-account list -g gitlab-org
GET https://gitlab.com/api/v4/groups/gitlab-org/service_accounts: 403 {message: 403 Forbidden}.

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