Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Sonar Bugs and some Criticals #316

Merged
merged 10 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ sonar.typescript.lcov.reportPaths=target/test-results/lcov.info
sonar.sourceEncoding=UTF-8
sonar.exclusions=src/main/webapp/content/**/*.*, src/main/webapp/i18n/*.js, target/classes/static/**/*.*

sonar.cpd.exclusions=src/**/domain/**/*.*, src/**/dto/**/*.*
sonar.test.exclusions=src/test/java/**/*.*
sonar.coverage.exclusions=**src/test/**/*.*

sonar.issue.ignore.multicriteria=S3437,S4502,S4684,UndocumentedApi,BoldAndItalicTagsCheck
# Rule https://sonarcloud.io/coding_rules?open=squid%3AS3437&rule_key=squid%3AS3437 is ignored, as a JPA-managed field cannot be transient
sonar.issue.ignore.multicriteria.S3437.resourceKey=src/main/java/**/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,28 @@ public class ApplicationProperties {

private JhipsterCmd jhipsterCmd = new JhipsterCmd();

private NpmCmd npmCmd = new NpmCmd();

private final Github github = new Github();

private final Gitlab gitlab = new Gitlab();

private final Mail mail = new Mail();

private String tmpFolder = "/tmp";
private String tmpFolder = System.getProperty("java.io.tmpdir");

public JhipsterCmd getJhipsterCmd() {
return jhipsterCmd;
}

public NpmCmd getNpmCmd() {
return npmCmd;
}

public void setNpmCmd(NpmCmd npmCmd) {
this.npmCmd = npmCmd;
}

public void setJhipsterCmd(JhipsterCmd jhipsterCmd) {
this.jhipsterCmd = jhipsterCmd;
}
Expand Down Expand Up @@ -89,6 +99,28 @@ public void setTimeout(Integer timeout) {
}
}

public static class NpmCmd {

private String cmd = "npm";
private Integer timeout = 120;

public String getCmd() {
return cmd;
}

public void setCmd(String cmd) {
this.cmd = cmd;
}

public Integer getTimeout() {
return timeout;
}

public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
}

public static class Github {

private String clientId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public SpringLiquibase liquibase(
ObjectProvider<DataSource> dataSource,
DataSourceProperties dataSourceProperties
) {
// If you don't want Liquibase to start asynchronously, substitute by this:
// SpringLiquibase liquibase = SpringLiquibaseUtil.createSpringLiquibase(liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties);
// If you don't want Liquibase to start asynchronously, substitute by this: //NOSONAR
// SpringLiquibase liquibase = SpringLiquibaseUtil.createSpringLiquibase(liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties); //NOSONAR
SpringLiquibase liquibase = SpringLiquibaseUtil.createAsyncSpringLiquibase(
this.env,
executor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public void configureCiCd(
this.gitService.createBranch(git, branchName);

this.logsService.addLog(ciCdId, "Generating Continuous Integration configuration");
//this.jHipsterService.installNpmDependencies(ciCdId, workingDir);
this.jHipsterService.addCiCd(ciCdId, workingDir, ciCdTool);

this.gitService.addAllFilesToRepository(git, workingDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,6 +39,14 @@
@Service
public class GeneratorService {

public static final String JHIPSTER = "jhipster";

public static final String APPLICATIONS = "applications";

public static final String OS_TEMP_DIR = System.getProperty("java.io.tmpdir");

public static final String FILE_SEPARATOR = System.getProperty("file.separator");

private final Logger log = LoggerFactory.getLogger(GeneratorService.class);

private final ApplicationProperties applicationProperties;
Expand Down Expand Up @@ -87,7 +96,9 @@ public void generateGitApplication(
}

private File generateApplication(String applicationId, String applicationConfiguration) throws IOException {
File workingDir = new File(applicationProperties.getTmpFolder() + "/jhipster/applications/" + applicationId);
final String fromConfig = applicationProperties.getTmpFolder();
final String tempDir = StringUtils.isBlank(fromConfig) ? OS_TEMP_DIR : fromConfig;
final File workingDir = new File(String.join(FILE_SEPARATOR, tempDir, JHIPSTER, APPLICATIONS, applicationId));
FileUtils.forceMkdir(workingDir);
this.generateYoRc(applicationId, workingDir, applicationConfiguration);
log.info(".yo-rc.json created");
Expand All @@ -98,12 +109,10 @@ private File generateApplication(String applicationId, String applicationConfigu

private void generateYoRc(String applicationId, File workingDir, String applicationConfiguration) throws IOException {
this.logsService.addLog(applicationId, "Creating `.yo-rc.json` file");
try (PrintWriter writer = new PrintWriter(workingDir + "/.yo-rc.json", StandardCharsets.UTF_8)) {
writer.print(applicationConfiguration);
} catch (IOException ioe) {
log.error("Error creating file .yo-rc.json", ioe);
throw ioe;
}
// removed the catch/log/throw since the exception is handled in calling code.
PrintWriter writer = new PrintWriter(workingDir + "/.yo-rc.json", StandardCharsets.UTF_8);
writer.print(applicationConfiguration);
writer.close();
}

private void zipResult(File workingDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void pushNewApplicationToGit(User user, File workingDir, String organizat
remoteAddCommand.call();

String currentBranch = git.getRepository().getFullBranch();
if (currentBranch.equals("refs/heads/master")) {
if ("refs/heads/master".equals(currentBranch)) {
git.branchRename().setNewName("main").call();
}
this.push(git, workingDir, user, organization, applicationName, gitProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ public class JHipsterService {

private final String jhipsterCommand;

private final String npmCommand;

private final Integer timeout;

public JHipsterService(LogsService logsService, ApplicationProperties applicationProperties, Executor taskExecutor) {
this.logsService = logsService;
this.taskExecutor = taskExecutor;

jhipsterCommand = applicationProperties.getJhipsterCmd().getCmd();
npmCommand = applicationProperties.getNpmCmd().getCmd();
timeout = applicationProperties.getJhipsterCmd().getTimeout();

log.info("JHipster service will be using \"{}\" to run generator-jhipster.", jhipsterCommand);
}

public void installNpmDependencies(String generationId, File workingDir) throws IOException {
this.logsService.addLog(generationId, "Installing the JHipster version used by the project");
this.runProcess(generationId, workingDir, "npm", "install", "--ignore-scripts", "--package-lock-only");
this.runProcess(generationId, workingDir, npmCommand, "install", "--ignore-scripts", "--package-lock-only");
}

public void generateApplication(String generationId, File workingDir) throws IOException {
Expand Down Expand Up @@ -106,40 +109,30 @@ public void addCiCd(String generationId, File workingDir, CiCdTool ciCdTool) thr

void runProcess(String generationId, File workingDir, String... command) throws IOException {
log.info("Running command: \"{}\" in directory: \"{}\"", command, workingDir);
BufferedReader input = null;
try {
String line;
ProcessBuilder processBuilder = new ProcessBuilder()
.directory(workingDir)
.command(command)
.redirectError(ProcessBuilder.Redirect.DISCARD);
Process p = processBuilder.start();

taskExecutor.execute(
() -> {
try {
p.waitFor(timeout, TimeUnit.SECONDS);
p.destroyForcibly();
} catch (InterruptedException e) {
log.error("Unable to execute process successfully.", e);
Thread.currentThread().interrupt();
}
ProcessBuilder processBuilder = new ProcessBuilder()
.directory(workingDir)
.command(command)
.redirectError(ProcessBuilder.Redirect.DISCARD);
Process p = processBuilder.start();

taskExecutor.execute(
() -> {
try {
p.waitFor(timeout, TimeUnit.SECONDS);
p.destroyForcibly();
} catch (InterruptedException e) {
log.error("Unable to execute process successfully.", e);
Thread.currentThread().interrupt();
}
);

input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
log.debug(line);
this.logsService.addLog(generationId, line);
}
);

input.close();
} catch (Exception e) {
log.error("Error while running the process", e);
if (input != null) {
input.close();
}
throw e;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
log.debug(line);
this.logsService.addLog(generationId, line);
}
input.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public void applyJdl(
);

this.logsService.addLog(applyJdlId, "Generating entities from JDL Model");
//this.jHipsterService.installNpmDependencies(applyJdlId, workingDir);
this.jHipsterService.runImportJdl(applyJdlId, workingDir, this.kebabCaseJdlName(jdlMetadata));

this.gitService.addAllFilesToRepository(git, workingDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class StatisticsService {

private final Logger log = LoggerFactory.getLogger(StatisticsService.class);

public static final String GENERATOR_IDENTITY_WAS_NOT_CORRECTLY_CREATED = "GeneratorIdentity {} was not correctly created";

private final YoRCService yoRCService;

private final GeneratorIdentityService generatorIdentityService;
Expand Down Expand Up @@ -118,7 +120,7 @@ public void addEntry(String entry, String host) throws IOException {
generatorIdentity.get().host(host);
yorc.owner(generatorIdentity.get());
} else {
log.info("GeneratorIdentity {} was not correctly created", generatorGuid);
log.info(GENERATOR_IDENTITY_WAS_NOT_CORRECTLY_CREATED, generatorGuid);
}

yoRCService.save(yoRCMapper.toDto(yorc));
Expand All @@ -138,7 +140,7 @@ public void addSubGenEvent(SubGenEventDTO subGenEventDTO, String generatorGuid)
if (generatorIdentity.isPresent()) {
subGenEvent.owner(generatorIdentity.get());
} else {
log.info("GeneratorIdentity {} was not correctly created", generatorGuid);
log.info(GENERATOR_IDENTITY_WAS_NOT_CORRECTLY_CREATED, generatorGuid);
}
subGenEventService.save(subGenEventMapper.toDto(subGenEvent));
}
Expand All @@ -157,7 +159,7 @@ public void addEntityStats(EntityStatsDTO entityStatsDTO, String generatorGuid)
if (generatorIdentity.isPresent()) {
entityStats.owner(generatorIdentity.get());
} else {
log.info("GeneratorIdentity {} was not correctly created", generatorGuid);
log.info(GENERATOR_IDENTITY_WAS_NOT_CORRECTLY_CREATED, generatorGuid);
}
entityStatsService.save(entityStatsMapper.toDto(entityStats));
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/github/jhipster/online/service/YoRCService.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ public void save(String applicationConfiguration) {
JsonNode jsonNodeRoot = mapper.readTree(applicationConfiguration);
JsonNode jsonNodeGeneratorJHipster = jsonNodeRoot.get("generator-jhipster");
YoRCDTO yorc = mapper.treeToValue(jsonNodeGeneratorJHipster, YoRCDTO.class);

// TODO: do something about apps that are generated by jhonline
// -> Optout settings
// -> What should be the guid for such case ?
// yorc.setOwner( ### );
/**
* TODO: do something about apps that are generated by jhonline
* -> Optout settings
* -> What should be the guid for such case ?
* yorc.setOwner( ### );
*/
save(yorc);

log.debug("Parsed json:\n{}", yorc);
} catch (IOException e) {
log.error("Generation failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ResponseEntity<String> configureCiCd(
organizationName = SanitizeInputs.sanitizeInput(organizationName);
ciCdTool = SanitizeInputs.sanitizeInput(ciCdTool);
boolean isGitHub = gitProvider.equalsIgnoreCase("github");
log.info("Configuring CI: {} on " + (isGitHub ? "GitHub" : "GitLab") + " {}/{}", ciCdTool, organizationName, projectName);
log.info("Configuring CI: {} on {} {}/{}", ciCdTool, (isGitHub ? "GitHub" : "GitLab"), organizationName, projectName);
User user = userService.getUser();
String ciCdId = "ci-" + System.nanoTime();

Expand Down
Loading