From 80166775678319507b43364f7c548ab76885636f Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Thu, 12 Nov 2020 14:41:35 -0500 Subject: [PATCH] move title cleanup to its own testable function Signed-off-by: Doug Hellmann --- tools/enhancements/title.go | 12 ++++++++++ tools/enhancements/title_test.go | 41 ++++++++++++++++++++++++++++++++ tools/go.mod | 1 + tools/go.sum | 9 +++++++ tools/report/main.go | 7 +----- 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tools/enhancements/title.go create mode 100644 tools/enhancements/title_test.go diff --git a/tools/enhancements/title.go b/tools/enhancements/title.go new file mode 100644 index 0000000000..a529fd6382 --- /dev/null +++ b/tools/enhancements/title.go @@ -0,0 +1,12 @@ +package enhancements + +import "strings" + +func CleanTitle(title string) string { + // Sometimes we have a superfluous "enhancement:" prefix in + // the PR title + if strings.HasPrefix(strings.ToLower(title), "enhancement:") { + title = strings.TrimLeft(title[12:], " ") + } + return title +} diff --git a/tools/enhancements/title_test.go b/tools/enhancements/title_test.go new file mode 100644 index 0000000000..3316665756 --- /dev/null +++ b/tools/enhancements/title_test.go @@ -0,0 +1,41 @@ +package enhancements + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCleanTitle(t *testing.T) { + for _, tc := range []struct { + Scenario string + Input string + Expected string + }{ + { + Scenario: "empty-string", + Input: "", + Expected: "", + }, + { + Scenario: "no-changes", + Input: "this is a perfectly nice title", + Expected: "this is a perfectly nice title", + }, + { + Scenario: "enhancement-prefix", + Input: "enhancement: remove the prefix", + Expected: "remove the prefix", + }, + { + Scenario: "enhancement-prefix-caps", + Input: "Enhancement: Remove the prefix", + Expected: "Remove the prefix", + }, + } { + t.Run(tc.Scenario, func(t *testing.T) { + actual := CleanTitle(tc.Input) + assert.Equal(t, tc.Expected, actual) + }) + } +} diff --git a/tools/go.mod b/tools/go.mod index cf96cfc5fd..d97efc064f 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/google/go-github/v32 v32.1.0 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.6.1 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d gopkg.in/yaml.v2 v2.3.0 ) diff --git a/tools/go.sum b/tools/go.sum index 508428b539..400dd9b302 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -1,4 +1,6 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -8,6 +10,11 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -28,3 +35,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tools/report/main.go b/tools/report/main.go index a41f64cafb..c7f3d0e0d3 100644 --- a/tools/report/main.go +++ b/tools/report/main.go @@ -88,12 +88,7 @@ func showPRs(name string, prds []*stats.PullRequestDetails, withDescription bool groupPrefix = "" } - // Sometimes we have a superfluous "enhancement:" prefix in - // the PR title - title := *prd.Pull.Title - if strings.HasPrefix(strings.ToLower(title), "enhancement:") { - title = strings.TrimLeft(title[12:], " ") - } + title := enhancements.CleanTitle(*prd.Pull.Title) fmt.Printf("- [%d](%s): (%d/%d) %s%s (%s)\n", *prd.Pull.Number,