Skip to content

Commit

Permalink
Merge pull request cli#5939 from pxeger/5934
Browse files Browse the repository at this point in the history
Strip whitespace when adding topics
  • Loading branch information
Nate Smith authored Jul 14, 2022
2 parents 9d70d62 + 0cf2600 commit 7b7929b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion pkg/cmd/repo/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/repo/edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7b7929b

Please sign in to comment.