Skip to content

Commit

Permalink
Test with logging and add null protection
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhave committed Nov 6, 2024
1 parent 6b1c70c commit 89610f6
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/io/spring/projectapi/github/GithubOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.spring.projectapi.github.ProjectDocumentation.Status;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.Cacheable;
Expand Down Expand Up @@ -64,6 +66,8 @@ public class GithubOperations {

private static final Comparator<ProjectDocumentation> VERSION_COMPARATOR = GithubOperations::compare;

private static final Logger logger = LoggerFactory.getLogger(GithubOperations.class);

private final RestTemplate restTemplate;

private final ObjectMapper objectMapper;
Expand All @@ -74,6 +78,8 @@ public class GithubOperations {

private static final String CONFIG_COMMIT_MESSAGE = "Update Spring Boot Config";

private static final String DEFAULT_SUPPORT_POLICY = "UPSTREAM";

private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = new ParameterizedTypeReference<>() {
};

Expand Down Expand Up @@ -212,6 +218,8 @@ private ResponseEntity<Map<String, Object>> getFile(String projectSlug, String f
return this.restTemplate.exchange(request, STRING_OBJECT_MAP);
}
catch (HttpClientErrorException ex) {
logger.info("*** Exception thrown for " + projectSlug + " and file " + fileName + " due to "
+ ex.getMessage() + " with status " + ex.getStatusCode());
HttpStatusCode statusCode = ex.getStatusCode();
if (statusCode.value() == 404) {
throwIfProjectDoesNotExist(projectSlug);
Expand Down Expand Up @@ -257,7 +265,10 @@ public List<Project> getProjects() {
body.forEach((project) -> {
String projectSlug = (String) project.get("name");
try {
projects.add(getProject(projectSlug));
Project fetchedProject = getProject(projectSlug);
if (fetchedProject != null) {
projects.add(fetchedProject);
}
}
catch (Exception ex) {
// Ignore project without an index file
Expand All @@ -281,6 +292,9 @@ public Project getProject(String projectSlug) {

public List<ProjectDocumentation> getProjectDocumentations(String projectSlug) {
ResponseEntity<Map<String, Object>> response = getFile(projectSlug, "documentation.json");
if (response == null) {
return Collections.emptyList();
}
String content = getFileContents(response);
return List.copyOf(convertToProjectDocumentation(content));
}
Expand All @@ -304,9 +318,11 @@ private <T> T readValue(String contents, TypeReference<T> type) {
}
}

@Cacheable(value = "support_policy", key = "#projectSlug")
public String getProjectSupportPolicy(String projectSlug) {
ResponseEntity<Map<String, Object>> indexResponse = getFile(projectSlug, "index.md");
if (indexResponse == null) {
return DEFAULT_SUPPORT_POLICY;
}
String indexContents = getFileContents(indexResponse);
Map<String, String> frontMatter = MarkdownUtils.getFrontMatter(indexContents);
InvalidGithubProjectIndexException.throwIfInvalid(Objects::nonNull, frontMatter, projectSlug);
Expand Down

0 comments on commit 89610f6

Please sign in to comment.