glance/internal/widget/repository-overview.go

59 lines
1.4 KiB
Go
Raw Normal View History

2024-05-10 12:05:42 +02:00
package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
2024-05-12 04:35:25 +02:00
type Repository struct {
2024-05-10 12:05:42 +02:00
widgetBase `yaml:",inline"`
RequestedRepository string `yaml:"repository"`
Token OptionalEnvString `yaml:"token"`
PullRequestsLimit int `yaml:"pull-requests-limit"`
IssuesLimit int `yaml:"issues-limit"`
2024-08-29 19:18:38 +02:00
CommitsLimit int `yaml:"commits-limit"`
2024-05-10 12:05:42 +02:00
RepositoryDetails feed.RepositoryDetails
}
2024-05-12 04:35:25 +02:00
func (widget *Repository) Initialize() error {
2024-05-10 12:05:42 +02:00
widget.withTitle("Repository").withCacheDuration(1 * time.Hour)
if widget.PullRequestsLimit == 0 || widget.PullRequestsLimit < -1 {
widget.PullRequestsLimit = 3
}
if widget.IssuesLimit == 0 || widget.IssuesLimit < -1 {
widget.IssuesLimit = 3
}
2024-08-29 19:18:38 +02:00
if widget.CommitsLimit == 0 || widget.CommitsLimit < -1 {
widget.CommitsLimit = -1
}
2024-05-10 12:05:42 +02:00
return nil
}
2024-05-12 04:35:25 +02:00
func (widget *Repository) Update(ctx context.Context) {
2024-05-10 12:05:42 +02:00
details, err := feed.FetchRepositoryDetailsFromGithub(
widget.RequestedRepository,
string(widget.Token),
widget.PullRequestsLimit,
widget.IssuesLimit,
2024-08-29 19:18:38 +02:00
widget.CommitsLimit,
2024-05-10 12:05:42 +02:00
)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
widget.RepositoryDetails = details
}
2024-05-12 04:35:25 +02:00
func (widget *Repository) Render() template.HTML {
return widget.render(widget, assets.RepositoryTemplate)
2024-05-10 12:05:42 +02:00
}