Skip to content

Fix git protocol and refactor Config interface - #8246

Merged
samcoe merged 3 commits into
trunkfrom
fix-git-protocol
Oct 27, 2023
Merged

samcoe merged 3 commits into
trunkfrom
fix-git-protocol

Conversation

@samcoe

@samcoe samcoe commented Oct 24, 2023 •

Copy link
Copy Markdown
Contributor

Fixes #8250

This PR refactors the Config interface to now include direct accessor functions for the default configuration keys. This allows callers to not have to know about key names and hides that implementation detail from them. Outside of the config command and tests there should be no places aware of the configuration keys. As part of this work the AuthConfig.GitProtocol function got moved to now live on Config. I also added in more constants in the config package for the configuration keys so that we can stop writing them out without the compilers help. This PR is not as long as it looks as most of these changes are just updating call points. Lastly, there was a stale prompter mock that is included in the PR but is unrelated to the changes above.

@samcoe samcoe self-assigned this Oct 24, 2023
@samcoe samcoe changed the title Fix git protocol Fix git protocol and refactor Config interface Oct 24, 2023
@samcoe
samcoe requested a review from andyfeller October 24, 2023 16:20
@samcoe
samcoe marked this pull request as ready for review October 24, 2023 16:20
@samcoe
samcoe requested a review from a team as a code owner October 24, 2023 16:20
require.ErrorAs(t, err, &keyNotFoundError)
}

func TestGitProtocolNotLoggedInDefaults(t *testing.T) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test is no longer relevant as GitProtocol no longer lives on authCfg.

Comment thread internal/config/auth_config_test.go Outdated

// When we get the git protocol
gitProtocol := authCfg.GitProtocol("github.com")
protocol, err := authCfg.cfg.Get([]string{hosts, "github.com", gitProtocol})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is kind of annoying as authCfg does not have anyway to access the git protocol now. Only used in this test so I think it is okay for now.

Comment thread internal/config/config.go
Comment on lines +34 to +39
Browser(string) string
Editor(string) string
GitProtocol(string) string
HTTPUnixSocket(string) string
Pager(string) string
Prompt(string) string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the big change, adding in these accessors.

Comment thread internal/config/config.go

// GitProtocol will retrieve the git protocol for the logged in user at the given hostname.
// If none is set it will return the default value.
func (c *AuthConfig) GitProtocol(hostname string) string {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is now moved off of AuthConfig and onto Config.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the problem with this old code that it didn't return a final default value if an error happens?

I'm having trouble tracing this back to a specific issue to make sure I understand the problem being fixed 🙇

Comment thread internal/config/config.go Outdated
// If the encrypt option is specified it will first try to store the auth token
// in encrypted storage and will fall back to the plain text config file.
func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) (bool, error) {
func (c *AuthConfig) Login(hostname, username, token, protocol string, secureStorage bool) (bool, error) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed the argument name to not conflict with the new const gitProtocol.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be useful in the future to suffix these constants like gitProtocolSetting, gitProtocolConfig, gitProtocolKey if need to avoid conflicts comes up again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is a good call out, I will make that change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure how this got stale, but when I did go generate this was updated so I included it in this PR. It is unrelated to the config changes.

@andyfeller andyfeller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All in all, I don't see anything glaring about these changes that has me concerned. The desire to decouple application logic from how the underlying configuration is handled makes sense, however I don't know where this tight coupling was causing problems much less how it manifested with the git protocol.

Having an issue link would help give me confidence in being able to ship these changes 🙇

Comment thread internal/config/config.go Outdated
// If the encrypt option is specified it will first try to store the auth token
// in encrypted storage and will fall back to the plain text config file.
func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) (bool, error) {
func (c *AuthConfig) Login(hostname, username, token, protocol string, secureStorage bool) (bool, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be useful in the future to suffix these constants like gitProtocolSetting, gitProtocolConfig, gitProtocolKey if need to avoid conflicts comes up again.

Comment thread internal/config/config.go

// GitProtocol will retrieve the git protocol for the logged in user at the given hostname.
// If none is set it will return the default value.
func (c *AuthConfig) GitProtocol(hostname string) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the problem with this old code that it didn't return a final default value if an error happens?

I'm having trouble tracing this back to a specific issue to make sure I understand the problem being fixed 🙇

@samcoe

samcoe commented Oct 25, 2023

Copy link
Copy Markdown
Contributor Author

@andyfeller Apologies for not expanding my PR body further. It was the end of my day and was just trying to get this open for review.

For more context I opened up #8250.

@andyfeller

Copy link
Copy Markdown
Contributor

It was the end of my day and was just trying to get this open for review.

Preach! 🙌 Communication is hard on top of actually fixing issues, keep that 💩 up

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.

Globally set git protocol is ignored in some places

3 participants