Changelog fixes
This commit is contained in:
@@ -60,9 +60,11 @@ type generator struct {
|
||||
}
|
||||
|
||||
type changelogEntry struct {
|
||||
Title string
|
||||
PullNumber string
|
||||
AuthorName string
|
||||
Title string
|
||||
ReferenceLabel string
|
||||
ReferenceURL string
|
||||
ReferenceSort string
|
||||
AuthorName string
|
||||
}
|
||||
|
||||
var pullNumberPattern = regexp.MustCompile(`\(#([0-9]+)\)`)
|
||||
@@ -278,7 +280,7 @@ func (g generator) renderCategory(category Category) (string, error) {
|
||||
}
|
||||
fmt.Fprintf(&buf, "### %s\n", section.Title)
|
||||
for _, entry := range entries {
|
||||
fmt.Fprintf(&buf, "- %s ([#%s](%s/pulls/%s)) - From %s\n", entry.Title, entry.PullNumber, g.repoURL, entry.PullNumber, entry.AuthorName)
|
||||
fmt.Fprintf(&buf, "- %s ([%s](%s)) - From %s\n", entry.Title, entry.ReferenceLabel, entry.ReferenceURL, entry.AuthorName)
|
||||
}
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
@@ -290,7 +292,7 @@ func (g generator) sectionEntries(section Section) ([]changelogEntry, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
args := []string{"log", "--pretty=format:%s%x1f%an"}
|
||||
args := []string{"log", "--first-parent", "--pretty=format:%H%x1f%s%x1f%an"}
|
||||
if g.previousTag != "" {
|
||||
args = append(args, fmt.Sprintf("%s..%s", g.previousTag, g.currentTag))
|
||||
} else {
|
||||
@@ -310,41 +312,57 @@ func (g generator) sectionEntries(section Section) ([]changelogEntry, error) {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, "\x1f", 2)
|
||||
subject := strings.TrimSpace(parts[0])
|
||||
fallbackAuthor := ""
|
||||
parts := strings.SplitN(line, "\x1f", 3)
|
||||
commitHash := strings.TrimSpace(parts[0])
|
||||
subject := ""
|
||||
if len(parts) > 1 {
|
||||
fallbackAuthor = strings.TrimSpace(parts[1])
|
||||
subject = strings.TrimSpace(parts[1])
|
||||
}
|
||||
fallbackAuthor := ""
|
||||
if len(parts) > 2 {
|
||||
fallbackAuthor = strings.TrimSpace(parts[2])
|
||||
}
|
||||
matches := pullNumberPattern.FindStringSubmatch(subject)
|
||||
if len(matches) != 2 {
|
||||
entryKey := commitHash
|
||||
if len(matches) == 2 {
|
||||
entryKey = "pr:" + matches[1]
|
||||
}
|
||||
if _, exists := seen[entryKey]; exists {
|
||||
continue
|
||||
}
|
||||
pullNumber := matches[1]
|
||||
if _, exists := seen[pullNumber]; exists {
|
||||
continue
|
||||
}
|
||||
seen[pullNumber] = struct{}{}
|
||||
seen[entryKey] = struct{}{}
|
||||
|
||||
pullInfo, err := g.pullDetails(pullNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authorName := strings.TrimSpace(pullInfo.AuthorName)
|
||||
if authorName == "" {
|
||||
authorName = fallbackAuthor
|
||||
}
|
||||
|
||||
entries = append(entries, changelogEntry{
|
||||
entry := changelogEntry{
|
||||
Title: sanitizeSubject(subject),
|
||||
PullNumber: pullNumber,
|
||||
AuthorName: authorName,
|
||||
})
|
||||
AuthorName: fallbackAuthor,
|
||||
}
|
||||
|
||||
if len(matches) == 2 {
|
||||
pullNumber := matches[1]
|
||||
pullInfo, err := g.pullDetails(pullNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authorName := strings.TrimSpace(pullInfo.AuthorName)
|
||||
if authorName == "" {
|
||||
authorName = fallbackAuthor
|
||||
}
|
||||
entry.ReferenceLabel = "#" + pullNumber
|
||||
entry.ReferenceURL = fmt.Sprintf("%s/pulls/%s", g.repoURL, pullNumber)
|
||||
entry.ReferenceSort = "pr:" + pullNumber
|
||||
entry.AuthorName = authorName
|
||||
} else {
|
||||
entry.ReferenceLabel = shortCommitHash(commitHash)
|
||||
entry.ReferenceURL = fmt.Sprintf("%s/commit/%s", g.repoURL, commitHash)
|
||||
entry.ReferenceSort = "commit:" + commitHash
|
||||
}
|
||||
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
if entries[i].Title == entries[j].Title {
|
||||
return entries[i].PullNumber < entries[j].PullNumber
|
||||
return entries[i].ReferenceSort < entries[j].ReferenceSort
|
||||
}
|
||||
return entries[i].Title < entries[j].Title
|
||||
})
|
||||
@@ -366,6 +384,14 @@ func sanitizeSubject(subject string) string {
|
||||
return title
|
||||
}
|
||||
|
||||
func shortCommitHash(commitHash string) string {
|
||||
commitHash = strings.TrimSpace(commitHash)
|
||||
if len(commitHash) <= 7 {
|
||||
return commitHash
|
||||
}
|
||||
return commitHash[:7]
|
||||
}
|
||||
|
||||
func (g generator) pullDetails(pullNumber string) (pullInfo, error) {
|
||||
if cached, ok := g.pullCache[pullNumber]; ok {
|
||||
return cached, nil
|
||||
|
||||
Reference in New Issue
Block a user