Skip to content
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

fix: Gitlab webhook creation #1812

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.terrakube.api.plugin.vcs.provider.gitlab;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -108,6 +112,7 @@ public String createWebhook(Workspace workspace, String webhookId) {
String secret = Base64.getEncoder()
.encodeToString(workspace.getId().toString().getBytes(StandardCharsets.UTF_8));
String ownerAndRepo = String.join("/", extractOwnerAndRepo(workspace.getSource()));
String token = workspace.getVcs().getAccessToken();
String webhookUrl = String.format("https://%s/webhook/v1/%s", hostname, webhookId);
RestTemplate restTemplate = new RestTemplate();

Expand All @@ -124,7 +129,15 @@ public String createWebhook(Workspace workspace, String webhookId) {
log.info(body);
// Create the entity
HttpEntity<String> entity = new HttpEntity<>(body, headers);
URI gitlabUri = UriComponentsBuilder.fromHttpUrl(workspace.getVcs().getApiUrl() + "/projects/" + ownerAndRepo + "/hooks").build(true).toUri();
String projectId = "";
try {
log.info("Search gitlab project id using {}, {}", ownerAndRepo, workspace.getVcs().getApiUrl());
projectId = getGitlabProjectId(ownerAndRepo, token, workspace.getVcs().getApiUrl());
} catch (InterruptedException | IOException e) {
log.error(e.getMessage());
Thread.currentThread().interrupt();
}
URI gitlabUri = UriComponentsBuilder.fromHttpUrl(workspace.getVcs().getApiUrl() + "/projects/" + projectId + "/hooks").build(true).toUri();

// Make the request using the GitLab API
ResponseEntity<String> response = restTemplate.exchange(
Expand All @@ -146,6 +159,36 @@ public String createWebhook(Workspace workspace, String webhookId) {

return id;
}

private String getGitlabProjectId(String ownerAndRepo, String accessToken, String gitlabBaseUrl) throws IOException, InterruptedException {
String projectId = "";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(gitlabBaseUrl + "/search?scope=projects&search=" + ownerAndRepo))
.header("Authorization", "Bearer " + accessToken)
.header("Content-Type", "application/json")
.GET()
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

// Check for successful response
if (response.statusCode() == 200) {
log.info("Response from Gitlab: {}" , response.body());
// Initialize Jackson ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();

// Parse the JSON string into a JsonNode
JsonNode jsonNode = objectMapper.readTree(response.body());

projectId = jsonNode.get(0).get("id").asText();
log.info("Parsed Project ID: {}", projectId);
} else {
log.error("Failed to retrieve project ID. HTTP Status: {}", response.statusCode());
log.error("Response: {}", response.body());
}
return projectId;
}

public void deleteWebhook(Workspace workspace, String webhookRemoteId) {
String ownerAndRepo = String.join("/", extractOwnerAndRepo(workspace.getSource()));
Expand Down