1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-26 07:33:48 +01:00
sip/modules/csv/csv.go
John Olheiser 0a6162ad05 Add release support and CSV output (#16)
Add CSV output

Refactor requireToken and add release creation

Start releases

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/16
2020-04-17 03:59:12 +00:00

94 lines
2.4 KiB
Go

package csv
import (
"fmt"
"sort"
"strings"
"code.gitea.io/sdk/gitea"
)
const TimeFormat = "Jan 2, 2006 at 03:04 PM"
func Issues(issues []*gitea.Issue) string {
headers := "ID,Index,Author,Title,Body,Labels,Milestone,Assignee,State,Created,Updated,Closed\r\n"
// Sort the issues coming in
sort.Slice(issues, func(i, j int) bool {
return issues[i].ID < issues[j].ID
})
rows := make([]string, 0)
for _, issue := range issues {
cols := make([]string, 12)
cols[0] = fmt.Sprintf("%d", issue.ID)
cols[1] = fmt.Sprintf("%d", issue.Index)
if issue.Poster != nil {
cols[2] = issue.Poster.UserName
}
cols[3] = issue.Title
cols[4] = issue.Body
labels := make([]string, 0)
for _, lbl := range issue.Labels {
labels = append(labels, lbl.Name)
}
cols[5] = strings.Join(labels, ",")
if issue.Milestone != nil {
cols[6] = issue.Milestone.Title
}
if issue.Assignee != nil {
cols[7] = issue.Assignee.UserName
}
state := string(issue.State)
if issue.PullRequest != nil && issue.PullRequest.HasMerged {
state = "merged"
}
cols[8] = state
cols[9] = issue.Created.Format(TimeFormat)
cols[10] = issue.Updated.Format(TimeFormat)
if issue.Closed != nil {
cols[11] = issue.Closed.Format(TimeFormat)
}
for idx, col := range cols {
cols[idx] = escape(col)
}
rows = append(rows, strings.Join(cols, ","))
}
return headers + strings.Join(rows, "\r\n")
}
func Releases(releases []*gitea.Release) string {
headers := "ID,Tag Name,Target,Title,Body,Draft,Pre-Release,Created,Published,Publisher\r\n"
// Sort the releases coming in
sort.Slice(releases, func(i, j int) bool {
return releases[i].ID < releases[j].ID
})
rows := make([]string, 0)
for _, release := range releases {
cols := make([]string, 10)
cols[0] = fmt.Sprintf("%d", release.ID)
cols[1] = release.TagName
cols[2] = release.Target
cols[3] = release.Title
cols[4] = release.Note
cols[5] = fmt.Sprintf("%t", release.IsDraft)
cols[6] = fmt.Sprintf("%t", release.IsPrerelease)
cols[7] = release.CreatedAt.Format(TimeFormat)
cols[8] = release.PublishedAt.Format(TimeFormat)
if release.Publisher != nil {
cols[9] = release.Publisher.UserName
}
for idx, col := range cols {
cols[idx] = escape(col)
}
rows = append(rows, strings.Join(cols, ","))
}
return headers + strings.Join(rows, "\r\n")
}
func escape(input string) string {
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(input, `"`, `""`))
}