main update for script management

add / modify / delete and push/pull to git
version_2
floxx2112 2 years ago
parent 8ecc9bd066
commit bd23448f42
  1. 5
      pom.xml
  2. 5
      src/main/java/com/apside/assistDbBackend/AssistDBBackendApplication.java
  3. 43
      src/main/java/com/apside/assistDbBackend/controller/ScriptController.java
  4. 20
      src/main/java/com/apside/assistDbBackend/model/Script.java
  5. 118
      src/main/java/com/apside/assistDbBackend/service/ScriptsService.java
  6. 3
      src/main/resources/application.properties

@ -61,6 +61,11 @@
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.4.0.202211300538-r</version>
</dependency>
</dependencies>
<build>

@ -2,20 +2,23 @@ package com.apside.assistDbBackend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
@SpringBootApplication
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class AssistDBBackendApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(AssistDBBackendApplication.class, args);
}
@Bean

@ -0,0 +1,43 @@
package com.apside.assistDbBackend.controller;
import com.apside.assistDbBackend.model.Script;
import com.apside.assistDbBackend.service.ScriptsService;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
@RestController
@RequestMapping("/api")
public class ScriptController {
@Autowired
ScriptsService scriptsService;
@GetMapping("/scripts")
public List<Script> getAllScripts() throws IOException, GitAPIException {
return scriptsService.retrieveScripts();
}
@DeleteMapping("/script/delete/{name}")
public void deleteScript(@PathVariable("name") final String name){
scriptsService.deleteOneScript(name);
}
@GetMapping("/script/add")
public void addScript(@RequestParam("content") String content, @RequestParam("name") String name) throws IOException, GitAPIException, URISyntaxException {
scriptsService.addOneScript(content, name);
}
@GetMapping("/script/edit")
public void editScript(@RequestParam("content") String content, @RequestParam("defaultname") String defaultName, @RequestParam("newname") String newName) throws IOException, GitAPIException, URISyntaxException {
scriptsService.simpleDeleteScript(defaultName);
scriptsService.addOneScript(content, newName);
}
}

@ -0,0 +1,20 @@
package com.apside.assistDbBackend.model;
import jakarta.persistence.Entity;
import lombok.Data;
@Data
public class Script {
private String extension;
private String name;
private String data;
public Script(){}
public Script(String extension, String name, String data){
this.extension = extension;
this.name = name;
this.data = data;
}
}

@ -0,0 +1,118 @@
package com.apside.assistDbBackend.service;
import com.apside.assistDbBackend.model.Script;
import lombok.Data;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@Data
@Service
public class ScriptsService {
public List<Script> retrieveScripts() throws IOException, GitAPIException {
pullFromGit();
List<Script> listOfScripts = new ArrayList<>();
//Creating a File object for directory
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
//List of all files and directories
File scriptDirectory = new File(tempDirectoryPath);
String contents[] = scriptDirectory.list();
for(int i=0; i<contents.length; i++) {
Path filePath = Path.of(tempDirectoryPath + "/" + contents[i]);
String allData = Files.readString(filePath);
String extension = contents[i].substring(contents[i].lastIndexOf(".") + 1);
Script tempScript = new Script(extension, contents[i], allData);
listOfScripts.add(tempScript);
//System.out.println(contents[i] + " Extension : " + extension);
}
return listOfScripts;
}
public void deleteOneScript(final String name){
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
pushToGit();
pullFromGit();
} catch (Exception e){
e.printStackTrace();
}
}
public void simpleDeleteScript(final String name){
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
} catch (Exception e){
e.printStackTrace();
}
}
public void addOneScript(final String content, final String name) throws IOException, GitAPIException, URISyntaxException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
File newFile = new File(tempDirectoryPath + "/" + name);
newFile.createNewFile();
FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name);
writerDataFile.write(content);
writerDataFile.close();
pushToGit();
}
@Value("${USERNAME_GIT}")
private String userGit;
@Value("${ACCESS_TOKEN_GIT}")
private String accesToken;
public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
Git git = Git.open(new File(tempDirectoryPath));
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
// you can add more settings here if needed
remoteAddCommand.call();
git.add().setUpdate(true).addFilepattern(".").call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
}
public void pullFromGit() throws IOException, GitAPIException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
Git git = Git.open(new File(tempDirectoryPath));
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
}
}

@ -5,5 +5,8 @@ spring.datasource.username=root
spring.datasource.password=Pompom.21
server.port=9001
USERNAME_GIT = fhibert@apside.fr
ACCESS_TOKEN_GIT = e54612b6d1d73eef4f0a49c88b0e35ccf02d45eb
logging.level.org.springframework.boot.web.embedded.tomcat=INFO
logging.level.org.springframework=error

Loading…
Cancel
Save