apps/kepler/atom.go 1.7 K raw
1
package main
2
3
import (
4
	"encoding/xml"
5
	"fmt"
6
	"time"
7
)
8
9
type atomFeed struct {
10
	XMLName xml.Name    `xml:"feed"`
11
	XMLNS   string      `xml:"xmlns,attr"`
12
	Title   string      `xml:"title"`
13
	ID      string      `xml:"id"`
14
	Updated string      `xml:"updated"`
15
	Link    []atomLink  `xml:"link"`
16
	Entries []atomEntry `xml:"entry"`
17
}
18
19
type atomLink struct {
20
	Rel  string `xml:"rel,attr,omitempty"`
21
	Href string `xml:"href,attr"`
22
}
23
24
type atomEntry struct {
25
	Title   string     `xml:"title"`
26
	ID      string     `xml:"id"`
27
	Updated string     `xml:"updated"`
28
	Link    atomLink   `xml:"link"`
29
	Author  atomAuthor `xml:"author"`
30
	Summary string     `xml:"summary"`
31
}
32
33
type atomAuthor struct {
34
	Name  string `xml:"name"`
35
	Email string `xml:"email,omitempty"`
36
}
37
38
func buildAtomFeed(siteName, repoName, baseURL string, commits []CommitInfo) ([]byte, error) {
39
	feed := atomFeed{
40
		XMLNS:   "http://www.w3.org/2005/Atom",
41
		Title:   fmt.Sprintf("%s / %s — commits", siteName, repoName),
42
		ID:      baseURL + "/" + repoName,
43
		Updated: time.Now().UTC().Format(time.RFC3339),
44
		Link: []atomLink{
45
			{Href: baseURL + "/" + repoName},
46
			{Rel: "self", Href: baseURL + "/" + repoName + "/atom.xml"},
47
		},
48
	}
49
	if len(commits) > 0 {
50
		feed.Updated = commits[0].When.UTC().Format(time.RFC3339)
51
	}
52
	for _, c := range commits {
53
		feed.Entries = append(feed.Entries, atomEntry{
54
			Title:   c.Subject,
55
			ID:      baseURL + "/" + repoName + "/commit/" + c.SHA,
56
			Updated: c.When.UTC().Format(time.RFC3339),
57
			Link:    atomLink{Href: baseURL + "/" + repoName + "/commit/" + c.SHA},
58
			Author:  atomAuthor{Name: c.Author, Email: c.Email},
59
			Summary: c.Body,
60
		})
61
	}
62
	out, err := xml.MarshalIndent(feed, "", "  ")
63
	if err != nil {
64
		return nil, err
65
	}
66
	return append([]byte(xml.Header), out...), nil
67
}