func()

in pkg/client/sonar/sonar.go [293:343]


func (sc Client) UploadProfile(profileName string, profilePath string) (string, error) {
	log.Info("attempting to upload quality profile...", "profileName", profileName, "profilePath", profilePath)

	profileExist, profileId, isDefault, err := sc.checkProfileExist(profileName)
	if err != nil {
		return "", fmt.Errorf("failed to get quality profile: %w", err)
	}

	if profileExist && isDefault {
		return profileId, nil
	}

	if profileExist && !isDefault {
		if err = sc.setDefaultProfile("java", profileName); err != nil {
			return "", err
		}

		return profileId, nil
	}

	if !helper.FileExists(profilePath) {
		return "", fmt.Errorf("file %s does not exist in path provided: %s", profileName, profilePath)
	}

	log.Info(fmt.Sprintf("Uploading profile %s from path %s", profileName, profilePath))

	resp, err := sc.resty.R().
		SetHeader(contentTypeField, "multipart/form-data").
		SetFile("backup", profilePath).
		Post("/qualityprofiles/restore")
	if err != nil {
		return "", fmt.Errorf("failed to send upload profile request!: %w", err)
	}
	if resp.IsError() {
		errMsg := fmt.Sprintf("Uploading profile %s failed. Response - %s", profileName, resp.Status())
		return "", errors.New(errMsg)
	}

	_, profileId, _, err = sc.checkProfileExist(profileName)
	if err != nil {
		return "", err
	}

	err = sc.setDefaultProfile("java", profileName)
	if err != nil {
		return "", err
	}

	log.Info(fmt.Sprintf("Profile %s in Sonar from path %v has been uploaded and is set as default", profileName, profilePath))
	return profileId, nil
}