<commit_before>package huawei

import (
	"crypto/tls"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/goharbor/harbor/src/replication/model"
)

// FetchImages gets resources from Huawei SWR
func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error) {

	resources := []*model.Resource{}

	urls := fmt.Sprintf("%s/dockyard/v2/repositories?filter=center::self", a.registry.URL)

	r, err := http.NewRequest("GET", urls, nil)
	if err != nil {
		return resources, err
	}

	r.Header.Add("content-type", "application/json; charset=utf-8")
	encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
	r.Header.Add("Authorization", "Basic "+encodeAuth)

	client := &http.Client{}
	if a.registry.Insecure == true {
		client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		}
	}
	resp, err := client.Do(r)
	if err != nil {
		return resources, err
	}

	defer resp.Body.Close()
	code := resp.StatusCode
	if code >= 300 || code < 200 {
		body, _ := ioutil.ReadAll(resp.Body)
		return resources, fmt.Errorf("[%d][%s]", code, string(body))
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return resources, err
	}
	repos := []hwRepoQueryResult{}
	err = json.Unmarshal(body, &repos)
	if err != nil {
		return resources, err
	}
	for _, repo := range repos {
		resource := parseRepoQueryResultToResource(repo)
		resource.Registry = a.registry
		resources = append(resources, resource)
	}
	return resources, nil

}

// ManifestExist check the manifest of Huawei SWR
func (a *adapter) ManifestExist(repository, reference string) (exist bool, digest string, err error) {
	token, err := getJwtToken(a, repository)
	if err != nil {
		return exist, digest, err
	}

	urls := fmt.Sprintf("%s/v2/%s/manifests/%s", a.registry.URL, repository, reference)

	r, err := http.NewRequest("GET", urls, nil)
	if err != nil {
		return exist, digest, err
	}

	r.Header.Add("content-type", "application/json; charset=utf-8")
	r.Header.Add("Authorization", "Bearer "+token.Token)

	client := &http.Client{}
	if a.registry.Insecure == true {
		client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		}
	}
	resp, err := client.Do(r)
	if err != nil {
		return exist, digest, err
	}

	defer resp.Body.Close()
	code := resp.StatusCode
	if code >= 300 || code < 200 {
		body, _ := ioutil.ReadAll(resp.Body)
		return exist, digest, fmt.Errorf("[%d][%s]", code, string(body))
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return exist, digest, err
	}
	exist = true
	manifest := hwManifest{}
	err = json.Unmarshal(body, &manifest)
	if err != nil {
		return exist, digest, err
	}
	return exist, manifest.Config.Digest, nil
}

// DeleteManifest delete the manifest of Huawei SWR
func (a *adapter) DeleteManifest(repository, reference string) error {
	token, err := getJwtToken(a, repository)
	if err != nil {
		return err
	}

	urls := fmt.Sprintf("%s/v2/%s/manifests/%s", a.registry.URL, repository, reference)

	r, err := http.NewRequest("DELETE", urls, nil)
	if err != nil {<commit_msg>fix a small issue, that if there is no manifest in huawei image repository, should return false rather than err<commit_after>package huawei

import (
	"crypto/tls"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/goharbor/harbor/src/replication/model"
)

// FetchImages gets resources from Huawei SWR
func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error) {

	resources := []*model.Resource{}

	urls := fmt.Sprintf("%s/dockyard/v2/repositories?filter=center::self", a.registry.URL)

	r, err := http.NewRequest("GET", urls, nil)
	if err != nil {
		return resources, err
	}

	r.Header.Add("content-type", "application/json; charset=utf-8")
	encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret)))
	r.Header.Add("Authorization", "Basic "+encodeAuth)

	client := &http.Client{}
	if a.registry.Insecure == true {
		client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		}
	}
	resp, err := client.Do(r)
	if err != nil {
		return resources, err
	}

	defer resp.Body.Close()
	code := resp.StatusCode
	if code >= 300 || code < 200 {
		body, _ := ioutil.ReadAll(resp.Body)
		return resources, fmt.Errorf("[%d][%s]", code, string(body))
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return resources, err
	}
	repos := []hwRepoQueryResult{}
	err = json.Unmarshal(body, &repos)
	if err != nil {
		return resources, err
	}
	for _, repo := range repos {
		resource := parseRepoQueryResultToResource(repo)
		resource.Registry = a.registry
		resources = append(resources, resource)
	}
	return resources, nil

}

// ManifestExist check the manifest of Huawei SWR
func (a *adapter) ManifestExist(repository, reference string) (exist bool, digest string, err error) {
	token, err := getJwtToken(a, repository)
	if err != nil {
		return exist, digest, err
	}

	urls := fmt.Sprintf("%s/v2/%s/manifests/%s", a.registry.URL, repository, reference)

	r, err := http.NewRequest("GET", urls, nil)
	if err != nil {
		return exist, digest, err
	}

	r.Header.Add("content-type", "application/json; charset=utf-8")
	r.Header.Add("Authorization", "Bearer "+token.Token)

	client := &http.Client{}
	if a.registry.Insecure == true {
		client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		}
	}
	resp, err := client.Do(r)
	if err != nil {
		return exist, digest, err
	}

	defer resp.Body.Close()
	code := resp.StatusCode
	if code >= 300 || code < 200 {
		if code == 404 {
			return false, digest, nil
		}
		body, _ := ioutil.ReadAll(resp.Body)
		return exist, digest, fmt.Errorf("[%d][%s]", code, string(body))
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return exist, digest, err
	}
	exist = true
	manifest := hwManifest{}
	err = json.Unmarshal(body, &manifest)
	if err != nil {
		return exist, digest, err
	}
	return exist, manifest.Config.Digest, nil
}

// DeleteManifest delete the manifest of Huawei SWR
func (a *adapter) DeleteManifest(repository, reference string) error {
	token, err := getJwtToken(a, repository)
	if err != nil {
		return err
	}

	urls := fmt.Sprintf("%s/v2/%s/manifests/%s", a.registry.URL, repository, reference)

	r, err := http.NewRequest("DELETE", urls, nil)
	if err != nil {
{'fraction_non_alphanumeric': 0.15703971119133575, 'fraction_numerical': 0.006768953068592058, 'mean_word_length': 4.142304717710751, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '4885217', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2331, 'n_words': 580}