create a new label and return it github id NOTE: since createLabel mutation is still in preview mode we use github api v3 to create labels see https://developer.github.com/v4/mutation/createlabel/ and https://developer.github.com/v4/previews/#labels-preview
(ctx context.Context, label, color string)
| 520 | // NOTE: since createLabel mutation is still in preview mode we use github api v3 to create labels |
| 521 | // see https://developer.github.com/v4/mutation/createlabel/ and https://developer.github.com/v4/previews/#labels-preview |
| 522 | func (ge *githubExporter) createGithubLabel(ctx context.Context, label, color string) (string, error) { |
| 523 | url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[confKeyOwner], ge.conf[confKeyProject]) |
| 524 | client := &http.Client{} |
| 525 | |
| 526 | params := struct { |
| 527 | Name string `json:"name"` |
| 528 | Color string `json:"color"` |
| 529 | Description string `json:"description"` |
| 530 | }{ |
| 531 | Name: label, |
| 532 | Color: color, |
| 533 | } |
| 534 | |
| 535 | data, err := json.Marshal(params) |
| 536 | if err != nil { |
| 537 | return "", err |
| 538 | } |
| 539 | |
| 540 | req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) |
| 541 | if err != nil { |
| 542 | return "", err |
| 543 | } |
| 544 | |
| 545 | ctx, cancel := context.WithTimeout(ctx, defaultTimeout) |
| 546 | defer cancel() |
| 547 | req = req.WithContext(ctx) |
| 548 | |
| 549 | // need the token for private repositories |
| 550 | req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.defaultToken.Value)) |
| 551 | |
| 552 | resp, err := client.Do(req) |
| 553 | if err != nil { |
| 554 | return "", err |
| 555 | } |
| 556 | |
| 557 | if resp.StatusCode != http.StatusCreated { |
| 558 | return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode) |
| 559 | } |
| 560 | |
| 561 | aux := struct { |
| 562 | ID int `json:"id"` |
| 563 | NodeID string `json:"node_id"` |
| 564 | Color string `json:"color"` |
| 565 | }{} |
| 566 | |
| 567 | data, _ = io.ReadAll(resp.Body) |
| 568 | defer resp.Body.Close() |
| 569 | |
| 570 | err = json.Unmarshal(data, &aux) |
| 571 | if err != nil { |
| 572 | return "", err |
| 573 | } |
| 574 | |
| 575 | return aux.NodeID, nil |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | // create github label using api v4 |
no test coverage detected