Skip to content

Commit

Permalink
fix: correct some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
saman2000hoseini committed Jul 12, 2023
1 parent 4fb1b6c commit 7d1ea5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
14 changes: 8 additions & 6 deletions internal/webhook-proxy/handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions internal/webhook-proxy/request/request.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 7d1ea5d

Please sign in to comment.