github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/reporting/policyViolation.go (about) 1 package reporting 2 3 import ( 4 "bytes" 5 "fmt" 6 "text/template" 7 "time" 8 9 "github.com/SAP/jenkins-library/pkg/orchestrator" 10 11 "golang.org/x/text/cases" 12 "golang.org/x/text/language" 13 ) 14 15 type PolicyViolationReport struct { 16 ArtifactID string 17 Branch string 18 CommitID string 19 Description string 20 DirectDependency string 21 Footer string 22 Group string 23 PackageURL string 24 PipelineName string 25 PipelineLink string 26 Version string 27 } 28 29 const policyViolationMdTemplate string = `# Policy Violation - {{ .PackageURL }} 30 31 ## Description 32 33 {{ .Description }} 34 35 ## Context 36 37 {{if .PipelineLink -}} 38 ### Pipeline 39 40 Pipeline run: [{{ .PipelineName }}]({{ .PipelineLink }}) 41 {{- end}} 42 43 ### Detected in 44 45 {{if .Branch}}**Branch:** {{ .Branch }}{{- end}} 46 {{if .CommitID}}**CommitId:** {{ .CommitID }}{{- end}} 47 {{if .DirectDependency}}**Dependency:** {{if (eq .DirectDependency "true")}}direct{{ else }}indirect{{ end }}{{- end}} 48 {{if .ArtifactID}}**ArtifactId:** {{ .ArtifactID }}{{- end}} 49 {{if .Group}}**Group:** {{ .Group }}{{- end}} 50 {{if .Version}}**Version:** {{ .Version }}{{- end}} 51 {{if .PackageURL}}**Package URL:** {{ .PackageURL }}{{- end}} 52 53 --- 54 55 {{.Footer}} 56 ` 57 58 func (p *PolicyViolationReport) ToMarkdown() ([]byte, error) { 59 funcMap := template.FuncMap{ 60 "date": func(t time.Time) string { 61 return t.Format("2006-01-02") 62 }, 63 "title": func(s string) string { 64 caser := cases.Title(language.AmericanEnglish) 65 return caser.String(s) 66 }, 67 } 68 69 // only fill with orchestrator information if orchestrator can be identified properly 70 if provider, err := orchestrator.NewOrchestratorSpecificConfigProvider(); err == nil { 71 // only add information if not yet provided 72 if len(p.CommitID) == 0 { 73 p.CommitID = provider.GetCommit() 74 } 75 if len(p.PipelineLink) == 0 { 76 p.PipelineLink = provider.GetJobURL() 77 p.PipelineName = provider.GetJobName() 78 } 79 } 80 81 md := []byte{} 82 tmpl, err := template.New("report").Funcs(funcMap).Parse(policyViolationMdTemplate) 83 if err != nil { 84 return md, fmt.Errorf("failed to create markdown issue template: %w", err) 85 } 86 buf := new(bytes.Buffer) 87 err = tmpl.Execute(buf, p) 88 if err != nil { 89 return md, fmt.Errorf("failed to execute markdown issue template: %w", err) 90 } 91 md = buf.Bytes() 92 return md, nil 93 }