From 5e8b7a5b466a37087352e0c8dbe99a2848728b7d Mon Sep 17 00:00:00 2001 From: Moshe Litvin <=> Date: Fri, 10 Apr 2020 17:06:40 +0300 Subject: [PATCH] Output the license URL to the xslx output --- license/github/repo_api.go | 29 ++++++++++++++++++++++++++++- license/license.go | 1 + output_xlsx.go | 5 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/license/github/repo_api.go b/license/github/repo_api.go index 1326a31..0deb88d 100644 --- a/license/github/repo_api.go +++ b/license/github/repo_api.go @@ -56,17 +56,44 @@ FETCH_RETRY: return nil, err } + url := getURL(m, matches[1], matches[2], rl) + // If the license type is "other" then we try to use go-license-detector // to determine the license, which seems to be accurate in these cases. if rl.GetLicense().GetKey() == "other" { - return detect(rl) + lic, err := detect(rl) + if lic != nil { + lic.URL = url + } + return lic, err } return &license.License{ Name: rl.GetLicense().GetName(), SPDX: rl.GetLicense().GetSPDXID(), + URL: url, }, nil } // githubRe is the regexp matching the package for a GitHub import. var githubRe = regexp.MustCompile(`^github\.com/([^/]+)/([^/]+)$`) + +func getURL(m module.Module, owner, repo string, rl *github.RepositoryLicense) string { + rawURL := rl.GetHTMLURL() + if rawURL == "" { + return "" + } + + base := fmt.Sprintf("github.com/%s/%s/blob/", owner, repo) + re := regexp.MustCompile(base + `([^/]+)/`) + return re.ReplaceAllString(rawURL, base+getTag(m.Version)+"/") +} + +func getTag(ver string) string { + if m := pseudoVerRe.FindStringSubmatch(ver); m != nil { + return m[1] + } + return ver +} + +var pseudoVerRe = regexp.MustCompile(`^v0.0.0-[0-9]+-([a-h0-9]+)$`) diff --git a/license/license.go b/license/license.go index f42bc38..c87cbf7 100644 --- a/license/license.go +++ b/license/license.go @@ -6,6 +6,7 @@ package license type License struct { Name string // Name is a human-friendly name like "MIT License" SPDX string // SPDX ID of the license, blank if unknown or unavailable + URL string // URL of license } func (l *License) String() string { diff --git a/output_xlsx.go b/output_xlsx.go index 63021a2..bc71f4e 100644 --- a/output_xlsx.go +++ b/output_xlsx.go @@ -61,6 +61,7 @@ func (o *XLSXOutput) Close() error { f.SetCellValue(s, "C1", "SPDX ID") f.SetCellValue(s, "D1", "License") f.SetCellValue(s, "E1", "Allowed") + f.SetCellValue(s, "F1", "URL") f.SetColWidth(s, "A", "A", 40) f.SetColWidth(s, "B", "B", 20) f.SetColWidth(s, "C", "C", 20) @@ -124,6 +125,10 @@ func (o *XLSXOutput) Close() error { f.SetCellValue(s, fmt.Sprintf("C%d", i+2), lic.SPDX) } f.SetCellValue(s, fmt.Sprintf("D%d", i+2), lic.String()) + if lic.URL != nil { + f.SetCellValue(s, fmt.Sprintf("F%d", i+2), *lic.URL) + f.SetCellHyperLink(s, fmt.Sprintf("F%d", i+2), *lic.URL, "External") + } if o.Config != nil { switch o.Config.Allowed(lic) { case config.StateAllowed: