Skip to content

Commit

Permalink
chore: Adjusts check-changelog-entry-file to verify the entry types o…
Browse files Browse the repository at this point in the history
…ur guidelines define
  • Loading branch information
AgustinBettati committed Apr 19, 2024
1 parent 91a38e6 commit 4d30815
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions tools/check-changelog-entry-file/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"log"
Expand All @@ -11,8 +12,9 @@ import (
)

var (
skipLabelName = "skip-changelog-check"
skipTitles = []string{"chore", "test", "doc", "ci"} // Dependabot uses chore.
skipLabelName = "skip-changelog-check"
skipTitles = []string{"chore", "test", "doc", "ci"} // Dependabot uses chore.
allowedTypeValues = getValidTypes("scripts/changelog/allowed-types.txt")
)

func main() {
Expand Down Expand Up @@ -53,12 +55,34 @@ func validateChangelog(filePath, body string) {
entry := changelog.Entry{
Body: body,
}
if err := entry.Validate(); err != nil {
log.Fatalf("Error validating changelog file: %s, err: %v", filePath, err)
notes := changelog.NotesFromEntry(entry)

if len(notes) < 1 {
log.Fatalf("Error validating changelog file: %s, no changelog entry found", filePath)
}

var unknownTypes []string
for _, note := range notes {
if !isValidType(note.Type) {
unknownTypes = append(unknownTypes, note.Type)
}
}
if len(unknownTypes) > 0 {
log.Fatalf("Error validating changelog file: %s. Unknown changelog types %v, please use only the configured changelog entry types %v", filePath, unknownTypes, allowedTypeValues)
}

fmt.Printf("Changelog entry file is valid: %s\n", filePath)
}

func isValidType(t string) bool {
for _, a := range allowedTypeValues {
if a == t {
return true
}
}
return false
}

func skipTitle(title string) bool {
for _, item := range skipTitles {
if strings.HasPrefix(title, item+":") {
Expand All @@ -76,3 +100,21 @@ func skipLabel(labels []string) bool {
}
return false
}

func getValidTypes(path string) []string {
file, err := os.Open(path)
if err != nil {
log.Fatalf("Error getting allowed entry types from %s", path)
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" {
lines = append(lines, line)
}
}
return lines
}

0 comments on commit 4d30815

Please sign in to comment.