relnotes.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. addl, err := additionalNotes(*ver)
  39. if err != nil {
  40. log.Fatalln("Gathering additional notes:", err)
  41. }
  42. notes, err := generatedNotes(*ver, *branch, *prevVer)
  43. if err != nil {
  44. log.Fatalln("Gathering github notes:", err)
  45. }
  46. if addl != "" {
  47. fmt.Println(addl)
  48. }
  49. fmt.Println(notes)
  50. }
  51. // Load potential additional release notes from within the repo
  52. func additionalNotes(newVer string) (string, error) {
  53. ver, _, _ := strings.Cut(newVer, "-")
  54. bs, err := os.ReadFile(fmt.Sprintf("relnotes/%s.md", ver))
  55. if os.IsNotExist(err) {
  56. return "", nil
  57. }
  58. return string(bs), err
  59. }
  60. // Load generated release notes (list of pull requests and contributors)
  61. // from GitHub.
  62. func generatedNotes(newVer, targetCommit, prevVer string) (string, error) {
  63. fields := map[string]string{
  64. "tag_name": newVer,
  65. "target_commitish": targetCommit,
  66. "previous_tag_name": prevVer,
  67. }
  68. bs, err := json.Marshal(fields)
  69. if err != nil {
  70. return "", err
  71. }
  72. req, err := http.NewRequest(http.MethodPost, "https://api.github.com/repos/"+githubRepo+"/releases/generate-notes", bytes.NewReader(bs)) //nolint:noctx
  73. if err != nil {
  74. return "", err
  75. }
  76. req.Header.Set("Accept", "application/vnd.github+json")
  77. req.Header.Set("Authorization", "Bearer "+githubToken)
  78. req.Header.Set("X-Github-Api-Version", "2022-11-28")
  79. res, err := http.DefaultClient.Do(req)
  80. if err != nil {
  81. return "", err
  82. }
  83. if res.StatusCode != http.StatusOK {
  84. bs, _ := io.ReadAll(res.Body)
  85. log.Print(string(bs))
  86. return "", errors.New(res.Status) //nolint:err113
  87. }
  88. defer res.Body.Close()
  89. var resJSON struct {
  90. Body string
  91. }
  92. if err := json.NewDecoder(res.Body).Decode(&resJSON); err != nil {
  93. return "", err
  94. }
  95. return resJSON.Body, nil
  96. }