Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya committed Oct 2, 2024
1 parent e415fc3 commit 3d2c158
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 59 deletions.
6 changes: 1 addition & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sourceSets {

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

Expand Down Expand Up @@ -128,11 +128,7 @@ jacocoTestReport {
}
}

<<<<<<< HEAD
String version = '6.9.2'
=======
String version = '7.0.0'
>>>>>>> ee5887ff (Refactor updateBuildFailureIssues library with new logic)

task updateVersion {
doLast {
Expand Down
1 change: 0 additions & 1 deletion src/utils/OpenSearchMetricsQuery.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class OpenSearchMetricsQuery {
""",
returnStdout: true
).trim()
this.script.println(response)
return new JsonSlurper().parseText(response)
}
}
95 changes: 87 additions & 8 deletions tests/jenkins/TestUpdateBuildFailuresIssues.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,109 @@ import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItem
import static org.hamcrest.CoreMatchers.not
import static org.hamcrest.MatcherAssert.assertThat
import groovy.json.JsonSlurper
import utils.OpenSearchMetricsQuery

class TestUpdateBuildFailuresIssues extends BuildPipelineTest {

@Override
@Before
void setUp() {
this.registerLibTester(new UpdateBuildFailureIssuesLibTester(["Error building common-utils, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component common-utils", "Error building performance-analyzer, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0ed in the next build. This might have performance impact if it keeps failing. Run the javaToolchains task for more det.yml --component performance-analyzer", "Error building asynchronous-search, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component asynchronous-search", "Error building asynchronous-search, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component asynchronous-search", "Error building anomaly-detection, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component anomaly-detection", "Error building performance-analyzer, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component performance-analyzer", "Error building notifications, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component notifications"], ["Successfully built sql", "Successfully built notifications", "Successfully built notifications", "Successfully built sql", "Successfully built anomaly-detection", "Successfully built index-management", "Successfully built sql", "Successfully built anomaly-detection"], 'tests/data/opensearch-2.2.0.yml'))
this.registerLibTester(new UpdateBuildFailureIssuesLibTester('tests/data/opensearch-2.2.0.yml', '32'))
super.setUp()
def unformattedResponseForPass = '''
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 15,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "OsNSLJIBhoDV_8nijeJt",
"_score": 0,
"_source": {
"component": "sql"
}
},
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "LsNSLJIBhoDV_8nijeJt",
"_score": 0,
"_source": {
"component": "notifications"
}
}
]
}
}
'''
def unformattedResponseForFail = '''
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 15,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "OsNSLJIBhoDV_8nijeJt",
"_score": 0,
"_source": {
"component": "asynchronous-search"
}
},
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "LsNSLJIBhoDV_8nijeJt",
"_score": 0,
"_source": {
"component": "notifications"
}
}
]
}
}
'''
helper.addShMock("""\n set -e\n set +x\n curl -s -XGET \"sample.url/opensearch-distribution-build-results/_search\" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"abc:xyz\" -H \"x-amz-security-token:sampleToken\" -H 'Content-Type: application/json' -d \"{\\"_source\\":[\\"component\\"],\\"query\\":{\\"bool\\":{\\"filter\\":[{\\"match_phrase\\":{\\"component_category\\":\\"OpenSearch\\"}},{\\"match_phrase\\":{\\"component_build_result\\":\\"passed\\"}},{\\"match_phrase\\":{\\"version\\":\\"2.2.0\\"}},{\\"match_phrase\\":{\\"distribution_build_number\\":\\"32\\"}},{\\"range\\":{\\"build_start_time\\":{\\"from\\":\\"now-6h\\",\\"to\\":\\"now\\"}}}]}}}\" | jq '.'\n """) { script ->
return [stdout: unformattedResponseForPass, exitValue: 0]
}
helper.addShMock("""\n set -e\n set +x\n curl -s -XGET \"sample.url/opensearch-distribution-build-results/_search\" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"abc:xyz\" -H \"x-amz-security-token:sampleToken\" -H 'Content-Type: application/json' -d \"{\\"_source\\":[\\"component\\"],\\"query\\":{\\"bool\\":{\\"filter\\":[{\\"match_phrase\\":{\\"component_category\\":\\"OpenSearch\\"}},{\\"match_phrase\\":{\\"component_build_result\\":\\"failed\\"}},{\\"match_phrase\\":{\\"version\\":\\"2.2.0\\"}},{\\"match_phrase\\":{\\"distribution_build_number\\":\\"32\\"}},{\\"range\\":{\\"build_start_time\\":{\\"from\\":\\"now-6h\\",\\"to\\":\\"now\\"}}}]}}}\" | jq '.'\n """) { script ->
return [stdout: unformattedResponseForFail, exitValue: 0]
}
}

@Test
public void testCommentOnExistingGithubIssue() {
helper.addShMock("date -d \"3 days ago\" +'%Y-%m-%d'") { script ->
return [stdout: "2023-10-24", exitValue: 0]
}
public void testGithubIssueEdit() {
helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/asynchronous-search.git -S "[AUTOCUT] Distribution Build Failed for asynchronous-search-2.2.0 in:title" --label autocut,v2.2.0 --json number --jq '.[0].number'""") { script ->
return [stdout: "22", exitValue: 0]
}
helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/asynchronous-search.git -S "[AUTOCUT] Distribution Build Failed for asynchronous-search-2.2.0 in:title is:closed closed:>=2023-10-24" --label autocut,v2.2.0 --json number --jq '.[0].number'""") { script ->
return [stdout: "", exitValue: 0]
}
runScript('tests/jenkins/jobs/UpdateBuildFailureIssue_Jenkinsfile')
assertThat(getCommands('println', ''), hasItem('Issue already exists, adding a comment'))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment bbb\nccc --repo https://github.com/opensearch-project/asynchronous-search.git --body \"***Received Error***: **Error building asynchronous-search, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component asynchronous-search**.\n asynchronous-search failed during the distribution build for version: 2.2.0.\n Please see build log at www.example.com/job/build_url/32/display/redirect.\n The failed build stage will be marked as unstable(!). Please see ./build.sh step for more details\", returnStdout=true}"))
assertThat(getCommands('println', ''), hasItem('Issue already exists, editing the issue body'))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue edit bbb\nccc --repo https://github.com/opensearch-project/asynchronous-search.git --body \"***Build Failed Error***: **asynchronous-search failed during the distribution build for version: 2.2.0.**\n Please see build log at www.example.com/job/build_url/32/display/redirect.\n The failed build stage will be marked as unstable :warning: . Please see ./build.sh step for more details.\n Checkout the [wiki](https://github.com/opensearch-project/opensearch-build/wiki/Building-an-OpenSearch-and-OpenSearch-Dashboards-Distribution) to reproduce the failure locally.\", returnStdout=true}"))
}

@Test
Expand All @@ -64,7 +143,7 @@ class TestUpdateBuildFailuresIssues extends BuildPipelineTest {
}
runScript('tests/jenkins/jobs/UpdateBuildFailureIssue_Jenkinsfile')
assertThat(getCommands('sh', 'notifications'), not(hasItem("{script=gh issue close bbb\nccc -R opensearch-project/notifications --comment \"Closing the issue as the distribution build for notifications has passed for version: **2.2.0**.\n Please see build log at www.example.com/job/build_url/32/display/redirect\", returnStdout=true}")))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment bbb\nccc --repo https://github.com/opensearch-project/notifications.git --body \"***Received Error***: **Error building notifications, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component notifications**.\n notifications failed during the distribution build for version: 2.2.0.\n Please see build log at www.example.com/job/build_url/32/display/redirect.\n The failed build stage will be marked as unstable(!). Please see ./build.sh step for more details\", returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue edit bbb\nccc --repo https://github.com/opensearch-project/notifications.git --body \"***Build Failed Error***: **notifications failed during the distribution build for version: 2.2.0.**\n Please see build log at www.example.com/job/build_url/32/display/redirect.\n The failed build stage will be marked as unstable :warning: . Please see ./build.sh step for more details.\n Checkout the [wiki](https://github.com/opensearch-project/opensearch-build/wiki/Building-an-OpenSearch-and-OpenSearch-Dashboards-Distribution) to reproduce the failure locally.\", returnStdout=true}"))
}

def getCommands(method, text) {
Expand Down
5 changes: 2 additions & 3 deletions tests/jenkins/jobs/UpdateBuildFailureIssue_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ pipeline {
steps {
script {
UpdateBuildFailureIssues(
failureMessages: ["Error building common-utils, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component common-utils", "Error building performance-analyzer, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0ed in the next build. This might have performance impact if it keeps failing. Run the javaToolchains task for more det.yml --component performance-analyzer", "Error building asynchronous-search, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component asynchronous-search", "Error building asynchronous-search, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component asynchronous-search", "Error building anomaly-detection, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component anomaly-detection", "Error building performance-analyzer, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component performance-analyzer", "Error building notifications, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component notifications"],
passMessages: ["Successfully built sql", "Successfully built notifications", "Successfully built notifications", "Successfully built sql", "Successfully built anomaly-detection", "Successfully built index-management", "Successfully built sql", "Successfully built anomaly-detection"],
inputManifestPath: 'tests/data/opensearch-2.2.0.yml'
inputManifestPath: 'tests/data/opensearch-2.2.0.yml',
distributionBuildNumber: '32'
)
}
}
Expand Down
30 changes: 20 additions & 10 deletions tests/jenkins/lib-testers/UpdateBuildFailureIssuesLibTester.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import static org.hamcrest.CoreMatchers.nullValue
import static org.hamcrest.MatcherAssert.assertThat

class UpdateBuildFailureIssuesLibTester extends LibFunctionTester{
private List<String> failureMessages
private List<String> passMessages
private String inputManifestPath
private String distributionBuildNumber

public UpdateBuildFailureIssuesLibTester(failureMessages, passMessages, inputManifestPath){
this.failureMessages = failureMessages
this.passMessages = passMessages
public UpdateBuildFailureIssuesLibTester(inputManifestPath, distributionBuildNumber){
this.inputManifestPath = inputManifestPath
this.distributionBuildNumber = distributionBuildNumber
}

@Override
Expand All @@ -28,22 +26,34 @@ class UpdateBuildFailureIssuesLibTester extends LibFunctionTester{

@Override
void parameterInvariantsAssertions(Object call) {
assertThat(call.args.failureMessages.first(), notNullValue())
assertThat(call.args.passMessages.first(), notNullValue())
assertThat(call.args.inputManifestPath.first(), notNullValue())
assertThat(call.args.distributionBuildNumber.first(), notNullValue())
}

@Override
boolean expectedParametersMatcher(Object call) {
return call.args.failureMessages.first().equals(this.failureMessages)
&& call.args.passMessages.first().equals(this.passMessages)
return call.args.distributionBuildNumber.first().equals(this.distributionBuildNumber)
&& call.args.inputManifestPath.first().equals(this.inputManifestPath)
}

@Override
void configure(Object helper, Object binding) {
helper.registerAllowedMethod('withCredentials', [Map])
helper.registerAllowedMethod('sleep', [Map])
binding.setVariable('env', ['RUN_DISPLAY_URL': 'www.example.com/job/build_url/32/display/redirect'])
binding.setVariable('env', [
'RUN_DISPLAY_URL': 'www.example.com/job/build_url/32/display/redirect',
'METRICS_HOST_URL': 'sample.url',
'AWS_ACCESS_KEY_ID': 'abc',
'AWS_SECRET_ACCESS_KEY':'xyz',
'AWS_SESSION_TOKEN': 'sampleToken'
])
helper.registerAllowedMethod('withCredentials', [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.registerAllowedMethod('withAWS', [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
}
}
61 changes: 29 additions & 32 deletions vars/UpdateBuildFailureIssues.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void call(Map args = [:]) {
def awsSessionToken = env.AWS_SESSION_TOKEN
def indexName = 'opensearch-distribution-build-results'

def componentBuildStatus = new ComponentBuildStatus(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, product, currentVersion, args.distributionBuildNumber, buildStartTimeFrom, buildStartTimeTo, this)
ComponentBuildStatus componentBuildStatus = new ComponentBuildStatus(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, product, currentVersion, args.distributionBuildNumber, buildStartTimeFrom, buildStartTimeTo, this)

passedComponents = componentBuildStatus.getComponents('passed')
failedComponents = componentBuildStatus.getComponents('failed')
Expand All @@ -46,35 +46,32 @@ void call(Map args = [:]) {
failedComponents = failedComponents.unique()
passedComponents = passedComponents.unique()

println(failedComponents)
println(passedComponents)

// for (component in inputManifest.components) {
// if (failedComponents.contains(component.name)) {
// println("Component ${component.name} failed, creating github issue")
// ghIssueBody = """***Build Failed Error***: **${component.name} failed during the distribution build for version: ${currentVersion}.**
// Please see build log at ${env.RUN_DISPLAY_URL}.
// The failed build stage will be marked as unstable :warning: . Please see ./build.sh step for more details.
// Checkout the [wiki](https://github.com/opensearch-project/opensearch-build/wiki/Building-an-OpenSearch-and-OpenSearch-Dashboards-Distribution) to reproduce the failure locally.""".stripIndent()
// createGithubIssue(
// repoUrl: component.repository,
// issueTitle: "[AUTOCUT] Distribution Build Failed for ${component.name}-${currentVersion}",
// issueBody: ghIssueBody,
// label: "autocut,v${currentVersion}",
// issueEdit: true
// )
// }
// // if (passedComponents.contains(component.name) && !failedComponents.contains(component.name)) {
// // println("Component ${component.name} passed, closing github issue")
// // ghIssueBody = """Closing the issue as the distribution build for ${component.name} has passed for version: **${currentVersion}**.
// // Please see build log at ${env.RUN_DISPLAY_URL}""".stripIndent()
// // closeGithubIssue(
// // repoUrl: component.repository,
// // issueTitle: "[AUTOCUT] Distribution Build Failed for ${component.name}-${currentVersion}",
// // closeComment: ghIssueBody,
// // label: "autocut,v${currentVersion}"
// // )
// // }
// sleep(time:3, unit:'SECONDS')
// }
for (component in inputManifest.components) {
if (failedComponents.contains(component.name)) {
println("Component ${component.name} failed, creating github issue")
ghIssueBody = """***Build Failed Error***: **${component.name} failed during the distribution build for version: ${currentVersion}.**
Please see build log at ${env.RUN_DISPLAY_URL}.
The failed build stage will be marked as unstable :warning: . Please see ./build.sh step for more details.
Checkout the [wiki](https://github.com/opensearch-project/opensearch-build/wiki/Building-an-OpenSearch-and-OpenSearch-Dashboards-Distribution) to reproduce the failure locally.""".stripIndent()
createGithubIssue(
repoUrl: component.repository,
issueTitle: "[AUTOCUT] Distribution Build Failed for ${component.name}-${currentVersion}",
issueBody: ghIssueBody,
label: "autocut,v${currentVersion}",
issueEdit: true
)
}
if (passedComponents.contains(component.name) && !failedComponents.contains(component.name)) {
println("Component ${component.name} passed, closing github issue")
ghIssueBody = """Closing the issue as the distribution build for ${component.name} has passed for version: **${currentVersion}**.
Please see build log at ${env.RUN_DISPLAY_URL}""".stripIndent()
closeGithubIssue(
repoUrl: component.repository,
issueTitle: "[AUTOCUT] Distribution Build Failed for ${component.name}-${currentVersion}",
closeComment: ghIssueBody,
label: "autocut,v${currentVersion}"
)
}
sleep(time:3, unit:'SECONDS')
}
}

0 comments on commit 3d2c158

Please sign in to comment.