This commit is contained in:
uPagge 2021-01-19 22:47:26 +03:00
parent ba5e70f8d5
commit bb2be82b4c
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
19 changed files with 189 additions and 36 deletions

View File

@ -22,12 +22,7 @@ telegram-config:
bot-username: ${TELEGRAM_BOT_USERNAME} bot-username: ${TELEGRAM_BOT_USERNAME}
bot-token: ${TELEGRAM_BOT_TOKEN} bot-token: ${TELEGRAM_BOT_TOKEN}
gitlab-bot: gitlab-bot:
version: 2.15.3 version: 0.0.2 Beta
scheduler:
comment:
settings:
no-comment-count: 20
comment-count: 100
person: person:
telegram-id: ${TELEGRAM_PERSON_ID} telegram-id: ${TELEGRAM_PERSON_ID}
token: ${GITLAB_PERSONAL_TOKEN} token: ${GITLAB_PERSONAL_TOKEN}
@ -42,8 +37,9 @@ gitlab-bot:
users-url: ${GITLAB_URL}/api/v4/users users-url: ${GITLAB_URL}/api/v4/users
url-note: "{0}#note_{1,number,#}" url-note: "{0}#note_{1,number,#}"
url-note-api: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes/{2,number,#}" url-note-api: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes/{2,number,#}"
url-pipelines: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines?&page={1,number,integer}&per_page=100" url-pipelines: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines?&page={1,number,#}&per_page=100"
url-pipeline: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines/{1,number,integer}" url-pipeline: "${GITLAB_URL}/api/v4/projects/{0,number,#}/pipelines/{1,number,#}"
url-commit: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/commits?&page=1&per_page=1"
teamcity: teamcity:
token: ${TEAMCITY_ADMIN_TOKEN} token: ${TEAMCITY_ADMIN_TOKEN}
project-url: ${TEAMCITY_URL}/app/rest/projects project-url: ${TEAMCITY_URL}/app/rest/projects

View File

@ -189,4 +189,10 @@
</createTable> </createTable>
</changeSet> </changeSet>
<changeSet id="2020-01-19-create-table-commit" author="uPagge">
<addColumn tableName="merge_request">
<column name="date_last_commit" type="datetime"/>
</addColumn>
</changeSet>
</databaseChangeLog> </databaseChangeLog>

View File

@ -87,6 +87,9 @@ public class MergeRequest implements BasicEntity<Long> {
@Column(name = "label") @Column(name = "label")
private Set<String> labels = new HashSet<>(); private Set<String> labels = new HashSet<>();
@Column(name = "date_last_commit")
private LocalDateTime dateLastCommit;
// @JoinTable // @JoinTable
// @OneToMany(fetch = FetchType.LAZY) // @OneToMany(fetch = FetchType.LAZY)
// private List<Note> notes = new ArrayList<>(); // private List<Note> notes = new ArrayList<>();

View File

@ -6,8 +6,12 @@ import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet; import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.repository.SimpleManagerRepository; import org.sadtech.haiti.context.repository.SimpleManagerRepository;
import java.util.List;
public interface TaskRepository extends SimpleManagerRepository<Task, Long> { public interface TaskRepository extends SimpleManagerRepository<Task, Long> {
Sheet<Task> findAllByResolved(boolean resolved, @NonNull Pagination pagination); Sheet<Task> findAllByResolved(boolean resolved, @NonNull Pagination pagination);
List<Task> findAllByResponsibleIdAndResolved(@NonNull Long userId, boolean resolved);
} }

View File

@ -6,8 +6,12 @@ import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet; import org.sadtech.haiti.context.page.Sheet;
import org.sadtech.haiti.context.service.SimpleManagerService; import org.sadtech.haiti.context.service.SimpleManagerService;
import java.util.List;
public interface TaskService extends SimpleManagerService<Task, Long> { public interface TaskService extends SimpleManagerService<Task, Long> {
Sheet<Task> getAllByResolved(boolean resolved, @NonNull Pagination pagination); Sheet<Task> getAllByResolved(boolean resolved, @NonNull Pagination pagination);
List<Task> getAllPersonTask(@NonNull Long userId, boolean resolved);
} }

View File

@ -13,7 +13,7 @@ import org.springframework.context.annotation.Configuration;
@Getter @Getter
@Setter @Setter
@Configuration @Configuration
@ConfigurationProperties(prefix = "bitbucketbot") @ConfigurationProperties(prefix = "gitlab-bot")
public class AppProperty { public class AppProperty {
private String version; private String version;

View File

@ -52,4 +52,6 @@ public class GitlabProperty {
private String urlPipeline; private String urlPipeline;
private String urlCommit;
} }

View File

@ -66,9 +66,7 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
final MergeRequest newMergeRequest = mergeRequestRepository.save(mergeRequest); final MergeRequest newMergeRequest = mergeRequestRepository.save(mergeRequest);
// if (!settingService.isFirstStart()) {
notifyNewPr(newMergeRequest); notifyNewPr(newMergeRequest);
// }
return newMergeRequest; return newMergeRequest;
} }
@ -104,7 +102,10 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
mergeRequest.setNotification(oldMergeRequest.getNotification()); mergeRequest.setNotification(oldMergeRequest.getNotification());
} }
if (!oldMergeRequest.getUpdatedDate().equals(mergeRequest.getUpdatedDate())) { if (
!oldMergeRequest.getUpdatedDate().equals(mergeRequest.getUpdatedDate())
&& !personInformation.getId().equals(oldMergeRequest.getAuthor().getId())
) {
final Project project = projectService.getById(mergeRequest.getProjectId()) final Project project = projectService.getById(mergeRequest.getProjectId())
.orElseThrow(() -> new NotFoundException("Проект не найден")); .orElseThrow(() -> new NotFoundException("Проект не найден"));
@ -120,7 +121,10 @@ public class MergeRequestsServiceImpl extends AbstractSimpleManagerService<Merge
} }
private void notifyUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { private void notifyUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) {
if (!personInformation.getId().equals(mergeRequest.getAuthor().getId())) { if (
!personInformation.getId().equals(mergeRequest.getAuthor().getId())
&& !oldMergeRequest.getDateLastCommit().equals(mergeRequest.getDateLastCommit())
) {
notifyService.send( notifyService.send(
UpdatePrNotify.builder() UpdatePrNotify.builder()
.author(oldMergeRequest.getAuthor().getName()) .author(oldMergeRequest.getAuthor().getName())

View File

@ -7,24 +7,29 @@ import org.sadtech.bot.gitlab.context.exception.NotFoundException;
import org.sadtech.bot.gitlab.context.repository.NoteRepository; import org.sadtech.bot.gitlab.context.repository.NoteRepository;
import org.sadtech.bot.gitlab.context.service.NoteService; import org.sadtech.bot.gitlab.context.service.NoteService;
import org.sadtech.bot.gitlab.context.service.NotifyService; import org.sadtech.bot.gitlab.context.service.NotifyService;
import org.sadtech.bot.gitlab.context.service.PersonService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class NoteServiceImpl extends AbstractNoteService<Note> implements NoteService { public class NoteServiceImpl extends AbstractNoteService<Note> implements NoteService {
private final NoteRepository noteRepository; private final NoteRepository noteRepository;
private final PersonService personService;
public NoteServiceImpl( public NoteServiceImpl(
NoteRepository noteRepository, NoteRepository noteRepository,
NotifyService notifyService, NotifyService notifyService,
PersonInformation personInformation PersonInformation personInformation,
) { PersonService personService) {
super(noteRepository, notifyService, personInformation); super(noteRepository, notifyService, personInformation);
this.noteRepository = noteRepository; this.noteRepository = noteRepository;
this.personService = personService;
} }
@Override @Override
public Note create(@NonNull Note note) { public Note create(@NonNull Note note) {
personService.create(note.getAuthor());
final Note newNote = noteRepository.save(note); final Note newNote = noteRepository.save(note);
notificationPersonal(note); notificationPersonal(note);
return newNote; return newNote;

View File

@ -7,37 +7,54 @@ import org.sadtech.bot.gitlab.context.domain.notify.task.TaskCloseNotify;
import org.sadtech.bot.gitlab.context.domain.notify.task.TaskNewNotify; import org.sadtech.bot.gitlab.context.domain.notify.task.TaskNewNotify;
import org.sadtech.bot.gitlab.context.repository.TaskRepository; import org.sadtech.bot.gitlab.context.repository.TaskRepository;
import org.sadtech.bot.gitlab.context.service.NotifyService; import org.sadtech.bot.gitlab.context.service.NotifyService;
import org.sadtech.bot.gitlab.context.service.PersonService;
import org.sadtech.bot.gitlab.context.service.TaskService; import org.sadtech.bot.gitlab.context.service.TaskService;
import org.sadtech.haiti.context.exception.NotFoundException; import org.sadtech.haiti.context.exception.NotFoundException;
import org.sadtech.haiti.context.page.Pagination; import org.sadtech.haiti.context.page.Pagination;
import org.sadtech.haiti.context.page.Sheet; import org.sadtech.haiti.context.page.Sheet;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public class TaskServiceImpl extends AbstractNoteService<Task> implements TaskService { public class TaskServiceImpl extends AbstractNoteService<Task> implements TaskService {
private final TaskRepository taskRepository; private final TaskRepository taskRepository;
private final NotifyService notifyService; private final NotifyService notifyService;
private final PersonInformation personInformation; private final PersonInformation personInformation;
private final PersonService personService;
public TaskServiceImpl( public TaskServiceImpl(
TaskRepository taskRepository, TaskRepository taskRepository,
NotifyService notifyService, NotifyService notifyService,
PersonInformation personInformation) { PersonInformation personInformation,
PersonService personService
) {
super(taskRepository, notifyService, personInformation); super(taskRepository, notifyService, personInformation);
this.taskRepository = taskRepository; this.taskRepository = taskRepository;
this.notifyService = notifyService; this.notifyService = notifyService;
this.personInformation = personInformation; this.personInformation = personInformation;
this.personService = personService;
} }
@Override @Override
public Task create(@NonNull Task task) { public Task create(@NonNull Task task) {
createPerson(task);
final Task newTask = taskRepository.save(task); final Task newTask = taskRepository.save(task);
notifyNewTask(task); notifyNewTask(task);
notificationPersonal(task); notificationPersonal(task);
return newTask; return newTask;
} }
private void createPerson(@NonNull Task task) {
personService.create(task.getAuthor());
if (task.getResolvedBy() != null) {
personService.create(task.getResolvedBy());
}
personService.create(task.getResponsible());
}
@Override @Override
public Task update(@NonNull Task task) { public Task update(@NonNull Task task) {
final Task oldTask = taskRepository.findById(task.getId()) final Task oldTask = taskRepository.findById(task.getId())
@ -92,4 +109,9 @@ public class TaskServiceImpl extends AbstractNoteService<Task> implements TaskSe
return taskRepository.findAllByResolved(resolved, pagination); return taskRepository.findAllByResolved(resolved, pagination);
} }
@Override
public List<Task> getAllPersonTask(@NonNull Long userId, boolean resolved) {
return taskRepository.findAllByResponsibleIdAndResolved(userId, resolved);
}
} }

View File

@ -10,6 +10,7 @@ import org.sadtech.bot.gitlab.context.service.MergeRequestsService;
import org.sadtech.bot.gitlab.context.service.ProjectService; import org.sadtech.bot.gitlab.context.service.ProjectService;
import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty; import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty;
import org.sadtech.bot.gitlab.core.config.properties.PersonProperty; import org.sadtech.bot.gitlab.core.config.properties.PersonProperty;
import org.sadtech.bot.gitlab.sdk.domain.CommitJson;
import org.sadtech.bot.gitlab.sdk.domain.MergeRequestJson; import org.sadtech.bot.gitlab.sdk.domain.MergeRequestJson;
import org.sadtech.haiti.context.domain.ExistsContainer; import org.sadtech.haiti.context.domain.ExistsContainer;
import org.sadtech.haiti.context.exception.NotFoundException; import org.sadtech.haiti.context.exception.NotFoundException;
@ -55,7 +56,11 @@ public class MergeRequestParser {
.header(ACCEPT) .header(ACCEPT)
.header(AUTHORIZATION, BEARER + personProperty.getToken()) .header(AUTHORIZATION, BEARER + personProperty.getToken())
.execute(MergeRequestJson.class) .execute(MergeRequestJson.class)
.map(json -> conversionService.convert(json, MergeRequest.class)) .map(json -> {
final MergeRequest newMergeRequest = conversionService.convert(json, MergeRequest.class);
parsingCommits(newMergeRequest);
return newMergeRequest;
})
.orElseThrow(() -> new NotFoundException("МержРеквест не найден, возможно удален")); .orElseThrow(() -> new NotFoundException("МержРеквест не найден, возможно удален"));
mergeRequestsService.update(mergeRequest); mergeRequestsService.update(mergeRequest);
} }
@ -93,8 +98,13 @@ public class MergeRequestParser {
if (!existsContainer.isAllFound()) { if (!existsContainer.isAllFound()) {
final List<MergeRequest> newMergeRequests = mergeRequestJsons.stream() final List<MergeRequest> newMergeRequests = mergeRequestJsons.stream()
.filter(json -> existsContainer.getIdNoFound().contains(json.getId())) .filter(json -> existsContainer.getIdNoFound().contains(json.getId()))
.map(json -> conversionService.convert(json, MergeRequest.class)) .map(json -> {
final MergeRequest mergeRequest = conversionService.convert(json, MergeRequest.class);
parsingCommits(mergeRequest);
return mergeRequest;
})
.collect(Collectors.toList()); .collect(Collectors.toList());
mergeRequestsService.createAll(newMergeRequests); mergeRequestsService.createAll(newMergeRequests);
} }
@ -102,6 +112,16 @@ public class MergeRequestParser {
} }
} }
private void parsingCommits(MergeRequest mergeRequest) {
final List<CommitJson> commitJson = HttpParse.request(
MessageFormat.format(gitlabProperty.getUrlCommit(), mergeRequest.getProjectId(), mergeRequest.getTwoId())
)
.header(ACCEPT)
.header(AUTHORIZATION, BEARER + personProperty.getToken())
.executeList(CommitJson.class);
mergeRequest.setDateLastCommit(commitJson.get(0).getCreatedDate());
}
private List<MergeRequestJson> getMergeRequestJsons(Project project, int page) { private List<MergeRequestJson> getMergeRequestJsons(Project project, int page) {
return HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPullRequestOpen(), project.getId(), page)) return HttpParse.request(MessageFormat.format(gitlabProperty.getUrlPullRequestOpen(), project.getId(), page))
.header(HttpHeader.of(AUTHORIZATION, BEARER + personProperty.getToken())) .header(HttpHeader.of(AUTHORIZATION, BEARER + personProperty.getToken()))

View File

@ -10,6 +10,8 @@ import org.sadtech.haiti.database.repository.manager.AbstractSimpleManagerReposi
import org.sadtech.haiti.database.util.Converter; import org.sadtech.haiti.database.util.Converter;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public class TaskRepositoryImpl extends AbstractSimpleManagerRepository<Task, Long> implements TaskRepository { public class TaskRepositoryImpl extends AbstractSimpleManagerRepository<Task, Long> implements TaskRepository {
@ -26,4 +28,9 @@ public class TaskRepositoryImpl extends AbstractSimpleManagerRepository<Task, Lo
taskRepositoryJpa.findAllByResolved(resolved, Converter.pagination(pagination)) taskRepositoryJpa.findAllByResolved(resolved, Converter.pagination(pagination))
); );
} }
@Override
public List<Task> findAllByResponsibleIdAndResolved(@NonNull Long userId, boolean resolved) {
return taskRepositoryJpa.findAllByResponsibleIdAndResolved(userId, resolved);
}
} }

View File

@ -5,8 +5,12 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface TaskRepositoryJpa extends JpaRepository<Task, Long> { public interface TaskRepositoryJpa extends JpaRepository<Task, Long> {
Page<Task> findAllByResolved(boolean resolved, Pageable pageable); Page<Task> findAllByResolved(boolean resolved, Pageable pageable);
List<Task> findAllByResponsibleIdAndResolved(Long responsibleId, boolean resolved);
} }

View File

@ -0,0 +1,27 @@
package org.sadtech.bot.gitlab.sdk.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import java.time.LocalDateTime;
/**
* // TODO: 19.01.2021 Добавить описание.
*
* @author upagge 19.01.2021
*/
@Data
public class CommitJson {
private String id;
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonProperty("created_at")
private LocalDateTime createdDate;
}

View File

@ -15,7 +15,7 @@ public enum MergeRequestStateJson {
CLOSED, CLOSED,
@JsonProperty("locked") @JsonProperty("locked")
LOCKED, LOCKED,
@JsonProperty("merger") @JsonProperty("merged")
MERGED MERGED
} }

View File

@ -78,7 +78,7 @@
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<gitlab.core.version>3.0.1-RELEASE</gitlab.core.version> <gitlab.core.version>3.0.1-RELEASE</gitlab.core.version>
<gitlab.app.version>3.0.1-RELEASE</gitlab.app.version> <gitlab.app.version>1.0.0-SNAPSHOT</gitlab.app.version>
<gitlab.teamcity.version>3.0.1-RELEASE</gitlab.teamcity.version> <gitlab.teamcity.version>3.0.1-RELEASE</gitlab.teamcity.version>
<gitlab.teamcity.core.version>3.0.1-RELEASE</gitlab.teamcity.core.version> <gitlab.teamcity.core.version>3.0.1-RELEASE</gitlab.teamcity.core.version>
<gitlab.teamcity.sdk.version>3.0.1-RELEASE</gitlab.teamcity.sdk.version> <gitlab.teamcity.sdk.version>3.0.1-RELEASE</gitlab.teamcity.sdk.version>

View File

@ -0,0 +1,38 @@
package org.sadtech.bot.gitlab.telegram.service;
import lombok.RequiredArgsConstructor;
import org.sadtech.bot.gitlab.context.domain.notify.SimpleTextNotify;
import org.sadtech.bot.gitlab.context.service.AppSettingService;
import org.sadtech.bot.gitlab.context.service.NotifyService;
import org.sadtech.bot.gitlab.core.config.properties.AppProperty;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* // TODO: 19.01.2021 Добавить описание.
*
* @author upagge 19.01.2021
*/
@Component
@RequiredArgsConstructor
public class StartNotify {
private final NotifyService notifyService;
private final AppProperty appProperty;
private final AppSettingService settingService;
@PostConstruct
public void sendStartNotification() {
if (!settingService.isFirstStart()) {
notifyService.send(
SimpleTextNotify.builder()
.message("Привет. Желаю продуктивного дня :)" +
"\n-- -- -- -- --\n" +
"Version " + appProperty.getVersion() + " | Developer: [uPagge](https://uPagge.ru)")
.build()
);
}
}
}

View File

@ -1,6 +1,9 @@
package org.sadtech.bot.gitlab.telegram.unit; package org.sadtech.bot.gitlab.telegram.unit;
import org.sadtech.bot.gitlab.context.domain.PersonInformation;
import org.sadtech.bot.gitlab.context.domain.entity.Note;
import org.sadtech.bot.gitlab.context.service.AppSettingService; import org.sadtech.bot.gitlab.context.service.AppSettingService;
import org.sadtech.bot.gitlab.context.service.TaskService;
import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty; import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty;
import org.sadtech.bot.gitlab.core.service.parser.ProjectParser; import org.sadtech.bot.gitlab.core.service.parser.ProjectParser;
import org.sadtech.social.bot.domain.unit.AnswerText; import org.sadtech.social.bot.domain.unit.AnswerText;
@ -29,7 +32,8 @@ public class MenuConfig {
public AnswerText menu( public AnswerText menu(
AppSettingService settingService, AppSettingService settingService,
AnswerText settings, AnswerText settings,
AnswerText textAddNewProject AnswerText textAddNewProject,
AnswerText getTasks
) { ) {
return AnswerText.builder() return AnswerText.builder()
.boxAnswer(message -> .boxAnswer(message ->
@ -66,6 +70,7 @@ public class MenuConfig {
) )
.nextUnit(settings) .nextUnit(settings)
.nextUnit(textAddNewProject) .nextUnit(textAddNewProject)
.nextUnit(getTasks)
.build(); .build();
} }
@ -114,4 +119,24 @@ public class MenuConfig {
.build(); .build();
} }
@Bean
public AnswerText getTasks(
TaskService taskService,
AppSettingService settingService,
PersonInformation personInformation
) {
return AnswerText.builder()
.boxAnswer(message ->
{
final Long userId = personInformation.getId();
final String text = taskService.getAllPersonTask(userId, false).stream()
// .collect(Collectors.groupingBy())
.map(Note::getBody)
.collect(Collectors.joining("\n"));
return BoxAnswer.of(text);
})
.phrase(settingService.getMessage("ui.menu.task"))
.build();
}
} }

View File

@ -1,14 +0,0 @@
package org.sadtech.bot.gitlab.telegram.utils;
import org.springframework.context.annotation.Configuration;
/**
* // TODO: 20.09.2020 Добавить описание.
*
* @author upagge 20.09.2020
*/
@Configuration
public class GeneratorKeyBoards {
}