<commit_before>package apb

import (
	b64 "encoding/base64"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strings"

	logging "github.com/op/go-logging"
	yaml "gopkg.in/yaml.v2"
)

// RHCCRegistry - Red Hat Container Catalog Registry
type RHCCRegistry struct {
	config RegistryConfig
	log    *logging.Logger
}

// Image - RHCC Registry Image that is returned from the RHCC Catalog api.
type Image struct {
	Description  string `json:"description"`
	IsOfficial   bool   `json:"is_official"`
	IsTrusted    bool   `json:"is_trusted"`
	Name         string `json:"name"`
	ShouldFilter bool   `json:"should_filter"`
	StarCount    int    `json:"star_count"`
}

// ImageResponse - RHCC Registry Image Response returned for the RHCC Catalog api
type ImageResponse struct {
	NumResults int      `json:"num_results"`
	Query      string   `json:"query"`
	Results    []*Image `json:"results"`
}

// Init - Initialize the Red Hat Container Catalog
func (r *RHCCRegistry) Init(config RegistryConfig, log *logging.Logger) error {
	log.Debug("RHCCRegistry::Init")
	r.config = config
	r.log = log
	return nil
}

// This function is used because our code expects an HTTP Url for talking to RHCC
func (r RHCCRegistry) cleanHTTPURL(url string) string {
	if strings.HasPrefix(url, "http://") == true {
		return url
	}

	if strings.HasPrefix(url, "https://") == true {
		return url
	}

	url = "http://" + url
	return url
}

// LoadSpecs - Load Red Hat Container Catalog specs
func (r RHCCRegistry) LoadSpecs() ([]*Spec, int, error) {
	r.log.Debug("RHCCRegistry::LoadSpecs")
	var specs []*Spec

	imageList, err := r.LoadImages("*-apb")
	if err != nil {
		return []*Spec{}, 0, err
	}

	numResults := imageList.NumResults
	r.log.Debug("Found %d images in RHCC", numResults)
	for _, image := range imageList.Results {
		spec, err := r.imageToSpec(image)
		if err != nil {
			return []*Spec{}, 0, err
		}
		specs = append(specs, spec)
	}

	return specs, numResults, nil
}

func (r RHCCRegistry) imageToSpec(image *Image) (*Spec, error) {
	r.log.Debug("RHCCRegistry::imageToSpec")
	_spec := &Spec{}
	url := r.cleanHTTPURL(r.config.URL)

	req, err := http.NewRequest("GET", url+"/v2/"+image.Name+"manifests/latest", nil)
	if err != nil {
		return nil, err
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	type label struct {
		Spec    string `json:"com.redhat.apb.spec"`
		Version string `json:"com.redhat.apb.version"`
	}

	type config struct {
		Label label `json:"Labels"`
	}

	hist := struct {
		History []map[string]string `json:"history"`
	}{}

	conf := struct {
		Config *config `json:"config"`
	}{}

	err = json.NewDecoder(resp.Body).Decode(&hist)
	if err != nil {<commit_msg>Fixes BZ#1466031 add Accept header with application/json to RHCC get (#243) (#246)<commit_after>package apb

import (
	b64 "encoding/base64"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strings"

	logging "github.com/op/go-logging"
	yaml "gopkg.in/yaml.v2"
)

// RHCCRegistry - Red Hat Container Catalog Registry
type RHCCRegistry struct {
	config RegistryConfig
	log    *logging.Logger
}

// Image - RHCC Registry Image that is returned from the RHCC Catalog api.
type Image struct {
	Description  string `json:"description"`
	IsOfficial   bool   `json:"is_official"`
	IsTrusted    bool   `json:"is_trusted"`
	Name         string `json:"name"`
	ShouldFilter bool   `json:"should_filter"`
	StarCount    int    `json:"star_count"`
}

// ImageResponse - RHCC Registry Image Response returned for the RHCC Catalog api
type ImageResponse struct {
	NumResults int      `json:"num_results"`
	Query      string   `json:"query"`
	Results    []*Image `json:"results"`
}

// Init - Initialize the Red Hat Container Catalog
func (r *RHCCRegistry) Init(config RegistryConfig, log *logging.Logger) error {
	log.Debug("RHCCRegistry::Init")
	r.config = config
	r.log = log
	return nil
}

// This function is used because our code expects an HTTP Url for talking to RHCC
func (r RHCCRegistry) cleanHTTPURL(url string) string {
	if strings.HasPrefix(url, "http://") == true {
		return url
	}

	if strings.HasPrefix(url, "https://") == true {
		return url
	}

	url = "http://" + url
	return url
}

// LoadSpecs - Load Red Hat Container Catalog specs
func (r RHCCRegistry) LoadSpecs() ([]*Spec, int, error) {
	r.log.Debug("RHCCRegistry::LoadSpecs")
	var specs []*Spec

	imageList, err := r.LoadImages("*-apb")
	if err != nil {
		return []*Spec{}, 0, err
	}

	numResults := imageList.NumResults
	r.log.Debug("Found %d images in RHCC", numResults)
	for _, image := range imageList.Results {
		spec, err := r.imageToSpec(image)
		if err != nil {
			return []*Spec{}, 0, err
		}
		specs = append(specs, spec)
	}

	return specs, numResults, nil
}

func (r RHCCRegistry) imageToSpec(image *Image) (*Spec, error) {
	r.log.Debug("RHCCRegistry::imageToSpec")
	_spec := &Spec{}
	url := r.cleanHTTPURL(r.config.URL)

	req, err := http.NewRequest("GET", url+"/v2/"+image.Name+"manifests/latest", nil)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Accept", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	type label struct {
		Spec    string `json:"com.redhat.apb.spec"`
		Version string `json:"com.redhat.apb.version"`
	}

	type config struct {
		Label label `json:"Labels"`
	}

	hist := struct {
		History []map[string]string `json:"history"`
	}{}

	conf := struct {
		Config *config `json:"config"`
	}{}

	err = json.NewDecoder(resp.Body).Decode(&hist)
	if err != nil {
{'fraction_non_alphanumeric': 0.14223826714801444, 'fraction_numerical': 0.005234657039711191, 'mean_word_length': 3.8098958333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625722', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1922, 'n_words': 539}