Skip to content

Commit

Permalink
Onboard repo code coverage metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Oct 29, 2024
1 parent 6c900a3 commit 411c0b4
Show file tree
Hide file tree
Showing 18 changed files with 707 additions and 32 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ dependencies {

implementation 'software.amazon.awssdk:s3:2.28.17'

implementation 'org.json:json:20240303'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/opensearchmetrics/dagger/MetricsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dagger.Provides;
import org.opensearchmetrics.metrics.general.*;
import org.opensearchmetrics.metrics.label.LabelMetrics;
import org.opensearchmetrics.metrics.release.CodeCoverage;
import org.opensearchmetrics.metrics.release.ReleaseBranchChecker;
import org.opensearchmetrics.metrics.release.ReleaseIssueChecker;
import org.opensearchmetrics.metrics.release.ReleaseLabelIssuesFetcher;
Expand Down Expand Up @@ -144,7 +145,9 @@ public LabelMetrics getLabelMetrics() {
public ReleaseMetrics getReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper,
ReleaseRepoFetcher releaseRepoFetcher, ReleaseLabelIssuesFetcher releaseLabelIssuesFetcher,
ReleaseLabelPullsFetcher releaseLabelPullsFetcher, ReleaseVersionIncrementChecker releaseVersionIncrementChecker,
ReleaseBranchChecker releaseBranchChecker, ReleaseNotesChecker releaseNotesChecker, ReleaseIssueChecker releaseIssueChecker) {
return new ReleaseMetrics(openSearchUtil, objectMapper, releaseRepoFetcher, releaseLabelIssuesFetcher, releaseLabelPullsFetcher, releaseVersionIncrementChecker, releaseBranchChecker, releaseNotesChecker, releaseIssueChecker);
ReleaseBranchChecker releaseBranchChecker, ReleaseNotesChecker releaseNotesChecker, ReleaseIssueChecker releaseIssueChecker, CodeCoverage codeCoverage) {
return new ReleaseMetrics(openSearchUtil, objectMapper, releaseRepoFetcher,

Check warning on line 149 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#L149

Added line #L149 was not covered by tests
releaseLabelIssuesFetcher, releaseLabelPullsFetcher, releaseVersionIncrementChecker,
releaseBranchChecker, releaseNotesChecker, releaseIssueChecker, codeCoverage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Void handleRequest(Void input, Context context) {
metricsCalculation.generateGeneralMetrics(keys);
metricsCalculation.generateLabelMetrics(keys);
metricsCalculation.generateReleaseMetrics();
metricsCalculation.generateCodeCovMetrics();
} catch (Exception e) {
throw new RuntimeException("Error running Metrics Calculation", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearchmetrics.metrics.general.*;
import org.opensearchmetrics.metrics.label.LabelMetrics;
import org.opensearchmetrics.metrics.release.CodeCoverage;
import org.opensearchmetrics.metrics.release.ReleaseInputs;
import org.opensearchmetrics.metrics.release.ReleaseMetrics;
import org.opensearchmetrics.model.codecov.CodeCovResponse;
import org.opensearchmetrics.model.codecov.CodeCovResult;
import org.opensearchmetrics.model.label.LabelData;
import org.opensearchmetrics.model.general.MetricsData;
import org.opensearchmetrics.model.release.ReleaseMetricsData;
Expand Down Expand Up @@ -199,4 +202,41 @@ public void generateReleaseMetrics() {
openSearchUtil.bulkIndex("opensearch_release_metrics", metricFinalData);
}

public void generateCodeCovMetrics() {
ReleaseInputs[] releaseInputs = ReleaseInputs.getAllReleaseInputs();
Map<String, String> metricFinalData =
Arrays.stream(releaseInputs)
.filter(ReleaseInputs::getTrack)
.flatMap(releaseInput -> releaseMetrics.getReleaseRepos(releaseInput.getVersion()).entrySet().stream()
.flatMap(entry -> {
String repoName = entry.getKey();
String componentName = entry.getValue();
CodeCovResult codeCovResult = new CodeCovResult();
codeCovResult.setRepository(repoName);
codeCovResult.setComponent(componentName);
codeCovResult.setCurrentDate(currentDate.toString());
try {
codeCovResult.setId(String.valueOf(UUID.nameUUIDFromBytes(MessageDigest.getInstance("SHA-1")
.digest(("codecov-metrics-" + releaseInput.getBranch() + releaseInput.getVersion() + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repoName)
.getBytes()))));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);

Check warning on line 223 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#L222-L223

Added lines #L222 - L223 were not covered by tests
}
codeCovResult.setReleaseVersion(releaseInput.getVersion());
codeCovResult.setVersion(releaseInput.getVersion());
codeCovResult.setReleaseState(releaseInput.getState());
codeCovResult.setBranch(releaseInput.getBranch());
CodeCovResponse codeCovResponse = releaseMetrics.getCodeCoverage(releaseInput.getBranch(), repoName);
codeCovResult.setCommitid(codeCovResponse.getCommitid());
codeCovResult.setState(codeCovResponse.getState());
codeCovResult.setCoverage(codeCovResponse.getCoverage());
codeCovResult.setUrl(codeCovResponse.getUrl());
return Stream.of(codeCovResult);
}))
.collect(Collectors.toMap(CodeCovResult::getId,
codeCovResult -> codeCovResult.getJson(codeCovResult, objectMapper)));
openSearchUtil.createIndexIfNotExists("opensearch_codecov_metrics");
openSearchUtil.bulkIndex("opensearch_codecov_metrics", metricFinalData);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.opensearchmetrics.metrics.release;

import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONArray;
import org.json.JSONObject;
import org.opensearchmetrics.model.codecov.CodeCovResponse;

import javax.inject.Inject;
import java.io.IOException;
import java.util.Optional;

public class CodeCoverage {
private final CloseableHttpClient httpClient;

@Inject
public CodeCoverage() {
this(HttpClients.createDefault());
}

Check warning on line 22 in src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java#L21-L22

Added lines #L21 - L22 were not covered by tests
CodeCoverage(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}

public CodeCovResponse coverage(String branch, String repo) {
String codeCovRepoURL = String.format("https://api.codecov.io/api/v2/github/opensearch-project/repos/%s/commits?branch=%s", repo, branch);
System.out.println("The codeCovRepoURL is " + codeCovRepoURL);
CodeCovResponse codeCovResponse = new CodeCovResponse();
codeCovResponse.setUrl(codeCovRepoURL);
HttpGet request = new HttpGet(codeCovRepoURL);
CloseableHttpResponse response;
try {
response = httpClient.execute(request);
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 37 in src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java#L36-L37

Added lines #L36 - L37 were not covered by tests
}
try {
if (response.getStatusLine().getStatusCode() == 200) {
String codeCovApiResult;
try {
codeCovApiResult = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
throw new RuntimeException(e);
}
JSONObject jsonObject = new JSONObject(codeCovApiResult);
JSONArray resultsCoverage = jsonObject.getJSONArray("results");
Optional<JSONObject> firstResult = Optional.ofNullable(resultsCoverage)
.filter(results -> results.length() > 0)
.map(results -> results.getJSONObject(0));
firstResult.ifPresentOrElse(
result -> {
codeCovResponse.setState(Optional.ofNullable(result.optString("state"))
.orElse("no-coverage"));
codeCovResponse.setCommitid(result.optString("commitid", "none"));
codeCovResponse.setCoverage(
Optional.ofNullable(result.optJSONObject("totals"))
.map(totals -> totals.optDouble("coverage", 0.0))
.orElse(0.0)
);
},
() -> {
codeCovResponse.setState("no-coverage");
codeCovResponse.setCommitid("none");
codeCovResponse.setCoverage(0.0);
}
);
}
} finally {
try {
response.close();
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 74 in src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java#L73-L74

Added lines #L73 - L74 were not covered by tests
}
}
return codeCovResponse;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.opensearchmetrics.metrics.release;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.opensearchmetrics.model.codecov.CodeCovResponse;
import org.opensearchmetrics.model.codecov.CodeCovResult;
import org.opensearchmetrics.util.OpenSearchUtil;

import javax.inject.Inject;
import java.io.IOException;
import java.nio.charset.CoderResult;
import java.util.List;
import java.util.Map;

Expand All @@ -24,11 +28,13 @@ public class ReleaseMetrics {

private final ReleaseIssueChecker releaseIssueChecker;

private final CodeCoverage codeCoverage;

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

Check warning on line 37 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#L37

Added line #L37 was not covered by tests
this.openSearchUtil = openSearchUtil;
this.objectMapper = objectMapper;
this.releaseRepoFetcher = releaseRepoFetcher;
Expand All @@ -38,6 +44,7 @@ public ReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper,
this.releaseBranchChecker = releaseBranchChecker;
this.releaseNotesChecker = releaseNotesChecker;
this.releaseIssueChecker = releaseIssueChecker;
this.codeCoverage = codeCoverage;

Check warning on line 47 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#L47

Added line #L47 was not covered by tests
}

public Map<String, String> getReleaseRepos(String releaseVersion) {
Expand Down Expand Up @@ -73,5 +80,9 @@ public String getReleaseIssue (String releaseVersion, String repo) {
return releaseIssueChecker.releaseIssue(releaseVersion, repo, openSearchUtil);
}

public CodeCovResponse getCodeCoverage (String branch, String repo) {
return codeCoverage.coverage(branch, repo);

Check warning on line 84 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#L84

Added line #L84 was not covered by tests
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -36,6 +37,7 @@ public boolean releaseVersionIncrement(String releaseVersion, String repo, Strin
}
}


public boolean checkOpenSearchVersion(String releaseVersion, String branch) {
String url = String.format("https://raw.githubusercontent.com/opensearch-project/OpenSearch/%s/buildSrc/version.properties", branch);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.opensearchmetrics.model.codecov;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.io.IOException;

@Data
public class CodeCovResponse {

@JsonProperty("commitid")
private String commitid;

@JsonProperty("url")
private String url;

@JsonProperty("state")
private String state;


@JsonSerialize(using = CodeCovDoubleSerializer.class)
@JsonProperty("coverage")
private Double coverage;
}

class CodeCovDoubleSerializer extends JsonSerializer<Double> {

@Override
public void serialize(Double value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (value == null) {
jsonGenerator.writeNumber(0.0);
} else {
jsonGenerator.writeNumber(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.opensearchmetrics.model.codecov;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Data
public class CodeCovResult {

@JsonProperty("id")
private String id;

@JsonProperty("current_date")
private String currentDate;

Check warning on line 19 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L19

Added line #L19 was not covered by tests

@JsonProperty("repository")
private String repository;

@JsonProperty("component")
private String component;

Check warning on line 25 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L25

Added line #L25 was not covered by tests

@JsonProperty("release_version")
private String releaseVersion;

Check warning on line 28 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L28

Added line #L28 was not covered by tests

@JsonProperty("release_state")
private String releaseState;

Check warning on line 31 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L31

Added line #L31 was not covered by tests

@JsonProperty("version")
private String version;

Check warning on line 34 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L34

Added line #L34 was not covered by tests

@JsonProperty("timestamp")
private String timestamp;

Check warning on line 37 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L37

Added line #L37 was not covered by tests

@JsonProperty("commitid")
private String commitid;

@JsonProperty("state")
private String state;

@JsonProperty("coverage")
private Double coverage;

@JsonProperty("branch")
private String branch;

Check warning on line 49 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L49

Added line #L49 was not covered by tests

@JsonProperty("url")
private String url;


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("component", component);
data.put("release_version", releaseVersion);
data.put("version", version);
data.put("release_state", releaseState);
data.put("commitid", commitid);
data.put("state", state);
data.put("coverage", coverage);
data.put("branch", branch);
data.put("url", url);
return mapper.writeValueAsString(data);
}

public String getJson(CodeCovResult codeCovResult, ObjectMapper objectMapper) {
try {
return codeCovResult.toJson(objectMapper);
} catch (JsonProcessingException e) {
System.out.println("Error while serializing ReportDataRow to JSON " + e);
throw new RuntimeException(e);

Check warning on line 77 in src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java#L75-L77

Added lines #L75 - L77 were not covered by tests
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.util.HashMap;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class ReleaseMetricsData {

@JsonProperty("autocut_issues_open")
private Long autocutIssuesOpen;

@JsonProperty("issues_closed")
private Long issuesClosed;

Expand Down
Loading

0 comments on commit 411c0b4

Please sign in to comment.