aboutsummaryrefslogtreecommitdiff
blob: 875744025c1a48421ca3da0f4ae45d2fdb4960dd (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
package utils

import (
	"encoding/json"
	"encoding/xml"
	"html"
	"net/http"
	"soko/pkg/models"
	"strings"
	"time"

	"github.com/gorilla/feeds"
)

type stabilization struct {
	XMLName  xml.Name `xml:"stabilization" json:"-"`
	Category string   `xml:"category" json:"category"`
	Package  string   `xml:"package" json:"package"`
	Version  string   `xml:"version" json:"version"`
	Message  string   `xml:"message" json:"message"`
}

func (s stabilization) String() string {
	return s.Category + "/" + s.Package + "-" + s.Version + " # " + s.Message
}

func StabilizationExport(w http.ResponseWriter, pageUrl string, results []*models.PkgCheckResult) {
	result := make([]stabilization, len(results))
	for i, pkgcheck := range results {
		result[i] = stabilization{
			Category: pkgcheck.Category,
			Package:  pkgcheck.Package,
			Version:  pkgcheck.Version,
			Message:  pkgcheck.Message,
		}
	}

	_, extension, _ := strings.Cut(pageUrl, ".")
	switch extension {
	case "json":
		b, err := json.Marshal(result)
		if err != nil {
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(b)
	case "xml":
		b, err := xml.Marshal(struct {
			XMLName  xml.Name `xml:"xml"`
			Packages []stabilization
		}{Packages: result})
		if err != nil {
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "application/xml")
		w.Write(b)
	case "list":
		var lines string
		for _, pkg := range result {
			lines += pkg.String() + "\n"
		}
		w.Header().Set("Content-Type", "text/plain")
		w.Write([]byte(lines))
	}
}

func StabilizationFeed(w http.ResponseWriter, link, title string, results []*models.PkgCheckResult) {
	feed := &feeds.Feed{
		Title:   "Stabilization candidates for " + title,
		Author:  &feeds.Author{Name: "Gentoo Packages Database"},
		Created: time.Now(),
		Link:    &feeds.Link{Href: link},
	}

	for _, pkgcheck := range results {
		feed.Add(&feeds.Item{
			Title:       pkgcheck.CPV,
			Description: html.EscapeString(pkgcheck.Message),
			Link:        &feeds.Link{Href: "https://packages.gentoo.org/packages/" + pkgcheck.Atom, Type: "text/html", Rel: "alternate"},
			Id:          pkgcheck.CPV,
		})
	}
	feed.WriteAtom(w)
}