Skip to content

Commit

Permalink
Chore: Java Prettier Formatting (#215)
Browse files Browse the repository at this point in the history
- fix: prettier cli command
- chore: prettier formatting
  • Loading branch information
GODrums authored Dec 17, 2024
1 parent 4bebd4e commit 4185b66
Show file tree
Hide file tree
Showing 109 changed files with 1,669 additions and 1,383 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"generate:api:intelligence-service:client": "npx openapi-generator-cli generate -i server/intelligence-service/openapi.yaml -g java --library resttemplate --api-package de.tum.in.www1.hephaestus.intelligenceservice.api --model-package de.tum.in.www1.hephaestus.intelligenceservice.model --invoker-package de.tum.in.www1.hephaestus.intelligenceservice --model-name-prefix IS --additional-properties useJakartaEe=true,performBeanValidation=true,hideGenerationTimestamp=true --package-name de.tum.in.www1.hephaestus.intelligenceservice -o tmp/java-client && shx cp -r tmp/java-client/src/main/java/de/tum/in/www1/hephaestus/intelligenceservice server/application-server/src/main/java/de/tum/in/www1/hephaestus && shx rm -rf tmp",
"generate:api:intelligence-service": "npm run generate:api:intelligence-service:clean && npm run generate:api:intelligence-service:specs && npm run generate:api:intelligence-service:client",
"generate:api": "npm run generate:api:intelligence-service && npm run generate:api:application-server",
"format:java:check": "prettier --check server/application-server/src/**/*.java",
"format:java:write": "prettier --write server/application-server/src/**/*.java",
"format:java:check": "prettier --check server/application-server/src/**/*.java --config-precedence prefer-file --config server/application-server/.prettierrc.yaml --ignore-path server/application-server/.prettierignore",
"format:java:write": "prettier --write server/application-server/src/**/*.java --config-precedence prefer-file --config server/application-server/.prettierrc.yaml --ignore-path server/application-server/.prettierignore",
"format:python:check": "cd server/intelligence-service/ && poetry run black --check .",
"format:python:write": "cd server/intelligence-service/ && poetry run black .",
"db:changelog:diff": "cd server/application-server && docker compose down postgres && shx --silent mv postgres-data postgres-data-temp && docker compose up -d postgres && docker compose exec postgres sh -c \"until pg_isready -h localhost -p 5432; do sleep 1; done\" && mvn liquibase:update && mvn liquibase:diff && docker compose down postgres && shx rm -rf postgres-data && shx --silent mv postgres-data-temp postgres-data"
Expand Down
2 changes: 1 addition & 1 deletion server/application-server/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ target
postgres-data

# Ignore OpenAPI generated files:
src\main\java\de\tum\in\www1\hephaestus\intelligenceservice
src/main/java/de/tum/in/www1/hephaestus/intelligenceservice
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
@SpringBootApplication
public class Application {

public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package de.tum.in.www1.hephaestus;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.boot.spi.IntegratorProvider;

import de.tum.in.www1.hephaestus.gitprovider.issue.IssueInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.issuecomment.IssueCommentInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.label.LabelInfoDTO;
Expand All @@ -18,26 +12,30 @@
import de.tum.in.www1.hephaestus.mentor.message.MessageDTO;
import de.tum.in.www1.hephaestus.mentor.session.SessionDTO;
import io.hypersistence.utils.hibernate.type.util.ClassImportIntegrator;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.boot.spi.IntegratorProvider;

public class ClassImportIntegratorIntegratorProvider implements IntegratorProvider {

@Override
public List<Integrator> getIntegrators() {
// Accessible DTOs
@SuppressWarnings("rawtypes")
List<Class> classes = new ArrayList<>();
classes.add(UserInfoDTO.class);
classes.add(TeamInfoDTO.class);
classes.add(IssueInfoDTO.class);
classes.add(LabelInfoDTO.class);
classes.add(MilestoneInfoDTO.class);
classes.add(PullRequestInfoDTO.class);
classes.add(IssueCommentInfoDTO.class);
classes.add(PullRequestReviewInfoDTO.class);
classes.add(RepositoryInfoDTO.class);
classes.add(MessageDTO.class);
classes.add(SessionDTO.class);
@Override
public List<Integrator> getIntegrators() {
// Accessible DTOs
@SuppressWarnings("rawtypes")
List<Class> classes = new ArrayList<>();
classes.add(UserInfoDTO.class);
classes.add(TeamInfoDTO.class);
classes.add(IssueInfoDTO.class);
classes.add(LabelInfoDTO.class);
classes.add(MilestoneInfoDTO.class);
classes.add(PullRequestInfoDTO.class);
classes.add(IssueCommentInfoDTO.class);
classes.add(PullRequestReviewInfoDTO.class);
classes.add(RepositoryInfoDTO.class);
classes.add(MessageDTO.class);
classes.add(SessionDTO.class);

return List.of(new ClassImportIntegrator(classes));
}
return List.of(new ClassImportIntegrator(classes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ public OpenApiCustomizer schemaCustomizer() {
if (components != null && components.getSchemas() != null) {
// Only include schemas with DTO suffix and remove the suffix
var schemas = components
.getSchemas()
.entrySet()
.stream()
.filter(entry -> entry.getKey().endsWith("DTO"))
.collect(Collectors.toMap(
.getSchemas()
.entrySet()
.stream()
.filter(entry -> entry.getKey().endsWith("DTO"))
.collect(
Collectors.toMap(
entry -> entry.getKey().substring(0, entry.getKey().length() - 3),
entry -> {
var schema = entry.getValue();
schema.setName(entry.getKey().substring(0, entry.getKey().length() - 3));
return schema;
}
));
)
);

// Remove DTO suffix from attribute names
schemas.forEach((key, value) -> {
Expand All @@ -70,50 +72,59 @@ public OpenApiCustomizer schemaCustomizer() {
if (paths != null) {
paths.forEach((path, pathItem) -> {
logger.info("Processing path: {}", path);
pathItem.readOperations().forEach(operation -> {
// Remove DTO suffix from response schemas
var responses = operation.getResponses();
if (responses != null) {
responses.forEach((responseCode, response) -> {
var content = response.getContent();
if (content != null) {
content.forEach((contentType, mediaType) -> {
if (mediaType != null && mediaType.getSchema() != null) {
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema());
} else {
logger.warn("MediaType or Schema is null for content type: {}", contentType);
}
});
} else {
logger.warn("Response with code {} has no content.", responseCode);
}
});
}
if (operation.getRequestBody() != null) {
var requestBodyContent = operation.getRequestBody().getContent();
requestBodyContent.forEach((contentType, mediaType) -> {
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema());
});
}
pathItem
.readOperations()
.forEach(operation -> {
// Remove DTO suffix from response schemas
var responses = operation.getResponses();
if (responses != null) {
responses.forEach((responseCode, response) -> {
var content = response.getContent();
if (content != null) {
content.forEach((contentType, mediaType) -> {
if (mediaType != null && mediaType.getSchema() != null) {
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema());
} else {
logger.warn(
"MediaType or Schema is null for content type: {}",
contentType
);
}
});
} else {
logger.warn("Response with code {} has no content.", responseCode);
}
});
}
if (operation.getRequestBody() != null) {
var requestBodyContent = operation.getRequestBody().getContent();
requestBodyContent.forEach((contentType, mediaType) -> {
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema());
});
}

// Remove -controller suffix from tags
if (operation.getTags() != null) {
operation.setTags(
operation.getTags()
.stream()
.filter(tag -> {
if (tag.length() > 11) {
return true;
} else {
logger.warn("Tag '{}' is shorter than expected and cannot be trimmed.", tag);
return false;
}
})
.map(tag -> tag.substring(0, tag.length() - 11))
.collect(Collectors.toList())
);
}
});
// Remove -controller suffix from tags
if (operation.getTags() != null) {
operation.setTags(
operation
.getTags()
.stream()
.filter(tag -> {
if (tag.length() > 11) {
return true;
} else {
logger.warn(
"Tag '{}' is shorter than expected and cannot be trimmed.",
tag
);
return false;
}
})
.map(tag -> tag.substring(0, tag.length() - 11))
.collect(Collectors.toList())
);
}
});
});
} else {
logger.warn("Paths are null in OpenAPI configuration.");
Expand All @@ -136,4 +147,4 @@ private void removeDTOSuffixesFromSchemaRecursively(Schema<?> schema) {
removeDTOSuffixesFromSchemaRecursively(schema.getItems());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
Expand All @@ -25,23 +24,28 @@
@Configuration
@EnableWebSecurity
public class SecurityConfig {
interface AuthoritiesConverter extends Converter<Map<String, Object>, Collection<GrantedAuthority>> {
}

interface AuthoritiesConverter extends Converter<Map<String, Object>, Collection<GrantedAuthority>> {}

@SuppressWarnings("unchecked")
@Bean
AuthoritiesConverter realmRolesAuthoritiesConverter() {
return claims -> {
final var realmAccess = Optional.ofNullable((Map<String, Object>) claims.get("realm_access"));
final var roles = realmAccess.flatMap(map -> Optional.ofNullable((List<String>) map.get("roles")));
return roles.map(List::stream).orElse(Stream.empty()).map(SimpleGrantedAuthority::new)
.map(GrantedAuthority.class::cast).toList();
return roles
.map(List::stream)
.orElse(Stream.empty())
.map(SimpleGrantedAuthority::new)
.map(GrantedAuthority.class::cast)
.toList();
};
}

@Bean
JwtAuthenticationConverter authenticationConverter(
Converter<Map<String, Object>, Collection<GrantedAuthority>> authoritiesConverter) {
Converter<Map<String, Object>, Collection<GrantedAuthority>> authoritiesConverter
) {
var authenticationConverter = new JwtAuthenticationConverter();
authenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> {
return authoritiesConverter.convert(jwt.getClaims());
Expand All @@ -51,18 +55,21 @@ JwtAuthenticationConverter authenticationConverter(

@Bean
SecurityFilterChain resourceServerSecurityFilterChain(
HttpSecurity http,
Converter<Jwt, AbstractAuthenticationToken> authenticationConverter) throws Exception {
HttpSecurity http,
Converter<Jwt, AbstractAuthenticationToken> authenticationConverter
) throws Exception {
http.oauth2ResourceServer(resourceServer -> {
resourceServer.jwt(jwtDecoder -> {
jwtDecoder.jwtAuthenticationConverter(authenticationConverter);
});
});

http.sessionManagement(sessions -> {
sessions.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}).csrf(csrf -> csrf.disable())
.cors(cors -> cors.configurationSource(corsConfigurationSource()));
http
.sessionManagement(sessions -> {
sessions.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
})
.csrf(csrf -> csrf.disable())
.cors(cors -> cors.configurationSource(corsConfigurationSource()));

http.authorizeHttpRequests(requests -> {
requests.requestMatchers("/workspace/**").hasAuthority("admin");
Expand All @@ -82,4 +89,4 @@ CorsConfigurationSource corsConfigurationSource() {
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package de.tum.in.www1.hephaestus.config;

import java.io.IOException;

import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;

@Configuration
public class GitHubConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package de.tum.in.www1.hephaestus.config;

import de.tum.in.www1.hephaestus.intelligenceservice.ApiClient;
import de.tum.in.www1.hephaestus.intelligenceservice.api.MentorApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.tum.in.www1.hephaestus.intelligenceservice.ApiClient;
import de.tum.in.www1.hephaestus.intelligenceservice.api.MentorApi;

@Configuration
public class IntelligenceServiceConfig {

Expand All @@ -19,6 +18,7 @@ public IntelligenceServiceApi intelligenceServiceApi() {
}

public class IntelligenceServiceApi extends MentorApi {

public IntelligenceServiceApi() {
super(new ApiClient().setBasePath(intelligenceServiceUrl));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.tum.in.www1.hephaestus.config;

import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;


@Configuration
public class KeycloakConfig {
Expand Down
Loading

0 comments on commit 4185b66

Please sign in to comment.