67 lines
2.2 KiB
Java
67 lines
2.2 KiB
Java
package org.example;
|
|
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.*;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
import org.eclipse.microprofile.rest.client.inject.RestClient;
|
|
|
|
import org.example.model.Branch;
|
|
import org.example.model.Repository;
|
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Path("/git")
|
|
public class GitHubResource {
|
|
|
|
@Inject
|
|
@RestClient
|
|
GitHubClient gitHubClient;
|
|
|
|
@ConfigProperty(name = "github.api.token")
|
|
String githubApiToken;
|
|
|
|
@GET
|
|
@Path("/{username}")
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
public Response getRepos(@PathParam("username") String username) {
|
|
try {
|
|
String authHeader = "Bearer " + githubApiToken;
|
|
|
|
List<Repository> repositories = gitHubClient.getRepos(username, authHeader);
|
|
|
|
if (repositories == null || repositories.isEmpty()) {
|
|
return Response.status(Response.Status.NOT_FOUND)
|
|
.entity(new ErrorResponse(404, "GitHub user not found."))
|
|
.type(MediaType.APPLICATION_JSON)
|
|
.build();
|
|
}
|
|
|
|
|
|
List<Repository> filteredRepos = repositories.stream()
|
|
.filter(repo -> !repo.isFork())
|
|
.collect(Collectors.toList());
|
|
|
|
for (Repository repo : filteredRepos) {
|
|
List<Branch> branches = gitHubClient.getBranches(repo.getOwner().getLogin(), repo.getName(), authHeader);
|
|
repo.setBranches(branches);
|
|
}
|
|
|
|
return Response.ok(filteredRepos).build();
|
|
|
|
} catch (NotFoundException e) {
|
|
return Response.status(Response.Status.NOT_FOUND)
|
|
.entity(new ErrorResponse(404, "GitHub user not found."))
|
|
.type(MediaType.APPLICATION_JSON)
|
|
.build();
|
|
} catch (Exception e) {
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
|
|
.entity(new ErrorResponse(500, "An error occurred while processing the request."))
|
|
.type(MediaType.APPLICATION_JSON)
|
|
.build();
|
|
}
|
|
}
|
|
}
|