Skip to content

Commit

Permalink
fix: db flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Oct 28, 2024
1 parent 1256dfa commit 2192e01
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AdminConfig> 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
Expand Down Expand Up @@ -80,7 +80,7 @@ public Set<String> updateRepositories(Set<String> repositories) {

public List<UserTeamsDTO> 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<UserInfoDTO> addTeamToUser(String login, Long teamId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -270,16 +269,12 @@ private void automaticTeamAssignment(PullRequest pr) {
});

// Match team via repository name
List<String> teamNames = teamRepository.findAll().stream().map(Team::getName).collect(Collectors.toList());
Optional<String> matchedRepo = teamNames.stream().filter(r -> pr.getRepository().getName().contains(r))
.findFirst();
if (matchedRepo.isPresent()) {
Optional<Team> 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<Team> repoTeam = teamRepository.findByName(repoName);
if (repoTeam.isPresent()) {
Team teamEntity = repoTeam.get();
teamEntity.addMember(pr.getAuthor());
teamRepository.save(teamEntity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
public interface TeamRepository extends JpaRepository<Team, Long> {

Optional<Team> findByName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team> getTeam(Long id) {
logger.info("Getting team with id: " + id);
return teamRepository.findById(id);
}

public Optional<Team> getTeam(String name) {
logger.info("Getting team with name: " + name);
return teamRepository.findByName(name);
}

public List<TeamInfoDTO> getAllTeams() {
List<TeamInfoDTO> teams = teamRepository.findAll().stream().map(TeamInfoDTO::fromTeam).toList();
logger.info("Getting all (" + teams.size() + ") teams");
return teams;
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("""
SELECT u
FROM User u
JOIN FETCH u.teams
LEFT JOIN FETCH u.teams
WHERE u.type = 'USER'
""")
List<User> findAllWithEagerTeams();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 0 additions & 1 deletion webapp/src/app/core/security/keycloak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions webapp/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -25,7 +25,6 @@ export class HomeComponent {
protected CircleX = CircleX;

securityStore = inject(SecurityStore);
metaService = inject(MetaService);
leaderboardService = inject(LeaderboardService);
teamService = inject(TeamService);

Expand Down

0 comments on commit 2192e01

Please sign in to comment.