Skip to content
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

feat(codemod): elasticsearch migrations v8 #1259

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"version": "1.0.1",
"private": false,
"name": "elasticsearch/8/migrate-plugins-extended",
"engine": "jscodeshift",
"applicability": {
"from": [["elasticsearch", ">=", "7.0.0"], ["elasticsearch", "<", "8.0.0"]],
"to": [["elasticsearch", ">=", "8.0.0"]]
},
"meta": {
"tags": ["elasticsearch", "8", "migration"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/elasticsearch/8/migrate-plugins-extended"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Codemod Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions packages/codemods/elasticsearch/8/migrate-plugins-extend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This codemod migrates the plugins option to client.extend() calls.

## What Changed

Previously, you could pass plugins as an option to the Client constructor. Now, plugins need to be added using the client.extend() method.
## Before

```jsx
const { Client } = require('elasticsearch');
const client = new Client({
node: 'http://localhost:9200',
plugins: ['pluginA', 'pluginB']
});

```

## After

```jsx
const { Client } = require('@elastic/elasticsearch');
const client = new Client({
node: 'http://localhost:9200'
});
client.extend('pluginA');
client.extend('pluginB');
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
await refresh({ dedupe: true });
await refresh({ dedupe: false });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
await refresh({ dedupe: "cancel" });
await refresh({ dedupe: "defer" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@codemod/elasticsearch-8-migrate-plugins-extended",
"dependencies": {},
"devDependencies": {
35C4n0r marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^5.2.2",
"ts-node": "^10.9.1",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10",
"vitest": "^1.0.1",
"@vitest/coverage-v8": "^1.0.1"
},
"scripts": {},
"files": ["./README.md", "./.codemodrc.json", "./dist/index.cjs"],
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import type {
API,
FileInfo,
Identifier,
Options,
StringLiteral,
} from "jscodeshift";

export default function transform(
file: FileInfo,
api: API,
options?: Options,
): string | undefined {
const j = api.jscodeshift;
const root = j(file.source);
let isDirty = false;

// Find Client import from 'elasticsearch'
root
.find(j.VariableDeclarator, {
id: {
type: "ObjectPattern",
properties: [
{
type: "ObjectProperty",
key: {
type: "Identifier",
name: "Client",
},
},
],
},
init: {
callee: {
type: "Identifier",
name: "require",
},
arguments: [{ type: "StringLiteral", value: "elasticsearch" }],
},
})
.forEach((path) => {
if (path.node.init && j.CallExpression.check(path.node.init)) {
(path.node.init.arguments[0] as StringLiteral).value =
"@elastic/elasticsearch";
isDirty = true;
}
});

// Find Client instantiation with plugins
root
.find(j.NewExpression, {
callee: { name: "Client" },
})
.forEach((path) => {
const clientInit = path.node.arguments[0];
const pluginsProperty = clientInit.properties.find(
(prop) =>
j.ObjectProperty.check(prop) &&
(prop.key as Identifier).name === "plugins",
);

if (pluginsProperty) {
// Remove plugins property from client initialization
clientInit.properties = clientInit.properties.filter(
(prop) => prop !== pluginsProperty,
);

// Insert separate client.extend calls for each plugin
const plugins = pluginsProperty.value.elements;
const clientVariable = path.parentPath.node.id.name;

if (Array.isArray(plugins)) {
plugins.reverse().forEach((plugin) => {
console.log(plugin);
const extendCall = j.expressionStatement(
j.callExpression(
j.memberExpression(
j.identifier(clientVariable),
j.identifier("extend"),
),
[plugin],
),
);

j(path.parentPath.parent).insertAfter(extendCall);
});
isDirty = true;
}
}
});

return isDirty ? root.toSource(options) : undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"module": "NodeNext",
"skipLibCheck": true,
"strict": true,
"target": "ES6",
"allowJs": true,
"noUncheckedIndexedAccess": true
},
"include": [
"./src/**/*.ts",
"./src/**/*.js",
"./test/**/*.ts",
"./test/**/*.js"
],
"exclude": ["node_modules", "./dist/**/*"],
"ts-node": {
"transpileOnly": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: [...configDefaults.include, "**/test/*.ts"],
},
});
22 changes: 22 additions & 0 deletions packages/codemods/elasticsearch/8/migration-recipe/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "elasticsearch/8/migration-recipe",
"version": "1.0.4",
"private": false,
"engine": "recipe",
"names": [
"elasticsearch/8/migrate-plugins-extended",
"elasticsearch/8/move-client-configurations",
"elasticsearch/8/rename-query-string",
"elasticsearch/8/unify-return-value"
],
"meta": {
"tags": ["migration", "elasticsearch"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/elasticsearch/8/migration-recipe"
},
"applicability": {
"from": [["elasticsearch", ">=", "7.0.0"], ["elasticsearch", "<", "8.0.0"]],
"to": [["elasticsearch", ">=", "8.0.0"]]
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
10 changes: 10 additions & 0 deletions packages/codemods/elasticsearch/8/migration-recipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


This recipe is a set of codemods that will help migrate to Elasticsearch 8.

The recipe includes the following codemods:

- elasticsearch/8/migrate-plugins-extended
- elasticsearch/8/move-client-configurations
- elasticsearch/8/rename-query-string
- elasticsearch/8/unify-return-value
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@codemod/elasticsearch-8-migration-recipe",
"files": ["./README.md", "./.codemodrc.json"],
"type": "module"
35C4n0r marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"version": "1.0.1",
"private": false,
"name": "elasticsearch/8/move-client-configurations",
"engine": "jscodeshift",
"applicability": {
"from": [["elasticsearch", ">=", "7.0.0"], ["elasticsearch", "<", "8.0.0"]],
"to": [["elasticsearch", ">=", "8.0.0"]]
},
"meta": {
"tags": ["elasticsearch", "8", "migration"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/elasticsearch/8/move-client-configurations"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Codemod Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
This codemod moves the client-related configuration parameters (ignore, headers, requestTimeout, and maxRetries) from the API object to the second options object.

## What Changed

Previously, ignore, headers, requestTimeout, and maxRetries could be passed directly inside the API object. Now, these parameters need to be specified in a second options object.

Now, both callbacks and promises return an object containing body, statusCode, headers, warnings, and meta.
## Before

```jsx
const body = await client.search({
index: 'my-index',
body: { foo: 'bar' },
ignore: [404]
});

client.search({
index: 'my-index',
body: { foo: 'bar' },
ignore: [404]
}, (err, body, statusCode, headers) => {
if (err) console.log(err);
});
```

## After

```jsx
const { body, statusCode, headers, warnings } = await client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
ignore: [404]
});

client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
ignore: [404]
}, (err, { body, statusCode, headers, warnings }) => {
if (err) console.log(err);
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const result = await client.b.c.index({
index: 'my-index',
body: { foo: 'bar' },
headers: { key: 'Value' },
ignore: ['404'],
});

client.index(
{
index: 'my-index',
body: { foo: 'bar' },
refresh: true,
},
(err, result) => {
if (err) console.log(err);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const result = await client.b.c.index(
{
index: 'my-index',
body: { foo: 'bar' },
},
{
headers: { key: 'Value' },
ignore: ['404'],
},
);

client.index(
{
index: 'my-index',
body: { foo: 'bar' },
refresh: true,
},
(err, result) => {
if (err) console.log(err);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@codemod/elasticsearch-8-move-client-configurations",
"dependencies": {},
35C4n0r marked this conversation as resolved.
Show resolved Hide resolved
"devDependencies": {
"typescript": "^5.2.2",
"ts-node": "^10.9.1",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10",
"vitest": "^1.0.1",
"@vitest/coverage-v8": "^1.0.1"
},
"scripts": {},
"files": ["./README.md", "./.codemodrc.json", "./dist/index.cjs"],
"type": "module"
}
Loading
Loading