forked from syncthing/syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelnotes.go
More file actions
136 lines (120 loc) · 3.23 KB
/
relnotes.go
File metadata and controls
136 lines (120 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2025 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
//go:build ignore
// +build ignore
package main
import (
"bytes"
"cmp"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
"text/template"
)
var (
githubToken = os.Getenv("GITHUB_TOKEN")
githubRepo = cmp.Or(os.Getenv("GITHUB_REPOSITORY"), "syncthing/syncthing")
)
func main() {
ver := flag.String("new-ver", "", "New version tag")
prevVer := flag.String("prev-ver", "", "Previous version tag")
branch := flag.String("branch", "HEAD", "Branch to release from")
flag.Parse()
log.SetOutput(os.Stderr)
if *ver == "" {
log.Fatalln("Must set --new-ver")
}
if githubToken == "" {
log.Fatalln("Must set $GITHUB_TOKEN")
}
notes, err := additionalNotes(*ver)
if err != nil {
log.Fatalln("Gathering additional notes:", err)
}
gh, err := generatedNotes(*ver, *branch, *prevVer)
if err != nil {
log.Fatalln("Gathering github notes:", err)
}
notes = append(notes, gh)
fmt.Println(strings.Join(notes, "\n\n"))
}
// Load potential additional release notes from within the repo
func additionalNotes(newVer string) ([]string, error) {
data := map[string]string{
"version": strings.TrimLeft(newVer, "v"),
}
var notes []string
ver, _, _ := strings.Cut(newVer, "-")
for {
file := fmt.Sprintf("relnotes/%s.md", ver)
if bs, err := os.ReadFile(file); err == nil {
tpl, err := template.New("notes").Parse(string(bs))
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, data); err != nil {
return nil, err
}
notes = append(notes, strings.TrimSpace(buf.String()))
} else if !os.IsNotExist(err) {
return nil, err
}
if idx := strings.LastIndex(ver, "."); idx > 0 {
ver = ver[:idx]
} else {
break
}
}
return notes, nil
}
// Load generated release notes (list of pull requests and contributors)
// from GitHub.
func generatedNotes(newVer, targetCommit, prevVer string) (string, error) {
fields := map[string]string{
"tag_name": newVer,
"target_commitish": targetCommit,
"previous_tag_name": prevVer,
}
bs, err := json.Marshal(fields)
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, "https://api.github.com/repos/"+githubRepo+"/releases/generate-notes", bytes.NewReader(bs)) //nolint:noctx
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", "Bearer "+githubToken)
req.Header.Set("X-Github-Api-Version", "2022-11-28")
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
bs, _ := io.ReadAll(res.Body)
log.Print(string(bs))
return "", errors.New(res.Status) //nolint:err113
}
defer res.Body.Close()
var resJSON struct {
Body string
}
if err := json.NewDecoder(res.Body).Decode(&resJSON); err != nil {
return "", err
}
return strings.TrimSpace(removeHTMLComments(resJSON.Body)), nil
}
func removeHTMLComments(s string) string {
return regexp.MustCompile(`<!--.*?-->`).ReplaceAllString(s, "")
}