aboutsummaryrefslogtreecommitdiff
blob: a3c71b73843f5bb67d00c170c323241dc1971c53 (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
// Used to show a specific package

package packages

import (
	b64 "encoding/base64"
	"encoding/json"
	"fmt"
	"net/http"
	"soko/pkg/app/layout"
	"soko/pkg/app/utils"
	"soko/pkg/database"
	"soko/pkg/models"
	"strings"
	"time"

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

// Show renders a template to show a given package
func Show(w http.ResponseWriter, r *http.Request) {
	category := r.PathValue("category")
	packageName := r.PathValue("package")
	pageName := r.PathValue("pageName")
	atom := category + "/" + packageName

	if pageName == "" && strings.HasSuffix(packageName, ".json") {
		buildJson(w, r)
		return
	}

	var currentSubTab string

	userPreferences := utils.GetUserPreferences(r)
	if userPreferences.General.LandingPageLayout == "full" {
		updateSearchHistory(atom, w, r)
	}

	var gpackage models.Package
	query := database.DBCon.Model(&gpackage).
		Relation("Bugs").
		Relation("PullRequests").
		Relation("Versions", func(q *pg.Query) (*pg.Query, error) {
			// performs mostly correct ordering of versions, which is perfected by sortVersionsDesc
			return q.Order("version DESC"), nil
		}).
		Relation("Versions.Bugs")

	switch pageName {
	case "changelog":
		currentSubTab = "Changelog"
		query = query.Relation("Commits", func(q *pg.Query) (*pg.Query, error) {
			// here be dragons
			const template = (`'%[1]s', (SELECT ARRAY_AGG("%[1]s") ` +
				`FROM jsonb_array_elements(COALESCE(NULLIF(changed_files -> '%[1]s', 'null'), '[]')) AS "%[1]s" ` +
				`WHERE "%[1]s" ->> 'Path' LIKE ?)`)
			return q.Column("commit_to_package.*",
				"commit.id", "preceding_commits", "message",
				"author_name", "author_email", "author_date",
				"committer_name", "committer_email", "committer_date").
				ColumnExpr(("json_build_object(" +
					fmt.Sprintf(template, "Modified") + "," +
					fmt.Sprintf(template, "Added") + "," +
					fmt.Sprintf(template, "Deleted") +
					") AS changed_files"), atom+"/%", atom+"/%", atom+"/%").
				Order("preceding_commits DESC").
				Limit(50), nil
		})
	case "changelog.json":
		changelogJSON(w, r)
		return
	case "qa-report":
		currentSubTab = "QA report"
		query = query.
			Relation("PkgCheckResults").
			Relation("Versions.PkgCheckResults")
	case "pull-requests":
		atom = strings.ReplaceAll(atom, "/pull-requests", "")
		currentSubTab = "Pull requests"
	case "bugs":
		atom = strings.ReplaceAll(atom, "/bugs", "")
		currentSubTab = "Bugs"
	case "security":
		atom = strings.ReplaceAll(atom, "/security", "")
		currentSubTab = "Security"
	case "dependencies":
		atom = strings.ReplaceAll(atom, "/dependencies", "")
		currentSubTab = "Dependencies"
		query = query.Relation("Versions.Dependencies")
	case "reverse-dependencies":
		atom = strings.ReplaceAll(atom, "/reverse-dependencies", "")
		currentSubTab = "Reverse Dependencies"
		query = query.Relation("ReverseDependencies")
	case "", "overview":
		query = query.Relation("Outdated").
			Relation("Versions.Masks").
			Relation("Versions.Deprecates")
		currentSubTab = "Overview"
	default:
		http.NotFound(w, r)
		return
	}

	err := query.Where("atom = ?", atom).Select()
	if err != nil || len(gpackage.Versions) == 0 {
		http.NotFound(w, r)
		return
	}

	sortVersionsDesc(gpackage.Versions)

	layout.Layout(gpackage.Atom, layout.Packages,
		show(&gpackage, currentSubTab, utils.GetUserPreferences(r)),
	).Render(r.Context(), w)
}

func updateSearchHistory(atom string, w http.ResponseWriter, r *http.Request) {
	cookie, err := r.Cookie("search_history")
	var packages string
	if err == nil {
		cookieValue, err := b64.StdEncoding.DecodeString(cookie.Value)
		if err == nil {
			packagesList := strings.Split(string(cookieValue), ",")
			if strings.Contains(string(cookieValue), atom) {
				newPackagesList := make([]string, 0, len(packagesList)-1)
				for _, gpackage := range packagesList {
					if gpackage != atom {
						newPackagesList = append(newPackagesList, gpackage)
					}
				}
				packagesList = newPackagesList
			}
			packagesList = append(packagesList, atom)
			if len(packagesList) > 10 {
				packagesList = packagesList[len(packagesList)-10:]
			}
			packages = strings.Join(packagesList, ",")
		} else {
			packages = atom
		}
	} else {
		packages = atom
	}

	updatedCookie := http.Cookie{
		Name:    "search_history",
		Path:    "/",
		Value:   b64.StdEncoding.EncodeToString([]byte(packages)),
		Expires: time.Now().Add(365 * 24 * time.Hour),
	}
	http.SetCookie(w, &updatedCookie)
}

// changelog renders a json version of the changelog
func changelogJSON(w http.ResponseWriter, r *http.Request) {

	atom := getAtom(r)
	gpackage := new(models.Package)
	err := database.DBCon.Model(gpackage).
		Where("atom = ?", atom).
		Relation("Commits", func(q *pg.Query) (*pg.Query, error) {
			return q.Order("preceding_commits DESC").Limit(5), nil
		}).
		Select()

	if err != nil && err != pg.ErrNoRows {
		http.Error(w, http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError)
		return
	}

	var jsonChanges []Change
	for _, commit := range gpackage.Commits {

		var changedPackages []string
		var commitToPackages []models.CommitToPackage
		err = database.DBCon.Model(&commitToPackages).
			Where("commit_id = ?", commit.Id).
			Select()
		if err != nil {
			continue
		}
		for _, changedPackage := range commitToPackages {
			changedPackages = append(changedPackages, changedPackage.PackageAtom)
		}

		jsonChanges = append(jsonChanges, Change{
			Id:             commit.Id,
			AuthorName:     commit.AuthorName,
			AuthorEmail:    commit.AuthorEmail,
			AuthorDate:     commit.AuthorDate,
			CommitterName:  commit.CommitterName,
			CommitterEmail: commit.CommitterEmail,
			CommitterDate:  commit.CommitterDate,
			Message:        commit.Message,
			Files:          *commit.ChangedFiles,
			Packages:       changedPackages,
		})
	}

	jsonData := Changes{
		Changes: jsonChanges,
	}

	b, err := json.Marshal(jsonData)

	if err != nil {
		http.Error(w, "Internal Server Error",
			http.StatusInternalServerError)
		return
	}

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

func countBugs(gpackage *models.Package) (securityBugs, nonSecurityBugs int) {
	for _, bug := range gpackage.Bugs {
		if bug.Component == string(models.BugComponentVulnerabilities) {
			securityBugs++
		} else {
			nonSecurityBugs++
		}
	}
	return
}

type Changes struct {
	Changes []Change `json:"changes"`
}

type Change struct {
	Id             string              `json:"id"`
	AuthorName     string              `json:"author_name"`
	AuthorEmail    string              `json:"author_email"`
	AuthorDate     time.Time           `json:"author_date"`
	CommitterName  string              `json:"committer_name"`
	CommitterEmail string              `json:"committer_email"`
	CommitterDate  time.Time           `json:"committer_date"`
	Message        string              `json:"message"`
	Files          models.ChangedFiles `json:"files"`
	Packages       []string            `json:"package"`
}