Skip to content

Commit

Permalink
(#20) Rearrange filters and issues (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale authored Feb 26, 2019
1 parent 8162e43 commit 76bb489
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
9 changes: 4 additions & 5 deletions cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
"os"

"github.com/llorllale/go-gitlint/internal/commits"
"github.com/llorllale/go-gitlint/internal/commits/filter"
"github.com/llorllale/go-gitlint/internal/commits/issues"
"github.com/llorllale/go-gitlint/internal/issues"
"github.com/llorllale/go-gitlint/internal/repo"
)

Expand All @@ -32,9 +31,9 @@ func main() {
issues.Printed(
os.Stdout, "\n",
issues.Collected(
[]func(*commits.Commit) issues.Issue{
filter.OfSubjectRegex(".{,1}"),
filter.OfBodyRegex(".{,1}"),
[]issues.Filter{
issues.OfSubjectRegex(".{,1}"),
issues.OfBodyRegex(".{,1}"),
},
commits.In(
repo.Filesystem("."),
Expand Down
28 changes: 13 additions & 15 deletions internal/commits/filter/filter.go → internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package filter
package issues

import (
"fmt"
"regexp"

"github.com/llorllale/go-gitlint/internal/commits"
"github.com/llorllale/go-gitlint/internal/commits/issues"
)

/*
Filters identify issues with a commit.
A filter returning a zero-valued Issue signals that it found no issue
with the commit.
*/
// Filter identifies an issue with a commit.
// A filter returning a zero-valued Issue signals that it found no issue
// with the commit.
type Filter func(*commits.Commit) Issue

// OfSubjectRegex tests a commit's subject with the regex.
func OfSubjectRegex(regex string) func(*commits.Commit) issues.Issue {
return func(c *commits.Commit) issues.Issue {
var issue issues.Issue
func OfSubjectRegex(regex string) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
matched, err := regexp.MatchString(regex, c.Subject())
if err != nil {
panic(err)
}
if !matched {
issue = issues.Issue{
issue = Issue{
Desc: fmt.Sprintf("subject does not match regex [%s]", regex),
Commit: *c,
}
Expand All @@ -47,15 +45,15 @@ func OfSubjectRegex(regex string) func(*commits.Commit) issues.Issue {
}

// OfBodyRegex tests a commit's body with the regex.
func OfBodyRegex(regex string) func(*commits.Commit) issues.Issue {
return func(c *commits.Commit) issues.Issue {
var issue issues.Issue
func OfBodyRegex(regex string) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
matched, err := regexp.MatchString(regex, c.Body())
if err != nil {
panic(err)
}
if !matched {
issue = issues.Issue{
issue = Issue{
Desc: fmt.Sprintf("body does not conform to regex [%s]", regex),
Commit: *c,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package filter
package issues

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (i *Issue) String() string {
type Issues func() []Issue

// Collected returns a collection of issues identified.
func Collected(filters []func(c *commits.Commit) Issue, cmts commits.Commits) Issues {
func Collected(filters []Filter, cmts commits.Commits) Issues {
return func() []Issue {
issues := make([]Issue, 0)
for _, c := range cmts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCollected(t *testing.T) {
{Hash: "456"},
}
issues := Collected(
[]func(*commits.Commit) Issue{
[]Filter{
func(c *commits.Commit) Issue {
var issue Issue
if c.Hash == "123" || c.Hash == "456" {
Expand Down

0 comments on commit 76bb489

Please sign in to comment.