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
| package sender
import ( "github.com/andygrunwald/go-jira" "github.com/didi/nightingale/v5/src/models" "github.com/didi/nightingale/v5/src/pkg/tplx" "github.com/didi/nightingale/v5/src/server/config" "github.com/toolkits/pkg/logger" "html/template" "io/ioutil" "path" )
func JiraSender(ctx *models.AlertCurEvent) { title := ctx.RuleName tplpath := path.Join(config.C.Alerting.TemplatesDir, "jira.tpl") tpl, err := template.New("jira.tpl").Funcs(tplx.TemplateFuncMap).ParseFiles(tplpath) if err != nil { logger.Errorf("failed to parse tpl: " + tplpath) return } message := BuildTplMessage(tpl, ctx) auth := jira.BasicAuthTransport{ Username: config.C.Jira.BasicAuthUser, Password: config.C.Jira.BasicAuthPass, } jiraClient, err := jira.NewClient(auth.Client(), config.C.Jira.Url) if err != nil { logger.Errorf("jira_sender: result=fail" + err.Error()) return } owner := ctx.GetTagValue("owner") if owner == "" { return } issue := jira.Issue{ Fields: &jira.IssueFields{ Assignee: &jira.User{ Name: owner, }, Description: message, Type: jira.IssueType{ Name: config.C.Jira.IssueType, }, Project: jira.Project{ Name: config.C.Jira.ProjectName, ID: config.C.Jira.ProjectId, }, Summary: title, }, } _, res, err := jiraClient.Issue.Create(&issue) if err != nil { response, err := ioutil.ReadAll(res.Body) logger.Errorf("jira_sender: result=fail", err, string(response)) return } else { logger.Infof("jira_sender: result=succ %s: %+v") } }
|