vegapokerbot/internal/integration/redmine/redmine.go
2024-05-07 21:49:09 +03:00

84 lines
1.8 KiB
Go

package redmine
import (
"fmt"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/integration/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/integration/null"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/integration/redmine/api"
"regexp"
"strconv"
"strings"
)
var issueNumberMatcher = regexp.MustCompile(`(?m)\/issues\/(\d+)`)
var sprintNames = []string{"sprint", "спринт"}
type Redmine struct {
api *api.Client
}
func New(params map[string]interface{}) iface.Integration {
uri, ok := params["url"]
if !ok {
return null.New()
}
key, ok := params["key"]
if !ok {
return null.New()
}
uriString, ok := uri.(string)
if !ok {
return null.New()
}
keyString, ok := key.(string)
if !ok {
return null.New()
}
return &Redmine{api: api.New(uriString, keyString)}
}
func (r *Redmine) GetTaskInfoText(input string) string {
taskNumber := r.getTaskNumber(input)
if taskNumber == 0 {
return ""
}
task, err := r.api.Issue(taskNumber)
if err != nil {
return ""
}
sprint := r.getSprint(task.CustomFields)
if sprint != "" {
sprint = fmt.Sprintf(" - **%s**", sprint)
}
return fmt.Sprintf("[(%s) %s: %s](%s/issues/%d)%s",
task.Project.Name, task.Tracker.Name, task.Subject, r.api.URL, taskNumber, sprint)
}
func (r *Redmine) getTaskNumber(input string) uint64 {
num, err := strconv.ParseUint(input, 10, 64)
if err == nil && num > 0 {
return num
}
matches := issueNumberMatcher.FindStringSubmatch(input)
if len(matches) < 2 {
return 0
}
number, err := strconv.ParseUint(matches[1], 10, 64)
if err != nil {
return 0
}
return number
}
func (r *Redmine) getSprint(fields []api.CustomField) string {
for _, field := range fields {
for _, name := range sprintNames {
if strings.ToLower(field.Name) == name {
return field.Value
}
}
}
return ""
}