config: add OverrideOrder option

This commit is contained in:
mappu 2022-12-31 14:18:47 +13:00
parent cb454938cc
commit 1e2ad32d54
4 changed files with 74 additions and 2 deletions

View File

@ -18,3 +18,8 @@ HomepageHeaderHTML="""
Teafolio is a web-based portfolio frontend for a Gitea server.
</p>
"""
# OverrideOrder allows reordering target repositories (0 for oldest)
# This affects the default "Recent Projects" sort ordering only.
#[OverrideOrder]
#tea=0

View File

@ -24,6 +24,7 @@ type Config struct {
HomepageHeaderHTML string
CustomLogoPngBase64 string
}
OverrideOrder map[string]int
}
type Application struct {

View File

@ -34,7 +34,7 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
<h2>Projects <small>(`+fmt.Sprintf("%d", len(this.reposCache))+`)</small></h2>
<table id="projtable-main" class="projtable">
`)
for _, repo := range this.reposCache {
for repoIdx, repo := range this.reposCache {
pageHref := html.EscapeString(`/` + url.PathEscape(repo.Name))
normalisedDesc := repo.Description
@ -62,7 +62,7 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", this.reposAlphabeticalOrder[repo.Name])+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.NewestCommit.Sub(repo.GiteaCreated).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("%d", repo.GiteaCreated.Unix())+`"
data-sort-ct="`+fmt.Sprintf("-%d", repoIdx)+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.NewestCommit.Unix())+`"
>
<td>

66
sync.go
View File

@ -8,6 +8,71 @@ import (
"time"
)
// rearrangeOrder applies rearrangements from the OverrideOrder configuration.
func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo {
if len(this.cfg.OverrideOrder) == 0 {
return repos // nothing to do
}
// Collect pre-existing positions for repos
reposByName := make(map[string]gitea.Repo, len(repos))
for _, repo := range repos {
reposByName[repo.Name] = repo
}
// Sort target insertion positions by lowest-first
type insertionPosition struct {
pos int
repoName string
}
insertAt := make([]insertionPosition, 0, len(this.cfg.OverrideOrder))
for rn, rpos := range this.cfg.OverrideOrder {
insertAt = append(insertAt, insertionPosition{len(repos) - rpos - 1, rn})
}
sort.Slice(insertAt, func(i, j int) bool {
return insertAt[i].pos < insertAt[j].pos
})
log.Printf("insertAt=%#v\n", insertAt)
// Walking-insertion loop
ret := make([]gitea.Repo, 0, len(repos))
nextRepo := 0
nextOverride := 0
for {
if nextOverride < len(insertAt) && insertAt[nextOverride].pos == len(ret) {
// We have an override to insert at this position
log.Printf("OVERRIDE insert of %q", insertAt[nextOverride].repoName)
ret = append(ret, reposByName[insertAt[nextOverride].repoName])
nextOverride++
} else if nextRepo < len(repos) {
// There are still other repos to insert generally
if _, ok := this.cfg.OverrideOrder[repos[nextRepo].Name]; ok {
// This repo has an overridden position. Don't insert it generally here
log.Printf("skipping natural insert of %q", repos[nextRepo].Name)
nextRepo++
} else {
// This repo does not have an overriden position. Here is fine
log.Printf("OK natural insert of %q", repos[nextRepo].Name)
ret = append(ret, repos[nextRepo])
nextRepo++
}
} else {
// There are no more overrides to insert and there are no remaining
// non-override repos
// That means we're done
break
}
}
return ret
}
func (this *Application) sync(ctx context.Context) (bool, error) {
// List repositories on Gitea
@ -79,6 +144,7 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
sort.Slice(repos, func(i, j int) bool {
return repos[i].GiteaCreated.After(repos[j].GiteaCreated)
})
repos = this.rearrangeOrder(repos)
// Commit our changes for the other threads to look at
this.reposMut.Lock()