@@ -37,7 +37,7 @@ public class GitHubApiClientService {
3737 * @return A Flux emitting all found repositories.
3838 */
3939 public Flux <ImportCodeRepoGitHubResponseDto > fetchAllRepositories (String repoUrl , String accessToken ) {
40- String initialUri = UriComponentsBuilder .fromHttpUrl (repoUrl )
40+ String initialUri = UriComponentsBuilder .fromHttpUrl (toApiBaseUrl ( repoUrl ) )
4141 .path ("/user/repos" )
4242 .queryParam ("per_page" , 100 )
4343 .build ()
@@ -75,7 +75,7 @@ private Optional<String> parseNextLinkFromHeader(HttpHeaders headers) {
7575
7676
7777 public Mono <ImportCodeRepoGitHubResponseDto > getProjectInfo (String name , String repoUrl , String accessToken ) {
78- String apiUrl = String .format ("%s/repos/%s" , repoUrl , name );
78+ String apiUrl = String .format ("%s/repos/%s" , toApiBaseUrl ( repoUrl ) , name );
7979
8080 return webClient .get ()
8181 .uri (apiUrl )
@@ -87,7 +87,7 @@ public Mono<ImportCodeRepoGitHubResponseDto> getProjectInfo(String name, String
8787 }
8888
8989 public Mono <HashMap <String , Integer >> getProjectLanguages (String name , String repoUrl , String accessToken ) {
90- String apiUrl = String .format ("%s/repos/%s/languages" , repoUrl . replace ( "https://github.com" , "https://api.github.com" ), name );
90+ String apiUrl = String .format ("%s/repos/%s/languages" , toApiBaseUrl ( repoUrl ), name );
9191
9292 return webClient .get ()
9393 .uri (apiUrl )
@@ -99,6 +99,28 @@ public Mono<HashMap<String, Integer>> getProjectLanguages(String name, String re
9999 .doOnError (throwable -> System .err .println ("Error fetching project info: " + throwable .getMessage ()));
100100 }
101101
102+ /**
103+ * Converts a GitHub host URL to the corresponding API base URL.
104+ * For github.com: https://github.com -> https://api.github.com
105+ * For GHE: https://opl.ghe.com -> https://opl.ghe.com/api/v3
106+ */
107+ private String toApiBaseUrl (String repoUrl ) {
108+ try {
109+ URL url = new URL (repoUrl );
110+ String host = url .getHost ();
111+ String protocol = url .getProtocol ();
112+ if ("github.com" .equalsIgnoreCase (host )) {
113+ return "https://api.github.com" ;
114+ } else {
115+ // GitHub Enterprise: API is at /api/v3
116+ return protocol + "://" + host + (url .getPort () > 0 ? ":" + url .getPort () : "" ) + "/api/v3" ;
117+ }
118+ } catch (MalformedURLException e ) {
119+ log .warn ("Failed to parse repoUrl '{}', falling back to replacing github.com" , repoUrl );
120+ return repoUrl .replace ("https://github.com" , "https://api.github.com" );
121+ }
122+ }
123+
102124 private HashMap <String , Integer > calculateLanguagePercentages (HashMap <String , Double > languageData ) {
103125 double totalLines = languageData .values ().stream ().mapToDouble (Double ::doubleValue ).sum ();
104126
@@ -113,9 +135,9 @@ private HashMap<String, Integer> calculateLanguagePercentages(HashMap<String, Do
113135
114136 public Mono <String > commentMergeRequest (CodeRepo codeRepo , Long iid , String comment ) throws MalformedURLException {
115137 // Construct the URL for the API endpoint
116- URL repoUrlHost = new URL (codeRepo .getRepourl ());
117- String apiUrl = String .format ("https://api.github.com /repos/%s/issues/%d/comments" ,
118- codeRepo .getName (), iid );
138+ String apiBaseUrl = toApiBaseUrl (codeRepo .getRepourl ());
139+ String apiUrl = String .format ("%s /repos/%s/issues/%d/comments" ,
140+ apiBaseUrl , codeRepo .getName (), iid );
119141
120142 // Create a new CommentMessage object and set the comment body
121143 CommentMessage commentMessage = new CommentMessage ();
0 commit comments