Skip to content

Commit

Permalink
Merge branch 'release/1.0.17'
Browse files Browse the repository at this point in the history
  • Loading branch information
ckalpakoglu committed Oct 12, 2021
2 parents 738b42d + b670836 commit a5f7288
Show file tree
Hide file tree
Showing 12 changed files with 1,091 additions and 449 deletions.
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -23,6 +24,11 @@ const (
userAgent = "KDT"
)

var (
ProductNotFound = errors.New("product not found")
ProjectNotFound = errors.New("project not found")
)

type Client struct {
httpClient *http.Client

Expand Down
150 changes: 150 additions & 0 deletions client/products.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright © 2019 Kondukto
*/

package client

import (
"fmt"
"net/http"
"path/filepath"
"strconv"

"github.com/kondukto-io/kdt/klog"
)

type Product struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ProjectsCount int `json:"projects_count"`
Links struct {
HTML string `json:"html"`
} `json:"links"`
}

func (p *Product) FieldsAsRow() []string {
return []string{p.Name, p.ID, strconv.Itoa(p.ProjectsCount), p.Links.HTML}
}

func (c *Client) FindProductByName(name string) (*Product, error) {
products, err := c.ListProducts(name)
if err != nil {
return nil, err
}

for _, p := range products {
if p.Name == name {
return &p, nil
}
}

return nil, ProductNotFound
}

func (c *Client) GetProductDetail(id string) (*ProductDetail, error) {
klog.Debug("retrieving product list...")

req, err := c.newRequest("GET", filepath.Join("/api/v2/products", id), nil)
if err != nil {
return nil, err
}

type getProductsResponse struct {
Product ProductDetail `json:"product"`
Error string `json:"error"`
}

var ps getProductsResponse
resp, err := c.do(req, &ps)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP response not OK : %s", ps.Error)
}

return &ps.Product, nil
}

func (c *Client) ListProducts(name string) ([]Product, error) {
products := make([]Product, 0)

klog.Debug("retrieving product list...")

req, err := c.newRequest("GET", "/api/v2/products", nil)
if err != nil {
return products, err
}

queryParams := req.URL.Query()
queryParams.Add("name", name)
req.URL.RawQuery = queryParams.Encode()

type getProductsResponse struct {
Products []Product `json:"products"`
Total int `json:"total"`
Error string `json:"error"`
}

var ps getProductsResponse
resp, err := c.do(req, &ps)
if err != nil {
return products, err
}

if resp.StatusCode != http.StatusOK {
return products, fmt.Errorf("HTTP response not OK : %s", ps.Error)
}

return ps.Products, nil
}

type ProductDetail struct {
ID string `json:"id"`
Name string `json:"name"`
Projects []Project `json:"projects"`
}

func (c *Client) CreateProduct(pd ProductDetail) (*Product, error) {
klog.Debug("creating a product")

req, err := c.newRequest(http.MethodPost, "/api/v2/products", pd)
if err != nil {
return nil, err
}

type productResponse struct {
Product Product `json:"product"`
Message string `json:"message"`
}
var pr productResponse
_, err = c.do(req, &pr)
if err != nil {
return nil, err
}

return &pr.Product, nil
}

func (c *Client) UpdateProduct(id string, pd ProductDetail) (*Product, error) {
klog.Debug("updating a product")

req, err := c.newRequest(http.MethodPatch, filepath.Join("/api/v2/products", id), pd)
if err != nil {
return nil, err
}

type productResponse struct {
Product Product `json:"product"`
Message string `json:"message"`
}
var pr productResponse
_, err = c.do(req, &pr)
if err != nil {
return nil, err
}

return &pr.Product, nil
}
21 changes: 18 additions & 3 deletions client/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (p *Project) LabelsAsString() string {
return l
}

func (c *Client) ListProjects(search, alm string) ([]Project, error) {
func (c *Client) ListProjects(name, repo string) ([]Project, error) {
projects := make([]Project, 0)

klog.Debug("retrieving project list...")
Expand All @@ -51,8 +51,8 @@ func (c *Client) ListProjects(search, alm string) ([]Project, error) {
}

queryParams := req.URL.Query()
queryParams.Add("search", search)
queryParams.Add("alm", alm)
queryParams.Add("name", name)
queryParams.Add("alm", repo)
req.URL.RawQuery = queryParams.Encode()

type getProjectsResponse struct {
Expand All @@ -74,6 +74,21 @@ func (c *Client) ListProjects(search, alm string) ([]Project, error) {
return ps.Projects, nil
}

func (c *Client) FindProjectByName(name string) (*Project, error) {
projects, err := c.ListProjects(name, "")
if err != nil {
return nil, err
}

for _, p := range projects {
if p.Name == name {
return &p, nil
}
}

return nil, ProjectNotFound
}

type ProjectDetail struct {
Source ProjectSource `json:"source"`
Team ProjectTeam `json:"team"`
Expand Down
61 changes: 52 additions & 9 deletions client/scanparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"

"github.com/google/go-querystring/query"
"github.com/kondukto-io/kdt/klog"
Expand All @@ -26,17 +27,37 @@ type (
PR bool `url:"pr"`
}
ScanparamResponse struct {
Data []ScanparamsDetail `json:"data"`
Total int `json:"total"`
Scanparams []Scanparams `json:"scanparams"`
Limit int `json:"limit"`
Start int `json:"start"`
Total int `json:"total"`
}
ScanparamsDetail struct {
Id string `json:"id"`
Scanparams struct {
ID string `json:"id"`
Branch string `json:"branch"`
BindName string `json:"bind_name"`
}

ScanparamsDetail struct {
Tool *ScanparamsItem `json:"tool"`
Project *ScanparamsItem `json:"project"`
Agent *ScanparamsItem `json:"agent"`
BindName string `json:"bind_name"`
Branch string `json:"branch"`
ScanType string `json:"scan_type"`
MetaData string `json:"meta_data"`
ForkScan bool `json:"fork_scan"`
PR PRInfo `json:"pr"`
Manual bool `json:"manual"`
Custom *Custom `json:"custom"`
}

ScanparamsItem struct {
ID string `json:"id,omitempty"`
}
)

func (c *Client) FindScanparams(project string, params *ScanparamSearchParams) (*ScanparamsDetail, error) {
func (c *Client) FindScanparams(project string, params *ScanparamSearchParams) (*Scanparams, error) {
klog.Debugf("retrieving scanparams")
if project == "" {
return nil, errors.New("missing project identifier")
Expand All @@ -54,15 +75,37 @@ func (c *Client) FindScanparams(project string, params *ScanparamSearchParams) (
}
req.URL.RawQuery = v.Encode()

var scanparams ScanparamResponse
_, err = c.do(req, &scanparams)
var scanparamsResponse ScanparamResponse
_, err = c.do(req, &scanparamsResponse)
if err != nil {
return nil, err
}

if scanparams.Total == 0 {
if scanparamsResponse.Total == 0 {
return nil, errors.New("scanparams not found")
}

return &scanparams.Data[0], nil
return &scanparamsResponse.Scanparams[0], nil
}

func (c *Client) CreateScanparams(pID string, sp ScanparamsDetail) (*Scanparams, error) {
klog.Debug("creating a project")

req, err := c.newRequest(http.MethodPost, filepath.Join("/api/v2/projects", pID, "scanparams"), sp)
if err != nil {
return nil, err
}

type scanparamsResponse struct {
Scanparams Scanparams `json:"scanparams"`
Message string `json:"message"`
}

var pr scanparamsResponse
_, err = c.do(req, &pr)
if err != nil {
return nil, err
}

return &pr.Scanparams, nil
}
25 changes: 10 additions & 15 deletions client/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,18 @@ func (c *Client) RestartScanWithOption(id string, opt *ScanPROptions) (string, e
return rsr.Event, nil
}

func (c *Client) ScanByImage(project, branch, tool, image string) (string, error) {
path := "/api/v1/scans/image"
type ImageScanParams struct {
Project string `json:"project"`
Tool string `json:"tool"`
Branch string `json:"branch"`
Image string `json:"image"`
MetaData string `json:"meta_data"`
}

type imageScanBody struct {
Project string
Tool string
Branch string
Image string
}
reqBody := imageScanBody{
Project: project,
Tool: tool,
Branch: branch,
Image: image,
}
func (c *Client) ScanByImage(pr *ImageScanParams) (string, error) {
path := "/api/v1/scans/image"

req, err := c.newRequest(http.MethodPost, path, &reqBody)
req, err := c.newRequest(http.MethodPost, path, pr)
if err != nil {
return "", fmt.Errorf("failed to create HTTP request: %w", err)
}
Expand Down
Loading

0 comments on commit a5f7288

Please sign in to comment.