Created
January 24, 2023 13:33
-
-
Save mszostok/defa5a5390e87b4f011b986742f714d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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