Skip to content

Commit

Permalink
handle missing checklist panic (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ authored May 29, 2024
1 parent 392322f commit fad2370
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/checkmate/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/checkmate/checklist_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/checkmate/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})))
Expand All @@ -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...))
Expand Down
14 changes: 11 additions & 3 deletions pkg/checkmate/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"regexp"
"sort"
"strings"

"github.com/sethvargo/go-githubactions"
)

type reMatch struct {
Expand All @@ -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
Expand All @@ -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),
})
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/checkmate/parse_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -121,12 +123,19 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "Indicator but no checklist",
args: args{content: "<!--Checkmate-->"},
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))
}
})
}
Expand Down

0 comments on commit fad2370

Please sign in to comment.