Skip to content

Commit

Permalink
Add compatibility check task
Browse files Browse the repository at this point in the history
Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya committed Jun 10, 2023
1 parent 5ebfbcd commit 6e5b81b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.opensearch.gradle.Version
import org.opensearch.gradle.VersionProperties
import org.opensearch.gradle.info.BuildParams
import org.opensearch.gradle.plugin.PluginBuildPlugin
import org.opensearch.gradle.CheckCompatibilityTask
import org.opensearch.gradle.tar.SymbolicLinkPreservingTar
import org.gradle.plugins.ide.eclipse.model.AccessRule
import org.gradle.plugins.ide.eclipse.model.EclipseJdt
Expand Down Expand Up @@ -643,3 +644,15 @@ tasks.withType(TestTaskReports).configureEach {
tasks.named(JavaBasePlugin.CHECK_TASK_NAME) {
dependsOn tasks.named('testAggregateTestReport', TestReport)
}

tasks.register('checkCompatibility', CheckCompatibilityTask) {
description('Checks compatibility for all or given components')
}

allprojects { project ->
project.afterEvaluate {
if (project.tasks.findByName('publishToMavenLocal')) {
checkCompatibility.dependsOn(project.tasks.publishToMavenLocal)
}
}
}
2 changes: 2 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ dependencies {
api 'org.jruby.jcodings:jcodings:1.0.58'
api 'org.jruby.joni:joni:2.1.48'
api "com.fasterxml.jackson.core:jackson-databind:${props.getProperty('jackson_databind')}"
api "org.ajoberstar.grgit:grgit-core:5.2.0"


testFixturesApi "junit:junit:${props.getProperty('junit')}"
testFixturesApi "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.opensearch.gradle

import groovy.json.JsonSlurper
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.operation.BranchListOp
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction

class CheckCompatibilityTask extends DefaultTask {

@Input
List repositoryUrls = project.hasProperty('repositoryUrls') ? project.property('repositoryUrls').split(',') : getRepoUrls()

@Input
String ref = project.hasProperty('ref') ? project.property('ref') : 'main'

@Internal
List failedComponents = []

@Internal
List gitFailedComponents = []

@Internal
List compatibleComponents = []

@TaskAction
void checkCompatibility() {
println("Checking compability for: $repositoryUrls for $ref")
repositoryUrls.parallelStream().forEach { repositoryUrl ->
def tempDir = File.createTempDir()
def gradleScript = getGradleExec()
try {
if (cloneAndCheckout(repositoryUrl, tempDir)){
project.exec {
workingDir = tempDir
def stdout = new ByteArrayOutputStream()
commandLine gradleScript, 'assemble'
standardOutput stdout
}
compatibleComponents.add(repositoryUrl)
} else {
println("Skipping compatibility check for $repositoryUrl")
}
} catch (ex) {
failedComponents.add(repositoryUrl)
logger.info("Gradle assemble failed. Error logs:", ex)
} finally {
tempDir.deleteDir()
}
}
if (!failedComponents.isEmpty()) {
println("Incompatible components: $failedComponents")
logger.info("Compatible components: $compatibleComponents")
} else {
println("Compatible components: $compatibleComponents")
}
println("Skipped components: $gitFailedComponents")
}

protected static List getRepoUrls() {
def json = new JsonSlurper().parse('https://raw.githubusercontent.com/opensearch-project/opensearch-plugins/main/plugins/.meta'.toURL())
def labels = json.projects.values()
return labels as List
}

protected static String getGradleExec() {
if(System.properties['os.name'].toLowerCase().contains('windows')){
return 'gradlew.bat'
}
return './gradlew'
}

protected boolean cloneAndCheckout(repoUrl, directory){
def grgit = Grgit.clone(dir: directory, uri: repoUrl)
def remoteBranches = grgit.branch.list(mode: BranchListOp.Mode.REMOTE)
String targetBranch = 'origin/' + ref
if (remoteBranches.find { it.name == targetBranch } == null) {
gitFailedComponents.add(repoUrl)
logger.info("$ref does not exist for $repoUrl. Skipping the compatibility check!!")
return false
} else {
logger.info("Checking out $targetBranch")
grgit.checkout(branch: targetBranch)
return true
}
}
}

0 comments on commit 6e5b81b

Please sign in to comment.