2020-02-29 00:49:55 +01:00
|
|
|
package model
|
|
|
|
|
2020-03-06 19:54:52 +01:00
|
|
|
import (
|
2020-04-27 11:42:29 +02:00
|
|
|
"bytes"
|
2020-03-06 19:54:52 +01:00
|
|
|
"fmt"
|
2020-04-27 11:42:29 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2020-04-28 08:51:06 +02:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2020-03-06 19:54:52 +01:00
|
|
|
)
|
|
|
|
|
2020-04-27 11:42:29 +02:00
|
|
|
type ConfigJira struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Project string `json:"project"`
|
|
|
|
Epic string `json:"epic"`
|
|
|
|
IssueType string `json:"issue_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigGithub struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
Repo string `json:"repo"`
|
|
|
|
Labels []string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
// ToDo: add key-value extra params as a map to allow for customfield_whatever = team
|
2020-02-29 00:49:55 +01:00
|
|
|
type Campaign struct {
|
2020-04-28 11:21:31 +02:00
|
|
|
Jira ConfigJira `json:"jira"`
|
|
|
|
Github ConfigGithub `json:"github"`
|
|
|
|
Summary string `json:"summary"`
|
|
|
|
IssueTemplate string `json:"issue_template"`
|
|
|
|
FooterTemplate string `json:"footer_template"`
|
|
|
|
Tickets []*Ticket `json:"tickets,omitempty"`
|
2020-02-29 00:49:55 +01:00
|
|
|
}
|
2020-03-05 22:37:01 +01:00
|
|
|
|
2020-03-07 13:27:11 +01:00
|
|
|
func (c *Campaign) NextJiraUnpublishedTicket() *Ticket {
|
2020-03-05 22:37:01 +01:00
|
|
|
for _, ticket := range c.Tickets {
|
2020-04-27 12:22:15 +02:00
|
|
|
if !ticket.IsPublishedJira() {
|
2020-03-05 22:37:01 +01:00
|
|
|
return ticket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-06 19:54:52 +01:00
|
|
|
|
2020-03-07 13:27:11 +01:00
|
|
|
func (c *Campaign) NextGithubUnpublishedTicket() *Ticket {
|
|
|
|
for _, ticket := range c.Tickets {
|
2020-04-27 12:22:15 +02:00
|
|
|
if ticket.IsPublishedJira() && !ticket.IsPublishedGithub() {
|
2020-03-07 13:27:11 +01:00
|
|
|
return ticket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:22:15 +02:00
|
|
|
func (c *Campaign) PrintStatus() {
|
2020-04-28 08:51:06 +02:00
|
|
|
totalTickets := len(c.Tickets)
|
|
|
|
var totalPublishedJira, totalPublishedGithub int
|
|
|
|
for _, t := range c.Tickets {
|
|
|
|
if t.IsPublishedJira() {
|
|
|
|
totalPublishedJira++
|
|
|
|
if t.IsPublishedGithub() {
|
|
|
|
totalPublishedGithub++
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 19:54:52 +01:00
|
|
|
}
|
2020-04-28 08:51:06 +02:00
|
|
|
|
|
|
|
fmt.Printf("Current campaign for %s with summary\n%s\n\n", color.GreenString(c.Github.Repo), color.CyanString(c.Summary))
|
|
|
|
fmt.Printf("\t%d\ttotal tickets\n", totalTickets)
|
|
|
|
fmt.Printf("\t%d/%d\tpublished in Jira\n", totalPublishedJira, totalTickets)
|
|
|
|
fmt.Printf("\t%d/%d\tpublished in Github\n\n", totalPublishedGithub, totalPublishedJira)
|
2020-03-06 19:54:52 +01:00
|
|
|
}
|
2020-04-27 11:42:29 +02:00
|
|
|
|
|
|
|
func (c *Campaign) FillTicket(t *Ticket) error {
|
|
|
|
summaryTmpl, err := template.New("").Parse(c.Summary)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var summaryBytes bytes.Buffer
|
|
|
|
if err := summaryTmpl.Execute(&summaryBytes, t.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Summary = summaryBytes.String()
|
|
|
|
|
2020-04-28 11:21:31 +02:00
|
|
|
descriptionTemplate, err := template.ParseFiles(c.IssueTemplate)
|
2020-04-27 11:42:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptionBytes bytes.Buffer
|
|
|
|
if err := descriptionTemplate.Execute(&descriptionBytes, t.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Description = descriptionBytes.String()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Campaign) RepoComponents() (string, string) {
|
|
|
|
parts := strings.Split(c.Github.Repo, "/")
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|