Добавление проекта из телеги

This commit is contained in:
uPagge 2021-01-17 00:25:21 +03:00
parent 87bc00e0ee
commit ea0cc8897a
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
7 changed files with 110 additions and 36 deletions

View File

@ -37,6 +37,7 @@ gitlab-bot:
url-pull-request-close: ${GITLAB_URL}
url-pull-request-comment: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests/{1}/notes
url-pull-request: ${GITLAB_URL}/api/v4/projects/{0}/merge_requests/{1}?page={2}&per_page=100
url-merge-request-add: ${GITLAB_URL}/api/v4/projects/{0}%2F{1}
user-url: ${GITLAB_URL}/api/v4/user
users-url: ${GITLAB_URL}/api/v4/users
teamcity:

View File

@ -10,6 +10,8 @@ ui.menu.task=My tasks
ui.menu.mr=Merge Request
ui.menu.setting=Settings
ui.menu.add_mr=Add project
ui.menu.add_mr.text=Copy the url of the project and send it to me
menu.add_project_success=Project added successfully
ui.menu.setting.text=This is the settings menu
ui.menu.setting.language=Language settings
ui.menu.setting.language.text=Choose your language

View File

@ -10,6 +10,8 @@ ui.menu.task=Мои задачи
ui.menu.mr=Merge Request
ui.menu.setting=Настройки
ui.menu.add_mr=Добавить проект
ui.menu.add_mr.text=Скопируйте url проекта и отправьте его мне
menu.add_project_success=Проект успешно добавлен
ui.menu.setting.text=Это меню настроек
ui.menu.setting.language=Настройки языка
ui.menu.setting.language.text=Выберете язык

View File

@ -42,4 +42,6 @@ public class GitlabProperty {
private String usersUrl;
private String urlMergeRequestAdd;
}

View File

@ -115,4 +115,16 @@ public class ProjectParser {
.executeList(ProjectJson.class);
}
public void parseByUrl(String projectUrl) {
final Project project = HttpParse.request(projectUrl)
.header(ACCEPT)
.header(AUTHORIZATION, BEARER + personProperty.getToken())
.execute(ProjectJson.class)
.map(json -> conversionService.convert(json, Project.class))
.orElseThrow(() -> new ConvertException("Ошибка получения проекта"));
if (!projectService.existsById(project.getId())) {
projectService.create(project);
}
}
}

View File

@ -1,7 +1,8 @@
package org.sadtech.bot.gitlab.telegram.unit;
import org.sadtech.bot.gitlab.context.domain.AppLocale;
import org.sadtech.bot.gitlab.context.service.AppSettingService;
import org.sadtech.bot.gitlab.core.config.properties.GitlabProperty;
import org.sadtech.bot.gitlab.core.service.parser.ProjectParser;
import org.sadtech.social.bot.domain.unit.AnswerText;
import org.sadtech.social.core.domain.BoxAnswer;
import org.sadtech.social.core.domain.keyboard.KeyBoard;
@ -11,6 +12,11 @@ import org.sadtech.social.core.utils.KeyBoards;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* // TODO: 16.01.2021 Добавить описание.
*
@ -22,12 +28,12 @@ public class MenuConfig {
@Bean
public AnswerText menu(
AppSettingService settingService,
AnswerText settings
AnswerText settings,
AnswerText textAddNewProject
) {
return AnswerText.builder()
.boxAnswer(message ->
{
final KeyBoardButtonText newMr = KeyBoardButtonText.builder().label(settingService.getMessage("ui.menu.add_mr")).build();
final KeyBoardButtonText tasks = KeyBoardButtonText.builder().label(settingService.getMessage("ui.menu.task")).build();
final KeyBoardButtonText pr = KeyBoardButtonText.builder().label(settingService.getMessage("ui.menu.mr")).build();
@ -59,6 +65,36 @@ public class MenuConfig {
}
)
.nextUnit(settings)
.nextUnit(textAddNewProject)
.build();
}
@Bean
public AnswerText textAddNewProject(
AppSettingService settingService,
AnswerText addNewProject
) {
return AnswerText.builder()
.boxAnswer(BoxAnswer.processing(settingService.getMessage("ui.menu.add_mr.text")))
.phrase(settingService.getMessage("ui.menu.add_mr"))
.nextUnit(addNewProject)
.build();
}
@Bean
public AnswerText addNewProject(
AppSettingService settingService,
ProjectParser projectParser,
GitlabProperty gitlabProperty
) {
return AnswerText.builder()
.boxAnswer(message -> {
final List<String> urlList = Arrays.stream(message.getText().split("/")).collect(Collectors.toList());
int lastElement = urlList.size() - 1;
final String projectUrl = MessageFormat.format(gitlabProperty.getUrlMergeRequestAdd(), urlList.get(lastElement - 1), urlList.get(lastElement));
projectParser.parseByUrl(projectUrl);
return BoxAnswer.of(settingService.getMessage("menu.add_project_success"));
})
.build();
}
@ -78,37 +114,4 @@ public class MenuConfig {
.build();
}
@Bean
public AnswerText settingsLanguage(
AppSettingService settingService,
AnswerText setLanguage
) {
return AnswerText.builder()
.boxAnswer(message ->
BoxAnswer.builder()
.message(settingService.getMessage("ui.menu.setting.language.text"))
.keyBoard(KeyBoards.verticalDuoMenuString("Русский", "English"))
.build())
.nextUnit(setLanguage)
.phrase(settingService.getMessage("ui.menu.setting.language"))
.build();
}
@Bean
public AnswerText setLanguage(
AppSettingService settingService
) {
return AnswerText.builder()
.boxAnswer(
message -> {
final AppLocale appLocale = AppLocale.of(message.getText());
settingService.setLocale(appLocale);
return BoxAnswer.of(
settingService.getMessage("ui.lang_changed")
);
}
)
.build();
}
}

View File

@ -0,0 +1,52 @@
package org.sadtech.bot.gitlab.telegram.unit.menu;
import org.sadtech.bot.gitlab.context.domain.AppLocale;
import org.sadtech.bot.gitlab.context.service.AppSettingService;
import org.sadtech.social.bot.domain.unit.AnswerText;
import org.sadtech.social.core.domain.BoxAnswer;
import org.sadtech.social.core.utils.KeyBoards;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* // TODO: 16.01.2021 Добавить описание.
*
* @author upagge 16.01.2021
*/
@Configuration
public class MenuSettingsConfig {
@Bean
public AnswerText settingsLanguage(
AppSettingService settingService,
AnswerText setLanguage
) {
return AnswerText.builder()
.boxAnswer(message ->
BoxAnswer.builder()
.message(settingService.getMessage("ui.menu.setting.language.text"))
.keyBoard(KeyBoards.verticalDuoMenuString("Русский", "English"))
.build())
.nextUnit(setLanguage)
.phrase(settingService.getMessage("ui.menu.setting.language"))
.build();
}
@Bean
public AnswerText setLanguage(
AppSettingService settingService
) {
return AnswerText.builder()
.boxAnswer(
message -> {
final AppLocale appLocale = AppLocale.of(message.getText());
settingService.setLocale(appLocale);
return BoxAnswer.of(
settingService.getMessage("ui.lang_changed")
);
}
)
.build();
}
}