aboutsummaryrefslogtreecommitdiff
blob: 4f8bc60edb669d601bfa31bedce851ce1e9d672d (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
// Used to show a specific category

package categories

import (
	"encoding/json"
	"net/http"
	"strings"

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

	"soko/pkg/app/handler/packages/components"
	"soko/pkg/app/utils"
	"soko/pkg/database"
	"soko/pkg/models"
)

func common(w http.ResponseWriter, r *http.Request) (categoryName string, category models.Category, err error) {
	categoryName = r.PathValue("category")

	err = database.DBCon.Model(&category).
		Where("category.name = ?", categoryName).
		Relation("PackagesInformation").Select()
	if err == pg.ErrNoRows {
		http.NotFound(w, r)
		return
	} else if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	return
}

type packageInfo struct {
	Package     string
	Description string
}

func ShowPackages(w http.ResponseWriter, r *http.Request) {
	categoryName := r.PathValue("category")
	if strings.HasSuffix(categoryName, ".json") {
		buildJson(w, categoryName[:len(categoryName)-5])
		return
	}
	categoryName, category, err := common(w, r)
	if err != nil {
		return
	}

	var packages []packageInfo
	err = database.DBCon.Model((*models.Version)(nil)).
		DistinctOn("package").
		Column("package", "description").
		Where("category = ?", categoryName).
		Order("package ASC").
		Select(&packages)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	renderShowPage(w, r, "Packages", &category,
		showPackages(categoryName, packages))
}

func ShowOutdated(w http.ResponseWriter, r *http.Request) {
	categoryName, category, 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("SPLIT_PART(atom, '/', 1) = ?", categoryName).
		Order("atom").
		Select(&outdated)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	renderShowPage(w, r, "Outdated", &category,
		components.Outdated(outdated))
}

func ShowPullRequests(w http.ResponseWriter, r *http.Request) {
	categoryName, category, err := common(w, r)
	if err != nil {
		return
	}

	var pullRequests []*models.GithubPullRequest
	err = database.DBCon.Model(&pullRequests).
		Join("JOIN package_to_github_pull_requests ON package_to_github_pull_requests.github_pull_request_id = github_pull_request.id").
		Where("package_to_github_pull_requests.package_atom LIKE ?", categoryName+"/%").
		Group("github_pull_request.id").
		Order("github_pull_request.created_at DESC").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	renderShowPage(w, r, "Pull requests", &category,
		components.PullRequests(category.PackagesInformation.PullRequests > 0, pullRequests))
}

func ShowStabilizations(w http.ResponseWriter, r *http.Request) {
	categoryName, category, 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("SPLIT_PART(atom, '/', 1) = ?", categoryName).
		OrderExpr("cpv").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	renderShowPage(w, r, "Stabilization", &category,
		components.Stabilizations(category.PackagesInformation.StableRequests > 0, results))
}

func ShowStabilizationFile(w http.ResponseWriter, r *http.Request) {
	categoryName := r.PathValue("category")
	var results []*models.PkgCheckResult
	err := database.DBCon.Model(&results).
		Column("category", "package", "version", "message").
		Where("class = ?", "StableRequest").
		Where("SPLIT_PART(atom, '/', 1) = ?", categoryName).
		OrderExpr("cpv").
		Select()
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	pageName := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:]
	utils.StabilizationExport(w, pageName, results)
}

// build the json for the category
func buildJson(w http.ResponseWriter, categoryName string) {
	var jsonCategory struct {
		Name     string `json:"name"`
		Href     string `json:"href"`
		Packages []struct {
			Package     string `json:"name"`
			Href        string `json:"href"`
			Description string `json:"description"`
		} `json:"packages"`
	}

	err := database.DBCon.Model((*models.Version)(nil)).
		DistinctOn("package").
		Column("package", "description").
		ColumnExpr("homepage ->> 0 AS href").
		Where("category = ?", categoryName).
		Order("package ASC").
		Select(&jsonCategory.Packages)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	jsonCategory.Name = categoryName
	jsonCategory.Href = "https://packages.gentoo.org/categories/" + categoryName

	b, err := json.Marshal(jsonCategory)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(b)
}