Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Add debug parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Jul 2, 2018
1 parent d032ba0 commit 4d78301
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"`
}

Expand Down
14 changes: 12 additions & 2 deletions server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type requestParams struct {
username string
icon string
dialect message.Dialect
debug bool
}

func parseRequestParams(r *http.Request) (*requestParams, error) {
Expand All @@ -41,26 +42,32 @@ 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)
log.Print(e)
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)
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion testdata/post_jira_events.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/sh -xe -o pipefail
#!/bin/bash
set -x
set -e
set -o pipefail

base_dir="`dirname $0`"

Expand Down

0 comments on commit 4d78301

Please sign in to comment.