-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotations.go
86 lines (71 loc) · 1.94 KB
/
annotations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package kubeci
import (
"bufio"
"context"
"io"
"log"
"strconv"
"strings"
"github.com/google/go-github/v45/github"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
// getPodLogs returns the logs for pod, the caller must close the stream
func getPodLogs(ctx context.Context, client kubernetes.Interface, name, namespace, container string) (io.ReadCloser, error) {
log.Printf("trying to stream logs for %s/%s[%s]", name, namespace, container)
req := client.CoreV1().Pods(namespace).GetLogs(name, &corev1.PodLogOptions{Container: container})
readCloser, err := req.Stream(ctx)
if err != nil {
return nil, err
}
return readCloser, nil
}
func parseAnnotations(r io.ReadCloser, filePrefix string) ([]*github.CheckRunAnnotation, error) {
log.Println("attempting to parse logs")
defer r.Close()
scanner := bufio.NewScanner(r)
var anns []*github.CheckRunAnnotation
for scanner.Scan() {
strs := strings.SplitN(scanner.Text(), ":", 3)
// we'll assume conventional file:line: message or file:line:col: message...
if len(strs) < 3 {
continue
}
// remove any preceding path info from the filename
fn := strings.TrimLeft(
strings.TrimPrefix(
strings.TrimSpace(strs[0]),
filePrefix),
`./\`)
line, err := strconv.Atoi(strs[1])
if err != nil {
continue
}
message := ""
rest := strs[2]
strs = strings.SplitN(rest, ":", 2)
switch len(strs) {
case 1:
if len(strs[0]) == 0 || strs[0][0] != ' ' {
continue
}
message = strings.TrimSpace(strs[0])
case 2:
if len(strs[1]) == 0 || strs[1][0] != ' ' {
continue
}
message = strings.TrimSpace(strs[1])
}
level := "notice"
// if we get here, we have something approaching a
anns = append(anns, &github.CheckRunAnnotation{
Path: &fn,
AnnotationLevel: &level,
StartLine: &line,
EndLine: &line,
Message: &message,
})
}
log.Printf("Found %d annotations", len(anns))
return anns, scanner.Err()
}