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

     1  package build
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
     9  	quay "github.com/redhat-appstudio/image-controller/pkg/quay"
    10  )
    11  
    12  var (
    13  	quayApiUrl = "https://quay.io/api/v1"
    14  	quayOrg    = utils.GetEnv("DEFAULT_QUAY_ORG", "redhat-appstudio-qe")
    15  	quayToken  = utils.GetEnv("DEFAULT_QUAY_ORG_TOKEN", "")
    16  	quayClient = quay.NewQuayClient(&http.Client{Transport: &http.Transport{}}, quayToken, quayApiUrl)
    17  )
    18  
    19  func GetQuayImageName(annotations map[string]string) (string, error) {
    20  	type imageAnnotation struct {
    21  		Image  string `json:"Image"`
    22  		Secret string `json:"Secret"`
    23  	}
    24  	image_annotation_str := annotations["image.redhat.com/image"]
    25  	var ia imageAnnotation
    26  	err := json.Unmarshal([]byte(image_annotation_str), &ia)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	tokens := strings.Split(ia.Image, "/")
    31  	return strings.Join(tokens[2:], "/"), nil
    32  }
    33  
    34  func GetRobotAccountName(imageName string) string {
    35  	tokens := strings.Split(imageName, "/")
    36  	return strings.Join(tokens, "")
    37  }
    38  
    39  func DoesImageRepoExistInQuay(quayImageRepoName string) (bool, error) {
    40  	exists, err := quayClient.DoesRepositoryExist(quayOrg, quayImageRepoName)
    41  	if exists {
    42  		return true, nil
    43  	} else if !exists && strings.Contains(err.Error(), "does not exist") {
    44  		return false, nil
    45  	} else {
    46  		return false, err
    47  	}
    48  }
    49  
    50  func DoesRobotAccountExistInQuay(robotAccountName string) (bool, error) {
    51  	_, err := quayClient.GetRobotAccount(quayOrg, robotAccountName)
    52  	if err != nil {
    53  		if err.Error() == "Could not find robot with specified username" {
    54  			return false, nil
    55  		} else {
    56  			return false, err
    57  		}
    58  	}
    59  	return true, nil
    60  }
    61  
    62  func DeleteImageRepo(imageName string) (bool, error) {
    63  	_, err := quayClient.DeleteRepository(quayOrg, imageName)
    64  	if err != nil {
    65  		return false, err
    66  	}
    67  	return true, nil
    68  }