package com.arms.api.util; import lombok.NoArgsConstructor; import org.yaml.snakeyaml.Yaml; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.Map; @NoArgsConstructor public class YamlFileUtils { @SuppressWarnings("java:S2647") public static List getYamlFilesFromDirectory( String directoryUrl, String branch, String username, String password) throws Exception { List yamlFiles = new ArrayList<>(); // 디렉토리 조회 API 호출 HttpURLConnection connection = (HttpURLConnection) new URL(directoryUrl + "?ref=" + branch).openConnection(); String auth = username + ":" + password; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes()); connection.setRequestProperty("Authorization", "Basic " + encodedAuth); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new RuntimeException("Failed to fetch directory: " + connection.getResponseMessage()); } // 응답 파싱 try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { Yaml yaml = new Yaml(); List> files = yaml.load(reader); for (Map fileInfo : files) { String type = (String) fileInfo.get("type"); String downloadUrl = (String) fileInfo.get("download_url"); // YAML 파일만 선택 if ("file".equals(type) && (downloadUrl.endsWith(".yml") || downloadUrl.endsWith(".yaml"))) { yamlFiles.add(downloadUrl); } } } return yamlFiles; } public static String extractFileNameWithoutExtension(String fileUrl) { String fullFileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1); return fullFileName.substring(0, fullFileName.lastIndexOf('.')); } }