Добавление функционала апрува

This commit is contained in:
Struchkov Mark 2024-01-22 11:05:31 +03:00
parent 905e13053e
commit 76ffd566af
No known key found for this signature in database
GPG Key ID: A3F0AC3F0FA52F3C
9 changed files with 103 additions and 1 deletions

View File

@ -92,6 +92,17 @@ public class MergeRequest {
) )
private List<Person> reviewers = new ArrayList<>(); private List<Person> 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<Person> approvals = new ArrayList<>();
@Column(name = "target_branch") @Column(name = "target_branch")
private String targetBranch; private String targetBranch;

View File

@ -46,6 +46,11 @@ public class GitlabProperty {
*/ */
private String mergeRequestUrl; private String mergeRequestUrl;
/**
* Адрес, по которому можно получить апрувы.
*/
private String mergeRequestApprovalUrl;
private String projectAddUrl; private String projectAddUrl;
private String noteUrl; private String noteUrl;

View File

@ -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.service.parser.forktask.GetSingleMergeRequestTask;
import dev.struchkov.bot.gitlab.core.utils.HttpParse; import dev.struchkov.bot.gitlab.core.utils.HttpParse;
import dev.struchkov.bot.gitlab.core.utils.StringUtils; 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.CommitJson;
import dev.struchkov.bot.gitlab.sdk.domain.MergeRequestJson; import dev.struchkov.bot.gitlab.sdk.domain.MergeRequestJson;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -32,6 +34,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static dev.struchkov.bot.gitlab.core.utils.HttpParse.ACCEPT; 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.checkNotEmpty;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResult; import static dev.struchkov.haiti.utils.concurrent.ForkJoinUtils.pullTaskResult;
@ -77,6 +80,7 @@ public class MergeRequestParser {
.map(mergeRequestJson -> { .map(mergeRequestJson -> {
final MergeRequest newMergeRequest = conversionService.convert(mergeRequestJson, MergeRequest.class); final MergeRequest newMergeRequest = conversionService.convert(mergeRequestJson, MergeRequest.class);
parsingCommits(newMergeRequest); parsingCommits(newMergeRequest);
parsingApprovals(newMergeRequest);
return newMergeRequest; return newMergeRequest;
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -124,6 +128,7 @@ public class MergeRequestParser {
.map(json -> { .map(json -> {
final MergeRequest mergeRequest = conversionService.convert(json, MergeRequest.class); final MergeRequest mergeRequest = conversionService.convert(json, MergeRequest.class);
parsingCommits(mergeRequest); parsingCommits(mergeRequest);
parsingApprovals(mergeRequest);
return mergeRequest; return mergeRequest;
}) })
.toList(); .toList();
@ -179,6 +184,7 @@ public class MergeRequestParser {
} }
} }
//TODO [19.01.2024|uPagge]: Переделать в многопоточный режим
private void parsingCommits(MergeRequest mergeRequest) { private void parsingCommits(MergeRequest mergeRequest) {
final List<CommitJson> commitJson = HttpParse.request( final List<CommitJson> commitJson = HttpParse.request(
MessageFormat.format(gitlabProperty.getLastCommitOfMergeRequestUrl(), mergeRequest.getProjectId(), mergeRequest.getTwoId()) MessageFormat.format(gitlabProperty.getLastCommitOfMergeRequestUrl(), mergeRequest.getProjectId(), mergeRequest.getTwoId())
@ -186,9 +192,30 @@ public class MergeRequestParser {
.header(ACCEPT) .header(ACCEPT)
.header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken())
.executeList(CommitJson.class); .executeList(CommitJson.class);
if (commitJson != null && !commitJson.isEmpty()) { if (checkNotEmpty(commitJson)) {
mergeRequest.setDateLastCommit(commitJson.get(0).getCreatedDate()); 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<Person> approvals = approvalContainer.getApprovals().stream()
.map(ApprovalJson::getUser)
.map(personJson -> conversionService.convert(personJson, Person.class))
.collect(Collectors.toList());
mergeRequest.setApprovals(approvals);
}
}
} }

View File

@ -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" 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" 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-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/" project-add-url: "${GITLAB_URL}/api/v4/projects/"
note-url: "{0}#note_{1,number,#}" 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,#}" notes-of-merge-request-url: "${GITLAB_URL}/api/v4/projects/{0,number,#}/merge_requests/{1,number,#}/notes/{2,number,#}"

View File

@ -4,5 +4,6 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd"> xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">
<include file="v.1.0.0/changelog.xml" relativeToChangelogFile="true"/> <include file="v.1.0.0/changelog.xml" relativeToChangelogFile="true"/>
<include file="v.2.0.0/changelog.xml" relativeToChangelogFile="true"/>
</databaseChangeLog> </databaseChangeLog>

View File

@ -0,0 +1,21 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">
<changeSet id="2024-01-20-create-table-approvals" author="struchkov">
<createTable tableName="merge_request_approvals">
<column name="merge_request_id" type="int">
<constraints nullable="false" foreignKeyName="fk_merge_request_approvals_merge_request_id"
references="merge_request(id)" deleteCascade="true"/>
</column>
<column name="person_id" type="int">
<constraints nullable="false" foreignKeyName="fk_merge_request_approvals_person_id"
references="person(id)" deleteCascade="true"/>
</column>
</createTable>
<addPrimaryKey tableName="merge_request_approvals" columnNames="merge_request_id, person_id"/>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,12 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.17.xsd">
<changeSet id="2024-01-20-add-tab-v-2-0-0" author="uPagge">
<tagDatabase tag="v.2.0.0"/>
</changeSet>
<include file="2024-01-20-create-merge-request-approvals.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@ -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<ApprovalJson> approvals;
}

View File

@ -0,0 +1,10 @@
package dev.struchkov.bot.gitlab.sdk.domain;
import lombok.Data;
@Data
public class ApprovalJson {
private PersonJson user;
}