Skip to content

Commit 0af58b5

Browse files
committed
annotate first line by default 🆙
1 parent 34cb2ad commit 0af58b5

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ less-advanced-security --app_id=12345 --install_id=87654321 --key_path=tmp/appli
7878
Defaults to `True` (disable with `--filter_annotations=false`).
7979

8080
When set to `True`, annotations are added only when they apply to a line modified in the pull request (or a line immediately around it based on the git patch). When set to `False`, all annotations are added regardless of file or line.
81+
82+
### `--annotate_beginning`
83+
Defaults to `True` (disable with `--annotate_beginning=false`).
84+
85+
When set to `True`, annotations are submitted for the start line of a finding only (rather than the full range of lines in the finding). With this set to `False`, GitHub's default of displaying annotations on the end line of a finding is used.
86+
8187
## Development
8288

8389
### Environment

github/annotation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ func CreateAnnotation(path string, startLine int, endLine int, level string, tit
6464
endLine: endLine,
6565
}, nil
6666
}
67+
68+
func removeEndLines(annotations []*Annotation) {
69+
for _, annotation := range annotations {
70+
annotation.endLine = annotation.startLine
71+
annotation.githubAnnotation.EndLine = annotation.githubAnnotation.StartLine
72+
annotation.githubAnnotation.EndColumn = nil
73+
}
74+
}

github/annotation_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,72 @@ func TestCreateAnnotation(t *testing.T) {
9999
})
100100

101101
}
102+
103+
func TestRemoveEndLines(t *testing.T) {
104+
six := 6
105+
seven := 7
106+
twelve := 12
107+
108+
one_line_annotation := Annotation{
109+
startLine: 6,
110+
endLine: 6,
111+
githubAnnotation: &github.CheckRunAnnotation{StartLine: &six, EndLine: &six},
112+
}
113+
multi_line_annotation := Annotation{
114+
startLine: 7,
115+
endLine: 12,
116+
githubAnnotation: &github.CheckRunAnnotation{StartLine: &seven, EndLine: &twelve},
117+
}
118+
flattened_multi_line_annotation := Annotation{
119+
startLine: 7,
120+
endLine: 7,
121+
githubAnnotation: &github.CheckRunAnnotation{StartLine: &seven, EndLine: &seven},
122+
}
123+
124+
tests := []struct {
125+
name string
126+
annotations, expectedAnnotations []*Annotation
127+
}{
128+
{
129+
"one one-liner",
130+
[]*Annotation{&one_line_annotation},
131+
[]*Annotation{&one_line_annotation},
132+
},
133+
{
134+
"one multi-liner",
135+
[]*Annotation{&multi_line_annotation},
136+
[]*Annotation{&flattened_multi_line_annotation},
137+
},
138+
{
139+
"multiple",
140+
[]*Annotation{&one_line_annotation, &multi_line_annotation},
141+
[]*Annotation{&one_line_annotation, &flattened_multi_line_annotation},
142+
},
143+
}
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
removeEndLines(tt.annotations)
147+
148+
for _, expectedAnnotation := range tt.expectedAnnotations {
149+
found := false
150+
for _, gotAnnotation := range tt.annotations {
151+
if gotAnnotation.startLine == expectedAnnotation.startLine &&
152+
gotAnnotation.endLine == expectedAnnotation.endLine &&
153+
*gotAnnotation.githubAnnotation.StartLine == *expectedAnnotation.githubAnnotation.StartLine &&
154+
*gotAnnotation.githubAnnotation.EndLine == *expectedAnnotation.githubAnnotation.EndLine &&
155+
gotAnnotation.githubAnnotation.EndColumn == nil {
156+
found = true
157+
break
158+
}
159+
}
160+
if !found {
161+
t.Errorf("Expected annotation %s but did not find it.", expectedAnnotation)
162+
}
163+
}
164+
165+
if len(tt.expectedAnnotations) != len(tt.annotations) {
166+
t.Errorf("expected %d annotations but got %d", len(tt.expectedAnnotations), len(tt.annotations))
167+
}
168+
})
169+
}
170+
}

github/pull_request_annotator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ func computeConclusion(annotations []*Annotation) string {
4242
return conclusion
4343
}
4444

45-
func (annotator *PullRequestAnnotator) PostAnnotations(annotations []*Annotation, checkName string, filterAnnotations bool) error {
45+
func (annotator *PullRequestAnnotator) PostAnnotations(annotations []*Annotation, checkName string, filterAnnotations bool, annotateStartLineOnly bool) error {
4646
if filterAnnotations {
4747
annotations = annotator.pr.filterAnnotations(annotations)
4848
}
4949

50+
if annotateStartLineOnly {
51+
removeEndLines(annotations)
52+
}
53+
5054
const MAX_ANNOTATIONS_PER_PAGE = 50
5155
// When creating a check run you can add only 50 annotations - later annotations must be added via an update to the
5256
// run. Split our annotations accordingly, and pull the github annotation off them.

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func main() {
4848
sarifPath := flag.String("sarif_path", "", "absolute path to your sarif file")
4949

5050
filterAnnotations := flag.Bool("filter_annotations", true, "filter annotations by lines found in the git patches, default true")
51+
annotateStartLineOnly := flag.Bool("annotate_beginning", true, "force annotations to start line of a finding (if set to false, GitHub default of end is used), default true")
5152

5253
flag.Parse()
5354

@@ -89,7 +90,7 @@ func main() {
8990
annotations = append(annotations, annotation)
9091
}
9192

92-
if err := annotator.PostAnnotations(annotations, tool.Name, *filterAnnotations); err != nil {
93+
if err := annotator.PostAnnotations(annotations, tool.Name, *filterAnnotations, *annotateStartLineOnly); err != nil {
9394
log.Fatal(errors.Wrap(err, "failed to post annotations"))
9495
}
9596
}

0 commit comments

Comments
 (0)