1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package anitya
import (
"encoding/json"
"log/slog"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
"soko/pkg/config"
"soko/pkg/database"
"soko/pkg/models"
)
// API documentation: https://release-monitoring.org/static/docs/api.html#get--api-v2-packages-
const itemsPerPage = 250
type ApiPackage struct {
Name string `json:"name"`
Project string `json:"project"`
StableVersion string `json:"stable_version"`
Version string `json:"version"`
}
type ApiResponse struct {
Items []ApiPackage `json:"items"`
TotalItems int `json:"total_items"`
}
func UpdateAnitya() {
anityaPackages, err := readAllResults()
if err != nil {
slog.Error("Failed fetching anitya data", slog.Any("err", err))
return
} else if len(anityaPackages) == 0 {
slog.Error("No anitya packages found")
}
packagesMap := make(map[string]int, len(anityaPackages))
packages := make([]*models.Package, len(anityaPackages))
for i, p := range anityaPackages {
packages[i] = &models.Package{Atom: p.Name}
packagesMap[p.Name] = i
}
err = database.DBCon.Model(&packages).WherePK().Relation("Versions").Select()
if err != nil {
slog.Error("Failed fetching packages", slog.Any("err", err))
return
}
outdatedEntries := make([]*models.OutdatedPackages, 0, len(packages))
nextPackage:
for _, p := range packages {
if p.Category == "dev-perl" || p.Category == "perl-core" {
// Skip dev-perl packages, since they have a special versioning scheme in Gentoo
// and Anitya does not know about it
// https://wiki.gentoo.org/wiki/Project:Perl/Version-Scheme
continue
}
anitya := anityaPackages[packagesMap[p.Atom]]
p.AnityaInfo = &models.AnityaInfo{
Project: anitya.Project,
}
if len(p.Versions) == 0 {
continue
}
latest := models.Version{Version: anitya.LatestVersion()}
currentLatest := p.Versions[0]
for _, v := range p.Versions {
if slices.Contains(v.Properties, "live") {
continue
}
if strings.HasPrefix(v.Version, latest.Version) || !latest.GreaterThan(*v) {
continue nextPackage
}
if v.GreaterThan(*currentLatest) {
currentLatest = v
}
}
outdatedEntries = append(outdatedEntries, &models.OutdatedPackages{
Atom: p.Atom,
GentooVersion: currentLatest.Version,
NewestVersion: anitya.StableVersion,
Source: models.OutdatedSourceAnitya,
})
}
_, err = database.DBCon.Model(&packages).Set("anitya_info = ?anitya_info").Update()
if err != nil {
slog.Error("Failed updating packages", slog.Any("err", err))
return
}
slog.Info("Updated anitya information", slog.Int("count", len(packages)))
_, _ = database.DBCon.Model((*models.OutdatedPackages)(nil)).Where("source = ?", models.OutdatedSourceAnitya).Delete()
res, err := database.DBCon.Model(&outdatedEntries).OnConflict("(atom) DO UPDATE").Insert()
if err != nil {
slog.Error("Error while inserting outdated packages", slog.Any("err", err))
} else {
slog.Info("Inserted outdated packages", slog.Int("res", res.RowsAffected()))
}
updateStatus()
}
var client = http.Client{Timeout: 1 * time.Minute}
func fetchResults(page int, params url.Values) (int, []ApiPackage, error) {
req, err := http.NewRequest("GET", "https://release-monitoring.org/api/v2/packages/?"+params.Encode(), nil)
if err != nil {
slog.Error("Failed creating request", slog.Int("page", page), slog.Any("err", err))
return 0, nil, err
}
req.Header.Set("User-Agent", config.UserAgent())
resp, err := client.Do(req)
if err != nil {
slog.Error("Failed fetching anitya data", slog.Int("page", page), slog.Any("err", err))
return 0, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
slog.Error("Failed fetching anitya data", slog.Int("page", page), slog.Int("status", resp.StatusCode))
return 0, nil, nil
}
var data ApiResponse
err = json.NewDecoder(resp.Body).Decode(&data)
return data.TotalItems, data.Items, err
}
func readAllResults() (result []ApiPackage, err error) {
params := url.Values{
"distribution": {"Gentoo"},
"items_per_page": {strconv.Itoa(itemsPerPage)},
}
totalPages := 1
for page := 1; page <= totalPages; page++ {
slog.Info("Fetching anitya data", slog.Int("page", page))
params.Set("page", strconv.Itoa(page))
total, items, err := fetchResults(page, params)
if err != nil {
return nil, err
}
if page == 1 {
totalPages = (total + itemsPerPage - 1) / itemsPerPage
result = make([]ApiPackage, 0, total)
}
result = append(result, items...)
}
return
}
func (p *ApiPackage) LatestVersion() (result string) {
result = p.Version
if p.StableVersion != "" {
result = p.StableVersion
}
result, _, _ = strings.Cut(result, ".post")
return
}
func updateStatus() {
database.DBCon.Model(&models.Application{
Id: "anitya",
LastUpdate: time.Now(),
Version: config.Version(),
}).OnConflict("(id) DO UPDATE").Insert()
}
|