Skip to content

Print help even if logged out#3850

Merged
vilmibm merged 12 commits intocli:trunkfrom
chemotaxis:fix-actions-help
Jun 22, 2021
Merged

Print help even if logged out#3850
vilmibm merged 12 commits intocli:trunkfrom
chemotaxis:fix-actions-help

Conversation

@chemotaxis
Copy link
Contributor

@chemotaxis chemotaxis commented Jun 16, 2021

Fixes #3823.

Testing note: I logged out first using gh auth logout.

Currently, if you're logged out, you can't access any help pages with the flags --help and -h for commands that require authentication.

While gh help actions works, gh actions --help does not.

If you try gh actions --help, you get a message about authentication:

Welcome to GitHub CLI!

To authenticate, please run `gh auth login`.

This pull request allows you to explicitly ask for help pages using the help flags, even if you are logged out.

After applying this change, gh actions will still print the same message above, but gh actions --help will now print this:

Welcome to GitHub Actions on the command line.

GitHub CLI integrates with Actions to help you manage runs and workflows.

Interacting with workflow runs  
gh run list:      List recent workflow runs  
gh run view:      View details for a workflow run or one of its jobs  
gh run watch:     Watch a workflow run while it executes  
gh run rerun:     Rerun a failed workflow run  
gh run download:  Download artifacts generated by runs

[Rest of help page]...

You have to explicitly ask for help using the help flags.  Otherwise,
`gh` will just print the authentication message.
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this entire check be moved into cmdutil.IsAuthCheckEnabled using Cobra introspection features?

  1. If the command to be executed has the name help, auth check should be skipped. This would handle gh help <command>
  2. If the command to be executed has a --help flag (as determined by cmd.Flags().Lookup("help")) and the flag is enabled, auth check should be skipped. This will transparently handle -h as well.

@chemotaxis
Copy link
Contributor Author

chemotaxis commented Jun 16, 2021

  1. If the command to be executed has the name help, auth check should be skipped. This would handle gh help <command>

gh help actions already skips authentication and returns the help page, even if you're logged out. gh returns false for cmdutil.IsAuthCheckEnabled().

  1. If the command to be executed has a --help flag (as determined by cmd.Flags().Lookup("help")) and the flag is enabled, auth check should be skipped. This will transparently handle -h as well.

When I run gh actions --help with this code:

func IsAuthCheckEnabled(cmd *cobra.Command) bool {
	log.SetFlags(log.Llongfile)
	log.Printf("%+v", cmd.Flags().Lookup("help"))
	log.Printf("%+v", cmd.Flags())
...

I get:

github.com/cli/cli/pkg/cmdutil/auth_check.go:43: <nil>
github.com/cli/cli/pkg/cmdutil/auth_check.go:44: &{
  Usage:<nil> 
  SortFlags:true 
  ParseErrorsWhitelist:{UnknownFlags:false} 
  name:actions 
  parsed:false 
  actual:map[] 
  orderedActual:[] 
  sortedActual:[] 
  formal:map[] 
  orderedFormal:[] 
  sortedFormal:[] 
  shorthands:map[] 
  args:[] 
  argsLenAtDash:-1 
  errorHandling:0 
  output:0xc0003a7e30 
  interspersed:true 
  normalizeNameFunc:<nil> 
  addedGoFlagSets:[]
}

I tried looking for an appropriate Cobra method, but it seems the flags aren't getting set somewhere. Am I missing something obvious? If I wasn't depending on expandedArgs in gh/main.go, I could move the check into cmdutil.IsAuthCheckEnabled.

Currently, this still checks authentication, but we skip the
authentication message and exit normally.
The linter picked up that the error value from cmd.Help() isn't checked.

Even though cmd.Help() returns an error value, it's always nil. The
inner HelpFunc() function directly prints the error message instead of
returning an error value.
Copy link

@Strawberry14388 Strawberry14388 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

@chemotaxis
Copy link
Contributor Author

I've updated the pull request and the pull request description. My latest commits now skip the authentication message and just print the help page. We still do the authentication check though.

I've dug around a little more, but I'm still not sure why cmd.Flags().Lookup("help") returns nil.

mislav added 4 commits June 17, 2021 16:01
With auth check being done via Cobra hooks, it is automatically skipped
for non-runnable commands and `-h/--help` flag usage.
The command is now non-runnable, meaning it's exempt from auth check.
@mislav
Copy link
Contributor

mislav commented Jun 17, 2021

Thanks for looking into all this. I didn't realize Cobra didn't make this more straightforward. I've pushed some changes to avoid manually checking for -h/--help flags and to avoid re-implementing the help system. It relies on Cobra's pre-run hooks. Since Cobra's implementation of hooks is pretty bad, I had to jump through hoops to ensure that the -R/--repo override hook doesn't accidentally override the auth-checking one.

At the moment, the "help footer" doesn't add any new information, but if
additional flags are added later, they should appear in the footer. This
change restores this help footer:

```shell
USAGE
  gh actions [flags]

INHERITED FLAGS
  --help   Show help for command

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual
```
@chemotaxis
Copy link
Contributor Author

No problem! I'm still trying to understand Cobra and the gh codebase, so I wasn't sure if I was missing something obvious.

The latest commit adds back the help "footer" that shows usage, inherited flags, etc. At the moment, the footer doesn't provide any new information, but if you add other flags to gh actions later, it should appear.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your help!

Trailing whitespace is significant in Markdown.

This reverts commit 682c15d.
@mislav mislav requested review from samcoe and vilmibm June 18, 2021 13:18
@mislav
Copy link
Contributor

mislav commented Jun 18, 2021

@chemotaxis I've reverted whitespace changes to the help doc because we feed all docs as Markdown to our documentation site, and whitespace (even trailing) in Markdown is significant.

@chemotaxis
Copy link
Contributor Author

@chemotaxis I've reverted whitespace changes to the help doc because we feed all docs as Markdown to our documentation site, and whitespace (even trailing) in Markdown is significant.

Sorry, I didn't realize that!

It looks like a similar check is done in ColorScheme.Bold() where it
checks whether the scheme is enabled or not.
In this branch, we originally avoided the authentication check by
getting rid of the run method attached to the command.  Instead of that,
this commit makes the `gh actions` command runnable again, but the
authentication is disabled with `cmdutil.DisableAuthCheck`; this mirrors
what's done for `gh version`.

`gh actions` and `gh actions [-h | --help]` all work while being logged
out.

In addition, this commit restores some original behavior.  Before this
commit, the help footer (usage, inherited flags, etc.) is appended
whether you use `gh actions` or `gh actions --help`.  This commit
restores the original behavior where `gh actions` prints just the text
for the actions explanation, but `gh actions --help` appends the help
footer.
@chemotaxis
Copy link
Contributor Author

chemotaxis commented Jun 19, 2021

This pull request now has two main fixes:

  • Fixing the general problem of bypassing authentication when explicitly asking for help using any command
    • Caveat: gh help <command> actually already works
  • Refactoring gh actions

I was looking over the changes one last time and noticed the cmdutil.DisableAuthCheck function. I thought I'd gone over it before, but decided to investigate uses of it in the codebase. gh actions can bypass authentication by just adding cmdutil.DisableAuthCheck to the command instead of making it non-runnable.

The latest commit makes gh actions runnable again, but I've disabled authentication with the disable function. There are quite a few uses of the disable function, but gh version is probably the simplest example.

This also restores the original print behavior where gh actions prints just the text for explaining actions and workflows, but gh actions --help appends the help footer (usage, inherited flags, etc.).

Feel free to revert b0f58d0, but I think this is a better solution since it maintains the original print behavior for gh actions.

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me

Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@vilmibm vilmibm merged commit 640a089 into cli:trunk Jun 22, 2021
@chemotaxis chemotaxis deleted the fix-actions-help branch June 23, 2021 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh actions --help requires login

6 participants