-
Notifications
You must be signed in to change notification settings - Fork 51
feat(codemod): elasticsearch migrations v8 #1259
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/codemods/elasticsearch/8/migrate-plugins-extend/.codemodrc.json
This file contains hidden or 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,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"] | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/codemods/elasticsearch/8/migrate-plugins-extend/.gitignore
This file contains hidden or 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,2 @@ | ||
node_modules | ||
dist |
9 changes: 9 additions & 0 deletions
9
packages/codemods/elasticsearch/8/migrate-plugins-extend/LICENSE
This file contains hidden or 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,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
26
packages/codemods/elasticsearch/8/migrate-plugins-extend/README.md
This file contains hidden or 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,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'); | ||
``` |
3 changes: 3 additions & 0 deletions
3
packages/codemods/elasticsearch/8/migrate-plugins-extend/__testfixtures__/fixture1.input.ts
This file contains hidden or 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,3 @@ | ||
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation> | ||
await refresh({ dedupe: true }); | ||
await refresh({ dedupe: false }); |
3 changes: 3 additions & 0 deletions
3
packages/codemods/elasticsearch/8/migrate-plugins-extend/__testfixtures__/fixture1.output.ts
This file contains hidden or 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,3 @@ | ||
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation> | ||
await refresh({ dedupe: "cancel" }); | ||
await refresh({ dedupe: "defer" }); |
16 changes: 16 additions & 0 deletions
16
packages/codemods/elasticsearch/8/migrate-plugins-extend/package.json
This file contains hidden or 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,16 @@ | ||
{ | ||
"name": "@codemod/elasticsearch-8-migrate-plugins-extended", | ||
"dependencies": {}, | ||
"private": true, | ||
"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" | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/codemods/elasticsearch/8/migrate-plugins-extend/src/index.ts
This file contains hidden or 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,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; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/codemods/elasticsearch/8/migrate-plugins-extend/tsconfig.json
This file contains hidden or 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,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 | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/codemods/elasticsearch/8/migrate-plugins-extend/vitest.config.ts
This file contains hidden or 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,7 @@ | ||
import { configDefaults, defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: [...configDefaults.include, "**/test/*.ts"], | ||
}, | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/codemods/elasticsearch/8/migration-recipe/.codemodrc.json
This file contains hidden or 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,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
10
packages/codemods/elasticsearch/8/migration-recipe/README.md
This file contains hidden or 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,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 |
6 changes: 6 additions & 0 deletions
6
packages/codemods/elasticsearch/8/migration-recipe/package.json
This file contains hidden or 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,6 @@ | ||
{ | ||
"name": "@codemod/elasticsearch-8-migration-recipe", | ||
"files": ["./README.md", "./.codemodrc.json"], | ||
"type": "module", | ||
"private": true | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/codemods/elasticsearch/8/move-client-configurations/.codemodrc.json
This file contains hidden or 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,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"] | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/codemods/elasticsearch/8/move-client-configurations/.gitignore
This file contains hidden or 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,2 @@ | ||
node_modules | ||
dist |
9 changes: 9 additions & 0 deletions
9
packages/codemods/elasticsearch/8/move-client-configurations/LICENSE
This file contains hidden or 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,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. |
44 changes: 44 additions & 0 deletions
44
packages/codemods/elasticsearch/8/move-client-configurations/README.md
This file contains hidden or 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,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); | ||
}); | ||
``` |
17 changes: 17 additions & 0 deletions
17
...es/codemods/elasticsearch/8/move-client-configurations/__testfixtures__/fixture1.input.ts
This file contains hidden or 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,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); | ||
}, | ||
); |
21 changes: 21 additions & 0 deletions
21
...s/codemods/elasticsearch/8/move-client-configurations/__testfixtures__/fixture1.output.ts
This file contains hidden or 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,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); | ||
}, | ||
); |
16 changes: 16 additions & 0 deletions
16
packages/codemods/elasticsearch/8/move-client-configurations/package.json
This file contains hidden or 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,16 @@ | ||
{ | ||
"name": "@codemod/elasticsearch-8-move-client-configurations", | ||
"dependencies": {}, | ||
35C4n0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"private": true, | ||
"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" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.