diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 529bee1..8091386 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -4,10 +4,29 @@ on: push: branches: [ main ] tags: [ v* ] - pull_request: - branches: [ main ] jobs: + lint: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: latest + test: + name: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + go-version: '1.19' + - run: go test -v ./... -covermode=atomic -coverprofile=coverage.out + - uses: codecov/codecov-action@v1 + with: + files: coverage.out docker: name: docker runs-on: ubuntu-latest @@ -39,4 +58,5 @@ jobs: platforms: linux/amd64 push: true tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} + diff --git a/deployments/webhook-proxy/templates/route.yaml b/deployments/webhook-proxy/templates/route.yaml index 03555d5..13fde6f 100644 --- a/deployments/webhook-proxy/templates/route.yaml +++ b/deployments/webhook-proxy/templates/route.yaml @@ -6,7 +6,7 @@ metadata: spec: host: webhook-proxy.apps.private.{{ .Values.region_hostname }} port: - targetPort: http + targetPort: 8080 to: kind: Service name: webhook-proxy diff --git a/deployments/webhook-proxy/values.yaml b/deployments/webhook-proxy/values.yaml index c8a03d6..df77afa 100644 --- a/deployments/webhook-proxy/values.yaml +++ b/deployments/webhook-proxy/values.yaml @@ -1,12 +1,12 @@ replicaCount: 1 image: - repository: quay.io/snapp/webhook-proxy + repository: ghcr.io/snapp-incubator/jira-element-proxy pullPolicy: Always - tag: "v1.0" + tag: "main@sha256:afb77917d7186242ace86974cc7276974a2d3efb98dd90462b1614902a085bdf" service: - port: 80 + port: 8080 resources: limits: diff --git a/go.mod b/go.mod index 52ed424..ef7d168 100644 --- a/go.mod +++ b/go.mod @@ -23,8 +23,8 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect - golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect - golang.org/x/sys v0.0.0-20220906165534-d0df966e6959 // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sys v0.5.0 // indirect + golang.org/x/text v0.7.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ac3727f..f998472 100644 --- a/go.sum +++ b/go.sum @@ -297,8 +297,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= -golang.org/x/net v0.0.0-20220906165146-f3363e06e74c h1:yKufUcDwucU5urd+50/Opbt4AYpqthk7wHpHok8f1lo= -golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -347,8 +347,8 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220906165534-d0df966e6959 h1:qSa+Hg9oBe6UJXrznE+yYvW51V9UbyIj/nj/KpDigo8= -golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -356,8 +356,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/webhook-proxy/handler/proxy.go b/internal/webhook-proxy/handler/proxy.go index da9529d..4323f1a 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" ) @@ -18,44 +20,46 @@ type ( ElementURL string } - elementBody struct { + ElementBody struct { Text string `json:"text"` DisplayName string `json:"displayName"` } ) 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(generateElementText(req), p.ElementURL) { return c.NoContent(http.StatusOK) } return c.NoContent(http.StatusInternalServerError) } -func (p *Proxy) proxyRequest(body []byte, url string) bool { - body, err := json.Marshal(elementBody{ - Text: string(body), +func (p *Proxy) proxyRequest(txt string, url string) bool { + body, err := json.Marshal(ElementBody{ + Text: txt, DisplayName: DisplayName, }) if err != nil { - logrus.Errorf("proxy request to element error: %s", err) + logrus.Errorf("marshal request body error: %s", err) return false } proxyReq, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) if err != nil { - logrus.Errorf("proxy request to element error: %s", err) + logrus.Errorf("create proxy request error: %s", err) return false } + proxyReq.Header.Add("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(proxyReq) if err != nil { logrus.Errorf("proxy request to element error: %s", err) @@ -68,6 +72,18 @@ func (p *Proxy) proxyRequest(body []byte, url string) bool { } responseBody, err := io.ReadAll(resp.Body) - logrus.Errorf("proxy request to element error: %s", responseBody) + if err != nil { + logrus.Errorf("element response body read error: %s", err) + return false + } + + logrus.Errorf("element response body read error: %s", responseBody) return false } + +func generateElementText(req *request.Jira) string { + return fmt.Sprintf( + "Type: %s\nSummary: %s\nIssuer: %s\nURL: %s", + req.Issue.Fields.RequestType.Name, req.Issue.Fields.Summary, req.User.Name, + req.Issue.Fields.CustomField11401.Links.Web) +} diff --git a/internal/webhook-proxy/request/request.go b/internal/webhook-proxy/request/request.go new file mode 100644 index 0000000..9744317 --- /dev/null +++ b/internal/webhook-proxy/request/request.go @@ -0,0 +1,43 @@ +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"` + RequestType struct { + Name string `json:"name"` + Description string `json:"description"` + } `json:"requestType"` + Summary string `json:"summary"` + IssueType struct { + Description string `json:"description"` + Name string `json:"name"` + } `json:"issuetype"` + } `json:"fields"` + } `json:"issue"` +}