-
Notifications
You must be signed in to change notification settings - Fork 5
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 0f1824d
Showing
19 changed files
with
988 additions
and
62 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
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
91 changes: 91 additions & 0 deletions
91
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,91 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearchmetrics.metrics.release; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
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()); | ||
} | ||
|
||
@VisibleForTesting | ||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.