diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/MergeRequest.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/MergeRequest.java index b51d692..a296630 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/MergeRequest.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/MergeRequest.java @@ -92,6 +92,17 @@ public class MergeRequest { ) private List reviewers = new ArrayList<>(); + @OneToMany( + fetch = FetchType.LAZY, + cascade = {CascadeType.PERSIST, CascadeType.MERGE} + ) + @JoinTable( + name = "merge_request_approvals", + joinColumns = @JoinColumn(name = "merge_request_id", referencedColumnName = "id"), + inverseJoinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id") + ) + private List approvals = new ArrayList<>(); + @Column(name = "target_branch") private String targetBranch; diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/config/properties/GitlabProperty.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/config/properties/GitlabProperty.java index 2fb2a8e..d528dd7 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/config/properties/GitlabProperty.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/config/properties/GitlabProperty.java @@ -46,6 +46,11 @@ public class GitlabProperty { */ private String mergeRequestUrl; + /** + * Адрес, по которому можно получить апрувы. + */ + private String mergeRequestApprovalUrl; + private String projectAddUrl; private String noteUrl; diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/MergeRequestParser.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/MergeRequestParser.java index fb40a57..3822413 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/MergeRequestParser.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/parser/MergeRequestParser.java @@ -13,6 +13,8 @@ import dev.struchkov.bot.gitlab.core.service.parser.forktask.GetAllMergeRequestF import dev.struchkov.bot.gitlab.core.service.parser.forktask.GetSingleMergeRequestTask; import dev.struchkov.bot.gitlab.core.utils.HttpParse; import dev.struchkov.bot.gitlab.core.utils.StringUtils; +import dev.struchkov.bot.gitlab.sdk.domain.ApprovalContainerJson; +import dev.struchkov.bot.gitlab.sdk.domain.ApprovalJson; import dev.struchkov.bot.gitlab.sdk.domain.CommitJson; import dev.struchkov.bot.gitlab.sdk.domain.MergeRequestJson; import lombok.extern.slf4j.Slf4j; @@ -32,6 +34,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import static dev.struchkov.bot.gitlab.core.utils.HttpParse.ACCEPT; +import static dev.struchkov.haiti.context.exception.ConvertException.convertException; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResult; @@ -77,6 +80,7 @@ public class MergeRequestParser { .map(mergeRequestJson -> { final MergeRequest newMergeRequest = conversionService.convert(mergeRequestJson, MergeRequest.class); parsingCommits(newMergeRequest); + parsingApprovals(newMergeRequest); return newMergeRequest; }) .collect(Collectors.toList()); @@ -124,6 +128,7 @@ public class MergeRequestParser { .map(json -> { final MergeRequest mergeRequest = conversionService.convert(json, MergeRequest.class); parsingCommits(mergeRequest); + parsingApprovals(mergeRequest); return mergeRequest; }) .toList(); @@ -179,6 +184,7 @@ public class MergeRequestParser { } } + //TODO [19.01.2024|uPagge]: Переделать в многопоточный режим private void parsingCommits(MergeRequest mergeRequest) { final List commitJson = HttpParse.request( MessageFormat.format(gitlabProperty.getLastCommitOfMergeRequestUrl(), mergeRequest.getProjectId(), mergeRequest.getTwoId()) @@ -186,9 +192,30 @@ public class MergeRequestParser { .header(ACCEPT) .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) .executeList(CommitJson.class); - if (commitJson != null && !commitJson.isEmpty()) { + if (checkNotEmpty(commitJson)) { mergeRequest.setDateLastCommit(commitJson.get(0).getCreatedDate()); } } + //TODO [19.01.2024|uPagge]: Переделать в многопоточный режим + private void parsingApprovals(MergeRequest mergeRequest) { + final ApprovalContainerJson approvalContainer = HttpParse.request( + MessageFormat.format(gitlabProperty.getMergeRequestApprovalUrl(), mergeRequest.getProjectId(), mergeRequest.getTwoId()) + ) + .header(ACCEPT) + .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) + .execute(ApprovalContainerJson.class) + .orElseThrow(convertException("Error request approvals")); + + + + if (checkNotEmpty(approvalContainer.getApprovals())) { + final List approvals = approvalContainer.getApprovals().stream() + .map(ApprovalJson::getUser) + .map(personJson -> conversionService.convert(personJson, Person.class)) + .collect(Collectors.toList()); + mergeRequest.setApprovals(approvals); + } + } + } diff --git a/gitlab-app/src/main/resources/application.yml b/gitlab-app/src/main/resources/application.yml index 1638eca..746ad05 100644 --- a/gitlab-app/src/main/resources/application.yml +++ b/gitlab-app/src/main/resources/application.yml @@ -55,6 +55,7 @@ gitlab-bot: close-merge-requests-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests?state=closed&page={1, number, integer}&per_page=100" comments-of-merge-request-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes?&page={2,number,#}&per_page=100" merge-request-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}" + merge-request-approval-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/approvals" project-add-url: "${GITLAB_URL}/api/v4/projects/" note-url: "{0}#note_{1,number,#}" notes-of-merge-request-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes/{2,number,#}" diff --git a/gitlab-app/src/main/resources/liquibase/changelog.xml b/gitlab-app/src/main/resources/liquibase/changelog.xml index 7035840..ced62ea 100644 --- a/gitlab-app/src/main/resources/liquibase/changelog.xml +++ b/gitlab-app/src/main/resources/liquibase/changelog.xml @@ -4,5 +4,6 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd"> + \ No newline at end of file diff --git a/gitlab-app/src/main/resources/liquibase/v.2.0.0/2024-01-20-create-merge-request-approvals.xml b/gitlab-app/src/main/resources/liquibase/v.2.0.0/2024-01-20-create-merge-request-approvals.xml new file mode 100644 index 0000000..9dfd9b9 --- /dev/null +++ b/gitlab-app/src/main/resources/liquibase/v.2.0.0/2024-01-20-create-merge-request-approvals.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gitlab-app/src/main/resources/liquibase/v.2.0.0/changelog.xml b/gitlab-app/src/main/resources/liquibase/v.2.0.0/changelog.xml new file mode 100644 index 0000000..67c13df --- /dev/null +++ b/gitlab-app/src/main/resources/liquibase/v.2.0.0/changelog.xml @@ -0,0 +1,12 @@ + + + + + + + + + \ No newline at end of file diff --git a/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalContainerJson.java b/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalContainerJson.java new file mode 100644 index 0000000..fd1611b --- /dev/null +++ b/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalContainerJson.java @@ -0,0 +1,14 @@ +package dev.struchkov.bot.gitlab.sdk.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.util.List; + +@Data +public class ApprovalContainerJson { + + @JsonProperty("approved_by") + private List approvals; + +} diff --git a/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalJson.java b/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalJson.java new file mode 100644 index 0000000..6ed76ad --- /dev/null +++ b/gitlab-sdk/src/main/java/dev/struchkov/bot/gitlab/sdk/domain/ApprovalJson.java @@ -0,0 +1,10 @@ +package dev.struchkov.bot.gitlab.sdk.domain; + +import lombok.Data; + +@Data +public class ApprovalJson { + + private PersonJson user; + +}