github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/utils/pipeline/results.go (about)

     1  package pipeline
     2  
     3  import (
     4  	"crypto/tls"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"time"
    11  )
    12  
    13  type ResultClient struct {
    14  	BaseURL    string
    15  	HTTPClient *http.Client
    16  	Token      string
    17  }
    18  
    19  func NewClient(url, token string) *ResultClient {
    20  	return &ResultClient{
    21  		BaseURL: url,
    22  		HTTPClient: &http.Client{
    23  			Timeout: time.Minute,
    24  			Transport: &http.Transport{
    25  				TLSClientConfig: &tls.Config{
    26  					InsecureSkipVerify: true,
    27  				},
    28  			},
    29  		},
    30  		Token: token,
    31  	}
    32  }
    33  
    34  func (c *ResultClient) sendRequest(path string) (body []byte, err error) {
    35  	requestURL := fmt.Sprintf("%s/%s", c.BaseURL, path)
    36  	req, err := http.NewRequest(http.MethodGet, requestURL, nil)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	req.Header.Set("Content-Type", "application/json; charset=utf-8")
    41  	req.Header.Set("Accept", "application/json; charset=utf-8")
    42  	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
    43  
    44  	res, err := c.HTTPClient.Do(req)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	body, err = io.ReadAll(res.Body)
    50  	if res.StatusCode != http.StatusOK {
    51  		return nil, fmt.Errorf("failed to access Tekton Result Service with status code: %d and\nbody: %s", res.StatusCode, string(body))
    52  	}
    53  
    54  	defer res.Body.Close()
    55  
    56  	return body, err
    57  }
    58  
    59  func (c *ResultClient) GetRecords(namespace, resultId string) (*Records, error) {
    60  	path := fmt.Sprintf("apis/results.tekton.dev/v1alpha2/parents/%s/results/%s/records", namespace, resultId)
    61  
    62  	body, err := c.sendRequest(path)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	var records *Records
    67  	err = json.Unmarshal(body, &records)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return records, nil
    72  }
    73  
    74  func (c *ResultClient) GetLogs(namespace, resultId string) (*Logs, error) {
    75  	path := fmt.Sprintf("apis/results.tekton.dev/v1alpha2/parents/%s/results/%s/logs", namespace, resultId)
    76  
    77  	body, err := c.sendRequest(path)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	var logs *Logs
    83  	err = json.Unmarshal(body, &logs)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return logs, nil
    88  }
    89  
    90  func (c *ResultClient) GetLogByName(logName string) (string, error) {
    91  	path := fmt.Sprintf("apis/results.tekton.dev/v1alpha2/parents/%s", logName)
    92  
    93  	body, err := c.sendRequest(path)
    94  	if err != nil {
    95  		return "", err
    96  	}
    97  
    98  	var result *Result
    99  
   100  	err = json.Unmarshal(body, &result)
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  	data, err := base64.StdEncoding.DecodeString(result.Result.Data)
   105  	if err != nil {
   106  		return "", err
   107  	}
   108  	return string(data), nil
   109  }
   110  
   111  type Record struct {
   112  	Name string `json:"name"`
   113  	ID   string `json:"id"`
   114  	UID  string `json:"uid"`
   115  }
   116  
   117  type Records struct {
   118  	Record []Record `json:"records"`
   119  }
   120  
   121  type Log struct {
   122  	Name string `json:"name"`
   123  	ID   string `json:"id"`
   124  	UID  string `json:"uid"`
   125  }
   126  type Logs struct {
   127  	Record []Record `json:"records"`
   128  }
   129  
   130  type Result struct {
   131  	Result Data `json:"result"`
   132  }
   133  
   134  type Data struct {
   135  	Name string `json:"name"`
   136  	Data string `json:"data"`
   137  }