Skip to content

Commit

Permalink
refactor: Remove code duplicates
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Apr 25, 2024
1 parent fea4a5d commit 850ebcd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 51 deletions.
47 changes: 22 additions & 25 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ public LPVSLicenseService(
this.exitHandler = exitHandler;
}

/**
* Load all license conflicts from the database.
*/
private void loadLicenseConflicts() {
List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
}

/**
* Initializes the LPVSLicenseService by loading licenses and license conflicts from the database.
*/
Expand Down Expand Up @@ -129,17 +146,7 @@ private void init() {
licenseConflicts = new ArrayList<>();

if (licenseConflictsSource.equalsIgnoreCase("db")) {
List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
loadLicenseConflicts();
// print info
log.info(
"LICENSE CONFLICTS: loaded "
Expand All @@ -163,20 +170,10 @@ private void init() {
public void reloadFromTables() {
if (licenses.isEmpty()) {
licenses = lpvsLicenseRepository.takeAllLicenses();
log.info("LOADED " + licenses.size() + " licenses from DB.");

List<LPVSLicenseConflict> conflicts =
lpvsLicenseConflictRepository.takeAllLicenseConflicts();
for (LPVSLicenseConflict conflict : conflicts) {
Conflict<String, String> conf =
new Conflict<>(
conflict.getConflictLicense().getSpdxId(),
conflict.getRepositoryLicense().getSpdxId());
if (!licenseConflicts.contains(conf)) {
licenseConflicts.add(conf);
}
}
log.info("LOADED " + licenseConflicts.size() + " license conflicts from DB.");
log.info("RELOADED " + licenses.size() + " licenses from DB.");

loadLicenseConflicts();
log.info("RELOADED " + licenseConflicts.size() + " license conflicts from DB.");
}
}

Expand Down
41 changes: 15 additions & 26 deletions src/main/java/com/lpvs/util/LPVSWebhookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@ public static boolean checkPayload(String payload) {
}

/**
* Retrieves the organization name from the repository URL in the LPVSQueue object.
* Checks if the given LPVSQueue object is not null and has a non-null repository URL.
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The organization name.
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the repository URL is absent.
* @param webhookConfig the LPVSQueue object to check
* @throws IllegalArgumentException If the webhook configuration is null or if the repository URL is absent.
*/
public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
private static void checkWebhookConfig(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
Expand All @@ -125,7 +124,16 @@ public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}
}

/**
* Retrieves the organization name from the repository URL in the LPVSQueue object.
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The organization name.
*/
public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
checkWebhookConfig(webhookConfig);
List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
return url.get(url.size() - 2);
}
Expand All @@ -135,19 +143,9 @@ public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
*
* @param webhookConfig LPVSQueue object containing repository information.
* @return The repository name.
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the repository URL is absent.
*/
public static String getRepositoryName(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}

checkWebhookConfig(webhookConfig);
List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
return url.get(url.size() - 1);
}
Expand Down Expand Up @@ -175,16 +173,7 @@ public static String getRepositoryUrl(LPVSQueue webhookConfig) {
* @throws IllegalArgumentException If the provided LPVSQueue object is null or if the pull request URL is absent.
*/
public static String getPullRequestId(LPVSQueue webhookConfig) {
if (null == webhookConfig) {
log.error("Webhook Config is absent");
throw new IllegalArgumentException("Webhook is absent");
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository URL info in webhook config");
throw new IllegalArgumentException("No repository URL info in webhook config");
}

checkWebhookConfig(webhookConfig);
if (null == webhookConfig.getPullRequestUrl()) {
log.error("Pull Request URL is absent in webhook config");
throw new IllegalArgumentException("Pull Request URL is absent in webhook config");
Expand Down

0 comments on commit 850ebcd

Please sign in to comment.