aboutsummaryrefslogtreecommitdiff
blob: dc80ec2af9f0fce1bca7c86ff522cd6c21c89b46 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package maintainer

import (
	"encoding/json"
	"html"
	"net/http"
	"soko/pkg/app/handler/packages/components"
	"soko/pkg/app/layout"
	"soko/pkg/app/utils"
	"soko/pkg/database"
	"soko/pkg/models"
	"strings"
	"time"

	"github.com/go-pg/pg/v10"
	"github.com/gorilla/feeds"
)

func common(w http.ResponseWriter, r *http.Request) (maintainer models.Maintainer, packagesQuery *pg.Query, packagesCount int, err error) {
	maintainerEmail := r.PathValue("email")
	if !strings.Contains(maintainerEmail, "@") {
		maintainerEmail += "@gentoo.org"
	}

	packagesQuery = database.DBCon.Model((*models.Package)(nil)).Column("atom")

	if maintainerEmail == "maintainer-needed@gentoo.org" {
		packagesQuery = packagesQuery.Where("NULLIF(maintainers, '[]') IS null")
	} else {
		packagesQuery = packagesQuery.Where("maintainers @> ?", `[{"Email": "`+maintainerEmail+`"}]`)
	}

	maintainer = models.Maintainer{Email: maintainerEmail}
	err = database.DBCon.Model(&maintainer).WherePK().Relation("Project").Relation("Projects").Select()
	if err != nil {
		http.NotFound(w, r)
		return
	}

	userPreferences := utils.GetUserPreferences(r)
	if userPreferences.Maintainers.IncludeProjectPackages && maintainer.Projects != nil && len(maintainer.Projects) > 0 {
		excludeList := strings.Join(userPreferences.Maintainers.ExcludedProjects, ",")
		for _, proj := range maintainer.Projects {
			if !strings.Contains(excludeList, proj.Email) {
				packagesQuery = packagesQuery.WhereOr("maintainers @> ?", `[{"Email": "`+proj.Email+`"}]`)
			}
		}
	}

	packagesCount, err = packagesQuery.Clone().Count()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	return
}

func ShowChangelog(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var commits []*models.Commit
	err = database.DBCon.Model(&commits).
		Join("JOIN commit_to_packages").JoinOn("commit.id = commit_to_packages.commit_id").
		Where("commit_to_packages.package_atom IN (?)", query).
		Order("preceding_commits DESC").
		Limit(50).
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Changelog", components.Changelog("", commits)),
	).Render(r.Context(), w)
}

func ShowChangelogFeed(w http.ResponseWriter, r *http.Request) {
	maintainer, query, _, err := common(w, r)
	if err != nil {
		return
	}
	var commits []*models.Commit
	err = database.DBCon.Model(&commits).
		Join("JOIN commit_to_packages").JoinOn("commit.id = commit_to_packages.commit_id").
		Where("commit_to_packages.package_atom IN (?)", query).
		Order("preceding_commits DESC").
		Limit(100).
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	feed := &feeds.Feed{
		Title:       "100 latest commits for " + maintainer.Name,
		Description: "100 latest commits for " + maintainer.Name,
		Author:      &feeds.Author{Name: "Gentoo Packages Database"},
		Created:     time.Now(),
		Link:        &feeds.Link{Href: "https://packages.gentoo.org/maintainer/" + maintainer.Email + "/changelog"},
	}

	for _, commit := range commits {
		feed.Add(&feeds.Item{
			Title:   html.EscapeString(commit.Message),
			Updated: commit.CommitterDate,
			Created: commit.AuthorDate,
			Author:  &feeds.Author{Name: commit.CommitterName, Email: commit.CommitterEmail},
			Link:    &feeds.Link{Href: "https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=" + commit.Id, Type: "text/html", Rel: "alternate"},
			Id:      commit.Id,
		})
	}
	feed.WriteAtom(w)
}

func ShowOutdated(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var outdated []components.OutdatedItem
	descriptionQuery := database.DBCon.Model((*models.Version)(nil)).
		Column("description").
		Where("atom = outdated_packages.atom").
		Limit(1)
	err = database.DBCon.Model((*models.OutdatedPackages)(nil)).
		Column("atom").ColumnExpr("(?) AS description", descriptionQuery).
		Where("atom IN (?)", query).
		Order("atom").
		Select(&outdated)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Outdated", components.Outdated(outdated)),
	).Render(r.Context(), w)
}

func ShowOutdatedFeed(w http.ResponseWriter, r *http.Request) {
	maintainer, query, _, err := common(w, r)
	if err != nil {
		return
	}
	var outdated []models.OutdatedPackages
	err = database.DBCon.Model(&outdated).
		Where("atom IN (?)", query).
		Order("atom").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	utils.OutdatedFeed(w, "https://packages.gentoo.org/maintainer/"+maintainer.Email+"/outdated", maintainer.Name+" <"+maintainer.Email+">", outdated)
}

func ShowPullRequests(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var pullRequests []*models.GithubPullRequest
	err = database.DBCon.Model(&pullRequests).
		DistinctOn("github_pull_request.id").
		OrderExpr("github_pull_request.id DESC").
		Join("JOIN package_to_github_pull_requests").JoinOn("github_pull_request.id = package_to_github_pull_requests.github_pull_request_id").
		Where("package_atom IN (?)", query).
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Pull requests", components.PullRequests(pullRequests)),
	).Render(r.Context(), w)
}

func ShowStabilization(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var results []*models.PkgCheckResult
	err = database.DBCon.Model(&results).
		Column("atom", "cpv", "message").
		Where("class = ?", "StableRequest").
		Where("atom IN (?)", query).
		OrderExpr("cpv").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Stabilization", components.Stabilizations(results)),
	).Render(r.Context(), w)
}

func ShowBugs(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var bugs []*models.Bug
	err = database.DBCon.Model(&bugs).
		DistinctOn("id::INT").
		Column("id", "summary", "component", "assignee").
		OrderExpr("id::INT").
		With("wanted", query).
		Where("id IN (?)",
			database.DBCon.Model((*models.PackageToBug)(nil)).
				Column("bug_id").
				Join("JOIN wanted").JoinOn("package_atom = wanted.atom")).
		WhereOr("id IN (?)",
			database.DBCon.Model((*models.VersionToBug)(nil)).
				Column("bug_id").
				Join("JOIN versions").JoinOn("version_id = versions.id").
				Join("JOIN wanted").JoinOn("versions.atom = wanted.atom")).
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	generalCount, stabilizationCount, keywordingCount := utils.CountBugsCategories(bugs)
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Bugs", components.Bugs("", generalCount, stabilizationCount, keywordingCount, bugs)),
	).Render(r.Context(), w)
}

func ShowSecurity(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var bugs []*models.Bug
	err = database.DBCon.Model(&bugs).
		DistinctOn("id::INT").
		Column("id", "summary", "component", "assignee").
		OrderExpr("id::INT").
		With("wanted", query).
		Where("component = ?", "Vulnerabilities").
		Where("id IN (?)",
			database.DBCon.Model((*models.PackageToBug)(nil)).
				Column("bug_id").
				Join("JOIN wanted").JoinOn("package_atom = wanted.atom")).
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Security", components.SecurityBugs("", bugs)),
	).Render(r.Context(), w)
}

func ShowStabilizationFile(w http.ResponseWriter, r *http.Request) {
	_, query, _, err := common(w, r)
	if err != nil {
		return
	}
	pageName := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:]

	var results []*models.PkgCheckResult
	err = database.DBCon.Model(&results).
		Column("category", "package", "version", "message").
		Where("class = ?", "StableRequest").
		Where("atom IN (?)", query).
		OrderExpr("cpv").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	utils.StabilizationExport(w, pageName, results)
}

func ShowStabilizationFeed(w http.ResponseWriter, r *http.Request) {
	maintainer, query, _, err := common(w, r)
	if err != nil {
		return
	}

	var results []*models.PkgCheckResult
	err = database.DBCon.Model(&results).
		Column("atom", "cpv", "message").
		Where("class = ?", "StableRequest").
		Where("atom IN (?)", query).
		OrderExpr("cpv").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	utils.StabilizationFeed(w, "https://packages.gentoo.org/maintainer/"+maintainer.Email+"/stabilization", maintainer.Name+" <"+maintainer.Email+">", results)
}

func ShowPackages(w http.ResponseWriter, r *http.Request) {
	maintainer, query, packagesCount, err := common(w, r)
	if err != nil {
		return
	}
	var gpackages []*models.Package
	err = query.Model(&gpackages).
		Column("category").
		Order("category", "name").
		Relation("Versions").Select()
	if err != nil {
		http.NotFound(w, r)
		return
	}
	layout.Layout(maintainer.Name, layout.Maintainers,
		show(packagesCount, &maintainer, "Packages", showPackages(gpackages, &maintainer)),
	).Render(r.Context(), w)
}

func ShowInfoJson(w http.ResponseWriter, r *http.Request) {
	maintainer, _, _, err := common(w, r)
	if err != nil {
		return
	}

	var reply struct {
		Email     string   `json:"email"`
		Name      string   `json:"name"`
		IsProject bool     `json:"is_project"`
		Members   []string `json:"members"`
		MemberOf  []string `json:"member_of"`
	}

	reply.Email = maintainer.Email
	reply.Name = maintainer.Name
	reply.IsProject = maintainer.Type == "project"

	for _, member := range maintainer.Project.Members {
		reply.Members = append(reply.Members, member.Email)
	}
	for _, project := range maintainer.Projects {
		reply.MemberOf = append(reply.MemberOf, project.Email)
	}

	w.Header().Set("Content-Type", "application/json")
	err = json.NewEncoder(w).Encode(reply)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
	}
}