MCPcopy
hub / github.com/cli/cli / createLabel

Function createLabel

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

Source from the content-addressed store, hash-verified

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

Callers 2

cloneLabelsFunction · 0.85
createRunFunction · 0.85

Calls 7

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

Tested by

no test coverage detected