Merge pull request #5939 from pxeger/5934

Strip whitespace when adding topics
This commit is contained in:
Nate Smith 2022-07-14 09:39:51 -05:00 committed by GitHub
commit 7b7929bb6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -326,7 +326,7 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
return err
}
if len(strings.TrimSpace(addTopics)) > 0 {
opts.AddTopics = strings.Split(addTopics, ",")
opts.AddTopics = parseTopics(addTopics)
}
if len(opts.topicsCache) > 0 {
@ -454,6 +454,14 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
return nil
}
func parseTopics(s string) []string {
topics := strings.Split(s, ",")
for i, topic := range topics {
topics[i] = strings.TrimSpace(topic)
}
return topics
}
func getTopics(ctx context.Context, httpClient *http.Client, repo ghrepo.Interface) ([]string, error) {
apiPath := fmt.Sprintf("repos/%s/%s/topics", repo.RepoOwner(), repo.RepoName())
req, err := http.NewRequestWithContext(ctx, "GET", ghinstance.RESTPrefix(repo.RepoHost())+apiPath, nil)

View file

@ -215,7 +215,7 @@ func Test_editRun_interactive(t *testing.T) {
askStubs: func(as *prompt.AskStubber) {
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Description", "Topics"})
as.StubPrompt("Description of the repository").AnswerWith("awesome repo description")
as.StubPrompt("Add topics?(csv format)").AnswerWith("a,b,c,d")
as.StubPrompt("Add topics?(csv format)").AnswerWith("a, b,c,d ")
as.StubPrompt("Remove Topics").AnswerWith([]string{"x"})
},
httpStubs: func(t *testing.T, reg *httpmock.Registry) {