Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/gradle/wrapper-val…
Browse files Browse the repository at this point in the history
…idation-action-2
  • Loading branch information
guzhongren authored Jan 31, 2024
2 parents 4de9f21 + 427a26f commit ca1d554
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@AllArgsConstructor
Expand Down Expand Up @@ -148,7 +147,8 @@ private LeadTime filterLeadTime(FetchedData.BuildKiteData buildKiteData,

private Map<String, String> getRepoMap(List<DeploymentEnvironment> deploymentEnvironments) {
return deploymentEnvironments.stream()
.collect(Collectors.toMap(DeploymentEnvironment::getId, DeploymentEnvironment::getRepository));
.collect(Collectors.toMap(DeploymentEnvironment::getId, DeploymentEnvironment::getRepository,
(previousValue, newValue) -> newValue));
}

private List<BuildKiteBuildInfo> getBuildKiteBuildInfo(String startTime, String endTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public ChangeFailureRate calculate(List<DeployTimes> deployTimesList) {

List<ChangeFailureRateOfPipeline> changeFailureRateOfPipelines = deployTimesList.stream().map(item -> {
int failedTimesOfPipeline = item.getFailed().size();
int passedTimesOfPipeline = item.getPassed().size();
int totalTimesOfPipeline = failedTimesOfPipeline + passedTimesOfPipeline;
int validPassedTimesOfPipeline = (int) item.getPassed()
.stream()
.filter(deployInfo -> item.getPipelineStep().equals(deployInfo.getJobName()))
.count();
int totalTimesOfPipeline = failedTimesOfPipeline + validPassedTimesOfPipeline;

float failureRateOfPipeline = totalTimesOfPipeline == 0 ? 0
: (float) failedTimesOfPipeline / totalTimesOfPipeline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ void shouldReturnCompletedTrueGivenResponseNonNullMetricsDataCompletedFalseWhenG
}

@Test
void shouldReturnCompletedNullGivenMetricsDataCompletedNullWhenGetDataFromCache() {
void shouldReturnMetricsCompletedStatusIsNullWhenAsyncMetricsStatusIsNull() {
String reportId = "reportId";
when(asyncReportRequestHandler.getReport(any())).thenReturn(ReportResponse.builder().build());
when(asyncMetricsDataHandler.getMetricsDataCompleted(reportId)).thenReturn(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -29,6 +31,7 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -50,6 +53,9 @@ public class PipelineServiceTest {
@Mock
private GitHubService gitHubService;

@Captor
ArgumentCaptor<Map<String, String>> roadMapArgumentCaptor;

@Nested
class FetchGithubData {

Expand Down Expand Up @@ -111,6 +117,37 @@ void shouldGetPipelineLeadTimeFromGithubServiceAndBuildkiteServiceWhenCodebaseSe
verify(gitHubService, times(1)).fetchPipelinesLeadTime(any(), any(), eq(token));
}

@Test
void shouldGetSecondValueInRoadMapWhenDeployEnvironmentListHasTwoElementWithSameKey() {
List<BuildKiteBuildInfo> fakeBuildKiteBuildInfos = new ArrayList<>();
String startTime = "startTime";
String endTime = "endTime";
String token = "token";
GenerateReportRequest request = GenerateReportRequest.builder()
.buildKiteSetting(BuildKiteSetting.builder()
.deploymentEnvList(List.of(DeploymentEnvironment.builder().id("env1").repository("repo1").build(),
DeploymentEnvironment.builder().id("env1").repository("repo2").build()))
.build())
.startTime(startTime)
.endTime(endTime)
.codebaseSetting(CodebaseSetting.builder().token(token).build())
.metrics(new ArrayList<>())
.build();

when(buildKiteService.fetchPipelineBuilds(eq(token), any(), eq(startTime), eq(endTime)))
.thenReturn(fakeBuildKiteBuildInfos);
when(buildKiteService.countDeployTimes(any(), eq(fakeBuildKiteBuildInfos), eq(startTime), eq(endTime)))
.thenReturn(DeployTimes.builder().build());
when(gitHubService.fetchPipelinesLeadTime(any(), any(), eq(token)))
.thenReturn(List.of(PipelineLeadTime.builder().build()));

pipelineService.fetchGithubData(request);

verify(gitHubService).fetchPipelinesLeadTime(any(), roadMapArgumentCaptor.capture(), any());
assertEquals("repo2", roadMapArgumentCaptor.getValue().get("env1"));

}

@Test
void shouldFilterAuthorByInputCrews() {
List<BuildKiteBuildInfo> fakeBuildKiteBuildInfos = List.of(
Expand Down Expand Up @@ -349,7 +386,7 @@ void shouldGenerateValueWithoutCommitWhenCodebaseSettingIsEmpty() {
List.of(DeploymentEnvironment.builder().id("env1").build()));

assertEquals(1, result.size());
assertEquals(null, result.get(0).getCommitInfo());
assertNull(result.get(0).getCommitInfo());
verify(buildKiteService, times(1)).getPipelineStepNames(any());
verify(buildKiteService, times(1)).getBuildKiteJob(any(), any(), any(), any(), any());
}
Expand All @@ -374,7 +411,7 @@ void shouldGenerateValueWithoutCommitWhenCodebaseSettingTokenIsEmpty() {
List.of(DeploymentEnvironment.builder().id("env1").build()));

assertEquals(1, result.size());
assertEquals(null, result.get(0).getCommitInfo());
assertNull(result.get(0).getCommitInfo());
verify(buildKiteService, times(1)).getPipelineStepNames(any());
verify(buildKiteService, times(1)).getBuildKiteJob(any(), any(), any(), any(), any());
}
Expand All @@ -399,7 +436,7 @@ void shouldGenerateValueWithoutCommitWhenCommitIdIsEmpty() {
List.of(DeploymentEnvironment.builder().id("env1").build()));

assertEquals(1, result.size());
assertEquals(null, result.get(0).getCommitInfo());
assertNull(result.get(0).getCommitInfo());
verify(buildKiteService, times(1)).getPipelineStepNames(any());
verify(buildKiteService, times(1)).getBuildKiteJob(any(), any(), any(), any(), any());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import heartbeat.client.dto.pipeline.buildkite.DeployInfo;
import heartbeat.client.dto.pipeline.buildkite.DeployTimes;
import heartbeat.controller.report.dto.response.ChangeFailureRate;
import heartbeat.service.pipeline.buildkite.builder.DeployInfoBuilder;
import heartbeat.service.pipeline.buildkite.builder.DeployTimesBuilder;
import heartbeat.service.report.calculator.ChangeFailureRateCalculator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -25,25 +25,26 @@ class CalculateChangeFailureRateTest {

private static final String JOB_FINISH_TIME_2023 = "2023-09-08T22:45:33.981Z";

private static final String PASSED_STATE = "passed";

private static final String FAILED_STATE = "failed";

private static final String OTHER_JOB_NAME = "JobName";

@InjectMocks
private ChangeFailureRateCalculator changeFailureRate;

@Test
public void testCalculateChangeFailureRate() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(DeployInfo.builder().jobFinishTime(JOB_FINISH_TIME_2023).state(PASSED_STATE).build()))
.withPassed(List.of(DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2023).build(),
DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2023).build()))
.withFailed(List.of(DeployInfo.builder().jobFinishTime(JOB_FINISH_TIME_2022).state(FAILED_STATE).build()))
.build();

ChangeFailureRate changeFailureRate = this.changeFailureRate.calculate(List.of(mockedDeployTimes));

assertThat(changeFailureRate.getAvgChangeFailureRate().getFailureRate()).isEqualTo(0.5F);
assertThat(changeFailureRate.getAvgChangeFailureRate().getFailureRate()).isEqualTo(0.3333F);
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalFailedTimes()).isEqualTo(1);
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalTimes()).isEqualTo(2);
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalTimes()).isEqualTo(3);
}

@Test
Expand All @@ -60,4 +61,21 @@ public void testCalculateChangeFailureRateWhenTotalDeployInfosTimesIsZero() {
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalTimes()).isEqualTo(0);
}

@Test
public void testCalculateChangeFailureRateWhenHavePassedDeployInfoWhoseJobNameIsNotEqualToPipelineStep() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(DeployInfoBuilder.withDefault()
.withJobName(OTHER_JOB_NAME)
.withJobFinishTime(JOB_FINISH_TIME_2023)
.build(), DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2023).build()))
.withFailed(List.of(DeployInfo.builder().jobFinishTime(JOB_FINISH_TIME_2022).state(FAILED_STATE).build()))
.build();

ChangeFailureRate changeFailureRate = this.changeFailureRate.calculate(List.of(mockedDeployTimes));

assertThat(changeFailureRate.getAvgChangeFailureRate().getFailureRate()).isEqualTo(0.5F);
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalFailedTimes()).isEqualTo(1);
assertThat(changeFailureRate.getAvgChangeFailureRate().getTotalTimes()).isEqualTo(2);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package heartbeat.service.report.calculator;

import heartbeat.client.dto.pipeline.buildkite.DeployInfo;
import heartbeat.client.dto.pipeline.buildkite.DeployTimes;
import heartbeat.controller.report.dto.response.AvgDeploymentFrequency;
import heartbeat.controller.report.dto.response.DeploymentFrequency;
import heartbeat.service.pipeline.buildkite.builder.DeployInfoBuilder;
import heartbeat.service.pipeline.buildkite.builder.DeployTimesBuilder;
import heartbeat.service.report.WorkDay;
import org.junit.jupiter.api.Test;
Expand All @@ -29,15 +29,11 @@ class CalculateDeploymentFrequencyTest {

private static final String JOB_FINISH_TIME_2023 = "2023-09-08T22:45:33.981Z";

private static final String PASSED_STATE = "passed";

private static final String START_TIME = "0000000000000";

private static final String END_TIME = "1662739199000";

private static final String JOB_NAME_EQUALS_PIPELINE_STEP = "xx";

private static final String OTHER_JOB_NAME = "yy";
private static final String OTHER_JOB_NAME = "JobName";

@InjectMocks
private DeploymentFrequencyCalculator deploymentFrequency;
Expand All @@ -48,22 +44,12 @@ class CalculateDeploymentFrequencyTest {
@Test
public void testCalculateDeploymentFrequency() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.build(),
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2023)
.state(PASSED_STATE)
.build()))
.withPassed(List.of(DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2022).build(),
DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2023).build()))
.build();
DeploymentFrequency expectedDeploymentFrequency = DeploymentFrequency.builder()
.avgDeploymentFrequency(AvgDeploymentFrequency.builder().deploymentFrequency(0.2F).build())
.build();

when(workDay.calculateWorkDaysBetween(anyLong(), anyLong())).thenReturn(10);

DeploymentFrequency deploymentFrequency = this.deploymentFrequency.calculate(List.of(mockedDeployTimes),
Expand All @@ -76,16 +62,11 @@ public void testCalculateDeploymentFrequency() {
@Test
public void testCalculateDeploymentFrequencyWhenWorkDayIsZero() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.build()))
.withPassed(List.of(DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2022).build()))
.build();
DeploymentFrequency expectedDeploymentFrequency = DeploymentFrequency.builder()
.avgDeploymentFrequency(AvgDeploymentFrequency.builder().deploymentFrequency(0.0F).build())
.build();

when(workDay.calculateWorkDaysBetween(anyLong(), anyLong())).thenReturn(0);

DeploymentFrequency deploymentFrequency = this.deploymentFrequency.calculate(List.of(mockedDeployTimes),
Expand All @@ -101,7 +82,6 @@ public void testCalculateDeploymentFrequencyWhenPassedDeployInfoIsEmpty() {
DeploymentFrequency expectedDeploymentFrequency = DeploymentFrequency.builder()
.avgDeploymentFrequency(AvgDeploymentFrequency.builder().deploymentFrequency(0.0F).build())
.build();

when(workDay.calculateWorkDaysBetween(anyLong(), anyLong())).thenReturn(10);

DeploymentFrequency deploymentFrequency = this.deploymentFrequency.calculate(List.of(mockedDeployTimes),
Expand All @@ -114,22 +94,12 @@ public void testCalculateDeploymentFrequencyWhenPassedDeployInfoIsEmpty() {
@Test
public void testCalculateDeploymentFrequencyWhenHaveTwoDeployInfo() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.build(),
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.build()))
.withPassed(List.of(DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2022).build(),
DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2022).build()))
.build();
DeploymentFrequency expectedDeploymentFrequency = DeploymentFrequency.builder()
.avgDeploymentFrequency(AvgDeploymentFrequency.builder().deploymentFrequency(0.2F).build())
.build();

when(workDay.calculateWorkDaysBetween(anyLong(), anyLong())).thenReturn(10);

DeploymentFrequency deploymentFrequency = this.deploymentFrequency.calculate(List.of(mockedDeployTimes),
Expand All @@ -156,27 +126,16 @@ public void testCalculateDeploymentFrequencyWhenDeployTimesIsEmpty() {
@Test
public void testCalculateDeploymentFrequencyWhenHaveDeployInfoWhoseJobNameIsNotEqualToPipelineStep() {
DeployTimes mockedDeployTimes = DeployTimesBuilder.withDefault()
.withPassed(List.of(
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.withPassed(List.of(DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2022).build(),
DeployInfoBuilder.withDefault()
.withJobName(OTHER_JOB_NAME)
.withJobFinishTime(JOB_FINISH_TIME_2022)
.build(),
DeployInfo.builder()
.jobName(OTHER_JOB_NAME)
.jobFinishTime(JOB_FINISH_TIME_2022)
.state(PASSED_STATE)
.build(),
DeployInfo.builder()
.jobName(JOB_NAME_EQUALS_PIPELINE_STEP)
.jobFinishTime(JOB_FINISH_TIME_2023)
.state(PASSED_STATE)
.build()))
DeployInfoBuilder.withDefault().withJobFinishTime(JOB_FINISH_TIME_2023).build()))
.build();
DeploymentFrequency expectedDeploymentFrequency = DeploymentFrequency.builder()
.avgDeploymentFrequency(AvgDeploymentFrequency.builder().deploymentFrequency(0.2F).build())
.build();

when(workDay.calculateWorkDaysBetween(anyLong(), anyLong())).thenReturn(10);

DeploymentFrequency deploymentFrequency = this.deploymentFrequency.calculate(List.of(mockedDeployTimes),
Expand Down
Loading

0 comments on commit ca1d554

Please sign in to comment.