-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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: Sayali Gaikawad <[email protected]>
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 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
97 changes: 97 additions & 0 deletions
97
buildSrc/src/main/groovy/org/opensearch/gradle/CheckCompatibilityTask.groovy
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,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 | ||
} | ||
} | ||
} |