MCPcopy Create free account
hub / github.com/cli/cli / createLabel

Function createLabel

pkg/cmd/label/create.go:129–160  ·  view source on GitHub ↗
(client *http.Client, repo ghrepo.Interface, opts *createOptions)

Source from the content-addressed store, hash-verified

127}
128
129func createLabel(client *http.Client, repo ghrepo.Interface, opts *createOptions) error {
130 apiClient := api.NewClientFromHTTP(client)
131 path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "labels")
132 if err != nil {
133 return err
134 }
135 requestByte, err := json.Marshal(map[string]string{
136 "name": opts.Name,
137 "description": opts.Description,
138 "color": opts.Color,
139 })
140 if err != nil {
141 return err
142 }
143 requestBody := bytes.NewReader(requestByte)
144 err = apiClient.REST(repo.RepoHost(), "POST", path.String(), requestBody, nil)
145
146 if httpError, ok := err.(api.HTTPError); ok && isLabelAlreadyExistsError(httpError) {
147 err = errLabelAlreadyExists
148 }
149
150 if opts.Force && errors.Is(err, errLabelAlreadyExists) {
151 editOpts := editOptions{
152 Description: opts.Description,
153 Color: opts.Color,
154 Name: opts.Name,
155 }
156 return updateLabel(apiClient, repo, &editOpts)
157 }
158
159 return err
160}
161
162func updateLabel(apiClient *api.Client, repo ghrepo.Interface, opts *editOptions) error {
163 path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "labels", opts.Name)

Callers 2

cloneLabelsFunction · 0.85
createRunFunction · 0.85

Calls 9

NewClientFromHTTPFunction · 0.92
JoinPathFunction · 0.92
updateLabelFunction · 0.85
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RESTMethod · 0.65
RepoHostMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected