aboutsummaryrefslogtreecommitdiff
blob: 24cb2e734bab833aafd7a6959a1c5babacb8298c (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
package packages

import (
	"encoding/json"
	"github.com/go-pg/pg"
	"github.com/go-pg/pg/v9/orm"
	"net/http"
	"soko/pkg/database"
	"soko/pkg/models"
	"strings"
	"time"
)

// build the json for the package
func buildJson(w http.ResponseWriter, r *http.Request) {

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

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

	sortVersionsDesc(gpackage.Versions)

	versions := getJSONVersions(gpackage)
	maintainers := getJSONMaintainers(gpackage)
	useflags := getJSONUseflag(gpackage)

	jsonPackage := Package{
		Atom:        gpackage.Atom,
		Description: gpackage.Versions[0].Description,
		Href:        "https://packages.gentoo.org/packages/" + gpackage.Atom,
		Versions:    versions,
		Herds:       []string{},
		Maintainers: maintainers,
		Use:         useflags,
		UpdatedAt:   gpackage.Commits[0].CommitterDate,
	}

	b, err := json.Marshal(jsonPackage)

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

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

// get all ebuild versions of the package in a format
// that is intended to be used to convert it to json
func getJSONVersions(gpackage *models.Package) []Version {
	var versions []Version
	for _, gversion := range gpackage.Versions {
		var masks []Mask
		for _, versionMask := range gversion.Masks {
			var maskedAtoms string
			if !strings.Contains(maskedAtoms, versionMask.Versions) {
				maskedAtoms = maskedAtoms + " " + versionMask.Versions
			}
			masks = append(masks, Mask{
				Author: strings.TrimSpace(versionMask.Author),
				Date:   versionMask.Date,
				Reason: strings.TrimSpace(versionMask.Reason),
				Atoms:  strings.Split(strings.TrimSpace(maskedAtoms), " "),
				Arches: []string{"*"},
			})
		}
		versions = append(versions, Version{
			Version:  gversion.Version,
			Keywords: strings.Split(gversion.Keywords, " "),
			Masks:    masks,
		})
	}
	return versions
}

// get all maintainers of the package in a format
// that is intended to be used to convert it to json
func getJSONMaintainers(gpackage *models.Package) []Maintainer {
	var maintainers []Maintainer
	for _, gmaintainers := range gpackage.Maintainers {
		maintainers = append(maintainers, Maintainer{
			Email:       gmaintainers.Email,
			Name:        gmaintainers.Name,
			Description: "",
			Type:        gmaintainers.Type,
			Members:     []Member{},
		})
	}
	return maintainers
}

// get all useflags in a format that is
// intended to be used to convert it to json
func getJSONUseflag(gpackage *models.Package) Use {
	useflags := Use{
		Local:     []Useflag{},
		Global:    []Useflag{},
		UseExpand: []Useflag{},
	}
	localUseflags, globalUseflags, useExpands := getPackageUseflags(gpackage)
	for _, useflag := range localUseflags {
		useflags.Local = append(useflags.Local, Useflag{
			Name:        useflag.Name,
			Description: useflag.Description,
		})
	}
	for _, useflag := range globalUseflags {
		useflags.Global = append(useflags.Global, Useflag{
			Name:        useflag.Name,
			Description: useflag.Description,
		})
	}
	for _, flags := range useExpands {
		for _, flag := range flags {
			useflags.UseExpand = append(useflags.UseExpand, Useflag{
				Name:        flag.Name,
				Description: flag.Description,
			})
		}
	}
	return useflags
}

type Package struct {
	Atom        string       `json:"atom"`
	Description string       `json:"description"`
	Href        string       `json:"href"`
	Versions    []Version    `json:"versions"`
	Herds       []string     `json:"herds"`
	Maintainers []Maintainer `json:"maintainers"`
	Use         Use          `json:"use"`
	UpdatedAt   time.Time    `json:"updated_at"`
}

type Version struct {
	Version  string   `json:"version"`
	Keywords []string `json:"keywords"`
	Masks    []Mask   `json:"masks,omitempty"`
}

type Mask struct {
	Author string    `json:"author"`
	Date   time.Time `json:"date"`
	Reason string    `json:"reason"`
	Atoms  []string  `json:"atom"`
	Arches []string  `json:"arches"`
}

type Maintainer struct {
	Email       string   `json:"email"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	Type        string   `json:"type"`
	Members     []Member `json:"members"`
}

type Member struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

type Use struct {
	Local     []Useflag `json:"local"`
	Global    []Useflag `json:"global"`
	UseExpand []Useflag `json:"use_expand"`
}

type Useflag struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}