Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add changelog-check command #22

Merged
merged 5 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions cmd/changelog-check/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"io"
"log"
"os"

"github.com/hashicorp/go-changelog"
)

func main() {
var err error

// default to reading from stdin
r := os.Stdin

// read from file arg instead if provided
// TODO: add --help text for [file] arg handling
filepath := ""
if len(os.Args) > 1 {
filepath = os.Args[1]
r, err = os.Open(filepath)
if err != nil {
log.Fatalf("error opening %s", filepath)
os.Exit(1)
}
}

b, err := io.ReadAll(r)
if err != nil {
if filepath != "" {
log.Fatalf("error reading from %s", filepath)
} else {
log.Fatalf("error reading from stdin")
}
os.Exit(1)
}

entry := changelog.Entry{
Body: string(b),
}

if err := entry.Validate(); err != nil {
log.Fatalf(err.Error())
os.Exit(1)
}
}
97 changes: 47 additions & 50 deletions cmd/changelog-pr-body-check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,57 @@ func main() {
log.Fatalf("Error retrieving pull request github.com/"+
"%s/%s/%d: %s", owner, repo, prNo, err)
}

entry := changelog.Entry{
Issue: pr,
Body: pullRequest.GetBody(),
}
notes := changelog.NotesFromEntry(entry)
if len(notes) < 1 {
log.Printf("no changelog entry found in %s: %s", entry.Issue,
string(entry.Body))
body := "Oops! It looks like no changelog entry is attached to" +
" this PR. Please include a release note block" +
" in the PR body, as described in https://github.com/GoogleCloudPlatform/magic-modules/blob/master/.ci/RELEASE_NOTES_GUIDE.md:" +
"\n\n~~~\n```release-note:TYPE\nRelease note" +
"\n```\n~~~"
_, _, err = client.Issues.CreateComment(ctx, owner, repo,
prNo, &github.IssueComment{
Body: &body,
})
if err != nil {
log.Fatalf("Error creating pull request comment on"+
" github.com/%s/%s/%d: %s", owner, repo, prNo,
err)
}
os.Exit(1)
}
var unknownTypes []string
for _, note := range notes {
if !changelog.TypeValid(note.Type) {
unknownTypes = append(unknownTypes, note.Type)
}
}
if len(unknownTypes) > 0 {
log.Printf("unknown changelog types %v", unknownTypes)
body := "Oops! It looks like you're using"
if len(unknownTypes) == 1 {
body += " an"
}
body += " unknown release-note type"
if len(unknownTypes) > 1 {
body += "s"
}
body += " in your changelog entries:"
for _, t := range unknownTypes {
body += "\n* " + t
}
body += "\n\nPlease only use the types listed in https://github.com/GoogleCloudPlatform/magic-modules/blob/master/.ci/RELEASE_NOTES_GUIDE.md."
_, _, err = client.Issues.CreateComment(ctx, owner, repo,
prNo, &github.IssueComment{
Body: &body,
})
if err != nil {
log.Fatalf("Error creating pull request comment on"+
" github.com/%s/%s/%d: %s", owner, repo, prNo,
err)

if err := entry.Validate(); err != nil {
log.Printf("error parsing changelog entry in %s: %s", entry.Issue, err)
switch err.Code {
case changelog.EntryErrorNotFound:
body := "Oops! It looks like no changelog entry is attached to" +
" this PR. Please include a release note block" +
" in the PR body, as described in https://github.com/GoogleCloudPlatform/magic-modules/blob/master/.ci/RELEASE_NOTES_GUIDE.md:" +
"\n\n~~~\n```release-note:TYPE\nRelease note" +
"\n```\n~~~"
_, _, err := client.Issues.CreateComment(ctx, owner, repo,
prNo, &github.IssueComment{
Body: &body,
})
if err != nil {
log.Fatalf("Error creating pull request comment on"+
" github.com/%s/%s/%d: %s", owner, repo, prNo,
err)
}
os.Exit(1)
case changelog.EntryErrorUnknownTypes:
unknownTypes := err.Details["unknownTypes"].([]string)

body := "Oops! It looks like you're using"
if len(unknownTypes) == 1 {
body += " an"
}
body += " unknown release-note type"
if len(unknownTypes) > 1 {
body += "s"
}
body += " in your changelog entries:"
for _, t := range unknownTypes {
body += "\n* " + t
}
body += "\n\nPlease only use the types listed in https://github.com/GoogleCloudPlatform/magic-modules/blob/master/.ci/RELEASE_NOTES_GUIDE.md."
_, _, err := client.Issues.CreateComment(ctx, owner, repo,
prNo, &github.IssueComment{
Body: &body,
})
if err != nil {
log.Fatalf("Error creating pull request comment on"+
" github.com/%s/%s/%d: %s", owner, repo, prNo,
err)
}
os.Exit(1)
}
os.Exit(1)
}
}
50 changes: 49 additions & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@ type EntryList struct {
es []*Entry
}

type EntryErrorCode string

const (
EntryErrorNotFound EntryErrorCode = "NOT_FOUND"
EntryErrorUnknownTypes EntryErrorCode = "UNKNOWN_TYPES"
)

type EntryValidationError struct {
message string
Code EntryErrorCode
Details map[string]interface{}
}

func (e *EntryValidationError) Error() string {
return e.message
}

// Validates that an Entry body contains properly formatted changelog notes
func (e *Entry) Validate() *EntryValidationError {
notes := NotesFromEntry(*e)

if len(notes) < 1 {
return &EntryValidationError{
message: fmt.Sprintf("no changelog entry found in: %s", string(e.Body)),
Code: EntryErrorNotFound,
}
}

var unknownTypes []string
for _, note := range notes {
if !TypeValid(note.Type) {
unknownTypes = append(unknownTypes, note.Type)
}
}

if len(unknownTypes) > 0 {
return &EntryValidationError{
message: fmt.Sprintf("unknown changelog types %v: please use only the configured changelog entry types: %v", unknownTypes, TypeValues),
Code: EntryErrorUnknownTypes,
Details: map[string]interface{}{
"unknownTypes": unknownTypes,
},
}
}

return nil
}

// NewEntryList returns an EntryList with capacity c
func NewEntryList(c int) *EntryList {
return &EntryList{
Expand Down Expand Up @@ -139,7 +187,7 @@ func Diff(repo, ref1, ref2, dir string) (*EntryList, error) {
if err := wt.Checkout(&git.CheckoutOptions{
Hash: *rev2,
Force: true,
}); err != nil {
}); err != nil {
return nil, fmt.Errorf("could not checkout repository at %s: %w", ref2, err)
}
entriesAfterFI, err := wt.Filesystem.ReadDir(dir)
Expand Down