|
| 1 | +package io.github.artemget.tagrelease.command; |
| 2 | + |
| 3 | +import io.github.artemget.entrys.Entry; |
| 4 | +import io.github.artemget.entrys.EntryException; |
| 5 | +import io.github.artemget.entrys.fake.EFake; |
| 6 | +import io.github.artemget.entrys.operation.ESplit; |
| 7 | +import io.github.artemget.tagrelease.domain.Service; |
| 8 | +import io.github.artemget.tagrelease.domain.Services; |
| 9 | +import io.github.artemget.tagrelease.domain.ServicesAll; |
| 10 | +import io.github.artemget.tagrelease.domain.Tag; |
| 11 | +import io.github.artemget.tagrelease.domain.TagEa; |
| 12 | +import io.github.artemget.tagrelease.domain.Tags; |
| 13 | +import io.github.artemget.tagrelease.domain.TagsGl; |
| 14 | +import io.github.artemget.tagrelease.exception.DomainException; |
| 15 | +import io.github.artemget.teleroute.command.Cmd; |
| 16 | +import io.github.artemget.teleroute.command.CmdException; |
| 17 | +import io.github.artemget.teleroute.send.Send; |
| 18 | +import io.github.artemget.teleroute.telegrambots.send.SendMessageWrap; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; |
| 25 | +import org.telegram.telegrambots.meta.api.objects.Update; |
| 26 | +import org.telegram.telegrambots.meta.bots.AbsSender; |
| 27 | + |
| 28 | +public class CmdListServicesAllTags implements Cmd<Update, AbsSender> { |
| 29 | + private final Logger log = LoggerFactory.getLogger(CmdListServicesAllTags.class); |
| 30 | + private final Services services; |
| 31 | + private final Tags tags; |
| 32 | + |
| 33 | + public CmdListServicesAllTags( |
| 34 | + final Entry<String> host, |
| 35 | + final Entry<String> project, |
| 36 | + final Entry<String> token |
| 37 | + ) { |
| 38 | + this(new ServicesAll(host, project, token), new TagsGl(host, token)); |
| 39 | + } |
| 40 | + |
| 41 | + public CmdListServicesAllTags(final Services services, final Tags tags) { |
| 42 | + this.services = services; |
| 43 | + this.tags = tags; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Send<AbsSender> execute(Update update) throws CmdException { |
| 48 | + final List<String> names; |
| 49 | + final String[] values = StringUtils.substringsBetween(update.getMessage().getText(), "{", "}"); |
| 50 | + try { |
| 51 | + names = new ESplit(new EFake<>(values[0]), ",").value(); |
| 52 | + } catch (final EntryException exception) { |
| 53 | + throw new CmdException("Failed to parse service names for tag fetch", exception); |
| 54 | + } |
| 55 | + final String prefix = values[1]; |
| 56 | + final String branch; |
| 57 | + if (values.length >= 3) { |
| 58 | + branch = values[2]; |
| 59 | + } else { |
| 60 | + branch = "develop"; |
| 61 | + } |
| 62 | + final List<Tag> succeed = new ArrayList<>(); |
| 63 | + final List<String> failed = new ArrayList<>(); |
| 64 | + for (final String name : names) { |
| 65 | + Service service; |
| 66 | + try { |
| 67 | + service = this.services.service(name); |
| 68 | + } catch (final DomainException exception) { |
| 69 | + log.error("Failed to fetch service:'{}' for tag build", name, exception); |
| 70 | + failed.add(name); |
| 71 | + continue; |
| 72 | + } |
| 73 | + final Tag tag; |
| 74 | + try { |
| 75 | + tag = this.tags.current(service.id(), branch, prefix); |
| 76 | + } catch (final DomainException exception) { |
| 77 | + log.error("Failed to fetch current tag for service:'{}'", name, exception); |
| 78 | + failed.add(name); |
| 79 | + continue; |
| 80 | + } |
| 81 | + succeed.add(new TagEa(service.name(), tag.name(), tag.branch(), tag.fromCommit(), tag.message(), tag.created())); |
| 82 | + } |
| 83 | + final SendMessage message = new SendMessage( |
| 84 | + update.getMessage().getChatId().toString(), |
| 85 | + String.format("Найдены теги:\n```\n%s\n```", new Tag.Printed(succeed).asString()) |
| 86 | + .concat(CmdListServicesAllTags.checked(failed)) |
| 87 | + ); |
| 88 | + message.setReplyToMessageId(update.getMessage().getMessageId()); |
| 89 | + message.enableMarkdownV2(true); |
| 90 | + return new SendMessageWrap<>(message); |
| 91 | + } |
| 92 | + |
| 93 | + private static String checked(List<String> failed) { |
| 94 | + final String message; |
| 95 | + if (failed.isEmpty()) { |
| 96 | + message = ""; |
| 97 | + } else { |
| 98 | + message = String.format( |
| 99 | + "\nОшибка поиска тегов по сервисам:\n%s", |
| 100 | + String.format("```\n%s\n```", String.join("\n", failed)) |
| 101 | + ); |
| 102 | + } |
| 103 | + return message; |
| 104 | + } |
| 105 | +} |
0 commit comments