changelog.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "flag"
  12. "fmt"
  13. "io/ioutil"
  14. "log"
  15. "net/http"
  16. "os"
  17. "os/exec"
  18. "regexp"
  19. "strconv"
  20. "strings"
  21. "github.com/mitchellh/go-wordwrap"
  22. )
  23. var (
  24. subjectIssues = regexp.MustCompile(`^([^(]+)\s+\((?:fixes|ref) ([^)]+)\)(?:[^\w])?$`)
  25. issueNumbers = regexp.MustCompile(`(#\d+)`)
  26. )
  27. func main() {
  28. flag.Parse()
  29. fmt.Printf("Resolved issues:\n\n")
  30. // Display changelog since the version given on the command line, or
  31. // figure out the last release if there were no arguments.
  32. var prevRel string
  33. if flag.NArg() > 0 {
  34. prevRel = flag.Arg(0)
  35. } else {
  36. bs, err := runError("git", "describe", "--abbrev=0", "HEAD^")
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. prevRel = string(bs)
  41. }
  42. // Get the git log with subject and author nickname
  43. bs, err := runError("git", "log", "--reverse", "--pretty=format:%s|%aN|%cN", prevRel+"..")
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. // Split into lines
  48. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  49. // Split into subject and author
  50. fields := bytes.Split(line, []byte{'|'})
  51. subj := fields[0]
  52. author := fields[1]
  53. committer := fields[2]
  54. // Check if subject contains a "(fixes ...)" or "(ref ...)""
  55. if m := subjectIssues.FindSubmatch(subj); len(m) > 0 {
  56. subj := m[1]
  57. issues := issueNumbers.FindAll(m[2], -1)
  58. for _, issue := range issues {
  59. n, err := strconv.Atoi(string(issue[1:]))
  60. if err != nil {
  61. continue
  62. }
  63. title, err := githubIssueTitle(n)
  64. if err != nil {
  65. fmt.Println(err)
  66. continue
  67. }
  68. // Format a changelog entry
  69. reviewed := ""
  70. if !bytes.Equal(committer, author) {
  71. reviewed = fmt.Sprintf(", reviewed by @%s", committer)
  72. }
  73. message := fmt.Sprintf("%s: %s\n\n%s (by @%s%s)\n", issue, title, subj, author, reviewed)
  74. para := wordwrap.WrapString(message, 74)
  75. for i, line := range strings.Split(para, "\n") {
  76. if i == 0 {
  77. fmt.Println("*", line)
  78. } else {
  79. fmt.Println(" ", line)
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. func runError(cmd string, args ...string) ([]byte, error) {
  87. ecmd := exec.Command(cmd, args...)
  88. bs, err := ecmd.CombinedOutput()
  89. if err != nil {
  90. return nil, err
  91. }
  92. return bytes.TrimSpace(bs), nil
  93. }
  94. func githubIssueTitle(n int) (string, error) {
  95. req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/syncthing/syncthing/issues/%d", n), nil)
  96. user, token := os.Getenv("GITHUB_USERNAME"), os.Getenv("GITHUB_TOKEN")
  97. if user != "" && token != "" {
  98. req.SetBasicAuth(user, token)
  99. }
  100. resp, err := http.DefaultClient.Do(req)
  101. if err != nil {
  102. return "", err
  103. }
  104. defer resp.Body.Close()
  105. bs, err := ioutil.ReadAll(resp.Body)
  106. if err != nil {
  107. return "", err
  108. }
  109. var res struct {
  110. Title string
  111. }
  112. err = json.Unmarshal(bs, &res)
  113. if err != nil {
  114. return "", err
  115. }
  116. return res.Title, nil
  117. }