diff --git a/pkg/checkmate/action.go b/pkg/checkmate/action.go index 41ffe88..efb6f09 100644 --- a/pkg/checkmate/action.go +++ b/pkg/checkmate/action.go @@ -37,7 +37,7 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git if comment != "" { action.Infof("Comment checklist %s", comment) - checklists = Parse(comment) + checklists = Parse(action, comment) } } @@ -48,7 +48,7 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git action.Infof("PR Body: %s", descriptionPR) - checklists = append(Parse(descriptionPR), checklists...) + checklists = append(Parse(action, descriptionPR), checklists...) return inspect(checklists, action) } diff --git a/pkg/checkmate/checklist_test.go b/pkg/checkmate/checklist_test.go index b0a4a8b..3baf2d8 100644 --- a/pkg/checkmate/checklist_test.go +++ b/pkg/checkmate/checklist_test.go @@ -1,9 +1,11 @@ package checkmate import ( + "io" "testing" "github.com/google/go-cmp/cmp" + "github.com/sethvargo/go-githubactions" "github.com/stretchr/testify/assert" ) @@ -104,9 +106,10 @@ func TestChecklist_MarkdownSummary(t *testing.T) { > - [ ] Pear`, }, } + action := githubactions.New(githubactions.WithWriter(io.Discard)) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - checklists := Parse(tt.checklistRaw) + checklists := Parse(action, tt.checklistRaw) assert.Equal(t, len(checklists), 1) c := checklists[0] if got := c.MarkdownSummary(); got != tt.want { diff --git a/pkg/checkmate/commenter.go b/pkg/checkmate/commenter.go index 2b29109..e9f7de1 100644 --- a/pkg/checkmate/commenter.go +++ b/pkg/checkmate/commenter.go @@ -94,7 +94,7 @@ func updateComment(ctx context.Context, action *githubactions.Action, cfg Config return commentBody, err } - checklists := Parse(comment.GetBody()) + checklists := Parse(action, comment.GetBody()) actualFilenames := sorted(lo.WithoutEmpty(lo.Map(checklists, func(item Checklist, _ int) string { return item.Meta.FilenameGlob }))) @@ -115,7 +115,7 @@ func updateComment(ctx context.Context, action *githubactions.Action, cfg Config action.Infof("Adding checklists for [ %s ]", strings.Join(globAdd, " ")) toAdd := lo.Map(globAdd, func(key string, _ int) Checklist { - return Parse(checklistConfig[key].ToChecklistItemsMD(key))[0] + return Parse(action, checklistConfig[key].ToChecklistItemsMD(key))[0] }) checklists = sortedByFilename(append(filtered, toAdd...)) diff --git a/pkg/checkmate/parse.go b/pkg/checkmate/parse.go index 2e74143..0516ea4 100644 --- a/pkg/checkmate/parse.go +++ b/pkg/checkmate/parse.go @@ -4,6 +4,8 @@ import ( "regexp" "sort" "strings" + + "github.com/sethvargo/go-githubactions" ) type reMatch struct { @@ -15,7 +17,7 @@ var ( headerRE = regexp.MustCompile(`(?im)^ {0,3}#{1,6}\s.*`) ) -func Parse(content string) (list []Checklist) { +func Parse(action *githubactions.Action, content string) (list []Checklist) { indicators := findRE(content, indicatorRE) if len(indicators) == 0 { return @@ -32,10 +34,16 @@ func Parse(content string) (list []Checklist) { return checklists[i].LineNumbers[0] > indLineNumber }) + if c >= len(checklists) { + action.Warningf("No checklist found for indicator at line %d", indLineNumber) + continue + } + + checklistForIndicator := checklists[c] list = append(list, Checklist{ - Items: blockToItems(checklists[c]), + Items: blockToItems(checklistForIndicator), Header: closestHeaderTo(headers, indLineNumber), - Raw: checklists[c].Raw, + Raw: checklistForIndicator.Raw, Meta: ParseIndicator(ind.Raw), }) } diff --git a/pkg/checkmate/parse_test.go b/pkg/checkmate/parse_test.go index af4cbf7..59db7d7 100644 --- a/pkg/checkmate/parse_test.go +++ b/pkg/checkmate/parse_test.go @@ -1,11 +1,13 @@ package checkmate import ( + "io" "regexp" "strings" "testing" "github.com/google/go-cmp/cmp" + "github.com/sethvargo/go-githubactions" "github.com/stretchr/testify/assert" ) @@ -121,12 +123,19 @@ func TestParse(t *testing.T) { }, }, }, + { + name: "Indicator but no checklist", + args: args{content: ""}, + expected: nil, + }, } + + action := githubactions.New(githubactions.WithWriter(io.Discard)) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actualList := Parse(tt.args.content) - if !cmp.Equal(actualList, tt.expected) { - t.Error(cmp.Diff(actualList, tt.expected)) + actualList := Parse(action, tt.args.content) + if !cmp.Equal(tt.expected, actualList) { + t.Error(cmp.Diff(tt.expected, actualList)) } }) }