github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/utils/pipeline/results.go (about)

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