aboutsummaryrefslogtreecommitdiff
blob: 306d9d8604e1b52fbd10caf166dcdd43d033768b (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
package maintainers

import (
	"log/slog"
	"soko/pkg/config"
	"soko/pkg/database"
	"soko/pkg/models"
	"strings"
	"time"
)

const maintainerNeededEmail = "maintainer-needed@gentoo.org"

func FullImport() {

	database.Connect()
	defer database.DBCon.Close()

	slog.Info("Loading all raw maintainers from the database")
	var allMaintainerInformation []*models.Maintainer
	database.DBCon.Model((*models.Package)(nil)).ColumnExpr("jsonb_array_elements(maintainers)->>'Name' as name, jsonb_array_elements(maintainers) ->> 'Email' as email, jsonb_array_elements(maintainers) ->> 'Type' as type").Select(&allMaintainerInformation)

	maintainers := map[string]*models.Maintainer{
		maintainerNeededEmail: {
			Email: maintainerNeededEmail,
		},
	}

	for _, rawMaintainer := range allMaintainerInformation {
		_, ok := maintainers[rawMaintainer.Email]
		if !ok {
			maintainers[rawMaintainer.Email] = rawMaintainer
		} else {
			if maintainers[rawMaintainer.Email].Name == "" {
				maintainers[rawMaintainer.Email].Name = rawMaintainer.Name
			}
		}
	}

	slog.Info("Loading all packages from the database")
	var gpackages []*models.Package
	database.DBCon.Model(&gpackages).
		Relation("Outdated").
		Relation("PullRequests").
		Relation("Bugs").
		Relation("Versions").
		Relation("Versions.Bugs").
		Relation("Versions.PkgCheckResults").
		Select()

	for _, maintainer := range maintainers {
		var outdated, stableRequests int
		pullRequestIds := make(map[string]struct{})
		maintainerPackages := []*models.Package{}

		for _, gpackage := range gpackages {
			found := false
			if len(gpackage.Maintainers) == 0 && maintainer.Email == maintainerNeededEmail {
				found = true
			} else {
				for _, packageMaintainer := range gpackage.Maintainers {
					if packageMaintainer.Email == maintainer.Email {
						found = true
					}
				}
			}

			if found {
				maintainerPackages = append(maintainerPackages, gpackage)

				outdated = outdated + len(gpackage.Outdated)

				for _, pullRequest := range gpackage.PullRequests {
					pullRequestIds[pullRequest.Id] = struct{}{}
				}

				// Find Stable Requests
				for _, version := range gpackage.Versions {
					for _, pkgcheckWarning := range version.PkgCheckResults {
						if pkgcheckWarning.Class == "StableRequest" {
							stableRequests++
						}
					}
				}
			}
		}

		securityBugs, nonSecurityBugs := countBugs(maintainerPackages)

		maintainer.PackagesInformation = models.MaintainerPackagesInformation{
			Outdated:       outdated,
			PullRequests:   len(pullRequestIds),
			Bugs:           nonSecurityBugs,
			SecurityBugs:   securityBugs,
			StableRequests: stableRequests,
		}

		maintainer.Name = strings.TrimSpace(maintainer.Name)

		if maintainer.Name == "" {
			name, _, _ := strings.Cut(maintainer.Email, "@")
			maintainer.Name = strings.Title(name)
		}

		if maintainer.Type == "project" && strings.HasPrefix(maintainer.Name, "Gentoo ") {
			maintainer.Name = strings.TrimPrefix(maintainer.Name, "Gentoo ")
		} else if maintainer.Type == "person" {
			if strings.HasSuffix(maintainer.Email, "@gentoo.org") {
				maintainer.Type = "gentoo-developer"
			} else {
				maintainer.Type = "proxied-maintainer"
			}
		}

	}

	// TODO in future we want an incremental update here
	// but for now we delete everything and insert it again
	// this is currently acceptable as it takes less than 2 seconds
	database.TruncateTable((*models.Maintainer)(nil))

	rows := make([]*models.Maintainer, 0, len(maintainers))
	for _, row := range maintainers {
		rows = append(rows, row)
	}
	res, err := database.DBCon.Model(&rows).OnConflict("(email) DO NOTHING").Insert()
	if err != nil {
		slog.Error("Failed inserting maintainers", slog.Any("err", err))
		return
	}
	slog.Info("Inserted maintainers", slog.Int("rows", res.RowsAffected()))

	updateStatus()
}

func countBugs(packages []*models.Package) (securityBugs, nonSecurityBugs int) {
	allBugs := make(map[string]*models.Bug)
	for _, gpackage := range packages {
		for _, bug := range gpackage.AllBugs() {
			allBugs[bug.Id] = bug
		}
	}

	for _, bug := range allBugs {
		if bug.Component == "Vulnerabilities" {
			securityBugs++
		} else {
			nonSecurityBugs++
		}
	}

	return
}

func updateStatus() {
	database.DBCon.Model(&models.Application{
		Id:         "maintainers",
		LastUpdate: time.Now(),
		Version:    config.Version(),
	}).OnConflict("(id) DO UPDATE").Insert()
}