Skip to content

Commit

Permalink
PR bool and URL, some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jimschubert committed Mar 14, 2020
1 parent ac42903 commit 7fa9156
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 73 deletions.
33 changes: 27 additions & 6 deletions changelog.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package changelog

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"sync"
"text/template"
"time"
Expand All @@ -23,7 +26,7 @@ const defaultTemplate = `
## {{.Version}}
{{range .Items -}}
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ([{{.Author}}]({{.AuthorURL}}))
* [{{.CommitHashShort}}]({{.CommitURL}}) {{.Title}} ({{if .IsPull}}[contributed]({{.PullURL}}) by {{end}}[{{.Author}}]({{.AuthorURL}}))
{{end}}
<em>For more details, see <a href="{{.CompareURL}}">{{.PreviousVersion}}..{{.Version}}</a></em>
Expand Down Expand Up @@ -123,6 +126,26 @@ func wait(ch chan struct{}, wg *sync.WaitGroup) {
ch <- struct{}{}
}

func (c *Changelog) applyPullPropertiesChangeItem(ci *model.ChangeItem) {
re := regexp.MustCompile(`.+?\(#(\d+)\)$`)
title := ci.Title()
match := re.FindStringSubmatch(title)
if match != nil && len(match) > 0 {
isPull := true
ci.IsPullRaw = &isPull
baseUrl := ci.CommitURL()
idx := strings.LastIndex(baseUrl, "commit")
if idx > 0 {
var buffer bytes.Buffer
buffer.WriteString(baseUrl[0:idx])
buffer.WriteString("pull/")
buffer.WriteString(match[1])
result := buffer.String()
ci.PullURLRaw = &result
}
}
}

func (c *Changelog) writeChangelog(all []model.ChangeItem, comparison *github.CommitsComparison, writer io.Writer) error {
compareURL := comparison.GetHTMLURL()
diffURL := comparison.GetDiffURL()
Expand Down Expand Up @@ -171,20 +194,18 @@ func (c *Changelog) convertToChangeItem(commit *github.RepositoryCommit, ch chan
t = (*(*commit.GetCommit()).GetAuthor()).Date
}

// TODO: Max count?
// TODO: Groupings
// TODO: Pull URL/Boolean
// TODO: Excludes
ci := &model.ChangeItem{
AuthorRaw: commit.Author.Login,
AuthorURLRaw: commit.Author.URL,
AuthorURLRaw: commit.Author.HTMLURL,
CommitMessageRaw: commit.Commit.Message,
DateRaw: t,
IsPullRaw: nil,
PullURLRaw: nil,
CommitHashRaw: commit.SHA,
CommitURLRaw: commit.HTMLURL,
GroupRaw: nil,
}
c.applyPullPropertiesChangeItem(ci)

ch <- ci
}
Expand Down
22 changes: 11 additions & 11 deletions model/ChangeItem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func pTime(t time.Time) *time.Time {

func TestChangeItem_getAuthor(t *testing.T) {
type fields struct {
Author *string
Author *string
}
tests := []struct {
name string
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestChangeItem_getAuthorURL(t *testing.T) {

func TestChangeItem_getCommit(t *testing.T) {
type fields struct {
Commit *string
Commit *string
}
tests := []struct {
name string
Expand All @@ -103,7 +103,7 @@ func TestChangeItem_getCommit(t *testing.T) {
func TestChangeItem_getDate(t *testing.T) {
d := time.Date(2020, time.February, 20, 10, 10, 2, 20, &time.Location{})
type fields struct {
Date *time.Time
Date *time.Time
}
tests := []struct {
name string
Expand All @@ -129,7 +129,7 @@ func TestChangeItem_getIsPull(t *testing.T) {
f := false
tt := true
type fields struct {
IsPull *bool
IsPull *bool
}
tests := []struct {
name string
Expand All @@ -156,7 +156,7 @@ func TestChangeItem_getPullID(t *testing.T) {
pull := "https://github.com/OpenAPITools/openapi-generator/pull/5472"
empty := ""
type fields struct {
PullURL *string
PullURL *string
}
tests := []struct {
name string
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestChangeItem_getPullID(t *testing.T) {
func TestChangeItem_getPullURL(t *testing.T) {
pull := "https://github.com/OpenAPITools/openapi-generator/pull/5472"
type fields struct {
PullURL *string
PullURL *string
}
tests := []struct {
name string
Expand All @@ -212,7 +212,7 @@ func TestChangeItem_getPullURL(t *testing.T) {

func TestChangeItem_getGroup(t *testing.T) {
type fields struct {
Group *string
Group *string
}
tests := []struct {
name string
Expand Down Expand Up @@ -260,15 +260,15 @@ func TestChangeItem_getCommitHashShort(t *testing.T) {

func TestChangeItem_CommitURL(t *testing.T) {
type fields struct {
CommitURLRaw *string
CommitURLRaw *string
}
tests := []struct {
name string
fields fields
want string
}{
{ "empty CommitURL", fields{CommitURLRaw: nil}, ""},
{ "populated CommitURL", fields{CommitURLRaw: p("https://github.com/jimschubert/changelog/commit/6db2267d8fbc2929655884825a76856bc3244acd")}, "https://github.com/jimschubert/changelog/commit/6db2267d8fbc2929655884825a76856bc3244acd"},
{"empty CommitURL", fields{CommitURLRaw: nil}, ""},
{"populated CommitURL", fields{CommitURLRaw: p("https://github.com/jimschubert/changelog/commit/6db2267d8fbc2929655884825a76856bc3244acd")}, "https://github.com/jimschubert/changelog/commit/6db2267d8fbc2929655884825a76856bc3244acd"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -352,4 +352,4 @@ func TestChangeItem_GoString(t *testing.T) {
}
})
}
}
}
7 changes: 1 addition & 6 deletions model/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,12 @@ func (c *Config) String() string {
}
buffer.WriteString(" Sort: ")
if c.SortDirection != nil {
buffer.WriteString(c.SortDirection.String())
buffer.WriteString((*c.SortDirection).String())
}
buffer.WriteString(" }")
return fmt.Sprintf(buffer.String())
}

func (c *Config) withDefaults() {
commits := Commits
c.ResolveType = &commits
}

// LoadOrNewConfig will attempt to load path, otherwise returns a newly constructed config.
func LoadOrNewConfig(path *string, owner string, repo string) *Config {
defaultResolveType := Commits
Expand Down
92 changes: 47 additions & 45 deletions model/Config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ func createTempConfig(t *testing.T, data string) (fileLocation string, cleanup f
return filePath, func() { _ = os.RemoveAll(filePath) }
}

func ptrResolveType(t ResolveType) *ResolveType {
return &t
}

func ptrStringArray(s ...string) *[]string {
arr := make([]string, 0)
if len(s) > 0 {
Expand All @@ -61,22 +57,23 @@ func ptrStringArray(s ...string) *[]string {

func TestConfig_Load(t *testing.T) {
type fields struct {
JSONData string
ResolveType *ResolveType
Owner string
Repo string
Groupings *[]string
Exclude *[]string
Enterprise *string
Template *string
JSONData string
ResolveType *ResolveType
Owner string
Repo string
Groupings *[]string
Exclude *[]string
Enterprise *string
Template *string
SortDirection *SortDirection
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{"Loads valid empty json", fields{JSONData: "{}"}, false},
{"Loads valid json resolve-only", fields{JSONData: `{"resolve": "commits"}`, ResolveType: ptrResolveType(Commits)}, false},
{"Loads valid json resolve-only", fields{JSONData: `{"resolve": "commits"}`, ResolveType: Commits.Ptr()}, false},
{"Fail on valid json with invalid data type resolve-only", fields{JSONData: `{"resolve": 1.0}`}, true}, // note that 1 would resolve since enum is an int
{"Loads valid json owner-only", fields{JSONData: `{"owner": "jimschubert"}`, Owner: "jimschubert"}, false},
{"Fail on valid json with invalid data type owner-only", fields{JSONData: `{"owner": []}`}, true},
Expand All @@ -89,17 +86,20 @@ func TestConfig_Load(t *testing.T) {
{"Loads valid json enterprise-only", fields{JSONData: `{"enterprise": "https://ghe.example.com"}`, Enterprise: p("https://ghe.example.com")}, false},
{"Fail on valid json with invalid data type enterprise-only", fields{JSONData: `{"enterprise": 0}`}, true},
{"Loads valid json template-only", fields{JSONData: `{"template": "/path/to/template"}`, Template: p("/path/to/template")}, false},
{"Loads valid json ascending sort-only", fields{JSONData: `{"sort": "asc"}`, SortDirection: Ascending.Ptr()}, false},
{"Loads valid json descending sort-only", fields{JSONData: `{"sort": "desc"}`, SortDirection: Descending.Ptr()}, false},
{"Fail on valid json with invalid data type template-only", fields{JSONData: `{"template": []}`}, true},
{"Loads valid full json",
fields{
JSONData: `{"resolve":"commits","owner":"jimschubert","repo":"ossify","groupings":["feature","bug"],"exclude":["wip","help wanted"],"enterprise":"https://ghe.example.com","template":"/path/to/template"}`,
ResolveType: ptrResolveType(Commits),
Owner: "jimschubert",
Repo: "ossify",
Groupings: ptrStringArray("feature", "bug"),
Exclude: ptrStringArray("wip", "help wanted"),
Enterprise: p("https://ghe.example.com"),
Template: p("/path/to/template"),
JSONData: `{"resolve":"commits","owner":"jimschubert","repo":"ossify","groupings":["feature","bug"],"exclude":["wip","help wanted"],"enterprise":"https://ghe.example.com","template":"/path/to/template","sort":"asc"}`,
ResolveType: Commits.Ptr(),
Owner: "jimschubert",
Repo: "ossify",
Groupings: ptrStringArray("feature", "bug"),
Exclude: ptrStringArray("wip", "help wanted"),
Enterprise: p("https://ghe.example.com"),
Template: p("/path/to/template"),
SortDirection: Ascending.Ptr(),
}, false},
{"Fails on invalid json",
fields{
Expand Down Expand Up @@ -130,13 +130,14 @@ func TestConfig_Load(t *testing.T) {

func TestConfig_String(t *testing.T) {
type fields struct {
ResolveType *ResolveType
Owner string
Repo string
Groupings *[]string
Exclude *[]string
Enterprise *string
Template *string
ResolveType *ResolveType
Owner string
Repo string
Groupings *[]string
Exclude *[]string
Enterprise *string
Template *string
SortDirection *SortDirection
}
tests := []struct {
name string
Expand All @@ -145,30 +146,31 @@ func TestConfig_String(t *testing.T) {
}{
{"outputs string",
fields{
ResolveType: ptrResolveType(Commits),
Owner: "jimschubert",
Repo: "ossify",
Groupings: ptrStringArray("feature", "bug"),
Exclude: ptrStringArray("wip", "help wanted"),
Enterprise: p("https://ghe.example.com"),
Template: p("/path/to/template"),
}, `Config: { ResolveType: commits Owner: jimschubert Repo: ossify Groupings: &[feature bug] Exclude: &[wip help wanted] Enterprise: https://ghe.example.com Template: /path/to/template Sort: }`},
ResolveType: Commits.Ptr(),
Owner: "jimschubert",
Repo: "ossify",
Groupings: ptrStringArray("feature", "bug"),
Exclude: ptrStringArray("wip", "help wanted"),
Enterprise: p("https://ghe.example.com"),
Template: p("/path/to/template"),
SortDirection: Ascending.Ptr(),
}, `Config: { ResolveType: commits Owner: jimschubert Repo: ossify Groupings: &[feature bug] Exclude: &[wip help wanted] Enterprise: https://ghe.example.com Template: /path/to/template Sort: asc }`},

{"outputs string for nil properties",
fields{
}, `Config: { ResolveType: <nil> Owner: Repo: Groupings: <nil> Exclude: <nil> Enterprise: Template: Sort: }`},
fields{}, `Config: { ResolveType: <nil> Owner: Repo: Groupings: <nil> Exclude: <nil> Enterprise: Template: Sort: }`},
}
// Config: {ResolveType: commits Owner: jimschubert Repo: ossify Groupings: &[feature bug] Exclude: &[wip help wanted] Enterprise: 0xc00003c5b0 Template: 0xc00003c5c0}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{
ResolveType: tt.fields.ResolveType,
Owner: tt.fields.Owner,
Repo: tt.fields.Repo,
Groupings: tt.fields.Groupings,
Exclude: tt.fields.Exclude,
Enterprise: tt.fields.Enterprise,
Template: tt.fields.Template,
ResolveType: tt.fields.ResolveType,
Owner: tt.fields.Owner,
Repo: tt.fields.Repo,
Groupings: tt.fields.Groupings,
Exclude: tt.fields.Exclude,
Enterprise: tt.fields.Enterprise,
Template: tt.fields.Template,
SortDirection: tt.fields.SortDirection,
}
if got := c.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
Expand Down
6 changes: 5 additions & 1 deletion model/ResolveType.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ResolveType uint8

const (
// Commits only
Commits ResolveType = 1 << iota
Commits ResolveType = 1 << iota
// PullRequests requests that we pull PR information if available
PullRequests ResolveType = 1 << iota
)
Expand Down Expand Up @@ -74,3 +74,7 @@ func (r ResolveType) String() string {
return "commits"
}
}

func (r ResolveType) Ptr() *ResolveType {
return &r
}
2 changes: 1 addition & 1 deletion model/ResolveType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ func TestResolveType_MarshalJSON1(t *testing.T) {
}
})
}
}
}
12 changes: 9 additions & 3 deletions model/SortDirection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const (

// MarshalJSON converts SortDirection into a string representation sufficient for JSON
func (s *SortDirection) MarshalJSON() ([]byte, error) {
if s == nil { return []byte(""), nil }
if s == nil {
return []byte(""), nil
}
it := *s
switch it {
case Ascending:
Expand Down Expand Up @@ -64,6 +66,10 @@ func (s SortDirection) String() string {
case Descending:
fallthrough
default:
return "decs"
return "desc"
}
}
}

func (s SortDirection) Ptr() *SortDirection {
return &s
}
Loading

0 comments on commit 7fa9156

Please sign in to comment.