Skip to content

Commit fa2c917

Browse files
author
Dhia Ayachi
committed
extract type validation and reuse it in changelog-pr-body-check
Also: fix linter issues
1 parent de7037f commit fa2c917

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

cmd/changelog-gen/main.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"github.com/go-git/go-git/v5"
1111
"github.com/google/go-github/github"
12+
"github.com/hashicorp/go-changelog"
1213
"github.com/manifoldco/promptui"
1314
"os"
1415
"regexp"
@@ -31,12 +32,6 @@ type Note struct {
3132
URL string
3233
}
3334

34-
type GithubCred struct {
35-
owner string
36-
repo string
37-
token string
38-
}
39-
4035
func main() {
4136
pwd, err := os.Getwd()
4237
if err != nil {
@@ -63,19 +58,11 @@ func main() {
6358
os.Exit(1)
6459
}
6560
}
66-
typeValues := []string{"enhancement",
67-
"feature",
68-
"bug",
69-
"note",
70-
"new-resource",
71-
"new-datasource",
72-
"deprecation",
73-
"breaking-change",
74-
}
61+
7562
if Type == "" {
7663
prompt := promptui.Select{
7764
Label: "Select a change type",
78-
Items: typeValues,
65+
Items: changelog.TypeValues,
7966
}
8067

8168
_, Type, err = prompt.Run()
@@ -87,7 +74,7 @@ func main() {
8774
os.Exit(1)
8875
}
8976
} else {
90-
if !TypeValid(typeValues, Type) {
77+
if !changelog.TypeValid(Type) {
9178
fmt.Fprintln(os.Stderr, "Must specify a valid type")
9279
fmt.Fprintln(os.Stderr, "")
9380
flag.Usage()
@@ -139,15 +126,6 @@ func main() {
139126
}
140127
}
141128

142-
func TypeValid(elements []string, Type string) bool {
143-
for _, a := range elements {
144-
if a == Type {
145-
return true
146-
}
147-
}
148-
return false
149-
}
150-
151129
func OpenGit(path string) (*git.Repository, error) {
152130
r, err := git.PlainOpen(path)
153131
if err != nil {
@@ -190,7 +168,7 @@ func getPrNumberFromGithub(path string) (int, string, error) {
190168
}
191169
repoUrl := rem.Config().URLs[0]
192170

193-
reg := regexp.MustCompile(".*github\\.com:(.*)/(.*)\\.git")
171+
reg := regexp.MustCompile(`.*github\\.com:(.*)/(.*)\\.git`)
194172
m := reg.FindAllStringSubmatch(repoUrl, -1)
195173
if len(m) > 1 {
196174
if len(m[0]) < 2 {

cmd/changelog-pr-body-check/main.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,7 @@ func main() {
7474
}
7575
var unknownTypes []string
7676
for _, note := range notes {
77-
switch note.Type {
78-
case "none",
79-
"bug",
80-
"note",
81-
"enhancement",
82-
"new-resource",
83-
"new-datasource",
84-
"deprecation",
85-
"breaking-change",
86-
"feature":
87-
default:
77+
if !changelog.TypeValid(note.Type) {
8878
unknownTypes = append(unknownTypes, note.Type)
8979
}
9080
}

entry.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import (
1111
"github.com/go-git/go-git/v5/storage/memory"
1212
)
1313

14+
var TypeValues = []string{"enhancement",
15+
"feature",
16+
"bug",
17+
"note",
18+
"new-resource",
19+
"new-datasource",
20+
"deprecation",
21+
"breaking-change",
22+
}
23+
1424
type Entry struct {
1525
Issue string
1626
Body string
@@ -42,6 +52,9 @@ func Diff(repo, ref1, ref2, dir string) ([]Entry, error) {
4252
Hash: *rev2,
4353
Force: true,
4454
})
55+
if err != nil {
56+
return nil, err
57+
}
4558
entriesAfterFI, err := wt.Filesystem.ReadDir(dir)
4659
if err != nil {
4760
return nil, err
@@ -64,6 +77,9 @@ func Diff(repo, ref1, ref2, dir string) ([]Entry, error) {
6477
Hash: *rev1,
6578
Force: true,
6679
})
80+
if err != nil {
81+
return nil, err
82+
}
6783
entriesBeforeFI, err := wt.Filesystem.ReadDir(dir)
6884
if err != nil {
6985
return nil, err
@@ -84,3 +100,12 @@ func Diff(repo, ref1, ref2, dir string) ([]Entry, error) {
84100
})
85101
return entries, nil
86102
}
103+
104+
func TypeValid(Type string) bool {
105+
for _, a := range TypeValues {
106+
if a == Type {
107+
return true
108+
}
109+
}
110+
return false
111+
}

0 commit comments

Comments
 (0)