2023-10-25 04:14:32 +00:00
|
|
|
// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
2023-10-29 14:41:00 +00:00
|
|
|
"crypto/sha256"
|
2023-10-25 04:14:32 +00:00
|
|
|
"database/sql"
|
2024-02-24 02:16:50 +00:00
|
|
|
"errors"
|
2023-10-25 04:14:32 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2024-02-23 23:06:16 +00:00
|
|
|
"sync"
|
2023-10-25 04:14:32 +00:00
|
|
|
"time"
|
|
|
|
|
2023-11-29 00:19:14 +00:00
|
|
|
"github.com/unascribed/FlexVer/go/flexver"
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
"git.sr.ht/~amolith/willow/db"
|
|
|
|
"git.sr.ht/~amolith/willow/git"
|
|
|
|
"git.sr.ht/~amolith/willow/rss"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Project struct {
|
2024-02-23 23:06:16 +00:00
|
|
|
ID string
|
2023-10-25 04:14:32 +00:00
|
|
|
URL string
|
|
|
|
Name string
|
|
|
|
Forge string
|
|
|
|
Running string
|
|
|
|
Releases []Release
|
|
|
|
}
|
|
|
|
|
|
|
|
type Release struct {
|
2024-02-23 23:06:16 +00:00
|
|
|
ID string
|
|
|
|
ProjectID string
|
|
|
|
URL string
|
|
|
|
Tag string
|
|
|
|
Content string
|
|
|
|
Date time.Time
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleases returns a list of all releases for a project from the database
|
2024-02-23 23:06:16 +00:00
|
|
|
func GetReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error) {
|
2024-02-24 02:16:50 +00:00
|
|
|
proj.ID = GenProjectID(proj.URL, proj.Name, proj.Forge)
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
ret, err := db.GetReleases(dbConn, proj.ID)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return proj, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) == 0 {
|
2024-02-24 02:16:50 +00:00
|
|
|
proj, err = fetchReleases(dbConn, mu, proj)
|
|
|
|
if err != nil {
|
|
|
|
return proj, err
|
|
|
|
}
|
|
|
|
err = upsertReleases(dbConn, mu, proj.ID, proj.Releases)
|
|
|
|
if err != nil {
|
|
|
|
return proj, err
|
|
|
|
}
|
|
|
|
return proj, nil
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range ret {
|
|
|
|
proj.Releases = append(proj.Releases, Release{
|
2024-02-24 02:16:50 +00:00
|
|
|
ID: row["id"],
|
|
|
|
ProjectID: proj.ID,
|
|
|
|
Tag: row["tag"],
|
|
|
|
Content: row["content"],
|
|
|
|
URL: row["release_url"],
|
|
|
|
Date: time.Time{},
|
2023-10-25 04:14:32 +00:00
|
|
|
})
|
|
|
|
}
|
2023-11-29 00:19:14 +00:00
|
|
|
proj.Releases = SortReleases(proj.Releases)
|
2023-10-25 04:14:32 +00:00
|
|
|
return proj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchReleases fetches releases from a project's forge given its URI
|
2024-02-23 23:06:16 +00:00
|
|
|
func fetchReleases(dbConn *sql.DB, mu *sync.Mutex, p Project) (Project, error) {
|
2023-10-25 04:14:32 +00:00
|
|
|
var err error
|
|
|
|
switch p.Forge {
|
|
|
|
case "github", "gitea", "forgejo":
|
|
|
|
rssReleases, err := rss.GetReleases(p.URL)
|
|
|
|
if err != nil {
|
2023-10-26 00:16:36 +00:00
|
|
|
fmt.Println("Error getting RSS releases:", err)
|
2023-10-25 04:14:32 +00:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
for _, release := range rssReleases {
|
|
|
|
p.Releases = append(p.Releases, Release{
|
2024-02-23 23:06:16 +00:00
|
|
|
ID: GenReleaseID(p.URL, release.URL, release.Tag),
|
2023-10-25 04:14:32 +00:00
|
|
|
Tag: release.Tag,
|
|
|
|
Content: release.Content,
|
|
|
|
URL: release.URL,
|
|
|
|
Date: release.Date,
|
|
|
|
})
|
2024-02-24 02:16:50 +00:00
|
|
|
err = upsertReleases(dbConn, mu, p.ID, p.Releases)
|
2023-10-29 14:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error upserting release: %v", err)
|
|
|
|
return p, err
|
|
|
|
}
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
gitReleases, err := git.GetReleases(p.URL, p.Forge)
|
|
|
|
if err != nil {
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
for _, release := range gitReleases {
|
|
|
|
p.Releases = append(p.Releases, Release{
|
2024-02-23 23:06:16 +00:00
|
|
|
ID: GenReleaseID(p.URL, release.URL, release.Tag),
|
2023-10-25 04:14:32 +00:00
|
|
|
Tag: release.Tag,
|
|
|
|
Content: release.Content,
|
|
|
|
URL: release.URL,
|
|
|
|
Date: release.Date,
|
|
|
|
})
|
2024-02-24 02:16:50 +00:00
|
|
|
err = upsertReleases(dbConn, mu, p.ID, p.Releases)
|
2023-10-29 14:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error upserting release: %v", err)
|
|
|
|
return p, err
|
|
|
|
}
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-29 00:19:14 +00:00
|
|
|
p.Releases = SortReleases(p.Releases)
|
2023-10-25 04:14:32 +00:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2023-11-29 00:19:14 +00:00
|
|
|
func SortReleases(releases []Release) []Release {
|
|
|
|
sort.Slice(releases, func(i, j int) bool {
|
|
|
|
return !flexver.Less(releases[i].Tag, releases[j].Tag)
|
|
|
|
})
|
|
|
|
return releases
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:08:13 +00:00
|
|
|
func SortProjects(projects []Project) []Project {
|
|
|
|
sort.Slice(projects, func(i, j int) bool {
|
|
|
|
return strings.ToLower(projects[i].Name) < strings.ToLower(projects[j].Name)
|
|
|
|
})
|
|
|
|
return projects
|
|
|
|
}
|
|
|
|
|
2024-02-24 02:16:50 +00:00
|
|
|
// upsertReleases updates or inserts a release in the database
|
|
|
|
func upsertReleases(dbConn *sql.DB, mu *sync.Mutex, projID string, releases []Release) error {
|
2023-10-29 14:41:00 +00:00
|
|
|
for _, release := range releases {
|
|
|
|
date := release.Date.Format("2006-01-02 15:04:05")
|
2024-02-24 02:16:50 +00:00
|
|
|
err := db.UpsertRelease(dbConn, mu, release.ID, projID, release.URL, release.Tag, release.Content, date)
|
2023-10-29 14:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error upserting release: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
// GenReleaseID generates a likely-unique ID from its project's URL, its release's URL, and its tag
|
|
|
|
func GenReleaseID(projectURL, releaseURL, tag string) string {
|
2023-12-20 19:18:12 +00:00
|
|
|
idByte := sha256.Sum256([]byte(projectURL + releaseURL + tag))
|
|
|
|
return fmt.Sprintf("%x", idByte)
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
// GenProjectID generates a likely-unique ID from a project's URI, name, and forge
|
|
|
|
func GenProjectID(url, name, forge string) string {
|
|
|
|
idByte := sha256.Sum256([]byte(url + name + forge))
|
|
|
|
return fmt.Sprintf("%x", idByte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Track(dbConn *sql.DB, mu *sync.Mutex, manualRefresh *chan struct{}, name, url, forge, release string) {
|
|
|
|
id := GenProjectID(url, name, forge)
|
|
|
|
err := db.UpsertProject(dbConn, mu, id, url, name, forge, release)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error upserting project:", err)
|
|
|
|
}
|
|
|
|
*manualRefresh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
func Untrack(dbConn *sql.DB, mu *sync.Mutex, id string) {
|
|
|
|
err := db.DeleteProject(dbConn, mu, id)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error deleting project:", err)
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
err = git.RemoveRepo(id)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
func RefreshLoop(dbConn *sql.DB, mu *sync.Mutex, interval int, manualRefresh, req *chan struct{}, res *chan []Project) {
|
2023-10-25 04:14:32 +00:00
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(interval))
|
|
|
|
|
|
|
|
fetch := func() []Project {
|
|
|
|
projectsList, err := GetProjects(dbConn)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting projects:", err)
|
|
|
|
}
|
|
|
|
for i, p := range projectsList {
|
2024-02-23 23:06:16 +00:00
|
|
|
p, err := fetchReleases(dbConn, mu, p)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
projectsList[i] = p
|
|
|
|
}
|
|
|
|
sort.Slice(projectsList, func(i, j int) bool {
|
|
|
|
return strings.ToLower(projectsList[i].Name) < strings.ToLower(projectsList[j].Name)
|
|
|
|
})
|
|
|
|
for i := range projectsList {
|
2024-02-24 02:16:50 +00:00
|
|
|
err = upsertReleases(dbConn, mu, projectsList[i].ID, projectsList[i].Releases)
|
2023-10-29 14:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error upserting release:", err)
|
|
|
|
continue
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return projectsList
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := fetch()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
projects = fetch()
|
|
|
|
case <-*manualRefresh:
|
|
|
|
ticker.Reset(time.Second * 3600)
|
|
|
|
projects = fetch()
|
|
|
|
case <-*req:
|
|
|
|
projectsCopy := make([]Project, len(projects))
|
|
|
|
copy(projectsCopy, projects)
|
|
|
|
*res <- projectsCopy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProject returns a project from the database
|
2024-02-24 02:16:50 +00:00
|
|
|
func GetProject(dbConn *sql.DB, proj Project) (Project, error) {
|
|
|
|
projectDB, err := db.GetProject(dbConn, proj.ID)
|
|
|
|
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return proj, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return proj, err
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
2023-11-29 01:01:26 +00:00
|
|
|
p := Project{
|
2024-02-24 02:16:50 +00:00
|
|
|
ID: proj.ID,
|
|
|
|
URL: proj.URL,
|
|
|
|
Name: proj.Name,
|
|
|
|
Forge: proj.Forge,
|
2023-10-25 04:14:32 +00:00
|
|
|
Running: projectDB["version"],
|
|
|
|
}
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
// GetProjectWithReleases returns a single project from the database along with its releases
|
2024-02-24 02:16:50 +00:00
|
|
|
func GetProjectWithReleases(dbConn *sql.DB, mu *sync.Mutex, proj Project) (Project, error) {
|
|
|
|
project, err := GetProject(dbConn, proj)
|
2024-02-23 23:06:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return Project{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetReleases(dbConn, mu, project)
|
|
|
|
}
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
// GetProjects returns a list of all projects from the database
|
|
|
|
func GetProjects(dbConn *sql.DB) ([]Project, error) {
|
|
|
|
projectsDB, err := db.GetProjects(dbConn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := make([]Project, len(projectsDB))
|
|
|
|
for i, p := range projectsDB {
|
|
|
|
projects[i] = Project{
|
2024-02-23 23:06:16 +00:00
|
|
|
ID: p["id"],
|
2023-10-25 04:14:32 +00:00
|
|
|
URL: p["url"],
|
|
|
|
Name: p["name"],
|
|
|
|
Forge: p["forge"],
|
|
|
|
Running: p["version"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:08:13 +00:00
|
|
|
return SortProjects(projects), nil
|
2023-10-25 04:14:32 +00:00
|
|
|
}
|
2024-02-23 23:06:16 +00:00
|
|
|
|
|
|
|
// GetProjectsWithReleases returns a list of all projects and all their releases
|
|
|
|
// from the database
|
|
|
|
func GetProjectsWithReleases(dbConn *sql.DB, mu *sync.Mutex) ([]Project, error) {
|
|
|
|
projects, err := GetProjects(dbConn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range projects {
|
|
|
|
projects[i], err = GetReleases(dbConn, mu, projects[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
projects[i].Releases = SortReleases(projects[i].Releases)
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:08:13 +00:00
|
|
|
return SortProjects(projects), nil
|
2024-02-23 23:06:16 +00:00
|
|
|
}
|