relnotes.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2025 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. //go:build ignore
  7. // +build ignore
  8. package main
  9. import (
  10. "bytes"
  11. "cmp"
  12. "encoding/json"
  13. "errors"
  14. "flag"
  15. "fmt"
  16. "io"
  17. "log"
  18. "net/http"
  19. "os"
  20. "strings"
  21. )
  22. var (
  23. githubToken = os.Getenv("GITHUB_TOKEN")
  24. githubRepo = cmp.Or(os.Getenv("GITHUB_REPOSITORY"), "syncthing/syncthing")
  25. )
  26. func main() {
  27. ver := flag.String("new-ver", "", "New version tag")
  28. prevVer := flag.String("prev-ver", "", "Previous version tag")
  29. branch := flag.String("branch", "HEAD", "Branch to release from")
  30. flag.Parse()
  31. log.SetOutput(os.Stderr)
  32. if *ver == "" {
  33. log.Fatalln("Must set --new-ver")
  34. }
  35. if githubToken == "" {
  36. log.Fatalln("Must set $GITHUB_TOKEN")
  37. }
  38. notes, err := additionalNotes(*ver)
  39. if err != nil {
  40. log.Fatalln("Gathering additional notes:", err)
  41. }
  42. gh, err := generatedNotes(*ver, *branch, *prevVer)
  43. if err != nil {
  44. log.Fatalln("Gathering github notes:", err)
  45. }
  46. notes = append(notes, gh)
  47. fmt.Println(strings.Join(notes, "\n\n"))
  48. }
  49. // Load potential additional release notes from within the repo
  50. func additionalNotes(newVer string) ([]string, error) {
  51. var notes []string
  52. ver, _, _ := strings.Cut(newVer, "-")
  53. for {
  54. file := fmt.Sprintf("relnotes/%s.md", ver)
  55. if bs, err := os.ReadFile(file); err == nil {
  56. notes = append(notes, strings.TrimSpace(string(bs)))
  57. } else if !os.IsNotExist(err) {
  58. return nil, err
  59. }
  60. if idx := strings.LastIndex(ver, "."); idx > 0 {
  61. ver = ver[:idx]
  62. } else {
  63. break
  64. }
  65. }
  66. return notes, nil
  67. }
  68. // Load generated release notes (list of pull requests and contributors)
  69. // from GitHub.
  70. func generatedNotes(newVer, targetCommit, prevVer string) (string, error) {
  71. fields := map[string]string{
  72. "tag_name": newVer,
  73. "target_commitish": targetCommit,
  74. "previous_tag_name": prevVer,
  75. }
  76. bs, err := json.Marshal(fields)
  77. if err != nil {
  78. return "", err
  79. }
  80. req, err := http.NewRequest(http.MethodPost, "https://api.github.com/repos/"+githubRepo+"/releases/generate-notes", bytes.NewReader(bs)) //nolint:noctx
  81. if err != nil {
  82. return "", err
  83. }
  84. req.Header.Set("Accept", "application/vnd.github+json")
  85. req.Header.Set("Authorization", "Bearer "+githubToken)
  86. req.Header.Set("X-Github-Api-Version", "2022-11-28")
  87. res, err := http.DefaultClient.Do(req)
  88. if err != nil {
  89. return "", err
  90. }
  91. if res.StatusCode != http.StatusOK {
  92. bs, _ := io.ReadAll(res.Body)
  93. log.Print(string(bs))
  94. return "", errors.New(res.Status) //nolint:err113
  95. }
  96. defer res.Body.Close()
  97. var resJSON struct {
  98. Body string
  99. }
  100. if err := json.NewDecoder(res.Body).Decode(&resJSON); err != nil {
  101. return "", err
  102. }
  103. return strings.TrimSpace(resJSON.Body), nil
  104. }