-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prudhvi Godithi <[email protected]>
- Loading branch information
1 parent
6c900a3
commit 411c0b4
Showing
18 changed files
with
707 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/org/opensearchmetrics/metrics/release/CodeCoverage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
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); | ||
} | ||
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); | ||
} | ||
} | ||
return codeCovResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/opensearchmetrics/model/codecov/CodeCovResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/org/opensearchmetrics/model/codecov/CodeCovResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
@JsonProperty("repository") | ||
private String repository; | ||
|
||
@JsonProperty("component") | ||
private String component; | ||
|
||
@JsonProperty("release_version") | ||
private String releaseVersion; | ||
|
||
@JsonProperty("release_state") | ||
private String releaseState; | ||
|
||
@JsonProperty("version") | ||
private String version; | ||
|
||
@JsonProperty("timestamp") | ||
private String timestamp; | ||
|
||
@JsonProperty("commitid") | ||
private String commitid; | ||
|
||
@JsonProperty("state") | ||
private String state; | ||
|
||
@JsonProperty("coverage") | ||
private Double coverage; | ||
|
||
@JsonProperty("branch") | ||
private String branch; | ||
|
||
@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); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.