-
Notifications
You must be signed in to change notification settings - Fork 2
/
kb.go
85 lines (73 loc) · 2.49 KB
/
kb.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
package db
import (
"strings"
"github.com/rs/zerolog/log"
)
type IssueCode string
func (i IssueCode) Name() string {
return strings.ReplaceAll(string(i), "_", " ")
}
func (i IssueCode) String() string {
return string(i)
}
type IssueTemplate struct {
Code IssueCode `json:"code"`
Title string `json:"title"`
Description string `json:"description"`
Remediation string `json:"remediation"`
Cwe int `json:"cwe"`
Severity string `json:"severity"`
References []string `json:"references"`
}
func GetIssueTemplateByCode(code IssueCode) *Issue {
for _, issueTemplate := range issueTemplates {
if issueTemplate.Code == code {
return &Issue{
Code: string(issueTemplate.Code),
Title: issueTemplate.Title,
Description: issueTemplate.Description,
Remediation: issueTemplate.Remediation,
Cwe: issueTemplate.Cwe,
Severity: NewSeverity(issueTemplate.Severity),
References: StringSlice(issueTemplate.References),
}
}
}
return nil
}
func FillIssueFromHistoryAndTemplate(history *History, code IssueCode, details string, confidence int, severity string, workspaceID, taskID, taskJobID *uint) *Issue {
issue := GetIssueTemplateByCode(code)
issue.URL = history.URL
issue.Request = history.RawRequest
issue.Response = history.RawResponse
issue.StatusCode = history.StatusCode
issue.HTTPMethod = history.Method
issue.Confidence = confidence
issue.Details = details
issue.WorkspaceID = workspaceID
issue.TaskID = taskID
issue.TaskJobID = taskJobID
issue.Requests = []History{*history}
if severity != "" {
issue.Severity = NewSeverity(severity)
}
return issue
}
func CreateIssueFromHistoryAndTemplate(history *History, code IssueCode, details string, confidence int, severity string, workspaceID, taskID, taskJobID *uint) (Issue, error) {
issue := FillIssueFromHistoryAndTemplate(history, code, details, confidence, severity, workspaceID, taskID, taskJobID)
createdIssue, err := Connection.CreateIssue(*issue)
if err != nil {
log.Error().Err(err).Str("issue", issue.Title).Str("url", history.URL).Msg("Failed to create issue")
return createdIssue, err
}
workspaceIDValue := uint(0)
if workspaceID != nil {
workspaceIDValue = *workspaceID
}
taskIDValue := uint(0)
if taskID != nil {
taskIDValue = *taskID
}
log.Warn().Uint("id", createdIssue.ID).Str("issue", issue.Title).Str("url", history.URL).Uint("workspace", workspaceIDValue).Uint("task", taskIDValue).Msg("New issue found")
return createdIssue, nil
}