Repository navigation
Refactor parseGitURL - #4983
Refactor parseGitURL#4983
Conversation
|
Skipping CI for Draft Pull Request. |
|
|
||
| func parsePath(n string) string { | ||
| parsed, err := url.Parse(n) | ||
| // TODO(annasong): decide how to handle error, i.e. return error, empty string, etc. |
There was a problem hiding this comment.
This PR is suggesting removing this parsing entirely. I noticed that no tests fail if we do so, and so I was wondering what this is intended to catch. I looked back in the history, and it was introduced by #3900. IIUC that PR intended it as a more reliable way (vs the former regex) to separate query parts from the path. We still use it for that today; since #4863 we just do it a bit earlier in the parsing flow. At that earlier point, we're passing the whole raw value to the parser, whereas originally (in valid cases anyway) we were passing just the kust root path + query. So the Path value we would get back at our new invocation is not usable.
The question is, do we then need to continue to put the path through the URL parser at the end of the parsing flow, now on its own (the query long since removed)? I don't think so, because IIUC the original intent was not to validate the path; it was just convenient to take the value returned when we separated a query from it. If you go back one more step in the history, peelQuery was just splitting a query chunk off of n when present.
This also relates to the stance I explained on my last PR re: passing things through to git if they're not interfering with our ability to parse out the repo root. In this case, we've necessarily already done that.
LMK if you agree.
| } | ||
| var host, rest = n, "" | ||
| if sepIndex := findPathSeparator(n, acceptSCP); sepIndex > 0 { | ||
| host, rest = n[:sepIndex+1], n[sepIndex+1:] |
There was a problem hiding this comment.
Before, when we didn't find a separator, the whole thing was considered a path. Now it's considered a host. I think this makes more sense, and it maintains the current error messages in some cases. Notably, ssh://git.example.com should complain about orgRepo, not host, missing. Previously all parsing would complete before any errors would return, and for whatever reason orgRepo was checked first, resulting in the correct message for that case despite host also being empty.
|
@KnVerey: This PR has multiple commits, and the default merge method is: merge. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
|
||
| if strings.Contains(n, gitDelimiter) { | ||
| index := strings.Index(n, gitDelimiter) | ||
| // Adding _git/ to host |
There was a problem hiding this comment.
I don't know why it was originally added to the host segment in #1271. It's part of the path to to the repo, not the host (our new variable names are nice here!). At the end of the day, correcting this doesn't affect the clone spec.
8e0e801 to
4d8aed0
Compare
| if pathElements := strings.Split(cleanedPath, string(filepath.Separator)); len(pathElements) > 0 && | ||
| pathElements[0] == filesys.ParentDir { | ||
| return nil, fmt.Errorf("url path exits repo: %s", n) | ||
| } |
There was a problem hiding this comment.
I initially only moved the RepoPath and Host validations into their respective parser methods, but then I noticed that this one is effectively just a KustRootPath validation, so I moved it too. Then all I had left here was the IsAbs check, so I deleted this and renamed parseGitURL to NewRepoSpecFromURL (FYI because that's not what it looks like on Github).
4d8aed0 to
abbeb64
Compare
abbeb64 to
0020839
Compare
| // the parts. | ||
| // the different parts of URL, set into a RepoSpec object and return RepoSpec object. | ||
| // It MUST return an error if the input is not a git-like URL, as this is used by some code paths | ||
| // to distinguish between local and remote paths. |
There was a problem hiding this comment.
As of today, this seems to be the ONLY thing the errors are used for: to judge whether the string is a URL.
| Host: "[email protected]:", | ||
| RepoPath: "company/project", | ||
| KustRootPath: "/path", | ||
| KustRootPath: "path", |
There was a problem hiding this comment.
I don't think it matters whether we end up with a leading slash here, but previously we were inconsistent. Github URLs (which generally are best tested/supported) did not have the prefix previously, so I took them all in that direction.
| }, | ||
| }, | ||
| { | ||
| name: "unsupported protocol after username (invalid but currently passed through to git)", |
There was a problem hiding this comment.
This is a behaviour change vs before all our refactoring, though at the end of the day this URL could never work. This PR would require adding code to continue accepting this particular input, and since it isn't valid anyway, I didn't think it was worthwhile.
| name: "triple slash absolute path", | ||
| kustomization: ` | ||
| resources: | ||
| - file:///$ROOT/simple.git |
There was a problem hiding this comment.
It turns out $ROOT already contains a slash, so this test case used four slashes not three, and that's not valid.
| { | ||
| name: "t8", | ||
| name: "_git host delimiter in non-github url", | ||
| input: "https://itfs.mycompany.com/collection/project/_git/somerepos", |
There was a problem hiding this comment.
mycompany.com is already a used domain by an irrelevant company.
https://www.whois.com/whois/mycompany.com
Can we use a better example domain like example.com?
https://www.rfc-editor.org/rfc/rfc2606#section-3
There was a problem hiding this comment.
That's a good callout, and thank you for providing the references. TIL about that RFC! In this case, the test suite is just for the parsing step, so there's no chance of us actually trying to use these URLs--the names are for use case illustration purposes only. It uses a variety of invalid/irrelevant domains currently, and I think I'll leave them alone for the purposes of this PR.
| name: "double-slash path delimiter https", | ||
| input: "https://fake-git-hosting.org/path/to/repo//examples/multibases/dev", | ||
| cloneSpec: "https://fake-git-hosting.org/path/to.git", | ||
| cloneSpec: "https://fake-git-hosting.org/path/to/repo.git", |
There was a problem hiding this comment.
I think fake-git-hosting.org needs to change too.
|
I think this PR looks good! |
There was a problem hiding this comment.
Very cleanly and nicely done! Thanks. I see @koba1t left some comments, so I'm adding a hold so you can take a look at those comments. Feel free to remove the hold when you want to merge.
/lgtm
/hold
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: KnVerey, natasha41575 The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/unhold |
Part of #4866
Refactors the code that splits the RepoPath from the KustRootPath, and fixes inconsistencies and bugs in doing so:
//delimiter work for all URLs (we had skipped tests showing this was broken in some cases)TIL while working on PR that the error messages
NewRepoSpecFromURLnever seem to make it to the user AFAICT. Instead, the return of an error is interpreted as "this is probably not a URL at all". For example,(fl *fileLoader) Newthen tries to load it as a local file, and(fl *fileLoader) httpClientGetContentreturns an HTTP error instead of a "this was a remote repo" error. Stuff like this is (part of) why our error messages are so terrible. This PR does not attempt to change this, but the unusual implication of an error return value is good to keep in mind for review.Remaining follow-ups:
.gitsuffix entirely (originally included here but removed for clarity: see this comment)