Skip to content

Commit 3ff584a

Browse files
committed
sync with github enterprise
1 parent 9e3c4ea commit 3ff584a

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

backend/src/main/java/io/mixeway/mixewayflowapi/api/webhook/controller/BitbucketWebhookController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public ResponseEntity<?> pushEvent(@RequestBody BBPushEventDTO bbPushEventDTO) {
2828

2929
@PostMapping("/api/v1/webhook/bitbucket/pull-request")
3030
public ResponseEntity<?> pullRequestEvent(@RequestBody BBPullRequestEventDTO bbPullRequestEventDTO) {
31-
log.info("[Bitbucket PR] Pull request event for {}", bbPullRequestEventDTO.resolveRepositoryFullName());
31+
String repoName = bbPullRequestEventDTO.resolveRepositoryFullName();
32+
log.info("[Bitbucket PR] Pull request event for {}", repoName);
3233
bitbucketWebhookService.processPullRequest(bbPullRequestEventDTO);
3334
return new ResponseEntity<>(HttpStatus.OK);
3435
}

backend/src/main/java/io/mixeway/mixewayflowapi/integrations/repo/apiclient/GitHubApiClientService.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)