Skip to content

Commit 8d4deca

Browse files
authored
Merge pull request #53 from lucy66hw/Add_version
add version
2 parents 0996dd3 + d0f5d36 commit 8d4deca

5 files changed

Lines changed: 125 additions & 6 deletions

File tree

.github/workflows/convert-proto.yml

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ name: Auto Proto Convert
33
on:
44
workflow_dispatch:
55
inputs:
6-
input_param:
7-
description: ''
6+
opensearch_version:
7+
description: 'OpenSearch version (e.g., 3.4, 3.3.2). Leave empty to fetch latest.'
88
required: false
9+
type: string
910
jobs:
1011
auto-proto-convert:
1112
runs-on: ubuntu-latest
12-
if: github.repository == 'opensearch-project/opensearch-protobufs'
13+
# if: github.repository == 'opensearch-project/opensearch-protobufs'
1314
steps:
1415
- name: Checkout Repository
1516
uses: actions/checkout@v4
@@ -57,8 +58,55 @@ jobs:
5758
core.setOutput("latest_commit", latestCommit);
5859
console.log("Latest commit: " + latestCommit);
5960
61+
- name: Get Latest OpenSearch Core Version
62+
id: get_opensearch_version
63+
uses: actions/github-script@v6
64+
with:
65+
github-token: ${{ secrets.GITHUB_TOKEN }}
66+
script: |
67+
// Check if version was provided as input
68+
const inputVersion = "${{ inputs.opensearch_version }}";
69+
if (inputVersion && inputVersion.trim() !== "") {
70+
core.setOutput("version", inputVersion.trim());
71+
console.log("Using provided OpenSearch version: " + inputVersion.trim());
72+
return;
73+
}
74+
75+
// Otherwise fetch version from buildSrc/version.properties on main branch
76+
try {
77+
const response = await github.request(
78+
'GET /repos/{owner}/{repo}/contents/{path}',
79+
{
80+
owner: 'opensearch-project',
81+
repo: 'OpenSearch',
82+
path: 'buildSrc/version.properties',
83+
ref: 'main'
84+
}
85+
);
86+
87+
// Decode the file content
88+
const content = Buffer.from(response.data.content, 'base64').toString('utf-8');
89+
90+
// Extract opensearch version (e.g., "opensearch = 3.4.0")
91+
const match = content.match(/opensearch\s*=\s*([^\s]+)/);
92+
93+
if (match && match[1]) {
94+
const version = match[1].trim();
95+
core.setOutput("version", version);
96+
console.log("Fetched OpenSearch version from source: " + version);
97+
} else {
98+
console.log("Warning: Could not parse version from buildSrc/version.properties");
99+
core.setOutput("version", "unknown");
100+
}
101+
} catch (error) {
102+
console.log("Warning: Could not fetch OpenSearch version: " + error.message);
103+
core.setOutput("version", "unknown");
104+
}
105+
60106
- name: Run Proto Conversion
61-
run: npm ci && npm run preprocessing
107+
env:
108+
OPENSEARCH_VERSION: ${{ steps.get_opensearch_version.outputs.version }}
109+
run: npm ci && npm run preprocessing -- --opensearch-version "$OPENSEARCH_VERSION"
62110

63111
- name: Clone Protobuf Generator Repository
64112
run: |
@@ -105,10 +153,13 @@ jobs:
105153
token: ${{ secrets.GITHUB_TOKEN }}
106154
branch: auto-pr-branch
107155
commit-message: "Protobuf schema change detected"
108-
title: "[Automated PR]: Update generated protobuf schema (spec commit: ${{ steps.get_commit.outputs.latest_commit }})"
156+
title: "[Automated PR]: Update generated protobuf schema (OpenSearch: ${{ steps.get_opensearch_version.outputs.version }}, spec commit: ${{ steps.get_commit.outputs.latest_commit }})"
109157
signoff: true
110158
base: main
111159
delete-branch: true
112160
labels: skip-changelog
113161
body: |
114162
This pull request was automatically generated by GitHub Actions.
163+
164+
**OpenSearch Version**: ${{ steps.get_opensearch_version.outputs.version }}
165+
**API Spec Commit**: ${{ steps.get_commit.outputs.latest_commit }}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"lodash.isequal": "^4.5.0",
4949
"protobufjs": "^6.11.4",
5050
"qs": "^6.12.1",
51+
"semver": "^7.6.0",
5152
"smile-js": "^0.7.0",
5253
"titlecase": "^1.1.3",
5354
"tmp": "^0.2.4",

tools/proto-convert/src/PreProcessing.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as path from 'path';
77
import {SchemaModifier} from "./SchemaModifier";
88
import {VendorExtensionProcessor} from "./VendorExtensionProcessor";
99
import {GlobalParameterConsolidator} from "./GlobalParamWrapper";
10+
import {VersionProcessor} from "./VersionProcessor";
1011
import type {OpenAPIV3} from "openapi-types";
1112

1213
let config_filtered_path: string[] | undefined;
@@ -30,6 +31,7 @@ const command = new Command()
3031
.default(default_api_to_proto)
3132
)
3233
.addOption(new Option('--verbose', 'show merge details').default(false))
34+
.addOption(new Option('--opensearch-version <version>', 'current OpenSearch version for deprecation removal').default('3.4'))
3335
.allowExcessArguments(false)
3436
.parse();
3537

@@ -39,6 +41,7 @@ type PreprocessingOpts = {
3941
output: string;
4042
filtered_path: string[];
4143
verbose: boolean;
44+
opensearchVersion: string;
4245
};
4346

4447
const opts = command.opts() as PreprocessingOpts;
@@ -49,7 +52,8 @@ try {
4952
logger.info(`PreProcessing ${opts.filtered_path.join(', ')} into ${opts.output} ...`)
5053
const original_spec = read_yaml(opts.input)
5154
const filtered_spec = new Filter().filter_spec(original_spec, opts.filtered_path);
52-
const sanitized_spec = new Sanitizer().sanitize(filtered_spec);
55+
const version_processed_spec = new VersionProcessor(filtered_spec, logger).process(opts.opensearchVersion);
56+
const sanitized_spec = new Sanitizer().sanitize(version_processed_spec);
5357
const consolidated_spec = new GlobalParameterConsolidator(sanitized_spec).consolidate();
5458
const vendor_processed_spec = new VendorExtensionProcessor(consolidated_spec, logger).process();
5559
const schema_modified_spec = new SchemaModifier(vendor_processed_spec, logger).modify();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import _ from 'lodash';
2+
import * as semver from 'semver';
3+
import Logger from './utils/logger';
4+
import { deleteMatchingKeys } from './utils/helper';
5+
import type { OpenAPIV3 } from 'openapi-types';
6+
7+
/**
8+
* Processes version-related vendor extensions:
9+
* - x-version-added: Removes fields added after current version
10+
* - x-version-deprecated: Removes fields deprecated in current version or earlier
11+
* - x-version-removed: Removes fields removed before current version
12+
*/
13+
export class VersionProcessor {
14+
private _logger: Logger;
15+
private _spec: OpenAPIV3.Document;
16+
private _target_version: string;
17+
18+
constructor(spec: OpenAPIV3.Document, logger: Logger) {
19+
this._spec = spec;
20+
this._logger = logger;
21+
this._target_version = '';
22+
}
23+
24+
25+
process(currentVersion: string): OpenAPIV3.Document {
26+
this._target_version = currentVersion;
27+
this._logger.info(`Processing version constraints for OpenSearch ${currentVersion} ...`);
28+
deleteMatchingKeys(this._spec, (item: any) => {
29+
if (_.isObject(item) && this.#exclude_per_semver(item)) {
30+
return true;
31+
}
32+
return false;
33+
});
34+
this._logger.info('Version processing complete');
35+
return this._spec;
36+
}
37+
38+
#exclude_per_semver(obj: any): boolean {
39+
if (this._target_version == undefined) return false
40+
41+
const x_version_added = semver.coerce(obj['x-version-added'] as string)
42+
const x_version_deprecated = semver.coerce(obj['x-version-deprecated'] as string)
43+
const x_version_removed = semver.coerce(obj['x-version-removed'] as string)
44+
45+
// If field was added in a future version, exclude it
46+
if (x_version_added !== null && x_version_added !== undefined && !semver.satisfies(this._target_version, `>=${x_version_added.toString()}`)) {
47+
return true
48+
}
49+
50+
// If field was deprecated in current version or earlier, exclude it
51+
if (x_version_deprecated !== null && x_version_deprecated !== undefined && !semver.satisfies(this._target_version, `<${x_version_deprecated.toString()}`)) {
52+
return true
53+
}
54+
55+
// If field was removed in current version or earlier, exclude it
56+
if (x_version_removed !== null && x_version_removed !== undefined && !semver.satisfies(this._target_version, `<${x_version_removed.toString()}`)) {
57+
return true
58+
}
59+
60+
return false
61+
}
62+
}

0 commit comments

Comments
 (0)