forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview,knotserver: include language colour and calculate percentage

This commit updates the types related to fetching the languages used
within a repo to be more structured and include additional information
such as the language colour. Additional logic to calculate the language
percentage was added to the repo index.

Signed-off-by: BrookJeynes <jeynesbrook@gmail.com>

Changed files
+77 -5
appview
knotserver
types
+1 -1
appview/pages/pages.go
···
Raw bool
EmailToDidOrHandle map[string]string
VerifiedCommits commitverify.VerifiedCommits
-
Languages *types.RepoLanguageResponse
Pipelines map[string]db.Pipeline
types.RepoIndexResponse
}
···
Raw bool
EmailToDidOrHandle map[string]string
VerifiedCommits commitverify.VerifiedCommits
+
Languages []types.RepoLanguageDetails
Pipelines map[string]db.Pipeline
types.RepoIndexResponse
}
+64 -2
appview/repo/index.go
···
"log"
"net/http"
"slices"
"strings"
"tangled.sh/tangled.sh/core/appview/commitverify"
···
"tangled.sh/tangled.sh/core/types"
"github.com/go-chi/chi/v5"
)
func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) {
···
}
}
-
repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref)
if err != nil {
log.Printf("failed to compute language percentages: %s", err)
// non-fatal
···
BranchesTrunc: branchesTrunc,
EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap),
VerifiedCommits: vc,
-
Languages: repoLanguages,
Pipelines: pipelines,
})
return
}
func getForkInfo(
···
"log"
"net/http"
"slices"
+
"sort"
"strings"
"tangled.sh/tangled.sh/core/appview/commitverify"
···
"tangled.sh/tangled.sh/core/types"
"github.com/go-chi/chi/v5"
+
"github.com/go-enry/go-enry/v2"
)
func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) {
···
}
}
+
languageInfo, err := getLanguageInfo(repoInfo, rp, f, user, signedClient, ref)
if err != nil {
log.Printf("failed to compute language percentages: %s", err)
// non-fatal
···
BranchesTrunc: branchesTrunc,
EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap),
VerifiedCommits: vc,
+
Languages: languageInfo,
Pipelines: pipelines,
})
return
+
}
+
+
func getLanguageInfo(
+
repoInfo repoinfo.RepoInfo,
+
rp *Repo,
+
f *reporesolver.ResolvedRepo,
+
user *oauth.User,
+
signedClient *knotclient.SignedClient,
+
ref string,
+
) ([]types.RepoLanguageDetails, error) {
+
repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref)
+
if err != nil {
+
return []types.RepoLanguageDetails{}, err
+
}
+
if repoLanguages == nil {
+
repoLanguages = &types.RepoLanguageResponse{Languages: make(map[string]int)}
+
}
+
+
totalLanguageFileCount := 0
+
for _, fileCount := range repoLanguages.Languages {
+
totalLanguageFileCount += fileCount
+
}
+
+
var languageStats []types.RepoLanguageDetails
+
var otherLanguageStat *types.RepoLanguageDetails
+
var otherPercentage float32 = 0
+
+
for fileType, fileCount := range repoLanguages.Languages {
+
percentage := (float32(fileCount) / float32(totalLanguageFileCount)) * 100
+
+
if percentage <= 0.1 {
+
otherPercentage += percentage
+
continue
+
}
+
+
// Exclude languages
+
if fileType == "Text" {
+
otherPercentage += percentage
+
continue
+
}
+
+
color := enry.GetColor(fileType)
+
+
if fileType == "Other" {
+
otherLanguageStat = &types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color}
+
} else {
+
languageStats = append(languageStats, types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color})
+
}
+
}
+
+
sort.Slice(languageStats, func(i, j int) bool {
+
return languageStats[i].Percentage > languageStats[j].Percentage
+
})
+
+
if otherLanguageStat != nil {
+
otherLanguageStat.Percentage += otherPercentage
+
languageStats = append(languageStats, *otherLanguageStat)
+
}
+
+
return languageStats, nil
}
func getForkInfo(
+1
appview/repo/repo.go
···
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/go-chi/chi/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/posthog/posthog-go"
···
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/go-chi/chi/v5"
+
"github.com/go-enry/go-enry/v2"
"github.com/go-git/go-git/v5/plumbing"
"github.com/posthog/posthog-go"
+4 -1
knotserver/routes.go
···
content, _ := gr.FileContentN(absPath, 1024)
if !safe {
lang = enry.GetLanguage(absPath, content)
} else {
lang, _ = enry.GetLanguageByContent(absPath, content)
if len(lang) == 0 {
-
return
}
}
}
···
content, _ := gr.FileContentN(absPath, 1024)
if !safe {
lang = enry.GetLanguage(absPath, content)
+
if len(lang) == 0 {
+
lang = "Other"
+
}
} else {
lang, _ = enry.GetLanguageByContent(absPath, content)
if len(lang) == 0 {
+
lang = "Other"
}
}
}
+7 -1
types/repo.go
···
Status ForkStatus `json:"status"`
}
type RepoLanguageResponse struct {
-
// Language: Percentage
Languages map[string]int `json:"languages"`
}
···
Status ForkStatus `json:"status"`
}
+
type RepoLanguageDetails struct {
+
Name string
+
Percentage float32
+
Color string
+
}
+
type RepoLanguageResponse struct {
+
// Language: File count
Languages map[string]int `json:"languages"`
}