1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

feat: url log parsing / reporting when executing a command (#3461)

* Initial PR for url reporting

* Rename URLReport var to URLReportFileName

* Remove URLReportFileName from piper flags

* Update pkg/command/command.go

* Update pkg/command/command.go

* Update pkg/command/command.go

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Eugene Kortelyov 2022-02-25 17:35:44 +03:00 committed by GitHub
parent d86cfce6e6
commit af7496d012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
@ -15,9 +16,14 @@ import (
"github.com/pkg/errors"
)
const (
RelaxedURLRegEx = `(?:\b)((http(s?):\/\/)?(((www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(\/[a-zA-Z0-9\_\-\.\/\?\%\#\&\=]*)?)(?:\s|\b)`
)
// Command defines the information required for executing a call to any executable
type Command struct {
ErrorCategoryMapping map[string][]string
URLReportFileName string
dir string
stdin io.Reader
stdout io.Writer
@ -255,18 +261,50 @@ func (c *Command) startCmd(cmd *exec.Cmd) (*execution, error) {
}
go func() {
_, execution.errCopyStdout = piperutils.CopyData(c.stdout, srcOut)
if c.URLReportFileName != "" {
var buf bytes.Buffer
br := bufio.NewWriter(&buf)
_, execution.errCopyStdout = piperutils.CopyData(io.MultiWriter(c.stdout, br), srcOut)
br.Flush()
handleURLs(buf.String(), c.URLReportFileName)
} else {
_, execution.errCopyStdout = piperutils.CopyData(c.stdout, srcOut)
}
execution.wg.Done()
}()
go func() {
_, execution.errCopyStderr = piperutils.CopyData(c.stderr, srcErr)
if c.URLReportFileName != "" {
var buf bytes.Buffer
bw := bufio.NewWriter(&buf)
_, execution.errCopyStderr = piperutils.CopyData(io.MultiWriter(c.stderr, bw), srcErr)
bw.Flush()
handleURLs(buf.String(), c.URLReportFileName)
} else {
_, execution.errCopyStderr = piperutils.CopyData(c.stderr, srcErr)
}
execution.wg.Done()
}()
return &execution, nil
}
func handleURLs(s, file string) {
reg := regexp.MustCompile(RelaxedURLRegEx)
matches := reg.FindAllStringSubmatch(s, -1)
f, err := os.Create(file)
if err != nil {
log.Entry().WithError(err).Info("failed to create url report file")
}
defer f.Close()
for _, match := range matches {
_, err = f.WriteString(match[1] + "\n")
if err != nil {
log.Entry().WithError(err).Info("failed to write record to url report")
}
}
}
func (c *Command) scanLog(in io.Reader) {
scanner := bufio.NewScanner(in)
scanner.Split(scanShortLines)