Skip to content
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,50 @@
package fr.zenika.opensource.stats.beans.gitlab.graphql;

public class GitLabMergeRequestNode {
private String title;
private String webUrl;
private String state;
private String createdAt;
private GitLabMergeRequestProject project;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getWebUrl() {
return webUrl;
}

public void setWebUrl(String webUrl) {
this.webUrl = webUrl;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public GitLabMergeRequestProject getProject() {
return project;
}

public void setProject(GitLabMergeRequestProject project) {
this.project = project;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fr.zenika.opensource.stats.beans.gitlab.graphql;

public class GitLabMergeRequestProject {
private String name;
private String fullPath;
private String webUrl;
private String visibility;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFullPath() {
return fullPath;
}

public void setFullPath(String fullPath) {
this.fullPath = fullPath;
}

public String getWebUrl() {
return webUrl;
}

public void setWebUrl(String webUrl) {
this.webUrl = webUrl;
}

public String getVisibility() {
return visibility;
}

public void setVisibility(String visibility) {
this.visibility = visibility;
}

public boolean isPublic() {
return visibility == null || "public".equalsIgnoreCase(visibility);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package fr.zenika.opensource.stats.beans.gitlab.graphql;

import java.util.List;

public class GitLabMergeRequestsCount {
private int count;
private List<GitLabMergeRequestNode> nodes;

public int getCount() {
return count;
Expand All @@ -10,4 +13,12 @@ public int getCount() {
public void setCount(int count) {
this.count = count;
}

public List<GitLabMergeRequestNode> getNodes() {
return nodes;
}

public void setNodes(List<GitLabMergeRequestNode> nodes) {
this.nodes = nodes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fr.zenika.opensource.stats.config;

public class GitLabGraphQLQueries {

public static final String qGetMergedMergeRequestsForAPeriod = """
query ($username: String!, $updatedAfter: Time, $updatedBefore: Time) {
user(username: $username) {
authoredMergeRequests(
state: merged
updatedAfter: $updatedAfter
updatedBefore: $updatedBefore
first: 100
) {
count
nodes {
title
webUrl
state
createdAt
project {
name
fullPath
webUrl
visibility
}
}
}
}
}
""";
}

119 changes: 80 additions & 39 deletions src/main/java/fr/zenika/opensource/stats/services/GitLabServices.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package fr.zenika.opensource.stats.services;

import io.quarkus.logging.Log;
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient;
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClientBuilder;
import fr.zenika.opensource.stats.beans.ContributionDetail;
import fr.zenika.opensource.stats.beans.CustomStatsContributionsUserByMonth;
import fr.zenika.opensource.stats.beans.gitlab.GitLabEvent;
import fr.zenika.opensource.stats.beans.gitlab.GitLabMember;
import fr.zenika.opensource.stats.beans.gitlab.GitLabMergeRequest;
import fr.zenika.opensource.stats.beans.gitlab.GitLabProject;
import fr.zenika.opensource.stats.beans.gitlab.graphql.GitLabGraphQLUser;
import fr.zenika.opensource.stats.beans.gitlab.graphql.GitLabMergeRequestNode;
import fr.zenika.opensource.stats.beans.gitlab.graphql.GitLabMergeRequestProject;
import fr.zenika.opensource.stats.beans.gitlab.graphql.GitLabMergeRequestsCount;
import fr.zenika.opensource.stats.config.GitLabClient;
import fr.zenika.opensource.stats.config.GitLabGraphQLQueries;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.GenericType;
Expand All @@ -20,26 +27,37 @@
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@ApplicationScoped
public class GitLabServices {

private static final int NB_MRS_PER_PAGE = 100;

@Inject
@RestClient
GitLabClient gitLabClient;

@ConfigProperty(name = "quarkus.smallrye-graphql-client.gitlab-api.url", defaultValue = "https://gitlab.com/api/graphql")
String graphQLUrl;

@Inject
@ConfigProperty(name = "gitlab.token")
Optional<String> gitlabToken;

DynamicGraphQLClient dynamicGraphQLClient;

@PostConstruct
public void init() {
dynamicGraphQLClient = DynamicGraphQLClientBuilder.newBuilder()
.url(graphQLUrl)
.header("Authorization", getAuthHeader())
.build();
}

private String getAuthHeader() {
return "Bearer " + gitlabToken.orElse("");
}
Expand Down Expand Up @@ -81,38 +99,26 @@ public List<GitLabProject> getPersonalProjectsForAnUser(String username) {
* @return A list of monthly contribution stats.
*/
public List<CustomStatsContributionsUserByMonth> getContributionsForTheCurrentYear(String username, int year) {
return getUserInformation(username)
.map(user -> getContributionsByUserId(user.getId(), username, year))
.orElse(List.of());
if (username == null || username.isBlank()) {
return List.of();
}
return getContributionsByUserId(null, username, year);
}

/**
* Get contributions for a specific GitLab user ID.
* Get contributions for a specific GitLab user.
*
* @param userId The numeric GitLab user ID.
* @param username The username (for logging).
* @param userId The numeric GitLab user ID (optional when using GraphQL by username).
* @param username The GitLab username.
* @param year The year.
* @return A list of monthly contribution stats.
*/
public List<CustomStatsContributionsUserByMonth> getContributionsByUserId(String userId, String username,
int year) {
List<CustomStatsContributionsUserByMonth> result = new ArrayList<>();

// If userId is not numeric, it might be a username from old data. Try to
// resolve it.
String actualId = userId;
if (userId != null && !userId.matches("\\d+")) {
Log.warn("GitLab ID '" + userId + "' for " + username
+ " is not numeric. Attempting to resolve ID from username.");
Optional<GitLabMember> user = getUserInformation(username);
if (user.isPresent()) {
actualId = user.get().getId();
Log.info("Resolved GitLab username '" + username + "' to ID '" + actualId + "'");
} else {
Log.error("Failed to resolve GitLab ID for username '" + username + "'. Aborting contribution fetch.");
return List.of();
}
if (username == null || username.isBlank()) {
return List.of();
}
List<CustomStatsContributionsUserByMonth> result = new ArrayList<>();

for (Month month : Month.values()) {
if (year == LocalDate.now().getYear() && month.getValue() > LocalDate.now().getMonthValue()) {
Expand All @@ -126,25 +132,32 @@ public List<CustomStatsContributionsUserByMonth> getContributionsByUserId(String
String updatedBefore = endOfMonth.atTime(23, 59, 59).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "Z";

try {
// To fetch only public contributions, do not send Authorization header (unauthenticated search)
// Use scope=all to see MRs across all public projects
// Use updated_after/before for better compatibility with older GitLab versions
Response response = gitLabClient.getMergeRequests(actualId, "merged", updatedAfter, updatedBefore,
"all", NB_MRS_PER_PAGE);
String totalStr = response.getHeaderString("X-Total");
List<ContributionDetail> details = extractGitLabContributionDetails(response);

int total = Optional.ofNullable(totalStr)
.filter(s -> !s.isEmpty())
.map(Integer::parseInt)
.orElse(details.size());
var variables = new HashMap<String, Object>();
variables.put("username", username);
variables.put("updatedAfter", updatedAfter);
variables.put("updatedBefore", updatedBefore);

io.smallrye.graphql.client.Response gqlResponse = dynamicGraphQLClient.executeSync(
GitLabGraphQLQueries.qGetMergedMergeRequestsForAPeriod,
variables);

GitLabGraphQLUser userStats = Optional.ofNullable(gqlResponse)
.map(r -> r.getObject(GitLabGraphQLUser.class, "user"))
.orElse(null);

GitLabMergeRequestsCount mrContributions = Optional.ofNullable(userStats)
.map(GitLabGraphQLUser::getAuthoredMergeRequests)
.orElse(null);

List<ContributionDetail> details = extractGitLabGraphQLContributionDetails(mrContributions);
int total = details.size();

result.add(new CustomStatsContributionsUserByMonth(
month.getValue(),
month.getDisplayName(TextStyle.FULL, Locale.ENGLISH),
total,
details));
Log.info("Fetching GitLab merged MRs for " + username + " (ID: " + actualId + ") in " + month + " "
Log.info("Fetching GitLab merged MRs for " + username + " in " + month + " "
+ year + " | " + total);

} catch (Exception e) {
Expand All @@ -158,6 +171,34 @@ public List<CustomStatsContributionsUserByMonth> getContributionsByUserId(String
return result;
}

List<ContributionDetail> extractGitLabGraphQLContributionDetails(GitLabMergeRequestsCount mrContributions) {
return Optional.ofNullable(mrContributions)
.map(GitLabMergeRequestsCount::getNodes)
.orElse(List.of())
.stream()
.filter(Objects::nonNull)
.filter(mr -> Optional.ofNullable(mr.getProject())
.map(GitLabMergeRequestProject::isPublic)
.orElse(true))
.map(this::mapMergeRequestNodeToDetail)
.collect(Collectors.toList());
}

private ContributionDetail mapMergeRequestNodeToDetail(GitLabMergeRequestNode mr) {
Optional<GitLabMergeRequestProject> projOpt = Optional.ofNullable(mr.getProject());
String projectName = projOpt.map(GitLabMergeRequestProject::getName).orElse("");
String projectFullName = projOpt.map(GitLabMergeRequestProject::getFullPath).orElse(projectName);
String projectUrl = projOpt.map(GitLabMergeRequestProject::getWebUrl).orElse("");
return new ContributionDetail(
projectName,
projectFullName,
projectUrl,
Optional.ofNullable(mr.getTitle()).orElse(""),
Optional.ofNullable(mr.getWebUrl()).orElse(""),
Optional.ofNullable(mr.getState()).orElse(""),
Optional.ofNullable(mr.getCreatedAt()).orElse(""));
}

List<ContributionDetail> extractGitLabContributionDetails(Response response) {
if (response == null || !response.hasEntity()) {
return List.of();
Expand Down
Loading