From 4d78301baa8a6ead36285cdfad659fe32962361a Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Mon, 2 Jul 2018 14:38:51 +0900 Subject: [PATCH] Add debug parameter --- README.md | 1 + formatter/formatter.go | 2 +- jira/jira.go | 20 ++++++++++---------- server/webhook.go | 14 ++++++++++++-- testdata/post_jira_events.sh | 5 ++++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 20c0b3e..2439b91 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Name | Value | Example value `username` | Username of the BOT | `JIRA` `icon` | Icon emoji or URL of the BOT | `:speech_baloon:` or `http://.../jira.png` `dialect` | Slack API dialect (Default to `slack`) | `slack` or `mattermost` +`debug` | Dump JIRA and Slack messages to console (Default to `0`) | `0` or `1` For example: diff --git a/formatter/formatter.go b/formatter/formatter.go index 8ff1344..5875816 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -90,7 +90,7 @@ func (f *Formatter) JIRAEventToSlackMessage(event *jira.Event) *message.Message // title returns a message title for the JIRA event. func (f *Formatter) title(event *jira.Event, verb string, additionalMentions string) string { switch { - case event.Issue.Fields.Assignee == nil: + case event.Issue.Fields.Assignee.Name == "": return fmt.Sprintf("%s %s the issue: %s", f.Dialect.Mention(event.User.Name), verb, diff --git a/jira/jira.go b/jira/jira.go index b9b730f..b1f7757 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -7,13 +7,13 @@ import ( // Event is a JIRA event sent from a JIRA webhook. type Event struct { - WebhookEvent string `json:"webhookEvent"` - IssueEventType string `json:"issue_event_type_name"` - Timestamp int64 `json:"timestamp"` - User *User `json:"user"` - Issue *Issue `json:"issue"` - Comment *Comment `json:"comment"` - Changelog *Changelog `json:"changelog"` + WebhookEvent string `json:"webhookEvent"` + IssueEventType string `json:"issue_event_type_name"` + Timestamp int64 `json:"timestamp"` + User User `json:"user"` + Issue Issue `json:"issue"` + Comment Comment `json:"comment"` + Changelog Changelog `json:"changelog"` } // IsIssueCreated returns true when an issue is created @@ -33,7 +33,7 @@ func (s *Event) IsIssueAssigned() bool { // IsIssueFieldUpdated is sent when the issue is updated func (s *Event) IsIssueFieldUpdated(fields ...string) bool { - return s.WebhookEvent == "jira:issue_updated" && s.Changelog != nil && s.Changelog.ContainsField(fields...) + return s.WebhookEvent == "jira:issue_updated" && s.Changelog.ContainsField(fields...) } // IsIssueDeleted is sent when an issue is deleted @@ -55,10 +55,10 @@ type User struct { type Issue struct { Key string `json:"key"` Self string `json:"self"` - Fields *struct { + Fields struct { Summary string `json:"summary"` Description string `json:"description"` - Assignee *User `json:"assignee"` + Assignee User `json:"assignee"` } `json:"fields"` } diff --git a/server/webhook.go b/server/webhook.go index a6cdf42..03b6917 100644 --- a/server/webhook.go +++ b/server/webhook.go @@ -20,6 +20,7 @@ type requestParams struct { username string icon string dialect message.Dialect + debug bool } func parseRequestParams(r *http.Request) (*requestParams, error) { @@ -41,19 +42,21 @@ func parseRequestParams(r *http.Request) (*requestParams, error) { default: return nil, fmt.Errorf("dialect must be slack (default) or mattermost") } + switch q.Get("debug") { + case "1": + p.debug = true + } return p, nil } func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - p, err := parseRequestParams(r) if err != nil { log.Print(err) http.Error(w, err.Error(), http.StatusBadRequest) return } - var event jira.Event if err := json.NewDecoder(r.Body).Decode(&event); err != nil { e := fmt.Sprintf("Could not decode the request body: %s", err) @@ -61,6 +64,10 @@ func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, e, http.StatusBadRequest) return } + if p.debug { + log.Printf("Received parameters %+v", p) + log.Printf("Received event %+v", &event) + } f := formatter.New(p.dialect) m := f.JIRAEventToSlackMessage(&event) @@ -75,6 +82,9 @@ func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } else { m.IconEmoji = p.icon } + if p.debug { + log.Printf("Sending %+v", m) + } if err := message.Send(p.webhook, m); err != nil { e := fmt.Sprintf("Could not send the message to Slack: %s", err) log.Print(e) diff --git a/testdata/post_jira_events.sh b/testdata/post_jira_events.sh index 6f2bb23..f2f2636 100755 --- a/testdata/post_jira_events.sh +++ b/testdata/post_jira_events.sh @@ -1,4 +1,7 @@ -#!/bin/sh -xe -o pipefail +#!/bin/bash +set -x +set -e +set -o pipefail base_dir="`dirname $0`"