From 2192e01dbe2aa61c32b1b2c1e075d9c5721697f5 Mon Sep 17 00:00:00 2001 From: GODrums Date: Mon, 28 Oct 2024 03:18:52 +0100 Subject: [PATCH] fix: db flushing --- .../www1/hephaestus/admin/AdminService.java | 12 +++++----- .../github/GitHubPullRequestSyncService.java | 17 +++++--------- .../gitprovider/team/TeamRepository.java | 1 - .../gitprovider/team/TeamService.java | 19 +++++----------- .../gitprovider/user/UserRepository.java | 2 +- .../www1/hephaestus/meta/MetaController.java | 22 ------------------- .../in/www1/hephaestus/meta/MetaDataDTO.java | 8 ------- .../in/www1/hephaestus/meta/MetaService.java | 22 ------------------- .../syncing/GitHubDataSyncService.java | 6 ++--- .../src/app/core/security/keycloak.service.ts | 1 - webapp/src/app/home/home.component.ts | 3 +-- 11 files changed, 22 insertions(+), 91 deletions(-) delete mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaController.java delete mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaDataDTO.java delete mode 100644 server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaService.java diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/admin/AdminService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/admin/AdminService.java index a5c30b4e..3c1d3487 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/admin/AdminService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/admin/AdminService.java @@ -38,21 +38,21 @@ public class AdminService { @EventListener(ApplicationReadyEvent.class) public void run() { + logger.info("Updating AdminConfig..."); // make sure the admin config is present Optional optionalAdminConfig = adminRepository.findById(1L); if (optionalAdminConfig.isEmpty()) { logger.info("No admin config found, creating new one"); AdminConfig newAdminConfig = new AdminConfig(); newAdminConfig.setRepositoriesToMonitor(Set.of(repositoriesToMonitor)); - adminRepository.save(newAdminConfig); + adminRepository.saveAndFlush(newAdminConfig); } else { // repositories should match the environment variable AdminConfig adminConfig = optionalAdminConfig.get(); - if (adminConfig.getRepositoriesToMonitor().stream() - .anyMatch(repository -> !Set.of(repositoriesToMonitor).contains(repository))) { - logger.info("Adding missing repositories to monitor"); + if (!adminConfig.getRepositoriesToMonitor().equals(Set.of(repositoriesToMonitor))) { + logger.info("Adding new repositories to monitor"); adminConfig.setRepositoriesToMonitor(Set.of(repositoriesToMonitor)); - adminRepository.save(adminConfig); + adminRepository.saveAndFlush(adminConfig); } } // make sure teams are initialized @@ -80,7 +80,7 @@ public Set updateRepositories(Set repositories) { public List getUsersAsAdmin() { logger.info("Getting all users with their teams"); - return userRepository.findAllWithEagerTeams().stream().map(UserTeamsDTO::fromUser).toList(); + return userRepository.findAll().stream().map(UserTeamsDTO::fromUser).toList(); } public Optional addTeamToUser(String login, Long teamId) { diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/pullrequest/github/GitHubPullRequestSyncService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/pullrequest/github/GitHubPullRequestSyncService.java index b814b8bb..33ed44cb 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/pullrequest/github/GitHubPullRequestSyncService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/pullrequest/github/GitHubPullRequestSyncService.java @@ -6,7 +6,6 @@ import java.util.HashSet; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import java.util.ArrayList; import java.util.Date; import java.time.OffsetDateTime; @@ -270,16 +269,12 @@ private void automaticTeamAssignment(PullRequest pr) { }); // Match team via repository name - List teamNames = teamRepository.findAll().stream().map(Team::getName).collect(Collectors.toList()); - Optional matchedRepo = teamNames.stream().filter(r -> pr.getRepository().getName().contains(r)) - .findFirst(); - if (matchedRepo.isPresent()) { - Optional team = teamRepository.findByName(matchedRepo.get()); - if (team.isPresent()) { - Team teamEntity = team.get(); - teamEntity.addMember(pr.getAuthor()); - teamRepository.save(teamEntity); - } + String repoName = capitalize(pr.getRepository().getName().replaceAll("[0-9]", "")); + Optional repoTeam = teamRepository.findByName(repoName); + if (repoTeam.isPresent()) { + Team teamEntity = repoTeam.get(); + teamEntity.addMember(pr.getAuthor()); + teamRepository.save(teamEntity); } } } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamRepository.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamRepository.java index 6770ae8f..7dc28fe2 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamRepository.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamRepository.java @@ -7,5 +7,4 @@ public interface TeamRepository extends JpaRepository { Optional findByName(String name); - } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamService.java index 3d5edbad..11d4c29f 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/team/TeamService.java @@ -5,31 +5,23 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TeamService { private static final Logger logger = LoggerFactory.getLogger(TeamService.class); - private final TeamRepository teamRepository; - - public TeamService(TeamRepository teamRepository) { - this.teamRepository = teamRepository; - } + @Autowired + private TeamRepository teamRepository; public Optional getTeam(Long id) { logger.info("Getting team with id: " + id); return teamRepository.findById(id); } - public Optional getTeam(String name) { - logger.info("Getting team with name: " + name); - return teamRepository.findByName(name); - } - public List getAllTeams() { List teams = teamRepository.findAll().stream().map(TeamInfoDTO::fromTeam).toList(); - logger.info("Getting all (" + teams.size() + ") teams"); return teams; } @@ -43,12 +35,13 @@ public Team createTeam(String name, String color) { Team team = new Team(); team.setName(name); team.setColor(color); - return teamRepository.save(team); + return teamRepository.saveAndFlush(team); } public void deleteTeam(Long id) { logger.info("Deleting team with id: " + id); teamRepository.deleteById(id); + teamRepository.flush(); } public Boolean createDefaultTeams() { @@ -80,7 +73,7 @@ public Boolean createDefaultTeams() { Team ares = teamRepository.save(new Team()); ares.setName("Ares"); ares.setColor("#69feff"); - teamRepository.saveAll( + teamRepository.saveAllAndFlush( List.of(iris, athena, atlas, programming, hephaestus, communication, lectures, usability, ares)); return true; } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/user/UserRepository.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/user/UserRepository.java index 9d7ee596..3219f451 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/user/UserRepository.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/gitprovider/user/UserRepository.java @@ -19,7 +19,7 @@ public interface UserRepository extends JpaRepository { @Query(""" SELECT u FROM User u - JOIN FETCH u.teams + LEFT JOIN FETCH u.teams WHERE u.type = 'USER' """) List findAllWithEagerTeams(); diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaController.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaController.java deleted file mode 100644 index ff83ce16..00000000 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaController.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.tum.in.www1.hephaestus.meta; - -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/meta") -public class MetaController { - - private final MetaService metaService; - - public MetaController(MetaService metaService) { - this.metaService = metaService; - } - - @GetMapping - public ResponseEntity getMetaData() { - return ResponseEntity.ok(metaService.getMetaData()); - } -} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaDataDTO.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaDataDTO.java deleted file mode 100644 index cf116807..00000000 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaDataDTO.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.tum.in.www1.hephaestus.meta; - -import com.fasterxml.jackson.annotation.JsonInclude; - -@JsonInclude(JsonInclude.Include.NON_EMPTY) -public record MetaDataDTO(String[] repositoriesToMonitor) { - -} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaService.java deleted file mode 100644 index 766765cd..00000000 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/meta/MetaService.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.tum.in.www1.hephaestus.meta; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import de.tum.in.www1.hephaestus.admin.AdminService; - -@Service -public class MetaService { - private static final Logger logger = LoggerFactory.getLogger(MetaService.class); - - @Autowired - private AdminService adminService; - - public MetaDataDTO getMetaData() { - logger.info("Getting meta data..."); - return new MetaDataDTO( - adminService.getAdminConfig().getRepositoriesToMonitor().stream().sorted().toArray(String[]::new)); - } -} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/syncing/GitHubDataSyncService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/syncing/GitHubDataSyncService.java index 182dcab4..65f69a24 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/syncing/GitHubDataSyncService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/syncing/GitHubDataSyncService.java @@ -77,10 +77,8 @@ public void syncData() { var repositories = repositorySyncService.syncAllMonitoredRepositories(); labelSyncService.syncLabelsOfAllRepositories(repositories); milestoneSyncService.syncMilestonesOfAllRepositories(repositories); - var issues = issueSyncService.syncIssuesOfAllRepositories(repositories, Optional.of(cutoffDate)); // also - // contains - // PRs as - // issues + // also contains PRs as issues + var issues = issueSyncService.syncIssuesOfAllRepositories(repositories, Optional.of(cutoffDate)); issueCommentSyncService.syncIssueCommentsOfAllIssues(issues, Optional.of(cutoffDate)); var pullRequests = pullRequestSyncService.syncPullRequestsOfAllRepositories(repositories, Optional.of(cutoffDate)); diff --git a/webapp/src/app/core/security/keycloak.service.ts b/webapp/src/app/core/security/keycloak.service.ts index 2e04c80e..432210e2 100644 --- a/webapp/src/app/core/security/keycloak.service.ts +++ b/webapp/src/app/core/security/keycloak.service.ts @@ -44,7 +44,6 @@ export class KeycloakService { this.profile = (await this.keycloak.loadUserInfo()) as unknown as UserProfile; this.profile.token = this.keycloak.token || ''; this.profile.roles = this.keycloak.realmAccess?.roles || []; - console.log('KeycloakService.init', this.profile); return true; } diff --git a/webapp/src/app/home/home.component.ts b/webapp/src/app/home/home.component.ts index a499c41f..a2380ae9 100644 --- a/webapp/src/app/home/home.component.ts +++ b/webapp/src/app/home/home.component.ts @@ -11,7 +11,7 @@ import { LeaderboardComponent } from '@app/home/leaderboard/leaderboard.componen import { LeaderboardFilterComponent } from './leaderboard/filter/filter.component'; import { SecurityStore } from '@app/core/security/security-store.service'; import { HlmAlertModule } from '@spartan-ng/ui-alert-helm'; -import { MetaService, TeamService } from '@app/core/modules/openapi'; +import { TeamService } from '@app/core/modules/openapi'; dayjs.extend(isoWeek); @@ -25,7 +25,6 @@ export class HomeComponent { protected CircleX = CircleX; securityStore = inject(SecurityStore); - metaService = inject(MetaService); leaderboardService = inject(LeaderboardService); teamService = inject(TeamService);