2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2020-06-25 14:21:23 +01:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2016-07-28 04:55:09 +01:00
|
|
|
"encoding/csv"
|
2016-07-26 00:39:55 +01:00
|
|
|
"encoding/json"
|
2018-01-26 03:16:49 +00:00
|
|
|
"encoding/xml"
|
2019-10-31 08:22:38 +00:00
|
|
|
"fmt"
|
2016-10-18 06:36:35 +01:00
|
|
|
htmlTemplate "html/template"
|
2016-07-20 11:02:01 +01:00
|
|
|
"io"
|
2019-03-11 20:13:48 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-10-18 06:36:35 +01:00
|
|
|
plainTemplate "text/template"
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2020-04-14 09:39:16 +01:00
|
|
|
color "github.com/gookit/color"
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2018-03-05 12:20:24 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2018-10-11 13:45:31 +01:00
|
|
|
// ReportFormat enumerates the output format for reported issues
|
2016-07-20 11:02:01 +01:00
|
|
|
type ReportFormat int
|
|
|
|
|
|
|
|
const (
|
2017-12-13 12:35:47 +00:00
|
|
|
// ReportText is the default format that writes to stdout
|
2016-07-20 11:02:01 +01:00
|
|
|
ReportText ReportFormat = iota // Plain text format
|
2017-12-13 12:35:47 +00:00
|
|
|
|
|
|
|
// ReportJSON set the output format to json
|
|
|
|
ReportJSON // Json format
|
|
|
|
|
|
|
|
// ReportCSV set the output format to csv
|
|
|
|
ReportCSV // CSV format
|
2018-01-26 03:16:49 +00:00
|
|
|
|
2018-01-27 04:14:35 +00:00
|
|
|
// ReportJUnitXML set the output format to junit xml
|
|
|
|
ReportJUnitXML // JUnit XML format
|
2019-03-12 13:22:58 +00:00
|
|
|
|
2020-11-02 08:13:53 +00:00
|
|
|
// ReportSARIF set the output format to SARIF
|
|
|
|
ReportSARIF // SARIF format
|
|
|
|
|
2019-03-13 00:25:11 +00:00
|
|
|
//SonarqubeEffortMinutes effort to fix in minutes
|
2019-03-12 13:22:58 +00:00
|
|
|
SonarqubeEffortMinutes = 5
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var text = `Results:
|
2019-02-26 22:24:06 +00:00
|
|
|
{{range $filePath,$fileErrors := .Errors}}
|
|
|
|
Golang errors in file: [{{ $filePath }}]:
|
|
|
|
{{range $index, $error := $fileErrors}}
|
|
|
|
> [line {{$error.Line}} : column {{$error.Column}}] - {{$error.Err}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}
|
2016-07-20 11:02:01 +01:00
|
|
|
{{ range $index, $issue := .Issues }}
|
2020-04-14 08:50:02 +01:00
|
|
|
[{{ highlight $issue.FileLocation $issue.Severity }}] - {{ $issue.RuleID }} (CWE-{{ $issue.Cwe.ID }}): {{ $issue.What }} (Confidence: {{ $issue.Confidence}}, Severity: {{ $issue.Severity }})
|
2020-06-25 14:21:23 +01:00
|
|
|
{{ printCode $issue }}
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
{{ end }}
|
2020-04-14 08:50:02 +01:00
|
|
|
{{ notice "Summary:" }}
|
2016-07-20 11:02:01 +01:00
|
|
|
Files: {{.Stats.NumFiles}}
|
|
|
|
Lines: {{.Stats.NumLines}}
|
|
|
|
Nosec: {{.Stats.NumNosec}}
|
2020-04-14 08:50:02 +01:00
|
|
|
Issues: {{ if eq .Stats.NumFound 0 }}
|
|
|
|
{{- success .Stats.NumFound }}
|
|
|
|
{{- else }}
|
|
|
|
{{- danger .Stats.NumFound }}
|
|
|
|
{{- end }}
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
`
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
type reportInfo struct {
|
2019-02-26 22:24:06 +00:00
|
|
|
Errors map[string][]gosec.Error `json:"Golang errors"`
|
2018-07-19 17:42:25 +01:00
|
|
|
Issues []*gosec.Issue
|
|
|
|
Stats *gosec.Metrics
|
2017-04-26 01:57:12 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// CreateReport generates a report based for the supplied issues and metrics given
|
2020-04-14 08:50:02 +01:00
|
|
|
// the specified format. The formats currently accepted are: json, yaml, csv, junit-xml, html, sonarqube, golint and text.
|
|
|
|
func CreateReport(w io.Writer, format string, enableColor bool, rootPaths []string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
|
2017-04-26 01:57:12 +01:00
|
|
|
data := &reportInfo{
|
2019-02-26 22:24:06 +00:00
|
|
|
Errors: errors,
|
2017-04-26 01:57:12 +01:00
|
|
|
Issues: issues,
|
|
|
|
Stats: metrics,
|
|
|
|
}
|
2016-07-26 00:39:55 +01:00
|
|
|
var err error
|
2016-07-20 11:02:01 +01:00
|
|
|
switch format {
|
|
|
|
case "json":
|
2016-07-26 00:39:55 +01:00
|
|
|
err = reportJSON(w, data)
|
2018-03-05 12:20:24 +00:00
|
|
|
case "yaml":
|
|
|
|
err = reportYAML(w, data)
|
2016-07-26 00:39:55 +01:00
|
|
|
case "csv":
|
2016-07-28 04:55:09 +01:00
|
|
|
err = reportCSV(w, data)
|
2018-01-27 04:19:38 +00:00
|
|
|
case "junit-xml":
|
|
|
|
err = reportJUnitXML(w, data)
|
2016-10-18 06:36:35 +01:00
|
|
|
case "html":
|
|
|
|
err = reportFromHTMLTemplate(w, html, data)
|
2016-07-20 11:02:01 +01:00
|
|
|
case "text":
|
2020-04-14 08:50:02 +01:00
|
|
|
err = reportFromPlaintextTemplate(w, text, enableColor, data)
|
2019-03-11 20:13:48 +00:00
|
|
|
case "sonarqube":
|
2019-06-24 13:35:11 +01:00
|
|
|
err = reportSonarqube(rootPaths, w, data)
|
2020-01-03 09:56:21 +00:00
|
|
|
case "golint":
|
|
|
|
err = reportGolint(w, data)
|
2020-11-02 08:13:53 +00:00
|
|
|
case "sarif":
|
|
|
|
err = reportSARIFTemplate(rootPaths, w, data)
|
2016-07-20 11:02:01 +01:00
|
|
|
default:
|
2020-04-14 08:50:02 +01:00
|
|
|
err = reportFromPlaintextTemplate(w, text, enableColor, data)
|
2016-07-26 00:39:55 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:35:11 +01:00
|
|
|
func reportSonarqube(rootPaths []string, w io.Writer, data *reportInfo) error {
|
|
|
|
si, err := convertToSonarIssues(rootPaths, data)
|
2019-06-24 13:10:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
raw, err := json.MarshalIndent(si, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(raw)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-09 04:36:09 +01:00
|
|
|
func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, error) {
|
|
|
|
si := &sonarIssues{[]sonarIssue{}}
|
2019-03-11 20:13:48 +00:00
|
|
|
for _, issue := range data.Issues {
|
2019-06-24 13:35:11 +01:00
|
|
|
var sonarFilePath string
|
|
|
|
for _, rootPath := range rootPaths {
|
|
|
|
if strings.HasPrefix(issue.File, rootPath) {
|
|
|
|
sonarFilePath = strings.Replace(issue.File, rootPath+"/", "", 1)
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 08:22:38 +00:00
|
|
|
|
2019-06-24 13:35:11 +01:00
|
|
|
if sonarFilePath == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-11 20:13:48 +00:00
|
|
|
|
2019-06-24 13:35:11 +01:00
|
|
|
lines := strings.Split(issue.Line, "-")
|
2019-04-25 08:31:24 +01:00
|
|
|
startLine, err := strconv.Atoi(lines[0])
|
|
|
|
if err != nil {
|
2019-06-24 13:10:51 +01:00
|
|
|
return si, err
|
2019-04-25 08:31:24 +01:00
|
|
|
}
|
2019-03-11 20:13:48 +00:00
|
|
|
endLine := startLine
|
|
|
|
if len(lines) > 1 {
|
2019-04-25 08:31:24 +01:00
|
|
|
endLine, err = strconv.Atoi(lines[1])
|
|
|
|
if err != nil {
|
2019-06-24 13:10:51 +01:00
|
|
|
return si, err
|
2019-04-25 08:31:24 +01:00
|
|
|
}
|
2019-03-11 20:13:48 +00:00
|
|
|
}
|
2019-06-24 13:35:11 +01:00
|
|
|
|
2019-03-11 20:13:48 +00:00
|
|
|
s := sonarIssue{
|
2019-03-11 20:16:30 +00:00
|
|
|
EngineID: "gosec",
|
|
|
|
RuleID: issue.RuleID,
|
2019-03-11 20:13:48 +00:00
|
|
|
PrimaryLocation: location{
|
|
|
|
Message: issue.What,
|
2019-06-24 13:35:11 +01:00
|
|
|
FilePath: sonarFilePath,
|
2019-03-11 20:13:48 +00:00
|
|
|
TextRange: textRange{StartLine: startLine, EndLine: endLine},
|
|
|
|
},
|
|
|
|
Type: "VULNERABILITY",
|
|
|
|
Severity: getSonarSeverity(issue.Severity.String()),
|
2019-03-12 13:22:58 +00:00
|
|
|
EffortMinutes: SonarqubeEffortMinutes,
|
2019-10-31 08:22:38 +00:00
|
|
|
Cwe: issue.Cwe,
|
2019-03-11 20:13:48 +00:00
|
|
|
}
|
2019-03-11 21:32:32 +00:00
|
|
|
si.SonarIssues = append(si.SonarIssues, s)
|
2019-03-11 20:13:48 +00:00
|
|
|
}
|
2019-06-24 13:10:51 +01:00
|
|
|
return si, nil
|
2019-03-11 20:13:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 08:13:53 +00:00
|
|
|
func convertToSarifReport(rootPaths []string, data *reportInfo) (*sarifReport, error) {
|
|
|
|
sr := buildSarifReport()
|
|
|
|
|
2021-02-05 09:06:04 +00:00
|
|
|
type rule struct {
|
|
|
|
index int
|
|
|
|
rule *sarifRule
|
|
|
|
}
|
|
|
|
|
|
|
|
rules := make([]*sarifRule, 0)
|
|
|
|
rulesIndices := make(map[string]rule)
|
|
|
|
lastRuleIndex := -1
|
|
|
|
|
2021-01-21 09:21:59 +00:00
|
|
|
results := []*sarifResult{}
|
2020-11-02 08:13:53 +00:00
|
|
|
|
2021-02-05 09:06:04 +00:00
|
|
|
for _, issue := range data.Issues {
|
|
|
|
r, ok := rulesIndices[issue.RuleID]
|
|
|
|
if !ok {
|
|
|
|
lastRuleIndex++
|
|
|
|
r = rule{index: lastRuleIndex, rule: buildSarifRule(issue)}
|
|
|
|
rulesIndices[issue.RuleID] = r
|
|
|
|
rules = append(rules, r.rule)
|
|
|
|
}
|
2020-11-02 08:13:53 +00:00
|
|
|
|
|
|
|
location, err := buildSarifLocation(issue, rootPaths)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := &sarifResult{
|
2021-02-05 09:06:04 +00:00
|
|
|
RuleID: r.rule.ID,
|
|
|
|
RuleIndex: r.index,
|
2021-01-21 16:51:27 +00:00
|
|
|
Level: getSarifLevel(issue.Severity.String()),
|
2020-11-02 08:13:53 +00:00
|
|
|
Message: &sarifMessage{
|
|
|
|
Text: issue.What,
|
|
|
|
},
|
2021-02-05 09:06:04 +00:00
|
|
|
Locations: []*sarifLocation{location},
|
2020-11-02 08:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
tool := &sarifTool{
|
|
|
|
Driver: &sarifDriver{
|
|
|
|
Name: "gosec",
|
2021-02-05 09:06:04 +00:00
|
|
|
Version: "2.1.0",
|
2020-11-02 08:13:53 +00:00
|
|
|
InformationURI: "https://github.com/securego/gosec/",
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
run := &sarifRun{
|
|
|
|
Tool: tool,
|
|
|
|
Results: results,
|
|
|
|
}
|
|
|
|
|
|
|
|
sr.Runs = append(sr.Runs, run)
|
|
|
|
|
|
|
|
return sr, nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportJSON(w io.Writer, data *reportInfo) error {
|
2016-07-26 00:39:55 +01:00
|
|
|
raw, err := json.MarshalIndent(data, "", "\t")
|
|
|
|
if err != nil {
|
2019-03-13 00:23:45 +00:00
|
|
|
return err
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-07-26 00:39:55 +01:00
|
|
|
_, err = w.Write(raw)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:20:24 +00:00
|
|
|
func reportYAML(w io.Writer, data *reportInfo) error {
|
|
|
|
raw, err := yaml.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(raw)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportCSV(w io.Writer, data *reportInfo) error {
|
2016-07-28 04:55:09 +01:00
|
|
|
out := csv.NewWriter(w)
|
|
|
|
defer out.Flush()
|
|
|
|
for _, issue := range data.Issues {
|
|
|
|
err := out.Write([]string{
|
|
|
|
issue.File,
|
2017-10-01 01:31:39 +01:00
|
|
|
issue.Line,
|
2016-07-28 04:55:09 +01:00
|
|
|
issue.What,
|
|
|
|
issue.Severity.String(),
|
|
|
|
issue.Confidence.String(),
|
|
|
|
issue.Code,
|
2019-10-31 08:22:38 +00:00
|
|
|
fmt.Sprintf("CWE-%s", issue.Cwe.ID),
|
2016-07-28 04:55:09 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-03 09:56:21 +00:00
|
|
|
func reportGolint(w io.Writer, data *reportInfo) error {
|
|
|
|
// Output Sample:
|
|
|
|
// /tmp/main.go:11:14: [CWE-310] RSA keys should be at least 2048 bits (Rule:G403, Severity:MEDIUM, Confidence:HIGH)
|
|
|
|
|
|
|
|
for _, issue := range data.Issues {
|
|
|
|
what := issue.What
|
|
|
|
if issue.Cwe.ID != "" {
|
|
|
|
what = fmt.Sprintf("[CWE-%s] %s", issue.Cwe.ID, issue.What)
|
|
|
|
}
|
|
|
|
|
|
|
|
// issue.Line uses "start-end" format for multiple line detection.
|
|
|
|
lines := strings.Split(issue.Line, "-")
|
|
|
|
start := lines[0]
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, "%s:%s:%s: %s (Rule:%s, Severity:%s, Confidence:%s)\n",
|
|
|
|
issue.File,
|
|
|
|
start,
|
|
|
|
issue.Col,
|
|
|
|
what,
|
|
|
|
issue.RuleID,
|
|
|
|
issue.Severity.String(),
|
|
|
|
issue.Confidence.String(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:19:38 +00:00
|
|
|
func reportJUnitXML(w io.Writer, data *reportInfo) error {
|
2020-09-29 18:17:38 +01:00
|
|
|
junitXMLStruct := createJUnitXMLStruct(data)
|
2018-01-27 04:25:54 +00:00
|
|
|
raw, err := xml.MarshalIndent(junitXMLStruct, "", "\t")
|
2018-01-26 03:16:49 +00:00
|
|
|
if err != nil {
|
2018-01-30 01:54:30 +00:00
|
|
|
return err
|
2018-01-26 03:16:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 04:14:35 +00:00
|
|
|
xmlHeader := []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
|
|
|
raw = append(xmlHeader, raw...)
|
2018-01-26 03:16:49 +00:00
|
|
|
_, err = w.Write(raw)
|
|
|
|
if err != nil {
|
2018-01-30 01:54:30 +00:00
|
|
|
return err
|
2018-01-26 03:16:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:54:30 +00:00
|
|
|
return nil
|
2018-01-26 03:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 08:13:53 +00:00
|
|
|
func reportSARIFTemplate(rootPaths []string, w io.Writer, data *reportInfo) error {
|
|
|
|
sr, err := convertToSarifReport(rootPaths, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
raw, err := json.MarshalIndent(sr, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(raw)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-14 08:50:02 +01:00
|
|
|
func reportFromPlaintextTemplate(w io.Writer, reportTemplate string, enableColor bool, data *reportInfo) error {
|
|
|
|
t, e := plainTemplate.
|
|
|
|
New("gosec").
|
|
|
|
Funcs(plainTextFuncMap(enableColor)).
|
|
|
|
Parse(reportTemplate)
|
2016-10-18 06:36:35 +01:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportFromHTMLTemplate(w io.Writer, reportTemplate string, data *reportInfo) error {
|
2018-07-19 17:42:25 +01:00
|
|
|
t, e := htmlTemplate.New("gosec").Parse(reportTemplate)
|
2016-07-20 11:02:01 +01:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Execute(w, data)
|
|
|
|
}
|
2020-04-14 08:50:02 +01:00
|
|
|
|
|
|
|
func plainTextFuncMap(enableColor bool) plainTemplate.FuncMap {
|
|
|
|
if enableColor {
|
|
|
|
return plainTemplate.FuncMap{
|
|
|
|
"highlight": highlight,
|
|
|
|
"danger": color.Danger.Render,
|
|
|
|
"notice": color.Notice.Render,
|
|
|
|
"success": color.Success.Render,
|
2020-06-25 14:21:23 +01:00
|
|
|
"printCode": printCodeSnippet,
|
2020-04-14 08:50:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// by default those functions return the given content untouched
|
|
|
|
return plainTemplate.FuncMap{
|
|
|
|
"highlight": func(t string, s gosec.Score) string {
|
|
|
|
return t
|
|
|
|
},
|
2020-06-25 14:21:23 +01:00
|
|
|
"danger": fmt.Sprint,
|
|
|
|
"notice": fmt.Sprint,
|
|
|
|
"success": fmt.Sprint,
|
|
|
|
"printCode": printCodeSnippet,
|
2020-04-14 08:50:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errorTheme = color.New(color.FgLightWhite, color.BgRed)
|
|
|
|
warningTheme = color.New(color.FgBlack, color.BgYellow)
|
|
|
|
defaultTheme = color.New(color.FgWhite, color.BgBlack)
|
|
|
|
)
|
|
|
|
|
|
|
|
// highlight returns content t colored based on Score
|
|
|
|
func highlight(t string, s gosec.Score) string {
|
|
|
|
switch s {
|
|
|
|
case gosec.High:
|
|
|
|
return errorTheme.Sprint(t)
|
|
|
|
case gosec.Medium:
|
|
|
|
return warningTheme.Sprint(t)
|
|
|
|
default:
|
|
|
|
return defaultTheme.Sprint(t)
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 14:21:23 +01:00
|
|
|
|
2020-06-25 16:03:25 +01:00
|
|
|
// printCodeSnippet prints the code snippet from the issue by adding a marker to the affected line
|
2020-06-25 14:21:23 +01:00
|
|
|
func printCodeSnippet(issue *gosec.Issue) string {
|
2020-06-25 16:39:38 +01:00
|
|
|
start, end := parseLine(issue.Line)
|
2020-06-25 14:21:23 +01:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(issue.Code))
|
|
|
|
var buf bytes.Buffer
|
2020-06-25 16:39:38 +01:00
|
|
|
line := start
|
2020-06-25 14:21:23 +01:00
|
|
|
for scanner.Scan() {
|
|
|
|
codeLine := scanner.Text()
|
2020-06-25 16:39:38 +01:00
|
|
|
if strings.HasPrefix(codeLine, strconv.Itoa(line)) && line <= end {
|
2020-06-25 14:21:23 +01:00
|
|
|
codeLine = " > " + codeLine + "\n"
|
2020-06-25 16:39:38 +01:00
|
|
|
line++
|
2020-06-25 14:21:23 +01:00
|
|
|
} else {
|
|
|
|
codeLine = " " + codeLine + "\n"
|
|
|
|
}
|
|
|
|
buf.WriteString(codeLine)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
2020-06-25 16:39:38 +01:00
|
|
|
|
|
|
|
// parseLine extract the start and the end line numbers from a issue line
|
|
|
|
func parseLine(line string) (int, int) {
|
|
|
|
parts := strings.Split(line, "-")
|
|
|
|
start := parts[0]
|
|
|
|
end := start
|
|
|
|
if len(parts) > 1 {
|
|
|
|
end = parts[1]
|
|
|
|
}
|
|
|
|
s, err := strconv.Atoi(start)
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
e, err := strconv.Atoi(end)
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
return s, e
|
|
|
|
}
|