Commit a69c6683 authored by Dmitry Ermakov's avatar Dmitry Ermakov

Merge branch 'issue-28' into 'master'

Загрузка шаблонов из гитлаба

See merge request !4
parents e04aad89 81f16056
Pipeline #366 failed with stage
......@@ -29,8 +29,10 @@ services:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/template_db
- KEYCLOAK_URI=https://idp.applatform.qaztech.gov.kz
- KEYCLOAK_CLIENT_SECRET=1NYLMNlWXpPDW3QKeZ4VjlY76DuzNtaB
- GITLAB_BASE_PATH=http://gitlab.lan.arta.kz
- GITLAB_TOKEN=zif-LhgdzuLtpNW7uxYs
networks:
printform:
external: true
name: printform
\ No newline at end of file
name: printform
package kz.project.printedFormsService.config;
import kz.project.printedFormsService.exception.ValidationException;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
......@@ -45,4 +46,18 @@ public class SecurityContextUtils {
)
);
}
public static String getGitlabProjectBranchFromRole() throws ValidationException {
return SecurityContextHolder
.getContext()
.getAuthentication()
.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.filter(role -> role.contains("gitlab"))
.map(role -> role.replace("ROLE_gitlab_", ""))
.findFirst()
.orElseThrow(() -> new ValidationException("Не указана роль для доступа к gitlab"));
}
}
......@@ -42,15 +42,19 @@ public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationTo
private Collection<GrantedAuthority> extractAuthorities(Jwt jwt) {
if (jwt.getClaim("resource_access") != null) {
Map<String, Map<String,Object>> resourceAccess = jwt.getClaim("resource_access");
if (resourceAccess.containsKey("print_form")){
Map<String, Map<String, Object>> resourceAccess = jwt.getClaim("resource_access");
if (resourceAccess.containsKey("print_form")) {
ObjectMapper mapper = new ObjectMapper();
List<String> roles = mapper.convertValue(resourceAccess.get("print_form").get("roles"), new TypeReference<>() {
});
Set<GrantedAuthority> authorities = new HashSet<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.substring(role.lastIndexOf('_')+1)));
if (role.contains("gitlab")) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
} else {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.substring(role.lastIndexOf('_') + 1)));
}
}
return authorities;
......
......@@ -4,9 +4,9 @@ import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;
public interface GitlabClient {
Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath);
Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath, String branch);
Mono<String> downloadFileContentAsString(String projectName, String filePath);
Mono<String> downloadFileContentAsString(String projectName, String filePath, String branch);
MultipartFile downloadFileAsMultipartFile(String projectName, String filePath);
MultipartFile downloadFileAsMultipartFile(String projectName, String filePath, String branch);
}
......@@ -24,12 +24,12 @@ public class GitlabClientImpl implements GitlabClient {
//http://gitlab.lan.arta.kz/d.ermakov/templates/-/raw/master/dto.txt?inline=false
@Override
public Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath) {
public Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get()
.uri(uriBuilder -> uriBuilder
.path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from
.queryParam("ref", branch) // or the branch you want to fetch from
.build())
.retrieve()
.bodyToMono(byte[].class)
......@@ -37,12 +37,12 @@ public class GitlabClientImpl implements GitlabClient {
}
@Override
public Mono<String> downloadFileContentAsString(String projectName, String filePath) {
public Mono<String> downloadFileContentAsString(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get()
.uri(uriBuilder -> uriBuilder
.path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from
.queryParam("ref", branch) // or the branch you want to fetch from
.build())
.retrieve()
.bodyToMono(String.class)
......@@ -50,12 +50,12 @@ public class GitlabClientImpl implements GitlabClient {
}
@Override
public MultipartFile downloadFileAsMultipartFile(String projectName, String filePath) {
public MultipartFile downloadFileAsMultipartFile(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get()
.uri(uriBuilder -> uriBuilder
.path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from
.queryParam("ref", branch) // or the branch you want to fetch from
.build())
.retrieve()
.bodyToMono(byte[].class)
......
package kz.project.printedFormsService.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import kz.project.printedFormsService.config.SecurityContextUtils;
import kz.project.printedFormsService.data.dto.GitlabUploadRequest;
import kz.project.printedFormsService.data.dto.TemplateDto;
import kz.project.printedFormsService.data.dto.TemplateResponseDto;
......@@ -28,16 +29,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
@SneakyThrows
@Override
public TemplateDto saveFromGitlab(GitlabUploadRequest gitlabUploadRequest) {
String gitlabBranch = SecurityContextUtils.getGitlabProjectBranchFromRole();
List<MultipartFile> filesToSave = new ArrayList<>();
filesToSave.add(gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDataPath()
gitlabUploadRequest.getFileDataPath(),
gitlabBranch
));
String dtoString = gitlabClient.downloadFileContentAsString(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDtoPath()
gitlabUploadRequest.getFileDtoPath(),
gitlabBranch
)
.blockOptional()
.orElseThrow();
......@@ -48,7 +52,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
filesToSave.add(
gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileHeaderPath()
gitlabUploadRequest.getFileHeaderPath(),
gitlabBranch
)
);
}
......@@ -59,16 +64,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
@SneakyThrows
@Override
public TemplateResponseDto updateFromGitlab(GitlabUploadRequest gitlabUploadRequest) {
String gitlabBranch = SecurityContextUtils.getGitlabProjectBranchFromRole();
List<MultipartFile> filesToSave = new ArrayList<>();
filesToSave.add(gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDataPath()
gitlabUploadRequest.getFileDataPath(),
gitlabBranch
));
String dtoString = gitlabClient.downloadFileContentAsString(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDtoPath()
gitlabUploadRequest.getFileDtoPath(),
gitlabBranch
)
.blockOptional()
.orElseThrow();
......@@ -79,7 +87,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
filesToSave.add(
gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileHeaderPath()
gitlabUploadRequest.getFileHeaderPath(),
gitlabBranch
)
);
}
......
......@@ -84,6 +84,5 @@ process-logger:
error-prefix: "Неуспешный процесс: "
gitlab:
base-url: http://gitlab.lan.arta.kz
token: zif-LhgdzuLtpNW7uxYs
base-url: ${GITLAB_BASE_PATH:http://gitlab.lan.arta.kz}
token: ${GITLAB_TOKEN:zif-LhgdzuLtpNW7uxYs}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment