-
Notifications
You must be signed in to change notification settings - Fork 0
Add DvcService to retrieve NVR list by version from DVC repository #97
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
d127ef6
d031df5
4387385
0fe3f41
b007e1b
7193aa9
549075b
4f3f2b4
8f478eb
43b333e
42031d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would add some input validate, java is less forgiving compare to python
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package com.redhat.sast.api.service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
| import org.yaml.snakeyaml.Yaml; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @ApplicationScoped | ||
| @Slf4j | ||
| public class DvcService { | ||
|
|
||
| @ConfigProperty(name = "dvc.repo.url") | ||
| String dvcRepoUrl; | ||
|
|
||
| @ConfigProperty(name = "dvc.batch.yaml.path") | ||
| String batchYamlPath; | ||
|
|
||
| /** | ||
| * Get list of NVRs from DVC repository by version tag | ||
| * Fetches YAML file from DVC and extracts NVR list | ||
| * | ||
| * @param version DVC version tag (e.g., "1.0.0" or "v1.0.0") | ||
| * @return List of package NVR strings (empty list if no NVRs found) | ||
| * @throws Exception if DVC fetch fails or parsing fails | ||
| * @throws IllegalArgumentException if version is null or empty | ||
| */ | ||
| public List<String> getNvrListByVersion(String version) throws Exception { | ||
|
||
| // Validate version parameter | ||
| if (version == null || version.isBlank()) { | ||
| throw new IllegalArgumentException("DVC version cannot be null or empty"); | ||
| } | ||
|
|
||
| LOGGER.info("Fetching NVR list from DVC repository: version={}", version); | ||
| LOGGER.debug("Fetching YAML from DVC: path={}", batchYamlPath); | ||
|
|
||
| String yamlContent = fetchFromDvc(batchYamlPath, version); | ||
|
||
| LOGGER.debug("Raw YAML content from DVC ({} bytes)", yamlContent.length()); | ||
JudeNiroshan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Parse YAML to extract NVRs | ||
| Yaml yaml = new Yaml(); | ||
| Object data = yaml.load(yamlContent); | ||
|
|
||
| List<String> nvrList = new ArrayList<>(); | ||
|
|
||
| if (data instanceof java.util.Map) { | ||
| // YAML has a map structure, find list of strings | ||
| java.util.Map<String, Object> map = (java.util.Map<String, Object>) data; | ||
| for (Object value : map.values()) { | ||
| if (value instanceof List) { | ||
| List<?> list = (List<?>) value; | ||
| if (!list.isEmpty() && list.get(0) instanceof String) { | ||
| nvrList = (List<String>) list; | ||
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } else if (data instanceof List) { | ||
| // YAML is just a list of NVRs | ||
| nvrList = (List<String>) data; | ||
| } | ||
|
|
||
| if (nvrList.isEmpty()) { | ||
| LOGGER.warn("No NVRs found in YAML for DVC version {}", version); | ||
| return nvrList; | ||
| } | ||
|
|
||
| LOGGER.info("Successfully retrieved {} NVRs from YAML (DVC version {})", nvrList.size(), version); | ||
| LOGGER.debug("NVR list: {}", nvrList); | ||
| return nvrList; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches raw file content from DVC repository using DVC CLI | ||
| * | ||
| * @param filePath Path to file in DVC repo | ||
| * @param version DVC version tag | ||
| * @return File content as String | ||
| * @throws Exception if DVC command fails | ||
| */ | ||
| private String fetchFromDvc(String filePath, String version) throws Exception { | ||
| LOGGER.debug("Executing DVC get command: repo={}, path={}, version={}", dvcRepoUrl, filePath, version); | ||
|
|
||
| java.nio.file.Path tempFile = java.nio.file.Files.createTempFile("dvc-fetch-", ".tmp"); | ||
| try { | ||
| ProcessBuilder processBuilder = new ProcessBuilder( | ||
| "dvc", "get", dvcRepoUrl, filePath, "--rev", version, "-o", tempFile.toString(), "--force"); | ||
|
|
||
| Process process = processBuilder.start(); | ||
|
|
||
| // Read stderr for error messages | ||
| String error = new String(process.getErrorStream().readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); | ||
|
|
||
| int exitCode = process.waitFor(); | ||
|
||
|
|
||
| if (exitCode != 0) { | ||
| LOGGER.error("DVC command failed with exit code {}: {}", exitCode, error); | ||
| throw new Exception("Failed to fetch data from DVC: " + error); | ||
| } | ||
|
|
||
| // Read content from temp file - the nvrs content | ||
| String output = java.nio.file.Files.readString(tempFile, java.nio.charset.StandardCharsets.UTF_8); | ||
| LOGGER.debug("Successfully fetched {} bytes from DVC", output.length()); | ||
| return output; | ||
| } finally { | ||
| // Clean up temp file | ||
| java.nio.file.Files.deleteIfExists(tempFile); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.