diff --git a/src-plugins/jira/jira.go b/src-plugins/jira/jira.go new file mode 100644 index 0000000..abea517 --- /dev/null +++ b/src-plugins/jira/jira.go @@ -0,0 +1,78 @@ +package main + +import ( + "os" + "io/ioutil" + "encoding/json" + jira "gopkg.in/andygrunwald/go-jira.v1" + "fmt" +) + +type API struct { + Username string `json:"username"` + Password string `json:"password"` + Server string `json:"server"` + Project string `json:"project"` + Assignee string `json:"assignee"` +} + +type AlertText struct { + Html string `json:"html"` + PlainText string `json:"plain"` +} + +type AlertAPI struct { + Text AlertText `json:"text"` + Settings API `json:"settings"` +} + +func readAPI(path string) AlertAPI { + var api AlertAPI + tmp, err := ioutil.ReadFile(os.Args[1]) + if err != nil { fmt.Println("Cannot read API file: ", path) ; os.Exit(1) } //cannot read input JSON + err = json.Unmarshal(tmp, &api) + if err != nil { fmt.Println("Cannot read API JSON: ", path) ; os.Exit(1) } //cannot read input JSON + return api +} + +func main() { + // Parse the input API + api := readAPI(os.Args[1]) + tp := jira.BasicAuthTransport{ + Username: api.Settings.Username, + Password: api.Settings.Password, + } + + jiraClient, err := jira.NewClient(tp.Client(), api.Settings.Server) + if err != nil { + panic(err) + } + + i := jira.Issue{ + Fields: &jira.IssueFields{ + Assignee: &jira.User{ + Name: api.Settings.Assignee, + }, + Reporter: &jira.User{ + Name: api.Settings.Username, + }, + Description: "Test Issue", + Type: jira.IssueType{ + Name: "Bug", + }, + Project: jira.Project{ + Key: api.Settings.Project, + }, + Summary: api.Text.PlainText, + }, + } + issue, response, err := jiraClient.Issue.Create(&i) + if err != nil { + fmt.Printf(err.Error()) + body, _ := ioutil.ReadAll(response.Body) + fmt.Println(string(body)) + panic(err) + } + + fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary) +} diff --git a/src-plugins/jira/manifest.json b/src-plugins/jira/manifest.json new file mode 100644 index 0000000..c91b6b4 --- /dev/null +++ b/src-plugins/jira/manifest.json @@ -0,0 +1,47 @@ +{ + "name": "jira", + "summary": "Jira service integration", + "description": "Forward alerts to Jira as new tickets.", + "icon_url": "", + "version": "1.0", + "date_released": "2020-02-08", + "tags": ["jira", "ticketing", "service", "issue", "tracker"], + "maintainer": [ + { "name": "Aaron Ervin", "email": "aervin@ixsystems.com" }, + { "name": "iXsystems", "email": "support@ixsystems.com", "site_url": "http://ixsystems.com" } + ], + "exec": "jira", + "depends": [], + "api": [ + { + "fieldname": "username", + "summary": "Jira user", + "value": { "type": "string" }, + "is_required": true + }, + { + "fieldname": "password", + "summary": "User password", + "value": { "type": "string" }, + "is_required": true + }, + { + "fieldname": "server", + "summary": "Jira server address", + "value": { "type": "string" }, + "is_required": true + }, + { + "fieldname": "project", + "summary": "Jira project prefix", + "value": { "type": "string" }, + "is_required": true + }, + { + "fieldname": "assignee", + "summary": "Username of issue assignee", + "value": { "type": "string" }, + "is_required": true + } + ] +}