Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ It provides:
- **Contributor detail pages** — summary metrics, charts, repositories, pull
requests and reviews.
- **Repository list and detail pages** — contributors, commits, PRs, reviews,
last activity, archived status.
last activity, archived status. Every column header on the list sorts the
table; clicking the active column flips the direction.
- **Status page** — sync health, GraphQL rate limit, stored row counts, run history.
- **Global date filter** — 7 or 30 days, 3/6/12 months or a custom range, applied
consistently across every page.
Expand Down
126 changes: 123 additions & 3 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func TestOrganizationIsolation(t *testing.T) {
}
}

repos, err := db.RepositoryRanking(ctx, f)
repos, err := db.RepositoryRanking(ctx, f, metrics.DefaultRepositorySort, metrics.SortDesc)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestArchivedRepositoriesAreExcludedFromAnalytics(t *testing.T) {

// The repository list keeps archived repositories visible and flagged.
f.IncludeArchived = true
repos, err := db.RepositoryRanking(ctx, f)
repos, err := db.RepositoryRanking(ctx, f, metrics.DefaultRepositorySort, metrics.SortDesc)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -799,7 +799,7 @@ func TestRepositoryAndContributorDetailQueries(t *testing.T) {
}

// A repository with no activity in the window must still be listed.
ranking, err := db.RepositoryRanking(ctx, f)
ranking, err := db.RepositoryRanking(ctx, f, metrics.DefaultRepositorySort, metrics.SortDesc)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -895,3 +895,123 @@ func contains(haystack, needle string) bool {
return false
})()
}

func TestRepositoryRankingSortsByEveryColumn(t *testing.T) {
db := testDB(t)
ctx := context.Background()
org := mustOrg(t, db, "O_1", "acme")

// zulu: most commits, no reviews, active, newest activity.
zulu := mustRepo(t, db, org, "R_z", "zulu", false)
// alpha: fewest commits, most reviews, oldest activity.
alpha := mustRepo(t, db, org, "R_a", "alpha", false)
// mike is archived and has no activity at all.
mike := mustRepo(t, db, org, "R_m", "mike", true)

alice := mustContributor(t, db, "U_alice", "alice", false)
bob := mustContributor(t, db, "U_bob", "bob", false)

for i := 0; i < 6; i++ {
mustCommit(t, db, org.ID, zulu, &alice, fmt.Sprintf("z%d", i), day(10+i), 100, 10, 5)
}
mustCommit(t, db, org.ID, alpha, &alice, "a1", day(2), 5, 1, 1)
mustCommit(t, db, org.ID, alpha, &bob, "a2", day(2), 5, 1, 1)

prZ := mustPR(t, db, org.ID, zulu, &alice, "PR_z", 1, day(11), nil, nil)
prA := mustPR(t, db, org.ID, alpha, &alice, "PR_a1", 1, day(2), nil, nil)
merged := day(3)
mustPR(t, db, org.ID, alpha, &bob, "PR_a2", 2, day(2), &merged, &merged)
_ = prZ

mustReview(t, db, org.ID, alpha, prA, &bob, "RV_1", models.ReviewApproved, day(3), 1)
mustReview(t, db, org.ID, alpha, prA, &alice, "RV_2", models.ReviewCommented, day(3), 1)

rebuild(t, db, org.ID, []int64{zulu, alpha, mike})

f := fullRange()
f.OrganizationID = org.ID
f.IncludeArchived = true

cases := []struct {
key, dir string
wantTop string
}{
{"name", metrics.SortAsc, "alpha"},
{"name", metrics.SortDesc, "zulu"},
{"commits", metrics.SortDesc, "zulu"},
{"contributors", metrics.SortDesc, "alpha"},
{"additions", metrics.SortDesc, "zulu"},
{"prs_opened", metrics.SortDesc, "alpha"},
{"prs_merged", metrics.SortDesc, "alpha"},
{"reviews", metrics.SortDesc, "alpha"},
{"last_activity", metrics.SortDesc, "zulu"},
{"last_activity", metrics.SortAsc, "alpha"},
{"status", metrics.SortDesc, "mike"},
}
for _, c := range cases {
t.Run(c.key+"-"+c.dir, func(t *testing.T) {
rows, err := db.RepositoryRanking(ctx, f, c.key, c.dir)
if err != nil {
t.Fatalf("sort by %s %s: %v", c.key, c.dir, err)
}
if len(rows) != 3 {
t.Fatalf("got %d rows, want 3", len(rows))
}
if rows[0].Name != c.wantTop {
names := make([]string, len(rows))
for i, r := range rows {
names[i] = r.Name
}
t.Errorf("sorted by %s %s the order is %v, want %q first", c.key, c.dir, names, c.wantTop)
}
})
}
}

func TestRepositoriesWithoutActivitySortLastInBothDirections(t *testing.T) {
db := testDB(t)
ctx := context.Background()
org := mustOrg(t, db, "O_1", "acme")
active := mustRepo(t, db, org, "R_a", "active", false)
idle := mustRepo(t, db, org, "R_i", "idle", false)
alice := mustContributor(t, db, "U_alice", "alice", false)

mustCommit(t, db, org.ID, active, &alice, "c1", day(2), 10, 1, 1)
rebuild(t, db, org.ID, []int64{active, idle})

f := fullRange()
f.OrganizationID = org.ID

// A repository with no activity in the period has a NULL last_activity. It
// must never jump to the top of an ascending sort.
for _, dir := range []string{metrics.SortAsc, metrics.SortDesc} {
rows, err := db.RepositoryRanking(ctx, f, "last_activity", dir)
if err != nil {
t.Fatal(err)
}
if rows[len(rows)-1].Name != "idle" {
t.Errorf("sorting last_activity %s put %q last, want idle", dir, rows[len(rows)-1].Name)
}
}
}

func TestRepositoryRankingIgnoresAnInjectedSortKey(t *testing.T) {
db := testDB(t)
ctx := context.Background()
org := mustOrg(t, db, "O_1", "acme")
mustRepo(t, db, org, "R_1", "api", false)

f := fullRange()
f.OrganizationID = org.ID

rows, err := db.RepositoryRanking(ctx, f, "name; DROP TABLE repositories", "ASC; DROP TABLE repositories")
if err != nil {
t.Fatalf("an unknown sort must be ignored, not fail: %v", err)
}
if len(rows) != 1 {
t.Errorf("got %d rows, want 1", len(rows))
}
if countRows(t, db, "repositories") != 1 {
t.Fatal("the repositories table disappeared: the sort key reached SQL")
}
}
16 changes: 12 additions & 4 deletions internal/database/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,18 @@ SELECT r.id, r.organization_id, r.github_id, r.name, r.full_name, r.url, r.descr
WHERE r.organization_id = $1
AND ($4 OR NOT r.is_archived)`

// RepositoryRanking returns repository rows for the repositories page.
func (db *DB) RepositoryRanking(ctx context.Context, f Filter) ([]models.RepositoryStats, error) {
q := repositoryStatsSQL + `
ORDER BY COALESCE(stats.commits, 0) DESC, COALESCE(stats.prs_opened, 0) DESC, lower(r.name) ASC`
// RepositoryRanking returns repository rows for the repositories page, ordered
// by one column.
//
// sortKey and sortDir are resolved through the metrics allow-list, so the
// ORDER BY clause can only ever contain a known column reference and one of two
// direction keywords.
func (db *DB) RepositoryRanking(ctx context.Context, f Filter, sortKey, sortDir string) ([]models.RepositoryStats, error) {
col, dir := metrics.ResolveRepositorySort(sortKey, sortDir)
// Repositories with no activity in the period sort last either way, rather
// than jumping to the top of an ascending sort.
q := repositoryStatsSQL + fmt.Sprintf(
"\n ORDER BY %s %s NULLS LAST, lower(r.name) ASC", col.SortExpr, metrics.SQLDirection(dir))

rows, err := db.Pool.Query(ctx, q, f.args()...)
if err != nil {
Expand Down
74 changes: 71 additions & 3 deletions internal/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package http

import (
"encoding/json"
"html/template"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -190,7 +191,7 @@ func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
s.serverError(w, r, err)
return
}
repos, err := s.db.RepositoryRanking(ctx, pc.Filter)
repos, err := s.db.RepositoryRanking(ctx, pc.Filter, metrics.DefaultRepositorySort, metrics.SortDesc)
if err != nil {
s.serverError(w, r, err)
return
Expand Down Expand Up @@ -332,9 +333,28 @@ func (s *Server) handleContributor(w http.ResponseWriter, r *http.Request) {
s.render(w, r, http.StatusOK, "contributor.html", view)
}

// sortHeader is one clickable column header of the repositories table.
type sortHeader struct {
Label string
Title string
// Numeric headers are right-aligned, matching their cells.
Numeric bool
// Active marks the column the table is currently ordered by.
Active bool
// Ascending is meaningful only when Active is set.
Ascending bool
Href template.URL
// AriaSort is the value of the th aria-sort attribute.
AriaSort string
}

type repositoriesView struct {
layoutData
Rows []models.RepositoryStats
Headers []sortHeader
SortKey string
SortDir string
SortLabel string
IncludeArchived bool
}

Expand All @@ -343,8 +363,22 @@ func (s *Server) handleRepositories(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
view := repositoriesView{layoutData: pc.Layout, IncludeArchived: s.cfg.IncludeArchived}
q := r.URL.Query()
col, dir := metrics.ResolveRepositorySort(q.Get("sort"), q.Get("dir"))

view := repositoriesView{
layoutData: pc.Layout,
IncludeArchived: s.cfg.IncludeArchived,
SortKey: col.Key,
SortDir: dir,
SortLabel: col.Label,
Headers: repositoryHeaders(pc.Layout.Range.Query(), col.Key, dir),
}
view.Title = "Repositories"
// Keep the chosen column in the period picker and the navigation links.
view.Query = cloneValues(pc.Layout.Range.Query())
view.Query.Set("sort", col.Key)
view.Query.Set("dir", dir)

if pc.Org == nil {
s.render(w, r, http.StatusOK, "repositories.html", view)
Expand All @@ -355,7 +389,7 @@ func (s *Server) handleRepositories(w http.ResponseWriter, r *http.Request) {
// visible and clearly marked, even when excluded from analytics.
filter := pc.Filter
filter.IncludeArchived = true
rows, err := s.db.RepositoryRanking(r.Context(), filter)
rows, err := s.db.RepositoryRanking(r.Context(), filter, col.Key, dir)
if err != nil {
s.serverError(w, r, err)
return
Expand All @@ -364,6 +398,40 @@ func (s *Server) handleRepositories(w http.ResponseWriter, r *http.Request) {
s.render(w, r, http.StatusOK, "repositories.html", view)
}

// repositoryHeaders builds one header link per sortable column. Clicking the
// active column flips its direction; clicking any other starts at that column's
// natural direction.
func repositoryHeaders(base url.Values, activeKey, activeDir string) []sortHeader {
cols := metrics.RepositoryColumns()
out := make([]sortHeader, 0, len(cols))
for _, c := range cols {
active := c.Key == activeKey
next := c.ToggleDirection(activeKey, activeDir)

v := cloneValues(base)
v.Set("sort", c.Key)
v.Set("dir", next)

aria := "none"
if active {
aria = "descending"
if activeDir == metrics.SortAsc {
aria = "ascending"
}
}
out = append(out, sortHeader{
Label: c.Label,
Title: c.Title,
Numeric: c.Numeric,
Active: active,
Ascending: active && activeDir == metrics.SortAsc,
Href: template.URL("/repositories?" + v.Encode()),
AriaSort: aria,
})
}
return out
}

type repositoryView struct {
layoutData
Repository models.RepositoryStats
Expand Down
Loading
Loading