diff --git a/server/application-server/pom.xml b/server/application-server/pom.xml index 5a353d20..926e774d 100644 --- a/server/application-server/pom.xml +++ b/server/application-server/pom.xml @@ -125,6 +125,12 @@ org.springframework.boot spring-boot-starter-graphql + + org.kohsuke + github-api + 1.324 + + diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/Application.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/Application.java index 9a3d34fa..9b9bd2b2 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/Application.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/Application.java @@ -9,6 +9,7 @@ import de.tum.in.www1.hephaestus.codereview.CodeReviewService; import de.tum.in.www1.hephaestus.codereview.repository.Repository; +import de.tum.in.www1.hephaestus.codereview.repository.RepositoryService; @SpringBootApplication @Modulithic(systemName = "Hephaestus") @@ -20,10 +21,15 @@ public static void main(String[] args) { } @Bean - CommandLineRunner commandLineRunner(CodeReviewService service) { + CommandLineRunner commandLineRunner(CodeReviewService service, RepositoryService repositoryService) { return args -> { - Repository repo = service.getHephaestusRepository(); + Repository repo = service.fetchHephaestus(); System.out.println(repo); + repositoryService.saveRepository(repo); + System.out.println("Saved repo: " + repositoryService.countRepositories()); + + Repository repo2 = repositoryService.getAllRepositories().get(0); + System.out.println(repo2); }; } } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/CodeReviewService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/CodeReviewService.java index 6d218095..b7212e16 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/CodeReviewService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/CodeReviewService.java @@ -1,10 +1,14 @@ package de.tum.in.www1.hephaestus.codereview; +import java.io.IOException; import java.time.Instant; import java.util.HashMap; import java.util.List; import java.util.Optional; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GitHub; +import org.kohsuke.github.GitHubBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -14,6 +18,7 @@ import de.tum.in.www1.hephaestus.EnvConfig; import de.tum.in.www1.hephaestus.codereview.repository.Repository; +import de.tum.in.www1.hephaestus.codereview.repository.RepositoryConverter; @Service public class CodeReviewService { @@ -22,6 +27,8 @@ public class CodeReviewService { private final HttpSyncGraphQlClient graphQlClient; + private GitHub github; + private final CodeReviewRepository codeReviewRepository; private final EnvConfig envConfig; @@ -42,6 +49,24 @@ public CodeReviewService(CodeReviewRepository codeReviewRepository, EnvConfig en graphQlClient = HttpSyncGraphQlClient.builder(restClient) .headers(headers -> headers.setBearerAuth(githubPat)) .build(); + try { + github = new GitHubBuilder().withOAuthToken(envConfig.getGithubPat()).build(); + } catch (IOException e) { + logger.error("Error while creating GitHub client: " + e.getMessage()); + return; + } + } + + public Repository fetchHephaestus() throws IOException { + if (github == null) { + logger.error("GitHub client not initialized!"); + return null; + } + + GHRepository ghRepo = github.getRepository("ls1intum/hephaestus"); + System.out.println("Repository: " + ghRepo.toString()); + Repository repository = new RepositoryConverter().convert(ghRepo); + return repository; } public Repository getHephaestusRepository() { @@ -49,15 +74,11 @@ public Repository getHephaestusRepository() { example.setName("hephaestus"); example.setNameWithOwner("ls1intum/hephaestus"); Optional foundRepo = codeReviewRepository.findOne(Example.of(example)); - logger.info("Repository count: " + codeReviewRepository.count()); - - logger.info("Saved repo: " + foundRepo.toString()); if (foundRepo.isPresent()) { return foundRepo.get(); } logger.info("No repo found, creating new one..."); - HashMap variables = new HashMap<>(); variables.put("owner", "ls1intum"); variables.put("name", "hephaestus"); @@ -67,16 +88,12 @@ public Repository getHephaestusRepository() { .variables(variables) .retrieveSync("repository") .toEntity(Repository.class); - if (repository == null) { logger.error("Error while fetching repository!"); return null; } repository.setAddedAt(Instant.now()); - - System.out.println("Repository: " + repository.toString()); codeReviewRepository.saveAndFlush(repository); - System.out.println("New Repository count: " + codeReviewRepository.count()); return repository; } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/Comment.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/Comment.java index 17fcfa1c..1d371c78 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/Comment.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/Comment.java @@ -3,6 +3,7 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import de.tum.in.www1.hephaestus.codereview.actor.Actor; +import de.tum.in.www1.hephaestus.codereview.pullrequest.Pullrequest; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -10,7 +11,6 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToOne; import lombok.Getter; import lombok.Setter; @@ -60,8 +60,8 @@ public class Comment { /** * The parent connection to the pullrequest of the Comment entity. */ - @OneToOne(optional = false) - @JoinColumn(name = "c_connection_id", referencedColumnName = "id") - private CommentConnection connection; + @ManyToOne(optional = false) + @JoinColumn(name = "pullrequest_id", referencedColumnName = "id") + private Pullrequest pullrequest; } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/CommentConnection.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/CommentConnection.java deleted file mode 100644 index bc926473..00000000 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/comment/CommentConnection.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.tum.in.www1.hephaestus.codereview.comment; - -import java.util.List; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; -import lombok.Getter; -import lombok.Setter; - -@Entity -@Table -@Getter -@Setter -public class CommentConnection { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @OneToMany(mappedBy = "connection") - private List nodes; -} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/Pullrequest.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/Pullrequest.java index 26701001..c4c7abde 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/Pullrequest.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/Pullrequest.java @@ -1,7 +1,10 @@ package de.tum.in.www1.hephaestus.codereview.pullrequest; +import java.util.List; + import de.tum.in.www1.hephaestus.codereview.actor.Actor; -import de.tum.in.www1.hephaestus.codereview.comment.CommentConnection; +import de.tum.in.www1.hephaestus.codereview.comment.Comment; +import de.tum.in.www1.hephaestus.codereview.repository.Repository; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.CascadeType; @@ -12,7 +15,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToOne; +import jakarta.persistence.OneToMany; import lombok.Getter; import lombok.Setter; @@ -80,17 +83,13 @@ public class Pullrequest { @JoinColumn(name = "author_id") private Actor author; - /** - * The comments of the Pullrequest entity. - */ - @OneToOne(cascade = CascadeType.ALL, optional = false) - @JoinColumn(name = "c_connection_id", referencedColumnName = "id") - private CommentConnection comments; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "pullrequest") + private List pullRequests; /** * The parent connection of the Pullrequest entity. */ - @OneToOne(optional = false) - @JoinColumn(name = "pr_connection_id", referencedColumnName = "id") - private PullrequestConnection connection; + @ManyToOne(optional = false) + @JoinColumn(name = "repository_id", referencedColumnName = "id") + private Repository repository; } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullrequestConnection.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullrequestConnection.java deleted file mode 100644 index 29a89128..00000000 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/pullrequest/PullrequestConnection.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.tum.in.www1.hephaestus.codereview.pullrequest; - -import java.util.List; - -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import lombok.Getter; -import lombok.Setter; - -@Entity -@Table -@Getter -@Setter -public class PullrequestConnection { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @OneToMany(mappedBy = "connection") - private List nodes; -} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/Repository.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/Repository.java index c597988f..45ea91c2 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/Repository.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/Repository.java @@ -1,16 +1,16 @@ package de.tum.in.www1.hephaestus.codereview.repository; import java.time.Instant; +import java.util.List; -import de.tum.in.www1.hephaestus.codereview.pullrequest.PullrequestConnection; +import de.tum.in.www1.hephaestus.codereview.pullrequest.Pullrequest; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToOne; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import lombok.Getter; import lombok.Setter; @@ -43,15 +43,15 @@ public class Repository { @Column private String url; - @OneToOne(cascade = CascadeType.ALL, optional = false) - @JoinColumn(name = "connection_id", referencedColumnName = "id") - private PullrequestConnection pullRequests; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "repository") + private List pullRequests; @Column(name = "added_at") private Instant addedAt; public String toString() { return "Repository [id=" + id + ", name=" + name + ", nameWithOwner=" + nameWithOwner + ", description=" - + description + ", url=" + url + ", pullRequests=" + pullRequests + ", addedAt=" + addedAt + "]"; + + description + ", url=" + url + ", pullRequests=" + pullRequests + ", addedAt=" + addedAt + + "]"; } } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryConverter.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryConverter.java new file mode 100644 index 00000000..026130b2 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryConverter.java @@ -0,0 +1,28 @@ +package de.tum.in.www1.hephaestus.codereview.repository; + +import java.time.Instant; + +import org.kohsuke.github.GHRepository; +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.convert.ReadingConverter; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Service; + +@Service +@ReadingConverter +public class RepositoryConverter implements Converter { + + @Override + @Nullable + public Repository convert(@NonNull GHRepository source) { + Repository repository = new Repository(); + repository.setName(source.getName()); + repository.setNameWithOwner(source.getFullName()); + repository.setUrl(source.getHtmlUrl().toString()); + repository.setDescription(source.getDescription()); + repository.setAddedAt(Instant.now()); + return repository; + } + +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryRepository.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryRepository.java new file mode 100644 index 00000000..61faa18b --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryRepository.java @@ -0,0 +1,14 @@ +package de.tum.in.www1.hephaestus.codereview.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import de.tum.in.www1.hephaestus.codereview.repository.Repository; + +@org.springframework.stereotype.Repository +public interface RepositoryRepository + extends JpaRepository { + + @Query("SELECT r FROM Repository r WHERE r.nameWithOwner = ?1") + Repository findByNameWithOwner(String nameWithOwner); +} diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryService.java new file mode 100644 index 00000000..fbcac491 --- /dev/null +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/codereview/repository/RepositoryService.java @@ -0,0 +1,57 @@ +package de.tum.in.www1.hephaestus.codereview.repository; + +import java.time.Instant; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +@Service +public class RepositoryService { + private static final Logger logger = LoggerFactory.getLogger(Repository.class); + + private final RepositoryRepository repositoryRepository; + + public RepositoryService(RepositoryRepository repositoryRepository) { + this.repositoryRepository = repositoryRepository; + } + + /** + * Retrieves all {@link Repository} entities. + * + * @return A list of all Repository entities + */ + public List getAllRepositories() { + var repositories = repositoryRepository.findAll(); + logger.info("Getting Repositories: {}", repositories); + return repositoryRepository.findAll(); + } + + public Repository getRepository(String nameWithOwner) { + return repositoryRepository.findByNameWithOwner(nameWithOwner); + } + + /** + * Creates a new {@link Repository} entity with the current timestamp and saves + * it to + * the repository. + * + * @return The created Repository entity + */ + public Repository addRepository() { + Repository repository = new Repository(); + repository.setAddedAt(Instant.now()); + logger.info("Adding new Repository with timestamp: {}", repository.getAddedAt()); + return repositoryRepository.save(repository); + } + + public Repository saveRepository(Repository repository) { + logger.info("Adding Repository: {}", repository.getNameWithOwner()); + return repositoryRepository.save(repository); + } + + public long countRepositories() { + return repositoryRepository.count(); + } +}