utils/github/api: refactor keychain code to use system_command

This commit is contained in:
Bjorn Neergaard 2023-05-25 20:33:49 -06:00
parent d634296109
commit fc63aca46a

View File

@ -135,11 +135,14 @@ module GitHub
# but only if that password looks like a GitHub Personal Access Token. # but only if that password looks like a GitHub Personal Access Token.
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def self.keychain_username_password def self.keychain_username_password
github_credentials = Utils.popen_write("git", "credential-osxkeychain", "get") do |pipe| git_credential_out, _, result = system_command "git",
pipe.write "protocol=https\nhost=github.com\n" args: ["credential-osxkeychain", "get"],
end input: ["protocol=https\n", "host=github.com\n"],
github_username = github_credentials[/username=(.+)/, 1] print_stderr: false
github_password = github_credentials[/password=(.+)/, 1] return unless result.success?
github_username = git_credential_out[/username=(.+)/, 1]
github_password = git_credential_out[/password=(.+)/, 1]
return unless github_username return unless github_username
# Don't use passwords from the keychain unless they look like # Don't use passwords from the keychain unless they look like
@ -148,12 +151,6 @@ module GitHub
return unless GITHUB_PERSONAL_ACCESS_TOKEN_REGEX.match?(github_password) return unless GITHUB_PERSONAL_ACCESS_TOKEN_REGEX.match?(github_password)
github_password github_password
rescue Errno::EPIPE
# The above invocation via `Utils.popen` can fail, causing the pipe to be
# prematurely closed (before we can write to it) and thus resulting in a
# broken pipe error. The root cause is usually a missing or malfunctioning
# `git-credential-osxkeychain` helper.
nil
end end
# odeprecated: Not really deprecated; change the order to prefer `github_cli_token` over # odeprecated: Not really deprecated; change the order to prefer `github_cli_token` over