Skip to content

Commit

Permalink
♻️ drag out checks.json
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbm committed Jun 8, 2021
1 parent 09b8629 commit 256b07a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 62 deletions.
File renamed without changes.
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ services:
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
- GITLAB_ACCESS_TOKEN=${GITLAB_ACCESS_TOKEN}
- server.port=8080
- CONFIG_FILE=/home/amos/config.json
volumes: # more like: load nginx configuration
- ./config.json:/home/amos/config.json
depends_on:
- database
expose:
Expand All @@ -37,15 +40,14 @@ services:
# build: "./frontend"
volumes: # more like: load nginx configuration
- ./nginx.conf:/etc/nginx/templates/default.conf.template
- ./server/src/main/resources/config.json:/config.json # FIXME just config.json nach dem rausziehen
- ./config.json:/config.json
environment:
- PORT=${PORT}
- HOST=${HOST}
depends_on:
- backend
ports: # only connection to outside world
- ${PORT}:80
- 443:443
- ${PORT}:80 # TODO https
links:
- backend

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { MatChip, MatChipList } from '@angular/material/chips';
import { OnInit } from '@angular/core';
import * as configFile from '../../../server/src/main/resources/config.json';
import * as configFile from '../../../config.json';
import { MatTableDataSource } from '@angular/material/table';
import { CommonModule } from '@angular/common';
import { MatDialog } from '@angular/material/dialog';
Expand Down Expand Up @@ -41,7 +41,7 @@ export class AppComponent implements OnInit {
chipOptions: string[];
filterInfo = 'Momentan sortierts nach Tag: - und Kategorie: -';
toggleToTrue = true;
csvExportLink = environment.baseURL + "/export/csv";
csvExportLink = environment.baseURL + '/export/csv';

@ViewChild('parent', { read: ViewContainerRef }) container: ViewContainerRef;

Expand Down
40 changes: 40 additions & 0 deletions server/src/main/java/amosproj/server/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package amosproj.server;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Service
public class Config {

private static String configFile;

public Config(@Value("${CONFIG_FILE}") String CONFIG_FILE) {
configFile = CONFIG_FILE;
}

/**
* Gets the config.json and parses it into a JsonNode
*
* @return JsonNode of the parsed config.json
*/
public static JsonNode getConfigNode() {
File file = new File(configFile);
assert file.exists();
try {
InputStream fileStream = new FileInputStream(file);
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
return objectMapper.readTree(fileStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
3 changes: 1 addition & 2 deletions server/src/main/java/amosproj/server/GitLab.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package amosproj.server;

import amosproj.server.linter.Linter;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -26,7 +25,7 @@ public class GitLab {

private String apitoken;

private String gitlabHost = Linter.getConfigNode().get("settings").get("gitLabHost").asText();
private String gitlabHost = Config.getConfigNode().get("settings").get("gitLabHost").asText();

private final org.gitlab4j.api.GitLabApi api;

Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/amosproj/server/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class Scheduler {
private TaskScheduler executor;

public void scheduling(final Runnable task) {
executor.schedule(task, new CronTrigger(Linter.getConfigNode().get("settings").get("crawler").get("scheduler").asText()));
executor.schedule(task, new CronTrigger(Config.getConfigNode().get("settings").get("crawler").get("scheduler").asText()));
}
}
4 changes: 2 additions & 2 deletions server/src/main/java/amosproj/server/api/CSVExport.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package amosproj.server.api;

import amosproj.server.Config;
import amosproj.server.data.CheckResult;
import amosproj.server.data.LintingResult;
import amosproj.server.data.LintingResultRepository;
import amosproj.server.data.ProjectRepository;
import amosproj.server.linter.Linter;
import com.opencsv.CSVWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void exportResults(Writer writer) throws IOException {
CSVWriter w = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.RFC4180_LINE_END);
// get all checkNames in arraylist
ArrayList<String> checks = new ArrayList<>();
for (Iterator<String> it = Linter.getConfigNode().get("checks").fieldNames(); it.hasNext(); ) {
for (Iterator<String> it = Config.getConfigNode().get("checks").fieldNames(); it.hasNext(); ) {
checks.add(it.next());
}
// write header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package amosproj.server.api.schemas;

import amosproj.server.Config;
import amosproj.server.data.CheckResult;
import amosproj.server.data.LintingResult;
import amosproj.server.linter.Linter;
import com.fasterxml.jackson.databind.JsonNode;
import org.gitlab4j.api.utils.JacksonJson;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -29,7 +28,7 @@ public class LintingResultSchema {
public LintingResultSchema(LintingResult lr) {
BeanUtils.copyProperties(lr, this);
this.checkResults = new LinkedList<>();
JsonNode config = Linter.getConfigNode();
JsonNode config = Config.getConfigNode();
if (lr.getCheckResults() != null)
for (CheckResult cr : lr.getCheckResults())
this.checkResults.add(new CheckResultSchema(cr, config.get("checks").get(cr.getCheckName())));
Expand Down
13 changes: 6 additions & 7 deletions server/src/main/java/amosproj/server/linter/Crawler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package amosproj.server.linter;

import amosproj.server.Config;
import amosproj.server.GitLab;
import amosproj.server.Scheduler;
import amosproj.server.api.schemas.CrawlerStatusSchema;
import com.fasterxml.jackson.databind.JsonNode;
import org.gitlab4j.api.GitLabApiException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -33,7 +32,7 @@ public Crawler(GitLab gitLab, Scheduler scheduler, Linter linter) {
this.linter = linter;

crawlerActive = new AtomicBoolean(false);
progress = Linter.getConfigNode().get("settings").get("crawler").get("status").get("inactive").asText();
progress = Config.getConfigNode().get("settings").get("crawler").get("status").get("inactive").asText();
timeTaken = 0L;
idx = 0L;
lastError = "";
Expand All @@ -48,11 +47,11 @@ public Crawler(GitLab gitLab, Scheduler scheduler, Linter linter) {
*/
private synchronized void runCrawler() {
crawlerActive = new AtomicBoolean(true);
progress = Linter.getConfigNode().get("settings").get("crawler").get("status").get("init").asText();
progress = Config.getConfigNode().get("settings").get("crawler").get("status").get("init").asText();
try {
var projects = gitLab.getApi().getProjectApi().getProjects(0, Linter.getConfigNode().get("settings").get("crawler").get("maxProjects").asInt(Integer.MAX_VALUE));
var projects = gitLab.getApi().getProjectApi().getProjects(0, Config.getConfigNode().get("settings").get("crawler").get("maxProjects").asInt(Integer.MAX_VALUE));
size = (long) projects.size();
progress = Linter.getConfigNode().get("settings").get("crawler").get("status").get("active").asText();
progress = Config.getConfigNode().get("settings").get("crawler").get("status").get("active").asText();
LocalDateTime start = LocalDateTime.now();

for (org.gitlab4j.api.models.Project proj : projects) {
Expand All @@ -67,7 +66,7 @@ private synchronized void runCrawler() {
errorTime = LocalDateTime.now();
e.printStackTrace();
}
progress = Linter.getConfigNode().get("settings").get("crawler").get("status").get("inactive").asText();
progress = Config.getConfigNode().get("settings").get("crawler").get("status").get("inactive").asText();
crawlerActive.set(false);
idx = 0L;
}
Expand Down
47 changes: 11 additions & 36 deletions server/src/main/java/amosproj/server/linter/Linter.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package amosproj.server.linter;

import amosproj.server.Config;
import amosproj.server.GitLab;
import amosproj.server.data.*;
import amosproj.server.linter.checks.CheckGitlabFiles;
import amosproj.server.linter.checks.CheckGitlabRoles;
import amosproj.server.linter.checks.CheckGitlabSettings;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.gitlab4j.api.GitLabApiException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Iterator;

Expand All @@ -23,19 +21,14 @@
@Service
public class Linter {

// autowired
private final GitLab api;
private final LintingResultRepository lintingResultRepository;
private final CheckResultRepository checkResultRepository;
private final ProjectRepository projectRepository;
// end autowired

public Linter(GitLab api, LintingResultRepository lintingResultRepository, CheckResultRepository checkResultRepository, ProjectRepository projectRepository) {
this.api = api;
this.lintingResultRepository = lintingResultRepository;
this.checkResultRepository = checkResultRepository;
this.projectRepository = projectRepository;
}
@Autowired
private GitLab api;
@Autowired
private LintingResultRepository lintingResultRepository;
@Autowired
private CheckResultRepository checkResultRepository;
@Autowired
private ProjectRepository projectRepository;

/**
* entry point to start asynchronous linting process
Expand Down Expand Up @@ -76,7 +69,7 @@ public void checkEverything(org.gitlab4j.api.models.Project apiProject, LocalDat
lintingResultRepository.save(lintingResult);

// Fuehre Checks aus
JsonNode checks = getConfigNode().get("checks");
JsonNode checks = Config.getConfigNode().get("checks");

var fileChecks = new CheckGitlabFiles(api, apiProject, lintingResult, checkResultRepository);
var settingsChecks = new CheckGitlabSettings(api, apiProject, lintingResult, checkResultRepository);
Expand All @@ -100,22 +93,4 @@ public void checkEverything(org.gitlab4j.api.models.Project apiProject, LocalDat

}


/**
* Gets the config.json and parses it into a JsonNode
*
* @return JsonNode of the parsed config.json
*/
public static JsonNode getConfigNode() {
ClassPathResource file = new ClassPathResource("config.json");
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
JsonNode node = null;
try {
node = objectMapper.readTree(file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return node;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package amosproj.server.linter.checks;

import amosproj.server.Config;
import amosproj.server.GitLab;
import amosproj.server.data.CheckResultRepository;
import amosproj.server.data.LintingResult;
import amosproj.server.linter.Linter;
import com.fasterxml.jackson.databind.JsonNode;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
Expand All @@ -26,6 +26,7 @@
public class CheckGitlabFiles extends Check {

private GitLabApi api;

public CheckGitlabFiles(GitLab gitLab, Project project, LintingResult lintingResult, CheckResultRepository checkResultRepository) {
super(gitLab, project, lintingResult, checkResultRepository);
this.api = gitLab.getApi();
Expand Down Expand Up @@ -108,7 +109,7 @@ public boolean checkNoContributingChain() {
*/
public boolean checkReadmeHasLinks() {
// generiere regex
JsonNode links = Linter.getConfigNode().get("settings").get("readMeLinks");
JsonNode links = Config.getConfigNode().get("settings").get("readMeLinks");
for (JsonNode it : links) {
String link = it.asText();
final Pattern pattern = Pattern.compile(link, Pattern.MULTILINE);
Expand Down Expand Up @@ -144,7 +145,7 @@ public boolean eitherOwnersOrMaintainersExist() {

public boolean notDefaultReadme() {
String defaultReadme = "# " + project.getName(); // Sollte sich die Default Readme ändern, diese Zeile
// anpassen. Dabei "\n" und "\r" ignorieren.
// anpassen. Dabei "\n" und "\r" ignorieren.
if (!checkReadmeExistence())
return false;
URI uri = getRawReadme();
Expand All @@ -153,7 +154,7 @@ public boolean notDefaultReadme() {
Scanner scanner = new Scanner(uri.toURL().openStream());
String line = "";
while (scanner.hasNextLine()) {
line += scanner.nextLine();
line += scanner.nextLine();
}

if (line.equals(defaultReadme)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package amosproj.server.api.schemas;

import amosproj.server.Config;
import amosproj.server.data.CheckResult;
import amosproj.server.data.CheckSeverity;
import amosproj.server.linter.Linter;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa;
Expand All @@ -18,7 +18,7 @@ public class CheckResultSchemaTest {

@Test
public void testCheckResultSchema() {
JsonNode node = Linter.getConfigNode().get("checks").get("checkReadmeExistence");
JsonNode node = Config.getConfigNode().get("checks").get("checkReadmeExistence");
CheckResult checkResult = new CheckResult(null, "checkReadmeExistence", true);
CheckResultSchema checkResultSchema = new CheckResultSchema(checkResult, node);
// some assertions
Expand Down

0 comments on commit 256b07a

Please sign in to comment.