Prompt offers to set up gh as the git credential helper for hostname. On an expiring login the user can cancel the sign-in entirely, in which case the returned error is [ErrExpiringSetupCredHelperCancelled]. When gh is already the configured helper no prompt is shown and a nil error is returned.
(hostname string, expiring bool)
| 38 | // sign-in entirely, in which case the returned error is [ErrExpiringSetupCredHelperCancelled]. When gh is already the |
| 39 | // configured helper no prompt is shown and a nil error is returned. |
| 40 | func (flow *GitCredentialFlow) Prompt(hostname string, expiring bool) error { |
| 41 | flow.expiring = expiring |
| 42 | |
| 43 | // First we'll fetch the credential helper that would be used for this host |
| 44 | var configuredHelperErr error |
| 45 | flow.helper, configuredHelperErr = flow.HelperConfig.ConfiguredHelper(hostname) |
| 46 | // If the helper is gh itself, then we don't need to ask the user if they want to update their git credentials |
| 47 | // because it will happen automatically by virtue of the fact that gh will return the active token. |
| 48 | // |
| 49 | // Since gh is the helper, this token may be used for git operations, so we'll additionally request the workflow |
| 50 | // scope to ensure that git push operations that include workflow changes succeed. |
| 51 | if flow.helper.IsOurs() { |
| 52 | flow.scopes = append(flow.scopes, "workflow") |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // An expiring login needs gh as the credential helper so it can refresh the token, so we present a dedicated |
| 57 | // prompt that spells out the consequence of declining and offers to stop the sign-in entirely. |
| 58 | if expiring { |
| 59 | return flow.promptExpiring(hostname, configuredHelperErr) |
| 60 | } |
| 61 | |
| 62 | // Prompt the user for whether they want to configure git with the newly obtained token |
| 63 | result, err := flow.Prompter.Confirm("Authenticate Git with your GitHub credentials?", true) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | flow.shouldSetup = result |
| 68 | |
| 69 | if flow.shouldSetup { |
| 70 | // If the user does want to configure git, we'll check the error returned from fetching the configured helper |
| 71 | // above. If the error indicates that git isn't installed, we'll return an error now to ensure that the auth |
| 72 | // flow is aborted before the user goes any further. |
| 73 | // |
| 74 | // Note that this is _slightly_ naive because there may be other reasons that fetching the configured helper |
| 75 | // fails that might cause later failures but this code has existed for a long time and I don't want to change |
| 76 | // it as part of a refactoring. |
| 77 | // |
| 78 | // Refs: |
| 79 | // * https://git-scm.com/docs/git-config#_description |
| 80 | // * https://github.com/cli/cli/pull/4109 |
| 81 | if _, ok := errors.AsType[*git.NotInstalled](configuredHelperErr); ok { |
| 82 | return configuredHelperErr |
| 83 | } |
| 84 | |
| 85 | // On the other hand, if the user has requested setup we'll additionally request the workflow |
| 86 | // scope to ensure that git push operations that include workflow changes succeed. |
| 87 | flow.scopes = append(flow.scopes, "workflow") |
| 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // promptExpiring runs the credential-helper decision for an expiring login. gh must be the git credential helper to |
| 94 | // refresh an expiring token, so the consequence of declining is printed as a warning and the user is offered three |