Skip to content

[gh env] Introduce gh env list subcommand - #10423

Open
iamazeem wants to merge 11 commits into
cli:trunkfrom
iamazeem:5149-gh-env-list
Open

iamazeem wants to merge 11 commits into
cli:trunkfrom
iamazeem:5149-gh-env-list

Conversation

@iamazeem

@iamazeem iamazeem commented Feb 12, 2025 •

Copy link
Copy Markdown
Contributor

Related issue: #5149
Old AC: #5149 (comment)

Fixes #10502 with new AC.

API endpoints used

Tests

  • bin/gh help
# main help text entry for core commands
$ bin/gh help
Work seamlessly with GitHub from the command line.

USAGE
  gh <command> <subcommand> [flags]

CORE COMMANDS
  auth:        Authenticate gh and git with GitHub
  browse:      Open repositories, issues, pull requests, and more in the browser
  codespace:   Connect to and manage codespaces
  env:         Manage deployment environments
  gist:        Manage gists
  issue:       Manage issues
  org:         Manage organizations
  pr:          Manage pull requests
  project:     Work with GitHub Projects.
  release:     Manage releases
  repo:        Manage repositories
...
  • bin/gh help env
$ bin/gh help env
Work with GitHub deployment environments.

To learn more about GitHub deployment environments, see
<https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-deployments/managing-environments-for-deployment>.


USAGE
  gh env <command> [flags]

AVAILABLE COMMANDS
  list:        List GitHub deployment environments

FLAGS
  -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format

INHERITED FLAGS
  --help   Show help for command

EXAMPLES
  $ gh env list

LEARN MORE
  Use `gh <command> <subcommand> --help` for more information about a command.
  Read the manual at https://cli.github.com/manual
  Learn about exit codes using `gh help exit-codes`
  • bin/gh env list --help
$ bin/gh env list --help 
List GitHub deployment environments

For more information about output formatting flags, see `gh help formatting`.

USAGE
  gh env list [flags]

ALIASES
  gh env ls

FLAGS
  -q, --jq expression     Filter JSON output using a jq expression
      --json fields       Output JSON with the specified fields
  -L, --limit int         Maximum number of environments to fetch (default 30)
  -t, --template string   Format JSON output using a Go template; see "gh help formatting"
  -w, --web               Open environments in the browser

INHERITED FLAGS
      --help                     Show help for command
  -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format

JSON FIELDS
  canAdminBypass, createdAt, customBranchPolicies, htmlUrl, id, name, nodeId,
  protectedBranches, protectionRules, updatedAt, url

EXAMPLES
  # List environments for the current repository
  $ gh env list

LEARN MORE
  Use `gh <command> <subcommand> --help` for more information about a command.
  Read the manual at https://cli.github.com/manual
  Learn about exit codes using `gh help exit-codes`
  • bin/gh env list (no environments)
# no environments (TTY)
$ bin/gh env list --repo iamazeem/cli
No environments found in iamazeem/cli

# no environments (no TTY)
$ bin/gh env list | cat
  • bin/gh env list (with environments)
# environments (TTY)
$ bin/gh env list --repo iamazeem/test

Showing 2 of 2 environments in iamazeem/test

NAME          PROTECTION RULES  SECRETS  VARIABLES
Dev           0                 2        3
github-pages  1                 0        0


# environments (no TTY)
$ bin/gh env list --repo iamazeem/test | cat
Dev
github-pages
  • bin/gh env list --json id,name
# environments JSON (TTY)
$ bin/gh env list --repo iamazeem/test --json id,name
[
  {
    "id": 860059220,
    "name": "Dev"
  },
  {
    "id": 4594393556,
    "name": "github-pages"
  }
]


# environments JSON (no TTY)
$ bin/gh env list --repo iamazeem/test --json id,name | cat
[{"id":860059220,"name":"Dev"},{"id":4594393556,"name":"github-pages"}]

Notes

The token being used may not have all the required permissions.

For example:

  • /repos/{owner}/{repo}/environments (accessible)
  • /repos/{owner}/{repo}/environments/{environment_name}/secrets (not accessible)

Here's a sample run for a similar scenario for cli/cli repo:

# TTY
$ bin/gh env list --repo cli/cli
X Failed to get environments: could not get secrets total count for cli-discuss-automation environment: HTTP 403: Must have admin rights to Repository. (https://api.github.com/repos/cli/cli/environments/cli-discuss-automation/secrets?per_page=1)


# non-TTY
$ bin/gh env list --repo cli/cli | cat
cli-discuss-automation
github-pages
production
staging

@iamazeem
iamazeem requested a review from a team as a code owner February 12, 2025 13:37
@iamazeem
iamazeem requested a review from jtmcg February 12, 2025 13:37
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Feb 12, 2025
@jtmcg

jtmcg commented Feb 13, 2025

Copy link
Copy Markdown
Contributor

I took a glance at this and I'm really excited about it, @iamazeem! I'm gonna chat with @williammartin about this tomorrow before giving you my review 🙂

@jtmcg

jtmcg commented Feb 14, 2025

Copy link
Copy Markdown
Contributor

Update after chatting with the team:

Breaking this work up

We're going to take the original issue and break it into sub-issues so each subcommand is easier to follow and implement 🙌 We recently did something similar for gh repo autolinks. Hopefully this will reduce some of the cognitive load for you in terms of understanding what is required for each piece and allow us to give you better reviews. This may result in some minor tweaks to what gh env list is doing, but I don't foresee anything significant.

Preferred command pattern

We have a new pattern for commands that avoids adding http stubs to tests for anything besides http callers. This has been a major improvement in test confidence, readability, and extensibility, as it allows us for testing our functional interfaces more directly. You can see the pattern on display in the gh repo autolinks command suite. Essentially, we use dependency injection of the http client to avoid mocking the http server in command tests.

We'd like this command suite to also follow that pattern. It would be great if you could take a crack at it! I'll give a formal review once you work this out. Let me know if you're having issues and I can lend a hand.

Providing support

We think this is a valuable set of work and we will endeavor to provide you resources for getting this done. Thanks again!

@iamazeem

Copy link
Copy Markdown
Contributor Author

@jtmcg: Updated the PR accordingly. It's ready for review. Thanks!

@iamazeem

Copy link
Copy Markdown
Contributor Author

@jtmcg: Any update on this?

@RyanHecht

Copy link
Copy Markdown

Attaching the new sub-issue for this (working on writing up the others/translating the AC!): #10502

@jtmcg jtmcg linked an issue Feb 25, 2025 that may be closed by this pull request
@jtmcg

jtmcg commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Attaching the new sub-issue for this (working on writing up the others/translating the AC!): #10502

Thanks @RyanHecht! @iamazeem, can you take a look at the new issue and confirm you've satisfied all the AC? Once you do, I'll give this a review

@iamazeem

iamazeem commented Mar 1, 2025

Copy link
Copy Markdown
Contributor Author

@jtmcg:
Updated the PR w.r.t. old/new AC, APIs, and UI/UX.
Mentioned some points/concerns on #10502.
It is ready for an initial review. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for gh env list

5 participants