-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticket_format.go
85 lines (68 loc) · 2.28 KB
/
ticket_format.go
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
package main
import (
"bytes"
"text/template"
"time"
"github.com/Ajnasz/sfapi"
)
// TicketFormatterData Stores information to create Github body
type TicketFormatterData struct {
SFTicket *sfapi.Ticket
Project string
Category string
Imported time.Time
}
const ticketTemplate = `Imported from SourceForge on {{.Imported | formatDate "2006-01-02 15:04:05"}}
Created by **[{{.SFTicket.ReportedBy}}](https://sourceforge.net/u/{{.SFTicket.ReportedBy}}/)** on {{.SFTicket.CreatedTime | formatDate "2006-01-02 15:04:05"}}
Original: https://sourceforge.net/p/{{.Project}}/{{.Category}}/{{.SFTicket.TicketNum}}
---
{{.SFTicket.Description}}
{{ if (gt (len .SFTicket.Attachments) 0) }}Attachments:
{{ range .SFTicket.Attachments}}- {{.URL}}
{{ end }}
{{ end }}
`
type CommentFormatterData struct {
Project string
Category string
Imported time.Time
SFComment *sfapi.DiscussionPost
SFTicket *sfapi.Ticket
}
const commentTemplate = `
Imported from SourceForge on {{.Imported | formatDate "2006-01-02 15:04:05"}}
Created by **[{{ .SFComment.Author }}](https://sourceforge.net/u/{{.SFComment.Author}}/)** on {{ .SFComment.TimestampTime | formatDate "2006-01-02 15:04:05" }}
Original: https://sourceforge.net/p/{{ .Project }}/{{ .Category }}/{{ .SFTicket.TicketNum }}/#{{ .SFComment.Slug }}
---
{{ if (ne (printf "#%d %s" .SFTicket.TicketNum .SFTicket.Summary) .SFComment.Subject)}}
*{{ .SFComment.Subject }}*
{{ .SFComment.Text }}
{{ else }}
{{ .SFComment.Text }}
{{ end }}
{{ if (gt (len .SFComment.Attachments) 0) }}Attachments:
{{ range .SFComment.Attachments }}- {{ .URL }}
{{ end }}
{{ end }}
`
// FormatTicket Generates Github ticket body
func formatTpl(name string, templateString string, data interface{}) (string, error) {
funcMap := template.FuncMap{
"formatDate": func(format string, t time.Time) string {
return t.Format(format)
},
}
tpl, err := template.New(name).Funcs(funcMap).Parse(templateString)
if err != nil {
return "", err
}
var buf bytes.Buffer
tpl.Execute(&buf, data)
return buf.String(), nil
}
func FormatTicket(templateString string, data TicketFormatterData) (string, error) {
return formatTpl("ticket", templateString, data)
}
func FormatComment(templateString string, data CommentFormatterData) (string, error) {
return formatTpl("comment", templateString, data)
}