mirror of
https://github.com/glanceapp/glance.git
synced 2024-12-01 04:45:32 +01:00
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package widget
|
|
|
|
import (
|
|
"context"
|
|
"html/template"
|
|
"time"
|
|
|
|
"github.com/glanceapp/glance/internal/assets"
|
|
"github.com/glanceapp/glance/internal/feed"
|
|
)
|
|
|
|
type Releases struct {
|
|
widgetBase `yaml:",inline"`
|
|
Releases feed.AppReleases `yaml:"-"`
|
|
Repositories []string `yaml:"repositories"`
|
|
Token OptionalEnvString `yaml:"token"`
|
|
Limit int `yaml:"limit"`
|
|
CollapseAfter int `yaml:"collapse-after"`
|
|
Source string `yaml:"source"`
|
|
}
|
|
|
|
func (widget *Releases) Initialize() error {
|
|
widget.withTitle("Releases").withCacheDuration(2 * time.Hour)
|
|
|
|
if widget.Limit <= 0 {
|
|
widget.Limit = 10
|
|
}
|
|
|
|
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
|
|
widget.CollapseAfter = 5
|
|
}
|
|
|
|
if widget.Source == "" {
|
|
widget.Source = "github"
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (widget *Releases) Update(ctx context.Context) {
|
|
releases, err := feed.FetchLatestReleasesFromGitForge(widget.Repositories, string(widget.Token), widget.Source)
|
|
|
|
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
|
return
|
|
}
|
|
|
|
if len(releases) > widget.Limit {
|
|
releases = releases[:widget.Limit]
|
|
}
|
|
|
|
widget.Releases = releases
|
|
}
|
|
|
|
func (widget *Releases) Render() template.HTML {
|
|
return widget.render(widget, assets.ReleasesTemplate)
|
|
}
|