Skip to content

Commit

Permalink
Refactor metrics query to use any index and move it under utils (open…
Browse files Browse the repository at this point in the history
…search-project#509)

Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya authored Oct 2, 2024
1 parent 5e17480 commit 8a67658
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 40 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ configurations.all {
sourceSets {
main {
groovy {
srcDirs = ['src/jenkins', 'src/gradlecheck']
srcDirs = ['src/jenkins', 'src/gradlecheck', 'src/utils']
}
}

test {
groovy {
srcDirs = ['tests/jenkins', 'tests/gradlecheck']
srcDirs = ['tests/jenkins', 'tests/gradlecheck', 'tests/utils']
}
}

Expand Down Expand Up @@ -114,7 +114,8 @@ jacocoTestReport {
includes: [
'**/*_Jenkinsfile.*',
'**/jenkins/*',
'**/gradlecheck/*'
'**/gradlecheck/*',
'**/utils/*'
],
excludes: [
'**/*\$_get_closure*'
Expand Down
8 changes: 5 additions & 3 deletions src/gradlecheck/FetchPostMergeFailedTestClass.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
package gradlecheck

import groovy.json.JsonOutput
import gradlecheck.OpenSearchMetricsQuery
import utils.OpenSearchMetricsQuery

class FetchPostMergeFailedTestClass {
String metricsUrl
String awsAccessKey
String awsSecretKey
String awsSessionToken
String indexName
def script

FetchPostMergeFailedTestClass(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, def script) {
FetchPostMergeFailedTestClass(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, String indexName, def script) {
this.metricsUrl = metricsUrl
this.awsAccessKey = awsAccessKey
this.awsSecretKey = awsSecretKey
this.awsSessionToken = awsSessionToken
this.indexName = indexName
this.script = script
}

Expand Down Expand Up @@ -95,7 +97,7 @@ class FetchPostMergeFailedTestClass {
}

def getPostMergeFailedTestClass(timeFrame) {
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, script).fetchMetrics(getQuery(timeFrame))
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, indexName, script).fetchMetrics(getQuery(timeFrame))
def keys = jsonResponse.aggregations.test_class_keyword_agg.buckets.collect { it.key }
return keys
}
Expand Down
8 changes: 5 additions & 3 deletions src/gradlecheck/FetchPostMergeFailedTestName.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
package gradlecheck

import groovy.json.JsonOutput
import gradlecheck.OpenSearchMetricsQuery
import utils.OpenSearchMetricsQuery

class FetchPostMergeFailedTestName {
String metricsUrl
String awsAccessKey
String awsSecretKey
String awsSessionToken
String indexName
def script

FetchPostMergeFailedTestName(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, def script) {
FetchPostMergeFailedTestName(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, String indexName, def script) {
this.metricsUrl = metricsUrl
this.awsAccessKey = awsAccessKey
this.awsSecretKey = awsSecretKey
this.awsSessionToken = awsSessionToken
this.indexName = indexName
this.script = script
}

Expand Down Expand Up @@ -125,6 +127,6 @@ class FetchPostMergeFailedTestName {

}
def getPostMergeFailedTestName(testName, gitReference) {
return new OpenSearchMetricsQuery(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, script).fetchMetrics(getQuery(testName, gitReference))
return new OpenSearchMetricsQuery(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, script).fetchMetrics(getQuery(testName, gitReference))
}
}
8 changes: 5 additions & 3 deletions src/gradlecheck/FetchPostMergeTestGitReference.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
package gradlecheck

import groovy.json.JsonOutput
import gradlecheck.OpenSearchMetricsQuery
import utils.OpenSearchMetricsQuery

class FetchPostMergeTestGitReference {
String metricsUrl
String awsAccessKey
String awsSecretKey
String awsSessionToken
String indexName
def script

FetchPostMergeTestGitReference(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, def script) {
FetchPostMergeTestGitReference(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, String indexName, def script) {
this.metricsUrl = metricsUrl
this.awsAccessKey = awsAccessKey
this.awsSecretKey = awsSecretKey
this.awsSessionToken = awsSessionToken
this.indexName = indexName
this.script = script
}

Expand Down Expand Up @@ -98,7 +100,7 @@ class FetchPostMergeTestGitReference {
}

def getPostMergeTestGitReference(testName) {
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, script).fetchMetrics(getQuery(testName))
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, indexName, script).fetchMetrics(getQuery(testName))
def keys = jsonResponse.aggregations.git_reference_keyword_agg.buckets.collect { it.key }
return keys
}
Expand Down
8 changes: 5 additions & 3 deletions src/gradlecheck/FetchTestPullRequests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
package gradlecheck

import groovy.json.JsonOutput
import gradlecheck.OpenSearchMetricsQuery
import utils.OpenSearchMetricsQuery

class FetchTestPullRequests {
String metricsUrl
String awsAccessKey
String awsSecretKey
String awsSessionToken
String indexName
def script

FetchTestPullRequests(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, def script) {
FetchTestPullRequests(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, String indexName, def script) {
this.metricsUrl = metricsUrl
this.awsAccessKey = awsAccessKey
this.awsSecretKey = awsSecretKey
this.awsSessionToken = awsSessionToken
this.indexName = indexName
this.script = script
}

Expand Down Expand Up @@ -97,7 +99,7 @@ class FetchTestPullRequests {
return query.replace('"', '\\"')
}
List<String> getTestPullRequests(testName) {
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, script).fetchMetrics(getQuery(testName))
def jsonResponse = new OpenSearchMetricsQuery(metricsUrl,awsAccessKey, awsSecretKey, awsSessionToken, indexName, script).fetchMetrics(getQuery(testName))
def keys = jsonResponse.aggregations.pull_request_keyword_agg.buckets.collect { it.key }
return keys
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* compatible open source license.
*/

package gradlecheck
package utils

import groovy.json.JsonSlurper

Expand All @@ -16,13 +16,15 @@ class OpenSearchMetricsQuery {
String awsAccessKey
String awsSecretKey
String awsSessionToken
String indexName
def script

OpenSearchMetricsQuery(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, def script) {
OpenSearchMetricsQuery(String metricsUrl, String awsAccessKey, String awsSecretKey, String awsSessionToken, String indexName, def script) {
this.metricsUrl = metricsUrl
this.awsAccessKey = awsAccessKey
this.awsSecretKey = awsSecretKey
this.awsSessionToken = awsSessionToken
this.indexName = indexName
this.script = script
}

Expand All @@ -32,8 +34,7 @@ class OpenSearchMetricsQuery {
script: """
set -e
set +x
MONTH_YEAR=\$(date +"%m-%Y")
curl -s -XGET "${metricsUrl}/gradle-check/_search" --aws-sigv4 "aws:amz:us-east-1:es" --user "${awsAccessKey}:${awsSecretKey}" -H "x-amz-security-token:${awsSessionToken}" -H 'Content-Type: application/json' -d "${query}" | jq '.'
curl -s -XGET "${metricsUrl}/${indexName}/_search" --aws-sigv4 "aws:amz:us-east-1:es" --user "${awsAccessKey}:${awsSecretKey}" -H "x-amz-security-token:${awsSessionToken}" -H 'Content-Type: application/json' -d "${query}" | jq '.'
""",
returnStdout: true
).trim()
Expand Down
3 changes: 2 additions & 1 deletion tests/gradlecheck/FetchPostMergeFailedTestClassTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FetchPostMergeFailedTestClassTest {
private final String awsAccessKey = "testAccessKey"
private final String awsSecretKey = "testSecretKey"
private final String awsSessionToken = "testSessionToken"
private final String indexName = "gradle-check-*"
private def script

@Before
Expand All @@ -42,7 +43,7 @@ class FetchPostMergeFailedTestClassTest {
}
return ""
}
fetchPostMergeFailedTestClass = new FetchPostMergeFailedTestClass(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, script)
fetchPostMergeFailedTestClass = new FetchPostMergeFailedTestClass(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, script)
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion tests/gradlecheck/FetchPostMergeFailedTestNameTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FetchPostMergeFailedTestNameTest {
private final String awsAccessKey = "testAccessKey"
private final String awsSecretKey = "testSecretKey"
private final String awsSessionToken = "testSessionToken"
private final String indexName = "gradle-check-*"
private def script

@Before
Expand Down Expand Up @@ -54,7 +55,7 @@ class FetchPostMergeFailedTestNameTest {
}
return ""
}
fetchPostMergeFailedTestName = new FetchPostMergeFailedTestName(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, script)
fetchPostMergeFailedTestName = new FetchPostMergeFailedTestName(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, script)
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion tests/gradlecheck/FetchPostMergeTestGitReferenceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FetchPostMergeTestGitReferenceTest {
private final String awsAccessKey = "testAccessKey"
private final String awsSecretKey = "testSecretKey"
private final String awsSessionToken = "testSessionToken"
private final String indexName = "gradle-check-*"
private def script

@Before
Expand All @@ -42,7 +43,7 @@ class FetchPostMergeTestGitReferenceTest {
}
return ""
}
fetchPostMergeTestGitReference = new FetchPostMergeTestGitReference(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, script)
fetchPostMergeTestGitReference = new FetchPostMergeTestGitReference(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, script)
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion tests/gradlecheck/FetchTestPullRequestsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FetchTestPullRequestsTest {
private final String awsAccessKey = "testAccessKey"
private final String awsSecretKey = "testSecretKey"
private final String awsSessionToken = "testSessionToken"
private final String indexName = "gradle-check-*"
private def script

@Before
Expand All @@ -42,7 +43,7 @@ class FetchTestPullRequestsTest {
}
return ""
}
fetchTestPullRequests = new FetchTestPullRequests(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, script)
fetchTestPullRequests = new FetchTestPullRequests(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, script)
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions tests/jenkins/TestGradleCheckFlakyTestGitHubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class TestGradleCheckFlakyTestGitHubIssue extends BuildPipelineTest {
@Test
public void testDefaultIssueEdit() {
super.testPipeline("tests/jenkins/jobs/TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile", "tests/jenkins/jobs/TestGradleCheckFlakyTestGitHubIssueEdit_Jenkinsfile")
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue edit bbb\nccc --repo https://github.com/prudhvigodithi/OpenSearch --body-file \"SampleTest.md\", returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue edit bbb\nccc --repo https://github.com/opensearch-project/OpenSearch --body-file \"SampleTest.md\", returnStdout=true}"))
}

@Test
public void testIssueCreate() {
helper.addShMock("""gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number'""") { script ->
helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number'""") { script ->
return [stdout: "", exitValue: 0]
}
helper.addShMock("""gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number'""") { script ->
helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number'""") { script ->
return [stdout: "", exitValue: 0]
}
super.testPipeline("tests/jenkins/jobs/TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile", "tests/jenkins/jobs/TestGradleCheckFlakyTestGitHubIssueCreate_Jenkinsfile")
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue create --title \"[AUTOCUT] Gradle Check Flaky Test Report for SampleTest\" --body-file \"SampleTest.md\" --label \"autocut,>test-failure,flaky-test\" --label \"untriaged\" --repo https://github.com/prudhvigodithi/OpenSearch, returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue create --title \"[AUTOCUT] Gradle Check Flaky Test Report for SampleTest\" --body-file \"SampleTest.md\" --label \"autocut,>test-failure,flaky-test\" --label \"untriaged\" --repo https://github.com/opensearch-project/OpenSearch, returnStdout=true}"))
}

def getCommands(method, text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.echo(Executing on agent [label:none])
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.stage(Detect Gradle Check Flaky Tests, groovy.lang.Closure)
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.script(groovy.lang.Closure)
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.gradleCheckFlakyTestGitHubIssue({repoUrl=https://github.com/prudhvigodithi/OpenSearch, issueTitle=[AUTOCUT] Gradle Check Flaky Test Report for SampleTest, issueBodyFile=SampleTest.md, label=autocut,>test-failure,flaky-test, issueEdit=true})
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.gradleCheckFlakyTestGitHubIssue({repoUrl=https://github.com/opensearch-project/OpenSearch, issueTitle=[AUTOCUT] Gradle Check Flaky Test Report for SampleTest, issueBodyFile=SampleTest.md, label=autocut,>test-failure,flaky-test, issueEdit=true})
gradleCheckFlakyTestGitHubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
gradleCheckFlakyTestGitHubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.println(Creating new issue)
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue create --title "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest" --body-file "SampleTest.md" --label "autocut,>test-failure,flaky-test" --label "untriaged" --repo https://github.com/prudhvigodithi/OpenSearch, returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue create --title "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest" --body-file "SampleTest.md" --label "autocut,>test-failure,flaky-test" --label "untriaged" --repo https://github.com/opensearch-project/OpenSearch, returnStdout=true})
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.echo(Executing on agent [label:none])
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.stage(Detect Gradle Check Flaky Tests, groovy.lang.Closure)
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.script(groovy.lang.Closure)
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.gradleCheckFlakyTestGitHubIssue({repoUrl=https://github.com/prudhvigodithi/OpenSearch, issueTitle=[AUTOCUT] Gradle Check Flaky Test Report for SampleTest, issueBodyFile=SampleTest.md, label=autocut,>test-failure,flaky-test, issueEdit=true})
TestGradleCheckFlakyTestGitHubIssue_Jenkinsfile.gradleCheckFlakyTestGitHubIssue({repoUrl=https://github.com/opensearch-project/OpenSearch, issueTitle=[AUTOCUT] Gradle Check Flaky Test Report for SampleTest, issueBodyFile=SampleTest.md, label=autocut,>test-failure,flaky-test, issueEdit=true})
gradleCheckFlakyTestGitHubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
gradleCheckFlakyTestGitHubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/prudhvigodithi/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch -S "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest in:title is:closed" --json number --jq '.[0].number', returnStdout=true})
gradleCheckFlakyTestGitHubIssue.println(Issue already exists, editing the issue body)
gradleCheckFlakyTestGitHubIssue.sh({script=gh issue edit bbb
ccc --repo https://github.com/prudhvigodithi/OpenSearch --body-file "SampleTest.md", returnStdout=true})
ccc --repo https://github.com/opensearch-project/OpenSearch --body-file "SampleTest.md", returnStdout=true})
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pipeline {
steps {
script {
gradleCheckFlakyTestGitHubIssue(
repoUrl: "https://github.com/prudhvigodithi/OpenSearch",
repoUrl: "https://github.com/opensearch-project/OpenSearch",
issueTitle: "[AUTOCUT] Gradle Check Flaky Test Report for SampleTest",
issueBodyFile: "SampleTest.md",
label: 'autocut,>test-failure,flaky-test',
Expand Down
Loading

0 comments on commit 8a67658

Please sign in to comment.