internal/httpd/facts.go

4744c629ed4f0158bdea7d0f1d207fdccb1f7f99
gitbay/internal/httpd/facts.go history · blame · raw

57 lines · 1567 bytes

 1package httpd
 2
 3import (
 4	"gitbay.org/gitbay/internal/gitutil"
 5)
 6
 7// repoFacts is the "what is this repository" summary on a repo home: the
 8// counts a visitor uses to size up a project before reading any code.
 9type repoFacts struct {
10	Commits      int
11	Branches     int
12	Tags         int
13	Contributors []factContributor
14	Languages    []gitutil.Language
15	License      string
16	Release      string // newest release tag, if any
17	Build        string // latest build status on the default branch
18}
19
20type factContributor struct {
21	Name    string
22	User    string // account, when the email is verified here
23	Email   string
24	Commits int
25}
26
27const maxContributors = 12
28
29func (s *Server) factsFor(p repoPage) repoFacts {
30	f := repoFacts{
31		Commits: gitutil.CountCommits(p.Dir, p.Ref),
32		License: detectLicense(p.Dir, p.Ref),
33	}
34	if heads, err := gitutil.Refs(p.Dir, "heads"); err == nil {
35		f.Branches = len(heads)
36	}
37	if tags, err := gitutil.Refs(p.Dir, "tags"); err == nil {
38		f.Tags = len(tags)
39	}
40	f.Languages = gitutil.Languages(p.Dir, p.Ref, langOf)
41
42	names := s.authorNames()
43	for _, c := range gitutil.Contributors(p.Dir, p.Ref, maxContributors) {
44		user, _ := names.account(c.Email)
45		f.Contributors = append(f.Contributors, factContributor{
46			Name: names.name(c.Email, c.Name), User: user, Email: c.Email, Commits: c.Commits,
47		})
48	}
49
50	if rels, err := s.st.ListReleases(p.Repo.ID); err == nil && len(rels) > 0 {
51		f.Release = rels[0].Tag
52	}
53	if builds, err := s.st.ListBuilds(p.Repo.ID, 1); err == nil && len(builds) > 0 {
54		f.Build = builds[0].Status
55	}
56	return f
57}