changelog.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build ignore
  7. package main
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "errors"
  12. "flag"
  13. "fmt"
  14. "io/ioutil"
  15. "log"
  16. "net/http"
  17. "os"
  18. "os/exec"
  19. "regexp"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. )
  24. var (
  25. subjectIssues = regexp.MustCompile(`^([^(]+)\s+\((?:fixes|ref) ([^)]+)\)(?:[^\w])?$`)
  26. issueNumbers = regexp.MustCompile(`(#\d+)`)
  27. )
  28. type issue struct {
  29. number int
  30. subject string
  31. labels []string
  32. }
  33. func main() {
  34. flag.Parse()
  35. // Display changelog since the version given on the command line, or
  36. // figure out the last release if there were no arguments.
  37. var prevRel string
  38. if flag.NArg() > 0 {
  39. prevRel = flag.Arg(0)
  40. } else {
  41. bs, err := runError("git", "describe", "--abbrev=0", "HEAD^")
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. prevRel = string(bs)
  46. }
  47. // Get the git log with subject and author nickname
  48. bs, err := runError("git", "log", "--reverse", "--pretty=format:%s", prevRel+"..")
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. var resolved []issue
  53. // Split into lines
  54. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  55. // Check if subject contains a "(fixes ...)" or "(ref ...)""
  56. if m := subjectIssues.FindSubmatch(line); len(m) > 0 {
  57. issues := issueNumbers.FindAll(m[2], -1)
  58. for _, i := range issues {
  59. n, err := strconv.Atoi(string(i[1:]))
  60. if err != nil {
  61. continue
  62. }
  63. title, labels, err := githubIssueTitleLabels(n)
  64. if err != nil {
  65. continue
  66. }
  67. resolved = append(resolved, issue{n, title, labels})
  68. }
  69. }
  70. }
  71. sort.Slice(resolved, func(a, b int) bool {
  72. return resolved[a].number < resolved[b].number
  73. })
  74. var bugs, enhancements, other []issue
  75. var prev int
  76. for _, i := range resolved {
  77. if i.number == prev {
  78. continue
  79. }
  80. prev = i.number
  81. switch {
  82. case contains("unreleased", i.labels):
  83. continue
  84. case contains("bug", i.labels):
  85. bugs = append(bugs, i)
  86. case contains("enhancement", i.labels):
  87. enhancements = append(enhancements, i)
  88. default:
  89. other = append(other, i)
  90. }
  91. }
  92. fmt.Printf("--- markdown ---\n\n")
  93. markdown(prevRel, bugs, enhancements, other)
  94. fmt.Printf("\n--- text ---\n\n")
  95. text(prevRel, bugs, enhancements, other)
  96. }
  97. func markdown(version string, bugs, enhancements, other []issue) {
  98. fmt.Printf("## Resolved issues since %s\n\n", version)
  99. if len(bugs) > 0 {
  100. fmt.Printf("### Bugs\n\n")
  101. for _, issue := range bugs {
  102. fmt.Printf("* [#%d](https://github.com/syncthing/syncthing/issues/%d): %s\n", issue.number, issue.number, issue.subject)
  103. }
  104. fmt.Println()
  105. }
  106. if len(enhancements) > 0 {
  107. fmt.Printf("### Enhancements\n\n")
  108. for _, issue := range enhancements {
  109. fmt.Printf("* [#%d](https://github.com/syncthing/syncthing/issues/%d): %s\n", issue.number, issue.number, issue.subject)
  110. }
  111. fmt.Println()
  112. }
  113. if len(other) > 0 {
  114. fmt.Printf("### Unclassified\n\n")
  115. for _, issue := range other {
  116. fmt.Printf("* [#%d](https://github.com/syncthing/syncthing/issues/%d): %s\n", issue.number, issue.number, issue.subject)
  117. }
  118. fmt.Println()
  119. }
  120. }
  121. func text(version string, bugs, enhancements, other []issue) {
  122. fmt.Println(underline(fmt.Sprintf("Resolved issues since %s", version), "="))
  123. fmt.Println()
  124. if len(bugs) > 0 {
  125. fmt.Println(underline("Bugs", "-"))
  126. fmt.Println()
  127. for _, issue := range bugs {
  128. fmt.Printf("* #%d: %s\n", issue.number, issue.subject)
  129. }
  130. fmt.Println()
  131. }
  132. if len(enhancements) > 0 {
  133. fmt.Println(underline("Enhancements", "-"))
  134. fmt.Println()
  135. for _, issue := range enhancements {
  136. fmt.Printf("* #%d: %s\n", issue.number, issue.subject)
  137. }
  138. fmt.Println()
  139. }
  140. if len(other) > 0 {
  141. fmt.Println(underline("Unclassified", "-"))
  142. fmt.Println()
  143. for _, issue := range other {
  144. fmt.Printf("* #%d: %s\n", issue.number, issue.subject)
  145. }
  146. fmt.Println()
  147. }
  148. }
  149. func underline(s, c string) string {
  150. return fmt.Sprintf("%s\n%s", s, strings.Repeat(c, len(s)))
  151. }
  152. func runError(cmd string, args ...string) ([]byte, error) {
  153. ecmd := exec.Command(cmd, args...)
  154. bs, err := ecmd.CombinedOutput()
  155. if err != nil {
  156. return nil, err
  157. }
  158. return bytes.TrimSpace(bs), nil
  159. }
  160. func githubIssueTitleLabels(n int) (string, []string, error) {
  161. req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/syncthing/syncthing/issues/%d", n), nil)
  162. if err != nil {
  163. return "", nil, err
  164. }
  165. user, token := os.Getenv("GITHUB_USERNAME"), os.Getenv("GITHUB_TOKEN")
  166. if user != "" && token != "" {
  167. req.SetBasicAuth(user, token)
  168. }
  169. resp, err := http.DefaultClient.Do(req)
  170. if err != nil {
  171. return "", nil, err
  172. }
  173. defer resp.Body.Close()
  174. bs, err := ioutil.ReadAll(resp.Body)
  175. if err != nil {
  176. return "", nil, err
  177. }
  178. var res struct {
  179. Title string
  180. Labels []struct {
  181. Name string
  182. }
  183. PR struct {
  184. URL string
  185. } `json:"pull_request"`
  186. }
  187. err = json.Unmarshal(bs, &res)
  188. if err != nil {
  189. return "", nil, err
  190. }
  191. if res.PR.URL != "" {
  192. return "", nil, errors.New("pull request")
  193. }
  194. var labels []string
  195. for _, l := range res.Labels {
  196. labels = append(labels, l.Name)
  197. }
  198. return res.Title, labels, nil
  199. }
  200. func contains(s string, ss []string) bool {
  201. for _, x := range ss {
  202. if s == x {
  203. return true
  204. }
  205. }
  206. return false
  207. }