Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Output the license URL to the xslx output #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion license/github/repo_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+)$`)
1 change: 1 addition & 0 deletions license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions output_xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down