Skip to content

Commit

Permalink
Update release metrics fields
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Aug 20, 2024
1 parent e359325 commit 14c2327
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/opensearchmetrics/dagger/MetricsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.opensearchmetrics.metrics.general.*;
import org.opensearchmetrics.metrics.label.LabelMetrics;
import org.opensearchmetrics.metrics.release.ReleaseBranchChecker;
import org.opensearchmetrics.metrics.release.ReleaseIssueChecker;
import org.opensearchmetrics.metrics.release.ReleaseLabelIssuesFetcher;
import org.opensearchmetrics.metrics.release.ReleaseLabelPullsFetcher;
import org.opensearchmetrics.metrics.release.ReleaseMetrics;
Expand Down Expand Up @@ -143,7 +144,7 @@ public LabelMetrics getLabelMetrics() {
public ReleaseMetrics getReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper,
ReleaseRepoFetcher releaseRepoFetcher, ReleaseLabelIssuesFetcher releaseLabelIssuesFetcher,
ReleaseLabelPullsFetcher releaseLabelPullsFetcher, ReleaseVersionIncrementChecker releaseVersionIncrementChecker,
ReleaseBranchChecker releaseBranchChecker, ReleaseNotesChecker releaseNotesChecker) {
return new ReleaseMetrics(openSearchUtil, objectMapper, releaseRepoFetcher, releaseLabelIssuesFetcher, releaseLabelPullsFetcher, releaseVersionIncrementChecker, releaseBranchChecker, releaseNotesChecker);
ReleaseBranchChecker releaseBranchChecker, ReleaseNotesChecker releaseNotesChecker, ReleaseIssueChecker releaseIssueChecker) {
return new ReleaseMetrics(openSearchUtil, objectMapper, releaseRepoFetcher, releaseLabelIssuesFetcher, releaseLabelPullsFetcher, releaseVersionIncrementChecker, releaseBranchChecker, releaseNotesChecker, releaseIssueChecker);

Check warning on line 148 in src/main/java/org/opensearchmetrics/dagger/MetricsModule.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/dagger/MetricsModule.java#L148

Added line #L148 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public void generateReleaseMetrics() {
throw new RuntimeException(e);
}
releaseMetricsData.setReleaseVersion(releaseInput.getVersion());
releaseMetricsData.setVersion(releaseInput.getVersion());

Check warning on line 169 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L169

Added line #L169 was not covered by tests
releaseMetricsData.setReleaseState(releaseInput.getState());
releaseMetricsData.setIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", false));
releaseMetricsData.setAutocutIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", true));
Expand All @@ -175,6 +176,8 @@ public void generateReleaseMetrics() {
releaseMetricsData.setVersionIncrement(releaseMetrics.getReleaseVersionIncrement(releaseInput.getVersion(), repo, releaseInput.getBranch()));
releaseMetricsData.setReleaseNotes(releaseMetrics.getReleaseNotes(releaseInput.getVersion(), repo, releaseInput.getBranch()));
releaseMetricsData.setReleaseBranch(releaseMetrics.getReleaseBranch(releaseInput.getVersion(), repo));
releaseMetricsData.setReleaseOwners(releaseMetrics.getReleaseOwners(releaseInput.getVersion(), repo));
releaseMetricsData.setReleaseIssue(releaseMetrics.getReleaseIssue(releaseInput.getVersion(), repo));

Check warning on line 180 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L179-L180

Added lines #L179 - L180 were not covered by tests
return Stream.of(releaseMetricsData);
}))
.collect(Collectors.toMap(ReleaseMetricsData::getId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.opensearchmetrics.metrics.release;

import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.aggregations.AggregationBuilders;
import org.opensearch.search.aggregations.bucket.terms.Terms;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearchmetrics.util.OpenSearchUtil;

import javax.inject.Inject;
import java.util.Arrays;

public class ReleaseIssueChecker {

@Inject
public ReleaseIssueChecker() {}

public String[] releaseOwners(String releaseVersion, String repo, OpenSearchUtil openSearchUtil) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("repository.keyword", repo));
boolQueryBuilder.must(QueryBuilders.matchQuery("title.keyword", "[RELEASE] Release version " + releaseVersion));
boolQueryBuilder.must(QueryBuilders.matchQuery("issue_pull_request", false));
SearchRequest searchRequest = new SearchRequest("github_issues");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.size(9000);
searchSourceBuilder.aggregation(
AggregationBuilders.terms("issue_assignees")
.field("issue_assignees.keyword")
.size(50)
);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = openSearchUtil.search(searchRequest);
Terms termsAgg = searchResponse.getAggregations().get("issue_assignees");
return termsAgg.getBuckets().stream()
.map(Terms.Bucket::getKeyAsString)
.toArray(String[]::new);
}

public String releaseIssue(String releaseVersion, String repo, OpenSearchUtil openSearchUtil) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("repository.keyword", repo));
boolQueryBuilder.must(QueryBuilders.matchQuery("title.keyword", "[RELEASE] Release version " + releaseVersion));
boolQueryBuilder.must(QueryBuilders.matchQuery("issue_pull_request", false));
SearchRequest searchRequest = new SearchRequest("github_issues");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.size(9000);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = openSearchUtil.search(searchRequest);
SearchHit[] hits = searchResponse.getHits().getHits();
return Arrays.stream(hits)
.findFirst()
.map(hit -> (String) hit.getSourceAsMap().get("html_url"))
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public class ReleaseMetrics {

private final ReleaseNotesChecker releaseNotesChecker;

private final ReleaseIssueChecker releaseIssueChecker;

@Inject
public ReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper, ReleaseRepoFetcher releaseRepoFetcher,
ReleaseLabelIssuesFetcher releaseLabelIssuesFetcher, ReleaseLabelPullsFetcher releaseLabelPullsFetcher,
ReleaseVersionIncrementChecker releaseVersionIncrementChecker, ReleaseBranchChecker releaseBranchChecker,
ReleaseNotesChecker releaseNotesChecker) {
ReleaseNotesChecker releaseNotesChecker, ReleaseIssueChecker releaseIssueChecker) {

Check warning on line 30 in src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java#L30

Added line #L30 was not covered by tests
this.openSearchUtil = openSearchUtil;
this.objectMapper = objectMapper;
this.releaseRepoFetcher = releaseRepoFetcher;
Expand All @@ -35,41 +36,40 @@ public ReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper,
this.releaseVersionIncrementChecker = releaseVersionIncrementChecker;
this.releaseBranchChecker = releaseBranchChecker;
this.releaseNotesChecker = releaseNotesChecker;
this.releaseIssueChecker = releaseIssueChecker;

Check warning on line 39 in src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java#L39

Added line #L39 was not covered by tests
}


public List<String> getReleaseRepos(String releaseVersion) {
return releaseRepoFetcher.getReleaseRepos(releaseVersion);
}


public Long getReleaseLabelIssues(String releaseVersion, String repo, String issueState, boolean autoCut) {
return releaseLabelIssuesFetcher.releaseLabelIssues(releaseVersion, repo, issueState, autoCut, openSearchUtil);
}



public Long getReleaseLabelPulls(String releaseVersion, String repo, String pullState) {
return releaseLabelPullsFetcher.releaseLabelPulls(releaseVersion, repo, pullState, openSearchUtil);
}




public boolean getReleaseVersionIncrement (String releaseVersion, String repo, String branch) {
return releaseVersionIncrementChecker.releaseVersionIncrement(releaseVersion, repo, branch, objectMapper, openSearchUtil);
}


public Boolean getReleaseNotes (String releaseVersion, String repo, String releaseBranch) {
return releaseNotesChecker.releaseNotes(releaseVersion, repo, releaseBranch);
}



public Boolean getReleaseBranch (String releaseVersion, String repo) {
return releaseBranchChecker.releaseBranch(releaseVersion, repo);
}

public String[] getReleaseOwners (String releaseVersion, String repo) {
return releaseIssueChecker.releaseOwners(releaseVersion, repo, openSearchUtil);

Check warning on line 67 in src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java#L67

Added line #L67 was not covered by tests
}

public String getReleaseIssue (String releaseVersion, String repo) {
return releaseIssueChecker.releaseIssue(releaseVersion, repo, openSearchUtil);

Check warning on line 71 in src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/ReleaseMetrics.java#L71

Added line #L71 was not covered by tests
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
Expand All @@ -23,6 +24,9 @@ public class ReleaseMetricsData {
@JsonProperty("release_version")
private String releaseVersion;

@JsonProperty("version")
private String version;

@JsonProperty("release_state")
private String releaseState;

Expand All @@ -49,12 +53,19 @@ public class ReleaseMetricsData {
@JsonProperty("release_branch")
private boolean releaseBranch;

@JsonProperty("release_owners")
private String[] releaseOwners;

@JsonProperty("release_issue")
private String releaseIssue;

public String toJson(ObjectMapper mapper) throws JsonProcessingException {
Map<String, Object> data = new HashMap<>();
data.put("id", id);
data.put("current_date", currentDate);
data.put("repository", repository);
data.put("release_version", releaseVersion);
data.put("version", version);
data.put("release_state", releaseState);
data.put("issues_open", issuesOpen);
data.put("autocut_issues_open", autocutIssuesOpen);
Expand All @@ -64,6 +75,8 @@ public String toJson(ObjectMapper mapper) throws JsonProcessingException {
data.put("version_increment", versionIncrement);
data.put("release_notes", releaseNotes);
data.put("release_branch", releaseBranch);
data.put("release_owners", releaseOwners);
data.put("release_issue", releaseIssue);

return mapper.writeValueAsString(data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.opensearchmetrics.metrics.release;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.search.aggregations.Aggregations;
import org.opensearch.search.aggregations.bucket.terms.Terms;
import org.opensearchmetrics.util.OpenSearchUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ReleaseIssueCheckerTest {

@Mock
private OpenSearchUtil openSearchUtil;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testReleaseOwners() {
Terms.Bucket bucket1 = mock(Terms.Bucket.class);
when(bucket1.getKeyAsString()).thenReturn("sample_user_1");
Terms.Bucket bucket2 = mock(Terms.Bucket.class);
when(bucket2.getKeyAsString()).thenReturn("sample_user_2");
Terms.Bucket bucket3 = mock(Terms.Bucket.class);
when(bucket3.getKeyAsString()).thenReturn("sample_user_3");
Terms.Bucket bucket4 = mock(Terms.Bucket.class);
when(bucket4.getKeyAsString()).thenReturn("sample_user_4");
List<Terms.Bucket> buckets = Arrays.asList(bucket1, bucket2, bucket3, bucket4);
Terms termsAgg = mock(Terms.class);
when(termsAgg.getBuckets()).thenAnswer(invocation -> buckets);
Aggregations aggregations = mock(Aggregations.class);
when(aggregations.get("issue_assignees")).thenReturn(termsAgg);
SearchResponse searchResponse = mock(SearchResponse.class);
when(searchResponse.status()).thenReturn(RestStatus.OK);
when(searchResponse.getAggregations()).thenReturn(aggregations);
OpenSearchUtil openSearchUtil = mock(OpenSearchUtil.class);
when(openSearchUtil.search(any(SearchRequest.class))).thenReturn(searchResponse);
ReleaseIssueChecker releaseIssueChecker = new ReleaseIssueChecker();
String[] result = releaseIssueChecker.releaseOwners("2.16.0", "opensearch-build", openSearchUtil);
String[] expected = {"sample_user_1", "sample_user_2", "sample_user_3", "sample_user_4"};
assertArrayEquals(expected, result);
}

@Test
void testReleaseIssue() {
SearchHit hit1 = mock(SearchHit.class);
when(hit1.getSourceAsMap()).thenReturn(Map.of("html_url", "https://github.com/opensearch-project/opensearch-build/issues/4115"));
SearchHit hit2 = mock(SearchHit.class);
when(hit2.getSourceAsMap()).thenReturn(Map.of("html_url", "https://github.com/opensearch-project/opensearch-build/issues/4454"));
SearchHits searchHits = mock(SearchHits.class);
when(searchHits.getHits()).thenReturn(new SearchHit[]{hit1, hit2});
SearchResponse searchResponse = mock(SearchResponse.class);
when(searchResponse.getHits()).thenReturn(searchHits);
when(searchResponse.status()).thenReturn(RestStatus.OK);
OpenSearchUtil openSearchUtil = mock(OpenSearchUtil.class);
when(openSearchUtil.search(any(SearchRequest.class))).thenReturn(searchResponse);
ReleaseIssueChecker releaseIssueChecker = new ReleaseIssueChecker();
String result = releaseIssueChecker.releaseIssue("2.16.0", "opensearch-build", openSearchUtil);
assertEquals("https://github.com/opensearch-project/opensearch-build/issues/4115", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class ReleaseMetricsTest {
private ReleaseNotesChecker releaseNotesChecker;


@Mock
private ReleaseIssueChecker releaseIssueChecker;

@Test
public void testGetReleaseRepos() {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -103,4 +106,24 @@ public void testGetReleaseBranch() {
boolean result = releaseBranchChecker.releaseBranch("1.0.0", "testRepo");
assertEquals(expectedBranch, result);
}

@Test
public void testGetReleaseOwner() {
MockitoAnnotations.openMocks(this);
String[] releaseOwners = new String[]{"sample_user_1"};
when(releaseIssueChecker.releaseOwners(anyString(), anyString(), any()))
.thenReturn(releaseOwners);
String[] result = releaseIssueChecker.releaseOwners("1.0.0", "testRepo", openSearchUtil);
assertEquals(releaseOwners, result);
}

@Test
public void testGetReleaseIssue() {
MockitoAnnotations.openMocks(this);
String releaseIssue = "https://sample-release-issue/100";
when(releaseIssueChecker.releaseIssue(anyString(), anyString(), any()))
.thenReturn(releaseIssue);
String result = releaseIssueChecker.releaseIssue("1.0.0", "testRepo", openSearchUtil);
assertEquals(releaseIssue, result);
}
}
Loading

0 comments on commit 14c2327

Please sign in to comment.