Load generated release notes (list of pull requests and contributors) from GitHub.
(newVer, targetCommit, prevVer string)
| 94 | // Load generated release notes (list of pull requests and contributors) |
| 95 | // from GitHub. |
| 96 | func generatedNotes(newVer, targetCommit, prevVer string) (string, error) { |
| 97 | fields := map[string]string{ |
| 98 | "tag_name": newVer, |
| 99 | "target_commitish": targetCommit, |
| 100 | "previous_tag_name": prevVer, |
| 101 | } |
| 102 | bs, err := json.Marshal(fields) |
| 103 | if err != nil { |
| 104 | return "", err |
| 105 | } |
| 106 | req, err := http.NewRequest(http.MethodPost, "https://api.github.com/repos/"+githubRepo+"/releases/generate-notes", bytes.NewReader(bs)) //nolint:noctx |
| 107 | if err != nil { |
| 108 | return "", err |
| 109 | } |
| 110 | |
| 111 | req.Header.Set("Accept", "application/vnd.github+json") |
| 112 | req.Header.Set("Authorization", "Bearer "+githubToken) |
| 113 | req.Header.Set("X-Github-Api-Version", "2022-11-28") |
| 114 | res, err := http.DefaultClient.Do(req) |
| 115 | if err != nil { |
| 116 | return "", err |
| 117 | } |
| 118 | if res.StatusCode != http.StatusOK { |
| 119 | bs, _ := io.ReadAll(res.Body) |
| 120 | log.Print(string(bs)) |
| 121 | return "", errors.New(res.Status) //nolint:err113 |
| 122 | } |
| 123 | defer res.Body.Close() |
| 124 | |
| 125 | var resJSON struct { |
| 126 | Body string |
| 127 | } |
| 128 | if err := json.NewDecoder(res.Body).Decode(&resJSON); err != nil { |
| 129 | return "", err |
| 130 | } |
| 131 | return strings.TrimSpace(removeHTMLComments(resJSON.Body)), nil |
| 132 | } |
| 133 | |
| 134 | func removeHTMLComments(s string) string { |
| 135 | return regexp.MustCompile(`<!--.*?-->`).ReplaceAllString(s, "") |