Skip to content

Commit fad2370

Browse files
authored
handle missing checklist panic (#28)
1 parent 392322f commit fad2370

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

pkg/checkmate/action.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git
3737

3838
if comment != "" {
3939
action.Infof("Comment checklist %s", comment)
40-
checklists = Parse(comment)
40+
checklists = Parse(action, comment)
4141
}
4242
}
4343

@@ -48,7 +48,7 @@ func Run(ctx context.Context, cfg *Config, action *githubactions.Action, gh *git
4848

4949
action.Infof("PR Body: %s", descriptionPR)
5050

51-
checklists = append(Parse(descriptionPR), checklists...)
51+
checklists = append(Parse(action, descriptionPR), checklists...)
5252
return inspect(checklists, action)
5353
}
5454

pkg/checkmate/checklist_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package checkmate
22

33
import (
4+
"io"
45
"testing"
56

67
"github.com/google/go-cmp/cmp"
8+
"github.com/sethvargo/go-githubactions"
79
"github.com/stretchr/testify/assert"
810
)
911

@@ -104,9 +106,10 @@ func TestChecklist_MarkdownSummary(t *testing.T) {
104106
> - [ ] Pear`,
105107
},
106108
}
109+
action := githubactions.New(githubactions.WithWriter(io.Discard))
107110
for _, tt := range tests {
108111
t.Run(tt.name, func(t *testing.T) {
109-
checklists := Parse(tt.checklistRaw)
112+
checklists := Parse(action, tt.checklistRaw)
110113
assert.Equal(t, len(checklists), 1)
111114
c := checklists[0]
112115
if got := c.MarkdownSummary(); got != tt.want {

pkg/checkmate/commenter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func updateComment(ctx context.Context, action *githubactions.Action, cfg Config
9494
return commentBody, err
9595
}
9696

97-
checklists := Parse(comment.GetBody())
97+
checklists := Parse(action, comment.GetBody())
9898
actualFilenames := sorted(lo.WithoutEmpty(lo.Map(checklists, func(item Checklist, _ int) string {
9999
return item.Meta.FilenameGlob
100100
})))
@@ -115,7 +115,7 @@ func updateComment(ctx context.Context, action *githubactions.Action, cfg Config
115115

116116
action.Infof("Adding checklists for [ %s ]", strings.Join(globAdd, " "))
117117
toAdd := lo.Map(globAdd, func(key string, _ int) Checklist {
118-
return Parse(checklistConfig[key].ToChecklistItemsMD(key))[0]
118+
return Parse(action, checklistConfig[key].ToChecklistItemsMD(key))[0]
119119
})
120120

121121
checklists = sortedByFilename(append(filtered, toAdd...))

pkg/checkmate/parse.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"regexp"
55
"sort"
66
"strings"
7+
8+
"github.com/sethvargo/go-githubactions"
79
)
810

911
type reMatch struct {
@@ -15,7 +17,7 @@ var (
1517
headerRE = regexp.MustCompile(`(?im)^ {0,3}#{1,6}\s.*`)
1618
)
1719

18-
func Parse(content string) (list []Checklist) {
20+
func Parse(action *githubactions.Action, content string) (list []Checklist) {
1921
indicators := findRE(content, indicatorRE)
2022
if len(indicators) == 0 {
2123
return
@@ -32,10 +34,16 @@ func Parse(content string) (list []Checklist) {
3234
return checklists[i].LineNumbers[0] > indLineNumber
3335
})
3436

37+
if c >= len(checklists) {
38+
action.Warningf("No checklist found for indicator at line %d", indLineNumber)
39+
continue
40+
}
41+
42+
checklistForIndicator := checklists[c]
3543
list = append(list, Checklist{
36-
Items: blockToItems(checklists[c]),
44+
Items: blockToItems(checklistForIndicator),
3745
Header: closestHeaderTo(headers, indLineNumber),
38-
Raw: checklists[c].Raw,
46+
Raw: checklistForIndicator.Raw,
3947
Meta: ParseIndicator(ind.Raw),
4048
})
4149
}

pkg/checkmate/parse_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package checkmate
22

33
import (
4+
"io"
45
"regexp"
56
"strings"
67
"testing"
78

89
"github.com/google/go-cmp/cmp"
10+
"github.com/sethvargo/go-githubactions"
911
"github.com/stretchr/testify/assert"
1012
)
1113

@@ -121,12 +123,19 @@ func TestParse(t *testing.T) {
121123
},
122124
},
123125
},
126+
{
127+
name: "Indicator but no checklist",
128+
args: args{content: "<!--Checkmate-->"},
129+
expected: nil,
130+
},
124131
}
132+
133+
action := githubactions.New(githubactions.WithWriter(io.Discard))
125134
for _, tt := range tests {
126135
t.Run(tt.name, func(t *testing.T) {
127-
actualList := Parse(tt.args.content)
128-
if !cmp.Equal(actualList, tt.expected) {
129-
t.Error(cmp.Diff(actualList, tt.expected))
136+
actualList := Parse(action, tt.args.content)
137+
if !cmp.Equal(tt.expected, actualList) {
138+
t.Error(cmp.Diff(tt.expected, actualList))
130139
}
131140
})
132141
}

0 commit comments

Comments
 (0)