-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (65 loc) · 1.68 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strings"
)
type StaticCheckEntry struct {
Code string `json:"code"`
Severity string `json:"severity"`
Location struct {
File string `json:"file"`
Line int `json:"line"`
Column int `json:"column"`
} `json:"location"`
End interface{} `json:"end"`
Message string `json:"message"`
}
type GitlabCIEntry struct {
Description string `json:"description"`
Fingerprint string `json:"fingerprint"`
Severity string `json:"severity"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
var gitlabEntries = make([]GitlabCIEntry, 0)
for scanner.Scan() {
var entry StaticCheckEntry
err := json.Unmarshal([]byte(scanner.Text()), &entry)
if err != nil {
log.Fatal(err)
}
var gitlabEntry GitlabCIEntry
gitlabEntry.Description = entry.Message
gitlabEntry.Fingerprint = fmt.Sprintf("%s%s%d%d", entry.Code, entry.Location.File, entry.Location.Line, entry.Location.Column)
gitlabEntry.Severity = entry.Severity
gitlabEntry.Location.Path = getRelativePath(entry.Location.File)
gitlabEntry.Location.Lines.Begin = entry.Location.Line
gitlabEntries = append(gitlabEntries, gitlabEntry)
}
gitlabJson, err := json.Marshal(gitlabEntries)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
}
fmt.Println(string(gitlabJson))
if len(gitlabEntries) == 0 {
os.Exit(0)
}
os.Exit(1)
}
func getRelativePath(absolutePath string) string {
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return strings.ReplaceAll(absolutePath, path+"/", "")
}