aboutsummaryrefslogtreecommitdiff
blob: 8b66a187f125529627c2edad67dea4b139ba92ec (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
package maintainer

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

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

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, "maintainers",
		show(packagesCount, &maintainer, "Changelog", components.Changelog("", commits)),
	).Render(r.Context(), 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, "maintainers",
		show(packagesCount, &maintainer, "Outdated", components.Outdated(outdated)),
	).Render(r.Context(), w)
}

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, "maintainers",
		show(packagesCount, &maintainer, "Pull requests", components.PullRequests(len(pullRequests) > 0, 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, "maintainers",
		show(packagesCount, &maintainer, "Stabilization", components.Stabilizations(len(results) > 0, 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 := countBugsCategories(bugs)
	layout.Layout(maintainer.Name, "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, "maintainers",
		show(packagesCount, &maintainer, "Security", components.SecurityBugs(len(bugs) > 0, 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 gpackages []*models.Package
	err = query.Model(&gpackages).
		Relation("Versions").
		Relation("Versions.PkgCheckResults", func(q *pg.Query) (*pg.Query, error) {
			return q.Where("class = ?", "StableRequest"), nil
		}).Select()
	if err != nil {
		http.NotFound(w, r)
		return
	}
	utils.StabilizationExport(w, pageName, gpackages)
}

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, "maintainers",
		show(packagesCount, &maintainer, "Packages", showPackages(gpackages, &maintainer)),
	).Render(r.Context(), w)
}