CleanService

This commit is contained in:
uPagge 2021-02-08 20:15:20 +03:00
parent cb9ddea57e
commit 2b8c44d17f
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
19 changed files with 231 additions and 174 deletions

View File

@ -2,6 +2,7 @@ package org.sadtech.bot.gitlab.app.scheduler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sadtech.bot.gitlab.context.service.CleanService;
import org.sadtech.bot.gitlab.core.service.parser.MergeRequestParser;
import org.sadtech.bot.gitlab.core.service.parser.NoteParser;
import org.sadtech.bot.gitlab.core.service.parser.PipelineParser;
@ -21,6 +22,7 @@ public class SchedulerService {
private final PipelineParser pipelineParser;
private final MergeRequestParser mergeRequestParser;
private final NoteParser noteParser;
private final CleanService cleanService;
@Scheduled(cron = "*/30 * * * * *")
public void newMergeRequest() {
@ -52,4 +54,10 @@ public class SchedulerService {
pipelineParser.scanOldPipeline();
}
@Scheduled(cron = "0 */1 * * * *")
public void clean() {
cleanService.cleanOldPipelines();
cleanService.cleanMergedPullRequests();
}
}

View File

@ -1,68 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue"></script>
<meta charset="UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Авторизатор BitbucketBot</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link href="static/css/signin.css" rel="stylesheet">
</head>
<body>
<div id="app">
<div class="form-signin">
<h1 class="h3 mb-3 font-weight-normal">Авторизатор</h1>
<label for="telegramId" class="sr-only">Введите что-то тут...</label>
<input type="text" v-model="telegramId" id="telegramId" name="telegramId" class="form-control"
placeholder="Telegram Id">
<label for="login" class="sr-only">Введите что-то тут...</label>
<input type="text" v-model="login" id="login" name="login" class="form-control" placeholder="Username">
<label for="token" class="sr-only">И еще что то тут...</label>
<input type="text" v-model="token" id="token" class="form-control" placeholder="Token">
<button @click="auth" class="btn btn-lg btn-primary btn-block" style="margin: 10px">Забубениться</button>
<span>{{responseMessage}}</span>
<p class="mt-5 mb-3 text-muted">© 2020 Made by @DR_Demons for @uPagge bitbucketbot</p>
</div>
</div>
<script>
'use strict'
new Vue({
el: '#app',
data: function () {
return {
telegramId: '',
login: '',
token: '',
responseMessage: ''
}
},
methods: {
auth: async function () {
const url = '/api/user/reg';
const data = {
telegramId: this.telegramId,
login: this.login,
token: this.token,
responseMessage: this.responseMessage
};
try {
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
}
});
const json = await response.json();
console.log('Успех:', JSON.stringify(json));
} catch (error) {
console.error('Ошибка:', error);
}
console.log(4)
}
}
})
</script>
</body>
</html>

View File

@ -1,50 +0,0 @@
html,
body {
height: 100%;
}
body {
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-ms-flex-align: center;
-ms-flex-pack: center;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.form-signin {
width: 100%;
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .checkbox {
font-weight: 400;
}
.form-signin .form-control {
margin: 10px;
position: relative;
box-sizing: border-box;
height: auto;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

View File

@ -12,12 +12,17 @@ import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
@ -90,8 +95,8 @@ public class MergeRequest implements BasicEntity<Long> {
@Column(name = "date_last_commit")
private LocalDateTime dateLastCommit;
// @JoinTable
// @OneToMany(fetch = FetchType.LAZY)
// private List<Note> notes = new ArrayList<>();
@JoinTable
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
private List<Note> notes = new ArrayList<>();
}

View File

@ -0,0 +1,26 @@
package org.sadtech.bot.gitlab.context.domain.filter;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDateTime;
/**
* // TODO: 08.02.2021 Добавить описание.
*
* @author upagge 08.02.2021
*/
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PipelineFilter {
private LocalDateTime lessThanCreatedDate;
}

View File

@ -6,6 +6,7 @@ import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.SimpleManagerRepository;
import org.sadtech.haiti.filter.FilterOperation;
import java.util.Set;
@ -14,7 +15,7 @@ import java.util.Set;
*
* @author upagge 17.01.2021
*/
public interface PipelineRepository extends SimpleManagerRepository<Pipeline, Long> {
public interface PipelineRepository extends SimpleManagerRepository<Pipeline, Long>, FilterOperation<Pipeline> {
Sheet<Pipeline> findAllByStatuses(@NonNull Set<PipelineStatus> statuses, @NonNull Pagination pagination);
}

View File

@ -0,0 +1,14 @@
package org.sadtech.bot.gitlab.context.service;
/**
* // TODO: 08.02.2021 Добавить описание.
*
* @author upagge 08.02.2021
*/
public interface CleanService {
void cleanMergedPullRequests();
void cleanOldPipelines();
}

View File

@ -7,7 +7,6 @@ import org.sadtech.bot.gitlab.context.domain.filter.MergeRequestFilter;
import org.sadtech.haiti.context.service.SimpleManagerService;
import org.sadtech.haiti.filter.FilterService;
import java.util.List;
import java.util.Set;
public interface MergeRequestsService extends SimpleManagerService<MergeRequest, Long>, FilterService<MergeRequest, MergeRequestFilter> {
@ -20,6 +19,4 @@ public interface MergeRequestsService extends SimpleManagerService<MergeRequest,
*/
Set<IdAndStatusPr> getAllId(Set<MergeRequestState> statuses);
List<MergeRequest> getAllByAssignee(Long userId);
}

View File

@ -3,9 +3,11 @@ package org.sadtech.bot.gitlab.context.service;
import lombok.NonNull;
import org.sadtech.bot.gitlab.context.domain.PipelineStatus;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.sadtech.bot.gitlab.context.domain.filter.PipelineFilter;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.service.SimpleManagerService;
import org.sadtech.haiti.filter.FilterService;
import java.util.Set;
@ -14,7 +16,7 @@ import java.util.Set;
*
* @author upagge 17.01.2021
*/
public interface PipelineService extends SimpleManagerService<Pipeline, Long> {
public interface PipelineService extends SimpleManagerService<Pipeline, Long>, FilterService<Pipeline, PipelineFilter> {
Sheet<Pipeline> getAllByStatuses(@NonNull Set<PipelineStatus> statuses, @NonNull Pagination pagination);

View File

@ -0,0 +1,79 @@
package org.sadtech.bot.gitlab.core.service.impl;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.gitlab.context.domain.entity.MergeRequest;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.sadtech.bot.gitlab.context.domain.filter.MergeRequestFilter;
import org.sadtech.bot.gitlab.context.domain.filter.PipelineFilter;
import org.sadtech.bot.gitlab.context.service.CleanService;
import org.sadtech.bot.gitlab.context.service.MergeRequestsService;
import org.sadtech.bot.gitlab.context.service.PipelineService;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.core.page.PaginationImpl;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.sadtech.bot.gitlab.context.domain.MergeRequestState.CLOSED;
import static org.sadtech.bot.gitlab.context.domain.MergeRequestState.MERGED;
/**
* // TODO: 08.02.2021 Добавить описание.
*
* @author upagge 08.02.2021
*/
@Service
@RequiredArgsConstructor
public class CleanServiceImpl implements CleanService {
private static final int COUNT = 1000;
private static final MergeRequestFilter CLEAN_FILTER = MergeRequestFilter.builder()
.states(Stream.of(MERGED, CLOSED).collect(Collectors.toSet()))
.build();
private final MergeRequestsService mergeRequestsService;
private final PipelineService pipelineService;
@Override
public void cleanMergedPullRequests() {
int page = 0;
Sheet<MergeRequest> mergeRequestSheet = mergeRequestsService.getAll(CLEAN_FILTER, PaginationImpl.of(page, COUNT));
while (mergeRequestSheet.hasContent()) {
final Set<Long> ids = mergeRequestSheet.getContent().stream()
.map(MergeRequest::getId)
.collect(Collectors.toSet());
mergeRequestsService.deleteAllById(ids);
mergeRequestSheet = mergeRequestsService.getAll(CLEAN_FILTER, PaginationImpl.of(++page, COUNT));
}
}
@Override
public void cleanOldPipelines() {
int page = 0;
final PipelineFilter filter = cleanPipelineFilter();
Sheet<Pipeline> sheet = pipelineService.getAll(filter, PaginationImpl.of(page, COUNT));
while (sheet.hasContent()) {
final Set<Long> ids = sheet.getContent().stream()
.map(Pipeline::getId)
.collect(Collectors.toSet());
pipelineService.deleteAllById(ids);
sheet = pipelineService.getAll(filter, PaginationImpl.of(page, COUNT));
}
}
private PipelineFilter cleanPipelineFilter() {
final PipelineFilter pipelineFilter = new PipelineFilter();
pipelineFilter.setLessThanCreatedDate(LocalDateTime.now().minusDays(1L));
return pipelineFilter;
}
}

View File

@ -24,7 +24,6 @@ import org.sadtech.haiti.filter.FilterService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@ -96,11 +95,12 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
@Override
public MergeRequest update(@NonNull MergeRequest mergeRequest) {
personService.create(mergeRequest.getAuthor());
personService.create(mergeRequest.getAssignee());
final MergeRequest oldMergeRequest = mergeRequestRepository.findById(mergeRequest.getId())
.orElseThrow(() -> new NotFoundException("МержРеквест не найден"));
// forgottenNotification(oldMergeRequest);
if (mergeRequest.getNotification() == null) {
mergeRequest.setNotification(oldMergeRequest.getNotification());
}
@ -136,31 +136,6 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
}
}
protected void forgottenNotification(MergeRequest mergeRequest) {
// if (LocalDateTime.now().isAfter(mergeRequest.getUpdateDate().plusHours(2L))) {
// final Set<String> smartReviewers = mergeRequest.getReviewers().stream()
// .filter(
// reviewer -> ReviewerStatus.NEEDS_WORK.equals(reviewer.getStatus())
// && LocalDateTime.now().isAfter(reviewer.getDateChange().plusHours(2L))
// && reviewer.getDateSmartNotify() == null
// )
// .peek(reviewer -> reviewer.setDateSmartNotify(LocalDateTime.now()))
// .map(Reviewer::getPersonLogin)
// .collect(Collectors.toSet());
// if (!smartReviewers.isEmpty()) {
// notifyService.send(
// ForgottenSmartPrNotify.builder()
// .projectKey(mergeRequest.getProjectKey())
// .repositorySlug(mergeRequest.getRepositorySlug())
// .recipients(smartReviewers)
// .title(mergeRequest.getTitle())
// .url(mergeRequest.getUrl())
// .build()
// );
// }
// }
}
protected void notifyConflict(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) {
if (
!oldMergeRequest.isConflict()
@ -201,11 +176,6 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
return mergeRequestRepository.findAllIdByStateIn(statuses);
}
@Override
public List<MergeRequest> getAllByAssignee(@NonNull Long userId) {
return mergeRequestRepository.findAllByAssignee(userId);
}
@Override
public Sheet<MergeRequest> getAll(@NonNull MergeRequestFilter filter, Pagination pagination) {
return filterService.getAll(filter, pagination);

View File

@ -4,17 +4,20 @@ import lombok.NonNull;
import org.sadtech.bot.gitlab.context.domain.PersonInformation;
import org.sadtech.bot.gitlab.context.domain.PipelineStatus;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.sadtech.bot.gitlab.context.domain.filter.PipelineFilter;
import org.sadtech.bot.gitlab.context.domain.notify.pipeline.PipelineNotify;
import org.sadtech.bot.gitlab.context.repository.PipelineRepository;
import org.sadtech.bot.gitlab.context.service.NotifyService;
import org.sadtech.bot.gitlab.context.service.PersonService;
import org.sadtech.bot.gitlab.context.service.PipelineService;
import org.sadtech.bot.gitlab.core.service.impl.filter.PipelineFilterService;
import org.sadtech.haiti.context.exception.NotFoundException;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.core.service.AbstractSimpleManagerService;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -39,14 +42,16 @@ public class PipelineServiceImpl extends AbstractSimpleManagerService<Pipeline,
private final NotifyService notifyService;
private final PipelineRepository repository;
private final PersonService personService;
private final PipelineFilterService pipelineFilterService;
private final PersonInformation personInformation;
public PipelineServiceImpl(NotifyService notifyService, PipelineRepository repository, PersonService personService, PersonInformation personInformation) {
public PipelineServiceImpl(NotifyService notifyService, PipelineRepository repository, PersonService personService, PipelineFilterService pipelineFilterService, PersonInformation personInformation) {
super(repository);
this.notifyService = notifyService;
this.repository = repository;
this.personService = personService;
this.pipelineFilterService = pipelineFilterService;
this.personInformation = personInformation;
}
@ -110,4 +115,24 @@ public class PipelineServiceImpl extends AbstractSimpleManagerService<Pipeline,
public Sheet<Pipeline> getAllByStatuses(@NonNull Set<PipelineStatus> statuses, @NonNull Pagination pagination) {
return repository.findAllByStatuses(statuses, pagination);
}
@Override
public Sheet<Pipeline> getAll(@NonNull PipelineFilter filter, @NonNull Pagination pagination) {
return pipelineFilterService.getAll(filter, pagination);
}
@Override
public Optional<Pipeline> getFirst(@NonNull PipelineFilter filter) {
return pipelineFilterService.getFirst(filter);
}
@Override
public boolean exists(@NonNull PipelineFilter filter) {
return pipelineFilterService.exists(filter);
}
@Override
public long count(@NonNull PipelineFilter filter) {
return pipelineFilterService.count(filter);
}
}

View File

@ -22,15 +22,18 @@ public class MergeRequestFilterService extends AbstractFilterService<MergeReques
@Override
protected Filter createFilter(@NonNull MergeRequestFilter filter) {
return CriteriaFilter.<MergeRequest>create()
.and(
convertFilter(filter)
);
.and(convertFilter(filter))
.or(convertFilterOr(filter));
}
private FilterQuery convertFilterOr(MergeRequestFilter filter) {
return CriteriaQuery.<MergeRequest>create()
.matchPhrase(MergeRequest_.STATE, filter.getStates());
}
private FilterQuery convertFilter(@NonNull MergeRequestFilter filter) {
return CriteriaQuery.<MergeRequest>create()
.matchPhrase(MergeRequest_.ASSIGNEE, filter.getAssignee())
.matchPhrase(MergeRequest_.STATE, filter.getStates());
.matchPhrase(MergeRequest_.ASSIGNEE, filter.getAssignee());
}
}

View File

@ -0,0 +1,37 @@
package org.sadtech.bot.gitlab.core.service.impl.filter;
import lombok.NonNull;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline_;
import org.sadtech.bot.gitlab.context.domain.filter.PipelineFilter;
import org.sadtech.bot.gitlab.context.repository.PipelineRepository;
import org.sadtech.haiti.core.service.AbstractFilterService;
import org.sadtech.haiti.filter.Filter;
import org.sadtech.haiti.filter.FilterQuery;
import org.sadtech.haiti.filter.criteria.CriteriaFilter;
import org.sadtech.haiti.filter.criteria.CriteriaQuery;
import org.springframework.stereotype.Service;
/**
* // TODO: 08.02.2021 Добавить описание.
*
* @author upagge 08.02.2021
*/
@Service
public class PipelineFilterService extends AbstractFilterService<Pipeline, PipelineFilter> {
public PipelineFilterService(PipelineRepository pipelineRepository) {
super(pipelineRepository);
}
@Override
protected Filter createFilter(@NonNull PipelineFilter pipelineFilter) {
return CriteriaFilter.<Pipeline>create()
.and(convertAnd(pipelineFilter));
}
private FilterQuery convertAnd(PipelineFilter pipelineFilter) {
return CriteriaQuery.<Pipeline>create()
.lessThan(Pipeline_.CREATED, pipelineFilter.getLessThanCreatedDate());
}
}

View File

@ -90,7 +90,9 @@ public class TaskServiceImpl extends AbstractNoteService<Task> implements TaskSe
private void notifyNewTask(Task task) {
if (personInformation.getId().equals(task.getResponsible().getId())
&& !personInformation.getId().equals(task.getAuthor().getId())) {
&& !personInformation.getId().equals(task.getAuthor().getId())
&& task.getResolved() != null && !task.getResolved()
) {
notifyService.send(
TaskNewNotify.builder()
.authorName(task.getAuthor().getName())

View File

@ -18,6 +18,7 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -54,6 +55,8 @@ public class PipelineParser {
private final PersonProperty personProperty;
private final ConversionService conversionService;
private LocalDateTime lastUpdate = LocalDateTime.now();
public void scanNewPipeline() {
int page = 0;
Sheet<Project> projectSheet = projectService.getAll(PaginationImpl.of(page, COUNT));
@ -72,7 +75,8 @@ public class PipelineParser {
private void processingProject(Project project) {
int page = 1;
List<PipelineJson> pipelineJsons = getPipelineJsons(project.getId(), page);
LocalDateTime newLastUpdate = LocalDateTime.now();
List<PipelineJson> pipelineJsons = getPipelineJsons(project.getId(), page, lastUpdate);
while (!pipelineJsons.isEmpty()) {
@ -104,15 +108,17 @@ public class PipelineParser {
}
pipelineJsons = getPipelineJsons(project.getId(), ++page);
pipelineJsons = getPipelineJsons(project.getId(), ++page, lastUpdate);
}
lastUpdate = newLastUpdate;
}
private List<PipelineJson> getPipelineJsons(Long projectId, int page) {
private List<PipelineJson> getPipelineJsons(Long projectId, int page, LocalDateTime afterUpdate) {
return HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPipelines(), projectId, page))
.header(ACCEPT)
.header(HttpParse.AUTHORIZATION, HttpParse.BEARER + personProperty.getToken())
.getParameter("updated_after", afterUpdate.minusHours(12L).toString())
.executeList(PipelineJson.class);
}

View File

@ -7,7 +7,7 @@ import org.sadtech.bot.gitlab.context.repository.PipelineRepository;
import org.sadtech.bot.gitlab.data.jpa.PipelineJpaRepository;
import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerRepository;
import org.sadtech.haiti.database.repository.manager.FilterManagerRepository;
import org.sadtech.haiti.database.util.Converter;
import org.springframework.stereotype.Repository;
@ -19,7 +19,7 @@ import java.util.Set;
* @author upagge 17.01.2021
*/
@Repository
public class PipelineRepositoryImpl extends AbstractSimpleManagerRepository<Pipeline, Long> implements PipelineRepository {
public class PipelineRepositoryImpl extends FilterManagerRepository<Pipeline, Long> implements PipelineRepository {
private final PipelineJpaRepository jpaRepository;

View File

@ -4,7 +4,7 @@ import org.sadtech.bot.gitlab.context.domain.PipelineStatus;
import org.sadtech.bot.gitlab.context.domain.entity.Pipeline;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import java.util.Set;
@ -13,7 +13,7 @@ import java.util.Set;
*
* @author upagge 17.01.2021
*/
public interface PipelineJpaRepository extends JpaRepository<Pipeline, Long> {
public interface PipelineJpaRepository extends JpaRepositoryImplementation<Pipeline, Long> {
Page<Pipeline> findAllByStatusIn(Set<PipelineStatus> statuses, Pageable pageable);

View File

@ -86,7 +86,7 @@
<haiti.context.version>0.0.2-SNAPSHOT</haiti.context.version>
<haiti.database.version>0.0.3-SNAPSHOT</haiti.database.version>
<haiti.filter.criteria.version>0.0.2-RELEASE</haiti.filter.criteria.version>
<haiti.filter.criteria.version>0.0.3-SNAPSHOT</haiti.filter.criteria.version>
<sadtech.bot.telegram.core.version>0.0.2-SNAPSHOT</sadtech.bot.telegram.core.version>
<sadtech.bot.vsc.context.version>0.0.1-RELEASE</sadtech.bot.vsc.context.version>