Skip to content

Commit

Permalink
latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
egekocabas committed Feb 8, 2025
1 parent 46b2d42 commit 129e56b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.kohsuke.github.GHApp;
import org.kohsuke.github.GHAppInstallation;
import org.kohsuke.github.GHAppInstallationToken;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector;
Expand Down Expand Up @@ -203,14 +202,6 @@ public GitHub createGitHubClientWithCache() {

// Set token expiration time to 20 minutes from now
tokenExpirationTime = Instant.now().plusSeconds(60 * 20);

// Retrieve the repositories accessible to this installation
List<String> installedRepositories =
github.getInstallation().listRepositories().toList().stream()
.map(GHRepository::getFullName)
.toList();

log.info("Installed repositories for this GitHub App: {}", installedRepositories);
} else if (authType == AuthType.PAT) {
log.info("Creating GitHub client with PAT...");
// Set current token to ghAuthToken
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.cit.aet.helios.github;

import java.io.IOException;
import java.util.List;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHRepositorySearchBuilder;
Expand All @@ -26,4 +27,6 @@ public interface GitHubFacade {
public GHRepository getRepositoryById(long id) throws IOException;

public String getGithubAppName();

public List<String> getInstalledRepositoriesForGitHubApp() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package de.tum.cit.aet.helios.github;

import java.io.IOException;
import java.util.List;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHRepositorySearchBuilder;
import org.kohsuke.github.GHUser;

/** Facade for GitHub API. */
/**
* Facade for GitHub API.
*/
public class GitHubFacadeImpl implements GitHubFacade {

private final GitHubClientManager clientManager;
Expand Down Expand Up @@ -44,4 +47,18 @@ public GHRepository getRepositoryById(long id) throws IOException {
public String getGithubAppName() {
return clientManager.getAppName();
}

@Override
public List<String> getInstalledRepositoriesForGitHubApp() throws IOException {
if (GitHubClientManager.AuthType.APP.equals(clientManager.getAuthType())) {
return clientManager.getGitHubClient()
.getInstallation()
.listRepositories()
.toList()
.stream()
.map(GHRepository::getFullName)
.toList();
}
return List.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.cit.aet.helios.github.sync;


import de.tum.cit.aet.helios.github.GitHubFacade;
import de.tum.cit.aet.helios.gitrepo.GitRepoRepository;
import de.tum.cit.aet.helios.gitrepo.GitRepository;
import java.util.Arrays;
Expand All @@ -24,6 +25,7 @@ public class GitHubDataSyncScheduler {

private final GitRepoRepository gitRepositoryRepository;
private final GitHubDataSyncService dataSyncService;
private final GitHubFacade gitHubFacade;

@Value("${monitoring.runOnStartup:true}")
private boolean runOnStartup;
Expand All @@ -35,22 +37,39 @@ public class GitHubDataSyncScheduler {
public void run() {
if (runOnStartup) {
log.info("Starting initial GitHub data sync...");
syncData();
syncInstalledRepositories();
log.info("Initial GitHub data sync completed.");
}
}

@Scheduled(cron = "${monitoring.repository-sync-cron}")
public void syncDataCron() {
log.info("Starting scheduled GitHub data sync...");
syncData();
syncInstalledRepositories();
log.info("Scheduled GitHub data sync completed.");
}

public void syncData() {
Stream<String> envRepoNames = Arrays.stream(repositoriesToMonitor);
log.info("Repositories from environment: {}", Arrays.toString(repositoriesToMonitor));
private void syncInstalledRepositories() {
try {
Stream<String> envRepoNames = Arrays.stream(repositoriesToMonitor);
log.info("Repositories from environment: {}", Arrays.toString(repositoriesToMonitor));

List<String> installedRepositories = gitHubFacade.getInstalledRepositoriesForGitHubApp();
log.info("Installed repositories: {}", installedRepositories);

List<String> allRepositories = Stream.concat(envRepoNames, installedRepositories.stream())
.distinct()
.toList();

log.info("Final list of repositories to sync: {}", allRepositories);

dataSyncService.syncRepositories(allRepositories);
} catch (Exception ex) {
log.error("Failed to sync installed repositories: {} {}", ex.getMessage(), ex);
}
}

public void syncData() {
List<String> dbRepoList = gitRepositoryRepository.findAll().stream()
.map(GitRepository::getNameWithOwner)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -45,6 +46,20 @@ public class GitHubDataSyncService {
private final GitHubLabelSyncService gitHubLabelSyncService;


@Transactional
public void syncRepositories(List<String> nameWithOwners) {
for (String repositoryNameWithOwner : nameWithOwners) {
log.info("Started syncing repository: {}", repositoryNameWithOwner);
Optional<GHRepository> optionalRepository =
repositorySyncService.syncRepository(repositoryNameWithOwner);
if (optionalRepository.isEmpty()) {
log.error("Failed to sync repository: {}", repositoryNameWithOwner);
continue;
}
log.info("Successfully synced repository: {}", repositoryNameWithOwner);
}
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void syncRepositoryData(String repositoryNameWithOwner) {
var cutoffDate = OffsetDateTime.now().minusDays(timeframe);
Expand Down

0 comments on commit 129e56b

Please sign in to comment.