Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix catalog w/signoff" #1970

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 18 additions & 58 deletions assets/artifact-hub-pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ func main() {
os.Exit(1)
}

patterns, err := fetchCatalogPatterns()
catalogPatterns, err := fetchCatalogPatterns()
if err != nil {
log.Error(err)
return
}

for _, pattern := range patterns {
var patterns struct {
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.Unmarshal(catalogPatterns, &patterns); err != nil {
log.Error(utils.ErrUnmarshal(err))
return
}

for _, pattern := range patterns.Patterns {
if err := processPattern(pattern, token); err != nil {
log.Error(meshkitErrors.New(ErrProcessPatternCode, meshkitErrors.Alert,
[]string{"unable to process catalog pattern"},
Expand All @@ -99,46 +107,18 @@ func slugify(name string) string {

return s
}
func fetchCatalogPatterns() ([]CatalogPattern, error) {
// First request to get total count
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern?page=0&pagesize=10", mesheryCloudBaseURL))
func fetchCatalogPatterns() ([]byte, error) {
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern", mesheryCloudBaseURL))
if err != nil {
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?page=0&pagesize=10", mesheryCloudBaseURL))
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?trim=true", mesheryCloudBaseURL))
}
defer resp.Body.Close()

var initialResponse struct {
TotalCount int `json:"total_count"`
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.NewDecoder(resp.Body).Decode(&initialResponse); err != nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, ErrReadRespBody(err)
}

totalPages := (initialResponse.TotalCount + 9) / 10 // Calculate number of pages (rounding up)

// Store all patterns from all pages
allPatterns := initialResponse.Patterns

// Fetch patterns from remaining pages
for page := 1; page <= totalPages; page++ {
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern?page=%d&pagesize=10", mesheryCloudBaseURL, page))
if err != nil {
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?page=%d&pagesize=10", mesheryCloudBaseURL, page))
}
defer resp.Body.Close()

var pageResponse struct {
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.NewDecoder(resp.Body).Decode(&pageResponse); err != nil {
return nil, ErrReadRespBody(err)
}

allPatterns = append(allPatterns, pageResponse.Patterns...)
}

return allPatterns, nil
return body, nil
}

func processPattern(pattern CatalogPattern, token string) error {
Expand Down Expand Up @@ -229,30 +209,10 @@ func decodeURIComponent(encodedURI string) (string, error) {

func writePatternFile(pattern CatalogPattern, versionDir, patternType, patternInfo, patternCaveats, compatibility, patternImageURL string) error {
designFilePath := filepath.Join(versionDir, "design.yml")

var dataJson map[string]interface{}
if err := json.Unmarshal([]byte(pattern.PatternFile), &dataJson); err != nil {
// If JSON unmarshalling fails, try YAML unmarshalling
if yamlErr := yaml.Unmarshal([]byte(pattern.PatternFile), &dataJson); yamlErr != nil {
// Write faulty content to a file for debugging if both unmarshalling attempts fail
faultyFilePath := filepath.Join(versionDir, "faulty_pattern.yaml")
if writeErr := ioutil.WriteFile(faultyFilePath, []byte(pattern.PatternFile), 0644); writeErr != nil {
return fmt.Errorf("failed to write faulty data to file: %v", writeErr)
}
return fmt.Errorf("failed to unmarshal as JSON or YAML: %v; faulty data saved to %s", yamlErr, faultyFilePath)
}
}

// Step 2: Marshal the data into YAML format
yamlData, err := yaml.Marshal(dataJson)
if err != nil {
return fmt.Errorf("failed to marshal data to YAML: %v", err)
}

// Step 3: Write the YAML data to the file
if err := ioutil.WriteFile(designFilePath, yamlData, 0644); err != nil {
if err := ioutil.WriteFile(designFilePath, []byte(pattern.PatternFile), 0644); err != nil {
return utils.ErrWriteFile(err, designFilePath)
}

contenttemp, err := ioutil.ReadFile(designFilePath)
if err != nil {
return utils.ErrReadFile(err, designFilePath)
Expand Down