From 7d1ea5d254ec38b2b7dfbfcb3c158a6368e80e3b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 12 Jul 2023 12:27:42 +0330 Subject: [PATCH] fix: correct some issues --- internal/webhook-proxy/handler/proxy.go | 14 ++++++---- internal/webhook-proxy/request/request.go | 34 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 internal/webhook-proxy/request/request.go diff --git a/internal/webhook-proxy/handler/proxy.go b/internal/webhook-proxy/handler/proxy.go index e4d83cc..941a150 100644 --- a/internal/webhook-proxy/handler/proxy.go +++ b/internal/webhook-proxy/handler/proxy.go @@ -3,8 +3,10 @@ package handler import ( "bytes" "encoding/json" + "fmt" "github.com/labstack/echo/v4" "github.com/sirupsen/logrus" + "github.com/snapp-incubator/jira-element-proxy/internal/webhook-proxy/request" "io" "net/http" ) @@ -25,24 +27,24 @@ type ( ) func (p *Proxy) ProxyToElement(c echo.Context) error { - body, err := io.ReadAll(c.Request().Body) + req := &request.Jira{} + + err := c.Bind(req) if err != nil { logrus.Errorf("failed to read request body: %s", err.Error()) return c.NoContent(http.StatusBadRequest) } - logrus.Infof("jira request body: %s", string(body)) - - if p.proxyRequest(body, p.ElementURL) { + if p.proxyRequest(req, p.ElementURL) { return c.NoContent(http.StatusOK) } return c.NoContent(http.StatusInternalServerError) } -func (p *Proxy) proxyRequest(jiraBody []byte, url string) bool { +func (p *Proxy) proxyRequest(req *request.Jira, url string) bool { body, err := json.Marshal(ElementBody{ - Text: string(jiraBody), + Text: fmt.Sprintf("Issuer: %s\nURL: %s", req.User.Name, req.Issue.Fields.CustomField11401.Links.Web), DisplayName: DisplayName, }) if err != nil { diff --git a/internal/webhook-proxy/request/request.go b/internal/webhook-proxy/request/request.go new file mode 100644 index 0000000..eca180f --- /dev/null +++ b/internal/webhook-proxy/request/request.go @@ -0,0 +1,34 @@ +package request + +type Jira struct { + Timestamp int64 `json:"timestamp"` + WebhookEvent string `json:"webhookEvent"` + IssueEventTypeName string `json:"issue_event_type_name"` + User struct { + Self string `json:"self"` + Name string `json:"name"` + Key string `json:"key"` + EmailAddress string `json:"emailAddress"` + AvatarUrls struct { + Four8X48 string `json:"48x48"` + Two4X24 string `json:"24x24"` + One6X16 string `json:"16x16"` + Three2X32 string `json:"32x32"` + } `json:"avatarUrls"` + DisplayName string `json:"displayName"` + Active bool `json:"active"` + TimeZone string `json:"timeZone"` + } `json:"user"` + Issue struct { + ID string `json:"id"` + Self string `json:"self"` + Key string `json:"key"` + Fields struct { + CustomField11401 struct { + Links struct { + Web string `json:"web"` + } `json:"_links"` + } `json:"customfield_11401"` + } `json:"fields"` + } `json:"issue"` +}