Skip to content

Commit

Permalink
Rework for Github API
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Aug 21, 2024
1 parent 45a6140 commit 45f228d
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 82 deletions.
6 changes: 6 additions & 0 deletions server/application-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.324</version>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -22,6 +27,8 @@ public class CodeReviewService {

private final HttpSyncGraphQlClient graphQlClient;

private GitHub github;

private final CodeReviewRepository codeReviewRepository;

private final EnvConfig envConfig;
Expand All @@ -42,22 +49,36 @@ 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() {
Repository example = new Repository();
example.setName("hephaestus");
example.setNameWithOwner("ls1intum/hephaestus");
Optional<Repository> 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<String, Object> variables = new HashMap<>();
variables.put("owner", "ls1intum");
variables.put("name", "hephaestus");
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -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;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Comment> 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;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Pullrequest> 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
+ "]";
}
}
Original file line number Diff line number Diff line change
@@ -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<GHRepository, Repository> {

@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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Repository, Long> {

@Query("SELECT r FROM Repository r WHERE r.nameWithOwner = ?1")
Repository findByNameWithOwner(String nameWithOwner);
}
Original file line number Diff line number Diff line change
@@ -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<Repository> 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();
}
}

0 comments on commit 45f228d

Please sign in to comment.