Confirm name change before creating a repo with special characters#4562
Confirm name change before creating a repo with special characters#4562
Conversation
mislav
left a comment
There was a problem hiding this comment.
Thanks for this! I have an important request of having this logic work only in interactive mode.
mislav
left a comment
There was a problem hiding this comment.
Oops, I wanted to "Request Changes" instead 😅
|
Hopefully this is a little better :) I'm not sure how I feel about this though: Could the two confirmations at the end be combined? I suppose the first confirmation only shows if the name will be normalized, so not a big deal but it still seems kind of painful to confirm twice. |
pkg/cmd/repo/create/create.go
Outdated
| alreadyDashed = true | ||
| } | ||
| func normalizeRepoName(name string) string { | ||
| re := regexp.MustCompile(`[^a-zA-Z0-9-._]+`) |
There was a problem hiding this comment.
- Since
-has special meaning inside[...]in regular expressions, a literal-inside of[]should always be at the very end, just before the closing]. - Instead of
a-zA-Z0-9_you could use\wwhich is equivalent. - Since this regular expression is static, you can declare it on the package level instead of in the function:
var invalidRepoNameCharacters = regexp.MustCompile(...)
pkg/cmd/repo/create/create.go
Outdated
| } | ||
| func normalizeRepoName(name string) string { | ||
| re := regexp.MustCompile(`[^a-zA-Z0-9-._]+`) | ||
| if normalizedRepoName := strings.TrimSuffix(re.ReplaceAllString(name, "-"), ".git"); normalizedRepoName != "" { |
There was a problem hiding this comment.
There is no need for this if, I think. Just return the result of TrimSuffix and regexp replacement.
pkg/cmd/repo/create/create_test.go
Outdated
| Name string | ||
| LocalName string | ||
| RemoteName string | ||
| Name string |
There was a problem hiding this comment.
It doesn't look like the Name field is used in the tests, so you can remove it
pkg/cmd/repo/create/create.go
Outdated
| if inLocalRepo { | ||
| if normalizedRepoName != repoName { | ||
| var confirmNormalizedName bool | ||
| err := survey.AskOne(&survey.Confirm{ |
There was a problem hiding this comment.
Now that I see how this adds an additional prompt to the inLocalRepo case:
This will create the "OWNER/REPO" repository on GitHub. Continue? Yes
This will add an "origin" git remote to your local repository. Continue?
I wonder whether we could just fold this into a single prompt:
This will create "OWNER/REPO" on GitHub and add it as the "origin" git remote. Continue?
What do you think?
There was a problem hiding this comment.
Agreed, I think the confirm prompt should contain the new repo name regardless of inLocalRepo or if the name was normalized.
I removed the inLocalRepo parameter from confirmSubmit for this reason, was that safe to assume?
pkg/cmd/repo/create/create.go
Outdated
| return normalizedRepoName | ||
| } | ||
| return "-" | ||
| return strings.TrimSuffix(regexp.MustCompile(`[^\w.-]+`).ReplaceAllString(name, "-"), ".git") |
There was a problem hiding this comment.
This looks great now. I love how much simpler it has become 😄 Just had a thought: could you add a comment to this function explaining that this intends to match the logic by which GitHub the platform also normalize repo names?
There was a problem hiding this comment.
Thanks! I've been sharpening up on my regex skills because of this repo haha
I have a humble suggestion. Since I've been creating repos with the new feature, I tend to cross check and confirm that what I typed in is the same as what will be normalized in the confirm prompt every time.
Something that I like about the website, is that I can quickly know without cross checking, if my name will be changed thanks to the nifty color system.
Green -> what I typed in is the same as what will appear on the server
Yellow -> something in my repo name will be normalized
Red -> empty string (currently if .git is inputted, gh repo create will try to make a repo with an empty string as the name)
Could we do a colorizing system (Green / Yellow) for just the name so users can just confirm using the color? If this crowds the code or seems unnecessary I understand. Also if the name is an empty string, should we still call the API knowing that it will fail?
There was a problem hiding this comment.
The color switching for this isn't a bad idea, but I think it would be overkill for this feature right now. Let's hold off for now and see whether we feel it's actually necessary in the future.
|
Sorry for the hold up! I think I have it close to being ready. I added the conditional statement back in for This is what it looks like now in use
Gotcha! I'll keep an eye on this one :) |
pkg/cmd/repo/create/create.go
Outdated
| normalizedRepoName := normalizeRepoName(repoName) | ||
| if inLocalRepo { | ||
| promptString = `This will add an "origin" git remote to your local repository. Continue?` | ||
| promptString = fmt.Sprintf(`This will create "%s" on GitHub and add it as the "origin" git remote. Continue?`, normalizedRepoName) |
There was a problem hiding this comment.
Let's use the OWNER/REPO format in this prompt rather than just REPO since the else branch in this code also uses the OWNER/REPO format for the repository in the prompt. Rule of thumb is: repositories should always be displayed in OWNER/REPO format to reduce ambiguity.
pkg/cmd/repo/create/create.go
Outdated
|
|
||
| // normalizeRepoName takes in the repo name the user inputted and normalizes it using the same logic as GitHub (GitHub.com/new) | ||
| func normalizeRepoName(repoName string) string { | ||
| return strings.TrimSuffix(regexp.MustCompile(`[^\w.-]+`).ReplaceAllString(repoName, "-"), ".git") |
There was a problem hiding this comment.
Underscore _ is also an allowed character
There was a problem hiding this comment.
https://regex101.com/r/Sl6WtS/1
https://pkg.go.dev/regexp/syntax
I think \w includes _? I added _ to the regex just to be sure, but I supplied a test golang regex link above as well as the docs.
| targetRepo := normalizeRepoName(repoName) | ||
| if repoOwner != "" { | ||
| targetRepo = fmt.Sprintf("%s/%s", repoOwner, targetRepo) | ||
| } |
There was a problem hiding this comment.
I moved the targetRepo assignment outside of the if inLocalRepo statement. Is this what you meant above about HOST/REPO?
mislav
left a comment
There was a problem hiding this comment.
Thank you; this looks good! I've resolved conflicts with the base branch and I love how minimal we brought the diff down to 😄


Submitting a draft to close #4558
This feature asks for confirmation if the name typed in for a new repo will be different from what will end up on the GH backend.
Example:
passing name as an arg
interactive prompt
one liner
possible bug? As a user,
-ymeansSkip the confirmation prompt, however, why force something you didn't mean in the first place? Input needed.