-
Notifications
You must be signed in to change notification settings - Fork 1
support Java exporter in CI #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5b4678f
175351b
65ba45c
ae12426
26be5a2
57aa941
384ed5d
137bf5c
40c157e
a9ca14c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import childProcess from "child_process"; | ||
| import { FormatExporter, ConvertOptions } from "../convert"; | ||
| import { ParsedRequest, JSONValue } from "../parse"; | ||
| import { Request } from "../metamodel"; | ||
| import util from "util"; | ||
|
|
||
| const isBrowser = typeof window !== "undefined"; | ||
| const execAsync = !isBrowser ? util.promisify(childProcess.exec) : undefined; | ||
|
|
||
| type JavaRequest = { | ||
| api?: string; | ||
| params: Record<string, string | undefined>; | ||
| query?: Record<string, string>; | ||
| body: JSONValue; | ||
| }; | ||
|
|
||
| function getCodeGenParamNames( | ||
| params: Record<string, string | undefined>, | ||
| request: Request, | ||
| ) { | ||
| for (const [key, value] of Object.entries(params)) { | ||
| if (request?.path) { | ||
| for (const prop of request.path) { | ||
| if (prop.name === key && prop.codegenName !== undefined) { | ||
| delete params[key]; | ||
| params[prop.codegenName] = value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| export class JavaExporter implements FormatExporter { | ||
| available(): boolean { | ||
| return !isBrowser && !!process.env.JAVA_ES_REQUEST_CONVERTER_JAR; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| async check(requests: ParsedRequest[]): Promise<boolean> { | ||
| // only return true if all requests are for Elasticsearch | ||
| return requests | ||
| .map((req) => req.service == "es") | ||
| .reduce((prev, curr) => prev && curr, true); | ||
| } | ||
|
|
||
| async convert( | ||
| requests: ParsedRequest[], | ||
| options: ConvertOptions, | ||
| ): Promise<string> { | ||
| const javaRequests: JavaRequest[] = []; | ||
| for (const request of requests) { | ||
| if (request.request) { | ||
| const correctParams = getCodeGenParamNames( | ||
| request.params, | ||
| request.request, | ||
| ); | ||
| const body = request.body ?? {}; | ||
|
|
||
| const javaRequest: JavaRequest = { | ||
| api: request.api, | ||
| params: correctParams, | ||
| query: request.query, | ||
| body: body, | ||
| }; | ||
| javaRequests.push(javaRequest); | ||
| } | ||
| } | ||
|
|
||
| // escape single quotes that may appear in the payload | ||
| // (only for bash-style shells for now) | ||
| const req = JSON.stringify(javaRequests).replaceAll("'", "'\"'\"'"); | ||
| // other arguments to the Java converter | ||
| const complete = options.complete ? "true" : "false"; | ||
| const url = options.elasticsearchUrl ?? ""; | ||
|
|
||
| if (execAsync === undefined) { | ||
| throw new Error("Cannot use exec()"); | ||
| } | ||
| const { stdout, stderr } = await execAsync( | ||
| `java -jar ${process.env.JAVA_ES_REQUEST_CONVERTER_JAR} '${req}' '${complete}' '${url}'`, | ||
| ); | ||
| if (!stdout) { | ||
| throw new Error(`Could not invoke exporter: ${stderr}`); | ||
| } | ||
| return stdout; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| declare module "java-caller"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>app</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <name>app</name> | ||
| <!-- FIXME change it to the project's website --> | ||
| <url>http://www.example.com</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <maven.compiler.release>17</maven.compiler.release> | ||
| </properties> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit</groupId> | ||
| <artifactId>junit-bom</artifactId> | ||
| <version>5.11.0</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- Optionally: parameterized tests support --> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-params</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>co.elastic.clients</groupId> | ||
| <artifactId>elasticsearch-java</artifactId> | ||
| <version>9.2.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
| <plugins> | ||
| <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> | ||
| <plugin> | ||
| <artifactId>maven-clean-plugin</artifactId> | ||
| <version>3.4.0</version> | ||
| </plugin> | ||
| <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
| <plugin> | ||
| <artifactId>maven-resources-plugin</artifactId> | ||
| <version>3.3.1</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.13.0</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.3.0</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| <version>3.4.2</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-install-plugin</artifactId> | ||
| <version>3.1.2</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| <version>3.1.2</version> | ||
| </plugin> | ||
| <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> | ||
| <plugin> | ||
| <artifactId>maven-site-plugin</artifactId> | ||
| <version>3.12.1</version> | ||
| </plugin> | ||
| <plugin> | ||
| <artifactId>maven-project-info-reports-plugin</artifactId> | ||
| <version>3.6.1</version> | ||
| </plugin> | ||
| <plugin> | ||
|
Comment on lines
+53
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are all of these used somewhere?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is a template pom.xml file that I generated a while ago, I believe using |
||
| <artifactId>maven-assembly-plugin</artifactId> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass>org.example.App</mainClass> | ||
| </manifest> | ||
| </archive> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </pluginManagement> | ||
| </build> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #!/bin/bash | ||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
| CURRENT_DIR=$(pwd) | ||
| BRANCH=$(jq -r .version package.json | grep -Eo "^[0-9]+\.[0-9]+") | ||
|
|
||
| if [[ "$1" == "" ]]; then | ||
| # the `test:setup` command runs this script without arguments to initialize | ||
| # the environment, so we first delete any previous one | ||
| rm -rf $SCRIPT_DIR/.java-request-converter | ||
| rm -rf $SCRIPT_DIR/.java | ||
| fi | ||
|
|
||
| if [[ ! -d $SCRIPT_DIR/.java-es-request-converter ]]; then | ||
| git clone https://github.com/elastic/java-request-converter $SCRIPT_DIR/.java-request-converter | ||
| cd $SCRIPT_DIR/.java-request-converter | ||
| ./gradlew jar | ||
| cd $CURRENT_DIR | ||
| fi | ||
|
|
||
| if [[ ! -d $SCRIPT_DIR/.java ]]; then | ||
| mkdir $SCRIPT_DIR/.java | ||
| echo "Installing from branch $BRANCH." | ||
| git clone -b "$BRANCH" --depth=1 "https://github.com/elastic/elasticsearch-java.git" $SCRIPT_DIR/.java || | ||
| (echo "Branch $BRANCH not found. Cloning main branch." && | ||
| git clone -b "main" --depth=1 "https://github.com/elastic/elasticsearch-java.git" $SCRIPT_DIR/.java) | ||
| fi | ||
|
|
||
| if [[ "$1" != "" ]]; then | ||
| cd $SCRIPT_DIR/java-app | ||
| mkdir -p src/main/java/org/example | ||
| cp $CURRENT_DIR/$1 src/main/java/org/example/App.java | ||
| mvn clean compile assembly:single | ||
| if [[ "$?" == "0" ]]; then | ||
| java -jar target/app-1.0-SNAPSHOT-jar-with-dependencies.jar || true | ||
| fi | ||
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@l-trotta Here what I do for all the other languages is use a cloned copy of the source code. Because the CI runs on the
mainversion of the spec, so it is more correct to run it on the unreleased clients that will eventually be 9.3 then on the released 9.2.In the run-java.sh script I'm cloning the java client, but I haven't looked into how to include it here as a locally installed dependency. I hope it is possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, ignore the change then. to get it locally the dependency must have been previously published to the local .m2 repo running
mvn installin the project folder, then I think maven will try and get it by default if it's available, no modifications needed to the pom.