Skip to content

Instantly share code, notes, and snippets.

@mszostok
Created January 24, 2023 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mszostok/defa5a5390e87b4f011b986742f714d8 to your computer and use it in GitHub Desktop.
Save mszostok/defa5a5390e87b4f011b986742f714d8 to your computer and use it in GitHub Desktop.
func createGitHubIssue(cfg Config, title, mdBody string) (string, error) {
client, err := gh.HTTPClient(&ghapi.ClientOptions{
AuthToken: cfg.GitHub.Token,
})
if err != nil {
return "", err
}
url := fmt.Sprintf("%s/repos/%s/issues", gitHubAPI, cfg.GitHub.Repository)
body := struct {
Title string `json:"title"`
Body string `json:"body"`
Labels []string `json:"labels"`
}{
Title: title,
Body: mdBody,
Labels: []string{"bug"},
}
out, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("while marshaling request body: %w", err)
}
rawResp, err := client.Post(url, "application/vnd.github+json", bytes.NewReader(out))
if err != nil {
return "", fmt.Errorf("while creating an issue: %w", err)
}
defer rawResp.Body.Close()
if rawResp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("got unexpected status code, got %v, expected %v", rawResp.StatusCode, http.StatusCreated)
}
resp := struct {
URL string `json:"html_url"`
}{}
rawBody, err := io.ReadAll(rawResp.Body)
if err != nil {
return "", fmt.Errorf("while reading body: %w", err)
}
err = json.Unmarshal(rawBody, &resp)
if err != nil {
return "", fmt.Errorf("while unmarshalling response: %w", err)
}
return resp.URL, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment