Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with unprocessed Pull Requests in SonarQube #796

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ private static void checkPermission(ProjectDto project, UserSession userSession)

private static void addPullRequest(ProjectPullRequests.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid,
@Nullable LiveMeasureDto qualityGateMeasure, @Nullable String analysisDate) {
DbProjectBranches.PullRequestData pullRequestData = branch.getPullRequestData();

if (pullRequestData == null) return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use curly braces on all if statements

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I will add them in the new changes.


Optional<BranchDto> mergeBranch = Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid()));

ProjectPullRequests.PullRequest.Builder builder = ProjectPullRequests.PullRequest.newBuilder();
builder.setKey(branch.getKey());

DbProjectBranches.PullRequestData pullRequestData = Objects.requireNonNull(branch.getPullRequestData(), "Pull request data should be available for branch type PULL_REQUEST");
builder.setBranch(pullRequestData.getBranch());
Optional.ofNullable(Strings.emptyToNull(pullRequestData.getUrl())).ifPresent(builder::setUrl);
Optional.ofNullable(Strings.emptyToNull(pullRequestData.getTitle())).ifPresent(builder::setTitle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,92 @@ void shouldExecuteRequestWithValidParameter() {
assertThat(messageArgumentCaptor.getValue()).usingRecursiveComparison().isEqualTo(expected);
}

@Test
void shouldExExcludePullRequestsWithoutData() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? ExEc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I will refactor it.

Request request = mock(Request.class);
when(request.mandatoryParam("project")).thenReturn("project");

when(componentFinder.getProjectByKey(any(), any())).thenReturn(new ProjectDto().setKey("projectKey").setUuid("uuid0"));

when(userSession.hasPermission(any())).thenReturn(true);

BranchDao branchDao = mock(BranchDao.class);
when(dbClient.branchDao()).thenReturn(branchDao);
when(branchDao.selectByProject(any(), any())).thenReturn(List.of(new BranchDto()
.setBranchType(BranchType.PULL_REQUEST)
.setKey("prKey")
.setUuid("uuid1")
.setMergeBranchUuid("uuid2")
.setPullRequestData(DbProjectBranches.PullRequestData.newBuilder()
.setBranch("prBranch")
.setTitle("title")
.setTarget("target")
.setUrl("url")
.build()),
new BranchDto()
.setBranchType(BranchType.PULL_REQUEST)
.setKey("prKey2")
.setUuid("uuid3"),
new BranchDto()
.setBranchType(BranchType.PULL_REQUEST)
.setKey("prKey3")
.setUuid("uuid4")
.setMergeBranchUuid("uuid2")
.setPullRequestData(DbProjectBranches.PullRequestData.newBuilder()
.setBranch("prBranch2")
.setTitle("title3")
.setUrl("url3")
.build())));

when(branchDao.selectByUuids(any(), any())).thenReturn(List.of(new BranchDto()
.setUuid("uuid2")
.setKey("branch2Key")));

LiveMeasureDao liveMeasureDao = mock(LiveMeasureDao.class);
when(dbClient.liveMeasureDao()).thenReturn(liveMeasureDao);
when(liveMeasureDao.selectByComponentUuidsAndMetricKeys(any(), any(), any())).thenReturn(List.of(new LiveMeasureDto()
.setComponentUuid("uuid1")
.setData("live measure")));

SnapshotDao snapshotDao = mock(SnapshotDao.class);
when(dbClient.snapshotDao()).thenReturn(snapshotDao);
when(snapshotDao.selectLastAnalysesByRootComponentUuids(any(), any())).thenReturn(List.of(new SnapshotDto().setComponentUuid("componentUuid").setCreatedAt(1234L)));

Response response = mock(Response.class);

ProjectPullRequests.ListWsResponse expected = ProjectPullRequests.ListWsResponse.newBuilder()
.addPullRequests(ProjectPullRequests.PullRequest.newBuilder()
.setKey("prKey")
.setTitle("title")
.setBranch("prBranch")
.setBase("branch2Key")
.setStatus(ProjectPullRequests.Status.newBuilder()
.setQualityGateStatus("live measure")
.build())
.setUrl("url")
.setTarget("target")
.build())
.addPullRequests(ProjectPullRequests.PullRequest.newBuilder()
.setKey("prKey3")
.setTitle("title3")
.setBranch("prBranch2")
.setBase("branch2Key")
.setStatus(ProjectPullRequests.Status.newBuilder()
.build())
.setUrl("url3")
.setTarget("branch2Key")
.build())
.build();


underTest.handle(request, response);

ArgumentCaptor<ProjectPullRequests.ListWsResponse> messageArgumentCaptor = ArgumentCaptor.forClass(ProjectPullRequests.ListWsResponse.class);
verify(protoBufWriter).write(messageArgumentCaptor.capture(), eq(request), eq(response));

assertThat(messageArgumentCaptor.getValue()).usingRecursiveComparison().isEqualTo(expected);
}

@Test
void shouldNotExecuteRequestIfUserDoesNotHaveAnyPermissions() {
Request request = mock(Request.class);
Expand Down