|
| 1 | +package clone |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "path" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/databrickslabs/sandbox/go-libs/fileset" |
| 11 | + "github.com/databrickslabs/sandbox/go-libs/git" |
| 12 | + "github.com/databrickslabs/sandbox/go-libs/github" |
| 13 | + "github.com/databrickslabs/sandbox/metascan/inventory" |
| 14 | + "github.com/databrickslabs/sandbox/metascan/metadata" |
| 15 | + "github.com/yuin/goldmark" |
| 16 | + meta "github.com/yuin/goldmark-meta" |
| 17 | + "github.com/yuin/goldmark/parser" |
| 18 | + "github.com/yuin/goldmark/text" |
| 19 | +) |
| 20 | + |
| 21 | +type Clones []*Clone |
| 22 | + |
| 23 | +func (cc Clones) Metadatas(ctx context.Context) (out []metadata.Metadata, err error) { |
| 24 | + for _, c := range cc { |
| 25 | + m, err := c.Metadatas(ctx) |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + out = append(out, m...) |
| 30 | + } |
| 31 | + sort.Slice(out, func(i, j int) bool { |
| 32 | + return out[i].LastUpdated.After(out[j].LastUpdated) |
| 33 | + }) |
| 34 | + return out, nil |
| 35 | +} |
| 36 | + |
| 37 | +type Clone struct { |
| 38 | + Inventory inventory.Item |
| 39 | + Git *git.Checkout |
| 40 | + Repo github.Repo |
| 41 | + FileSet fileset.FileSet |
| 42 | +} |
| 43 | + |
| 44 | +func (c *Clone) Name() string { |
| 45 | + return fmt.Sprintf("%s/%s", c.Inventory.Org, c.Repo.Name) |
| 46 | +} |
| 47 | + |
| 48 | +func (c *Clone) Metadatas(ctx context.Context) ([]metadata.Metadata, error) { |
| 49 | + markdown := goldmark.New( |
| 50 | + goldmark.WithExtensions( |
| 51 | + meta.Meta, |
| 52 | + ), |
| 53 | + goldmark.WithParserOptions( |
| 54 | + parser.WithAutoHeadingID(), |
| 55 | + ), |
| 56 | + ) |
| 57 | + if c.Inventory.IsSandbox { |
| 58 | + history, err := c.Git.History(ctx) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + out := []metadata.Metadata{} |
| 63 | + readmes := c.FileSet.Filter(`README.md`) |
| 64 | + for _, readme := range readmes { |
| 65 | + folder := path.Dir(readme.Relative) |
| 66 | + if folder == "." { |
| 67 | + continue |
| 68 | + } |
| 69 | + subHistory := history.Filter(func(pathname string) bool { |
| 70 | + if strings.HasSuffix(pathname, ".md") { |
| 71 | + // exclude any documentation |
| 72 | + return false |
| 73 | + } |
| 74 | + return strings.HasPrefix(pathname, folder) |
| 75 | + }) |
| 76 | + authors := subHistory.Authors() |
| 77 | + raw, err := readme.Raw() |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + document := markdown.Parser().Parse(text.NewReader(raw)) |
| 82 | + doc := document.OwnerDocument() |
| 83 | + child := doc.FirstChild() |
| 84 | + title := string(child.Text(raw)) |
| 85 | + if title == "" { |
| 86 | + continue |
| 87 | + } |
| 88 | + if len(authors) == 0 { |
| 89 | + continue |
| 90 | + } |
| 91 | + // todo: need the rest of the readme file |
| 92 | + out = append(out, metadata.Metadata{ |
| 93 | + Title: title, |
| 94 | + Author: authors.Primary(), |
| 95 | + Language: subHistory.Language(), |
| 96 | + Date: subHistory.Started(), |
| 97 | + LastUpdated: subHistory.Ended(), |
| 98 | + Maturity: c.Inventory.Maturity, |
| 99 | + URL: fmt.Sprintf("%s/%s", c.Repo.HtmlURL, folder), |
| 100 | + }) |
| 101 | + } |
| 102 | + return out, nil |
| 103 | + } |
| 104 | + return []metadata.Metadata{{ |
| 105 | + Title: c.Repo.Description, |
| 106 | + Tags: c.Repo.Topics, |
| 107 | + Language: c.Repo.Langauge, |
| 108 | + Date: c.FileSet.LastUpdated(), |
| 109 | + Maturity: c.Inventory.Maturity, |
| 110 | + URL: c.Repo.HtmlURL, |
| 111 | + }}, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (c *Clone) Maintainers(ctx context.Context) ([]string, error) { |
| 115 | + history, err := c.Git.History(ctx) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + authors := history.Authors() |
| 120 | + atMost := 2 |
| 121 | + if atMost > len(authors) { |
| 122 | + atMost = len(authors) |
| 123 | + } |
| 124 | + // TODO: build up author stats remapper |
| 125 | + var out []string |
| 126 | + for _, v := range authors { |
| 127 | + if v.Email == "action@github.com" { |
| 128 | + continue |
| 129 | + } |
| 130 | + if v.Author == "dependabot[bot]" { |
| 131 | + continue |
| 132 | + } |
| 133 | + out = append(out, v.Author) |
| 134 | + } |
| 135 | + return out[:atMost], nil |
| 136 | +} |
0 commit comments