Skip to content

Commit

Permalink
refactor: Rename repository methods for name-based query derivation (#…
Browse files Browse the repository at this point in the history
…541)

* refactor: Rename repository methods for name-based query derivation

Signed-off-by: Oleg Kopysov <[email protected]>

* fix: Fix Java coding style

Signed-off-by: Oleg Kopysov <[email protected]>

* fix: Fix runtime errors in queries

Signed-off-by: Oleg Kopysov <[email protected]>

---------

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Jul 1, 2024
1 parent 49fa3d0 commit 313158b
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 106 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/lpvs/controller/LPVSWebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public ResponseEntity<HistoryEntity> newHistoryPageByUser(
// Validate and sanitize user inputs to prevent XSS attacks
sanitizeUserInputs(pr);

Boolean hasIssue = detectedLicenseRepository.existsIssue(pr);
Boolean hasIssue = detectedLicenseRepository.existsByIssueIsTrueAndPullRequest(pr);

lpvsHistories.add(
new LPVSHistory(
Expand Down Expand Up @@ -289,11 +289,11 @@ public ResponseEntity<LPVSResult> resultPage(
LPVSPullRequest pr = prOpt.get();

List<LPVSLicense> distinctByLicense =
detectedLicenseRepository.findDistinctByLicense(pr);
detectedLicenseRepository.findDistinctLicenseByPullRequest(pr);
List<String> detectedLicenses = new ArrayList<>();
Map<String, Integer> licenseCountMap = new HashMap<>();

List<String> allSpdxId = licenseRepository.takeAllSpdxId();
List<String> allSpdxId = licenseRepository.findAllSpdxId();
for (String spdxId : allSpdxId) {
licenseCountMap.put(spdxId, 0);
}
Expand All @@ -313,7 +313,7 @@ public ResponseEntity<LPVSResult> resultPage(
detectedLicenseRepository.findByPullRequest(pr, pageable);
List<LPVSDetectedLicense> dlList = detectedLicenseRepository.findByPullRequest(pr);
List<LPVSResultFile> lpvsResultFileList = new ArrayList<>();
Boolean hasIssue = detectedLicenseRepository.existsIssue(pr);
Boolean hasIssue = detectedLicenseRepository.existsByIssueIsTrueAndPullRequest(pr);

String licenseSpdxId;
String status;
Expand Down Expand Up @@ -343,7 +343,7 @@ public ResponseEntity<LPVSResult> resultPage(
}
}

Long count = detectedLicenseRepository.CountByDetectedLicenseWherePullRequestId(pr);
Long count = detectedLicenseRepository.countByPullRequestAndLicenseIsNotNull(pr);
String[] tempPullNumber = pr.getPullRequestUrl().split("/");
LPVSResult lpvsResult =
new LPVSResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
Expand All @@ -28,10 +27,7 @@ public interface LPVSDetectedLicenseRepository extends JpaRepository<LPVSDetecte
* @param pr The {@link LPVSPullRequest} to check.
* @return {@code true} if at least one detected license with an issue is associated with the pull request, {@code false} otherwise.
*/
@Query(
value =
"select count(dl)>0 from LPVSDetectedLicense dl where dl.issue = True and dl.pullRequest = :pr")
Boolean existsIssue(@Param("pr") LPVSPullRequest pr);
Boolean existsByIssueIsTrueAndPullRequest(@Param("pr") LPVSPullRequest pr);

/**
* Find all detected licenses associated with a specific pull request.
Expand All @@ -56,30 +52,21 @@ public interface LPVSDetectedLicenseRepository extends JpaRepository<LPVSDetecte
* @param pr The {@link LPVSPullRequest} to count detected licenses for.
* @return The count of detected licenses where the license is not null.
*/
@Query(
value =
"select count(*) from LPVSDetectedLicense dl where dl.pullRequest = :pr and dl.license is not null")
Long CountByDetectedLicenseWherePullRequestId(@Param("pr") LPVSPullRequest pr);
Long countByPullRequestAndLicenseIsNotNull(@Param("pr") LPVSPullRequest pr);

/**
* Find distinct licenses associated with a specific pull request.
*
* @param pr The {@link LPVSPullRequest} to retrieve distinct licenses for.
* @return List of distinct {@link LPVSLicense} associated with the pull request.
*/
@Query(
value =
"select distinct dl.license from LPVSDetectedLicense dl where dl.pullRequest = :pr")
List<LPVSLicense> findDistinctByLicense(@Param("pr") LPVSPullRequest pr);
List<LPVSLicense> findDistinctLicenseByPullRequest(@Param("pr") LPVSPullRequest pr);

/**
* Find detected licenses associated with a specific pull request where the license is not null.
*
* @param pr The {@link LPVSPullRequest} to retrieve detected licenses for.
* @return List of {@link LPVSDetectedLicense} associated with the pull request where the license is not null.
*/
@Query(
value =
"select dl from LPVSDetectedLicense dl where dl.pullRequest = :pr and dl.license is not null")
List<LPVSDetectedLicense> findNotNullDLByPR(@Param("pr") LPVSPullRequest pr);
List<LPVSDetectedLicense> findByPullRequestAndLicenseIsNotNull(@Param("pr") LPVSPullRequest pr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Repository interface for managing {@link LPVSLicenseConflict} entities.
* Extends {@link org.springframework.data.jpa.repository.JpaRepository} for basic CRUD operations.
*/
@Repository
public interface LPVSLicenseConflictRepository extends JpaRepository<LPVSLicenseConflict, Long> {

/**
* Retrieve all license conflicts from the database.
*
* @return List of {@link LPVSLicenseConflict} entities representing license conflicts.
*/
@Query("SELECT lc FROM LPVSLicenseConflict lc")
List<LPVSLicenseConflict> takeAllLicenseConflicts();

/**
* Find a specific license conflict between two licenses.
*
Expand All @@ -40,7 +30,7 @@ public interface LPVSLicenseConflictRepository extends JpaRepository<LPVSLicense
"SELECT lc FROM LPVSLicenseConflict lc "
+ "WHERE (lc.repositoryLicense.licenseId = :license1 AND lc.conflictLicense.licenseId = :license2) "
+ "OR (lc.repositoryLicense.licenseId = :license2 AND lc.conflictLicense.licenseId = :license1) "
+ "ORDER BY lc.id DESC "
+ "ORDER BY lc.conflictId DESC "
+ "LIMIT 1")
LPVSLicenseConflict findLicenseConflict(
@Param("license1") Long license1, @Param("license2") Long license2);
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/com/lpvs/repository/LPVSLicenseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,13 @@
@Repository
public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long> {

/**
* Retrieve all licenses from the database.
*
* @return List of {@link LPVSLicense} entities representing licenses.
*/
@Query("SELECT l FROM LPVSLicense l")
List<LPVSLicense> takeAllLicenses();

/**
* Search for a license by SPDX identifier.
*
* @param spdxId The SPDX identifier of the license.
* @return The latest {@link LPVSLicense} entity with the specified SPDX identifier.
*/
@Query("SELECT l FROM LPVSLicense l WHERE l.spdxId = :spdxId ORDER BY l.id DESC LIMIT 1")
LPVSLicense searchBySpdxId(@Param("spdxId") String spdxId);
LPVSLicense findFirstBySpdxIdOrderByLicenseIdDesc(@Param("spdxId") String spdxId);

/**
* Search for a license by alternative license names.
Expand All @@ -46,14 +37,14 @@ public interface LPVSLicenseRepository extends JpaRepository<LPVSLicense, Long>
*/
@Query(
"SELECT l FROM LPVSLicense l WHERE (CONCAT(',', l.alternativeNames, ',') LIKE CONCAT('%,', :licenseName, ',%')) "
+ "ORDER BY l.id DESC LIMIT 1")
+ "ORDER BY l.licenseId DESC LIMIT 1")
LPVSLicense searchByAlternativeLicenseNames(@Param("licenseName") String licenseName);

/**
* Retrieve all SPDX identifiers of licenses from the database.
*
* @return List of SPDX identifiers as Strings.
*/
@Query(value = "select licenses.spdxId from LPVSLicense licenses")
List<String> takeAllSpdxId();
@Query(value = "select l.spdxId from LPVSLicense l")
List<String> findAllSpdxId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ LPVSPullRequest findLatestByPullRequestInfo(
* @param pageable The pagination information.
* @return Page of {@link LPVSPullRequest} entities with the specified base name.
*/
@Query(value = "select pr from LPVSPullRequest pr where pr.pullRequestBase = :name")
Page<LPVSPullRequest> findByPullRequestBase(@Param("name") String name, Pageable pageable);

/**
Expand All @@ -69,7 +68,6 @@ LPVSPullRequest findLatestByPullRequestInfo(
* @param name The name of the pull request base.
* @return List of {@link LPVSPullRequest} entities with the specified base name.
*/
@Query(value = "select pr from LPVSPullRequest pr where pr.pullRequestBase = :name")
List<LPVSPullRequest> findByPullRequestBase(@Param("name") String name);

/**
Expand All @@ -78,8 +76,7 @@ LPVSPullRequest findLatestByPullRequestInfo(
* @param name The name of the pull request base.
* @return The count of pull requests with the specified base name.
*/
@Query(value = "select count(*) from LPVSPullRequest pr where pr.pullRequestBase = :name")
Long CountByPullRequestBase(@Param("name") String name);
Long countByPullRequestBase(@Param("name") String name);

/**
* Find all pull requests with the specified sender or pull request head, paginated.
Expand Down Expand Up @@ -114,5 +111,5 @@ Page<LPVSPullRequest> findBySenderOrPullRequestHead(
@Query(
value =
"select count(*) from LPVSPullRequest pr where pr.sender = :name or pr.pullRequestHead = :name")
Long CountBySenderOrPullRequestHead(@Param("name") String name);
Long countBySenderOrPullRequestHead(@Param("name") String name);
}
14 changes: 1 addition & 13 deletions src/main/java/com/lpvs/repository/LPVSQueueRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@

import com.lpvs.entity.LPVSQueue;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* Repository interface for managing {@link LPVSQueue} entities.
* Extends {@link org.springframework.data.jpa.repository.JpaRepository} for basic CRUD operations.
*/
public interface LPVSQueueRepository extends JpaRepository<LPVSQueue, Long> {

/**
* Retrieve the list of all entities from the "lpvs_queue" table.
*
* @return List of {@link LPVSQueue} entities representing the queue.
*/
@Query("SELECT q FROM LPVSQueue q")
List<LPVSQueue> getQueueList();
}
public interface LPVSQueueRepository extends JpaRepository<LPVSQueue, Long> {}
12 changes: 9 additions & 3 deletions src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,19 @@ public void commentResults(
commitCommentBuilder.append("<li>" + conflict.l1 + " and " + conflict.l2 + "</li>");
LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense();
detectedIssue.setPullRequest(lpvsPullRequest);
Long l1 = lpvsLicenseRepository.searchBySpdxId(conflict.l1).getLicenseId();
Long l2 = lpvsLicenseRepository.searchBySpdxId(conflict.l2).getLicenseId();
Long l1 =
lpvsLicenseRepository
.findFirstBySpdxIdOrderByLicenseIdDesc(conflict.l1)
.getLicenseId();
Long l2 =
lpvsLicenseRepository
.findFirstBySpdxIdOrderByLicenseIdDesc(conflict.l2)
.getLicenseId();
detectedIssue.setLicenseConflict(
lpvsLicenseConflictRepository.findLicenseConflict(l1, l2));
if (webhookConfig.getRepositoryLicense() != null) {
LPVSLicense repoLicense =
lpvsLicenseRepository.searchBySpdxId(
lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(
webhookConfig.getRepositoryLicense());
if (repoLicense == null) {
repoLicense =
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public LPVSLicenseService(
* Load all license conflicts from the database.
*/
private void loadLicenseConflicts() {
List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
List<LPVSLicenseConflict> conflicts = lpvsLicenseConflictRepository.findAll();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
Expand Down Expand Up @@ -155,7 +154,7 @@ private void init() {
} else {
try {
// 1. Load licenses from DB
licenses = lpvsLicenseRepository.takeAllLicenses();
licenses = lpvsLicenseRepository.findAll();
// print info
log.info("LICENSES: loaded " + licenses.size() + " licenses from DB.");

Expand Down Expand Up @@ -186,7 +185,7 @@ private void init() {
*/
public void reloadFromTables() {
if (licenses.isEmpty()) {
licenses = lpvsLicenseRepository.takeAllLicenses();
licenses = lpvsLicenseRepository.findAll();
log.info("RELOADED " + licenses.size() + " licenses from DB.");

loadLicenseConflicts();
Expand Down Expand Up @@ -376,7 +375,8 @@ public List<Conflict<String, String>> findConflicts(
String repositoryLicense = webhookConfig.getRepositoryLicense();

if (repositoryLicense != null) {
LPVSLicense repoLicense = lpvsLicenseRepository.searchBySpdxId(repositoryLicense);
LPVSLicense repoLicense =
lpvsLicenseRepository.findFirstBySpdxIdOrderByLicenseIdDesc(repositoryLicense);
if (repoLicense == null) {
repoLicense =
lpvsLicenseRepository.searchByAlternativeLicenseNames(repositoryLicense);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/service/LPVSLoginCheckService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public HistoryPageEntity pathCheck(
if ((type.equals("own") && findNickName.equals(name))
|| (type.equals("org") && findOrganization.equals(name))) {
prPage = lpvsPullRequestRepository.findByPullRequestBase(name, pageable);
count = lpvsPullRequestRepository.CountByPullRequestBase(name);
count = lpvsPullRequestRepository.countByPullRequestBase(name);
} else if (type.equals("send") && findNickName.equals(name)) {
prPage = lpvsPullRequestRepository.findBySenderOrPullRequestHead(name, pageable);
count = lpvsPullRequestRepository.CountBySenderOrPullRequestHead(name);
count = lpvsPullRequestRepository.countBySenderOrPullRequestHead(name);
} else {
throw new WrongAccessException("WrongAccessException");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public BlockingDeque<LPVSQueue> getQueue() {
public void checkForQueue() throws InterruptedException {
QUEUE.clear();
log.debug("Checking for previous queue");
List<LPVSQueue> webhookConfigList = queueRepository.getQueueList();
List<LPVSQueue> webhookConfigList = queueRepository.findAll();
for (LPVSQueue webhook : webhookConfigList) {
log.info("Add WebHook id = " + webhook.getId() + " to the queue.");
QUEUE.putFirst(webhook);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lpvs/service/LPVSStatisticsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Dashboard getDashboardEntity(String type, String name, Authentication aut
List<DashboardElementsByDate> dashboardByDates = new ArrayList<>();
Map<LocalDate, List<LPVSPullRequest>> datePrMap = new HashMap<>();

List<String> allSpdxId = lpvsLicenseRepository.takeAllSpdxId();
List<String> allSpdxId = lpvsLicenseRepository.findAllSpdxId();
for (String spdxId : allSpdxId) {
licenseCountMap.put(spdxId, 0);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public Dashboard getDashboardEntity(String type, String name, Authentication aut
Set<String> senderSet = new HashSet<>();
for (LPVSPullRequest pr : entry.getValue()) {
List<LPVSDetectedLicense> dlList =
lpvsDetectedLicenseRepository.findNotNullDLByPR(pr);
lpvsDetectedLicenseRepository.findByPullRequestAndLicenseIsNotNull(pr);
if (!(pr.getRepositoryName() == null || pr.getRepositoryName().isEmpty())) {
senderSet.add(pr.getSender());
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/lpvs/controller/LPVSWebControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public void testNewHistoryPageByUser() {

when(loginCheckService.pathCheck(type, name, pageable, authentication))
.thenReturn(new HistoryPageEntity(prPage, 1L));
when(detectedLicenseRepository.existsIssue(pullRequest)).thenReturn(false);
when(detectedLicenseRepository.existsByIssueIsTrueAndPullRequest(pullRequest))
.thenReturn(false);

HistoryEntity result =
webController.new WebApiEndpoints()
Expand Down Expand Up @@ -229,12 +230,13 @@ public void testResultPage() {

when(loginCheckService.getMemberFromMemberMap(authentication)).thenReturn(new LPVSMember());
when(lpvsPullRequestRepository.findById(prId)).thenReturn(Optional.of(pullRequest));
when(detectedLicenseRepository.findDistinctByLicense(pullRequest)).thenReturn(licenses);
when(licenseRepository.takeAllSpdxId()).thenReturn(List.of("MIT", "Apache-2.0"));
when(detectedLicenseRepository.findDistinctLicenseByPullRequest(pullRequest))
.thenReturn(licenses);
when(licenseRepository.findAllSpdxId()).thenReturn(List.of("MIT", "Apache-2.0"));
when(detectedLicenseRepository.findByPullRequest(pullRequest, pageable))
.thenReturn(new PageImpl<>(detectedLicenses));
when(detectedLicenseRepository.findByPullRequest(pullRequest)).thenReturn(detectedLicenses);
when(detectedLicenseRepository.CountByDetectedLicenseWherePullRequestId(pullRequest))
when(detectedLicenseRepository.countByPullRequestAndLicenseIsNotNull(pullRequest))
.thenReturn(1L);

LPVSResult result =
Expand Down
Loading

0 comments on commit 313158b

Please sign in to comment.