From 2fe06033f11e70ee2a0d034882d2f901be329443 Mon Sep 17 00:00:00 2001 From: Ian Yenien Serrano <63758389+yenienserrano@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:44:33 -0300 Subject: [PATCH] Implement new versioning methodology (#5060) * change code stage * change revision number * add commit property * add test * add script tags * Update tag.py script to support multiple platforms * Add prebuild task to Makefile Co-authored-by: Alex Ruiz Becerra (cherry picked from commit 128a48783fa10c1f5174e23ce9eea8e0fc5954ff) --- Makefile | 11 +++++ package.json | 5 +- public/package.test.ts | 50 +++++++++++++++++++ scripts/tag.py | 108 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 public/package.test.ts create mode 100644 scripts/tag.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..4b356de816 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +# Generate tags for a release automatically. +# Update the tag.py file before running this script. + +prebuild: + @echo "- Updating project's versions ..." + @node scripts/generate-build-version + +tags: prebuild + @echo "- Generating Git tags ..." + @python3 scripts/tag.py + diff --git a/package.json b/package.json index 0ed7633555..6210a1ec18 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "wazuh", "version": "4.4.0", - "revision": "4400", - "code": "4400", + "revision": "00", + "stage": "alpha", + "commit": "f4f8345", "pluginPlatform": { "version": "7.10.2" }, diff --git a/public/package.test.ts b/public/package.test.ts new file mode 100644 index 0000000000..0ee26aa170 --- /dev/null +++ b/public/package.test.ts @@ -0,0 +1,50 @@ +import packageValue from '../package.json'; + +const isNaN = (currentValue: string) => Number.isNaN(Number(currentValue)); + +describe('package.json version', () => { + it('should have a version', () => { + expect(packageValue.version).toBeDefined(); + }); + it("Wazuh's revision should be to follow the major.minor.patch.pattern.", () => { + const versionSplit = packageValue.version.split('.'); + + const versionIsNaN = versionSplit.every(isNaN); + + expect(versionSplit.length).toBe(3); + expect(versionIsNaN).toBeFalsy(); + }); +}); + +describe('package.json revison', () => { + it('should have a revison', () => { + expect(packageValue.revision).toBeDefined(); + }); + it('the revision should be a 2 digit number.', () => { + const revision = packageValue.revision; + + const revisionIsNaN = isNaN(revision); + + expect(revision.length).toBe(2); + expect(revisionIsNaN).toBeFalsy(); + }); +}); + +describe('package.json stage', () => { + it('should have a stage', () => { + expect(packageValue.stage).toBeDefined(); + }); + it('the state should be one of the defined.', () => { + const stateDefined = [ + 'pre-alpha', + 'alpha', + 'beta', + 'release-candidate', + 'stable', + ]; + + const stage = packageValue.stage; + + expect(stateDefined).toContain(stage); + }); +}); diff --git a/scripts/tag.py b/scripts/tag.py new file mode 100644 index 0000000000..b6c0054772 --- /dev/null +++ b/scripts/tag.py @@ -0,0 +1,108 @@ +import json +import logging +import os +import subprocess + +# ==================== CONFIGURATION ==================== # +# Values to modify: +# - version +# - revision +# - stage +# - supported_versions & kbn_versions ONLY IF NEEDED (e.g. new Kibana version) +# ======================================================= # + +# Wazuh version: major.minor.patch +version = '4.4.0' +# App's revision number (previous rev + 1) +revision = '00' +# One of 'pre-alpha', 'alpha', 'beta', 'release-candidate', 'stable' +stage = 'alpha' + +# Global variable. Will be set later +branch = None +minor = ".".join(version.split('.')[:2]) + +# Supported versions of Kibana +kbn_versions = [ + [f'7.16.{x}' for x in range(0, 4)], + [f'7.17.{x}' for x in range(0, 9)] +] + +# Platforms versions +supported_versions = { + 'OpenDistro': { + 'branch': f'{minor}-7.10', + 'versions': ['7.10.2'] + }, + 'Kibana': { + 'branch': f'{minor}-7.16', + # Flatten 2D list kbn_versions using lists comprehension + 'versions': [item for sublist in kbn_versions for item in sublist] + }, + 'Wazuh Dashboard': { + 'branch': f'{minor}-2.4-wzd', + 'versions': ['2.4.1'] + } +} + + +def get_git_revision_short_hash() -> str: + return subprocess.check_output(['git', 'rev-parse', '--short', branch]).decode('ascii').strip() + + +def update_package_json() -> tuple: + logging.info(f'Updating package.json') + data, success = {}, True + + # Read JSON and update keys. + with open('package.json', 'r') as f: + data, success = json.load(f), False + + # Update file + data['commit'] = get_git_revision_short_hash() + data['version'] = version + data['revision'] = revision + data['stage'] = stage + + with open('package.json', 'w') as f: + json.dump(data, f, indent=2) + + return data, success + + +def setup(): + logging.info( + f'Switching to branch "{branch}" and removing outdated tags...') + os.system(f'git checkout {branch}') + os.system('git fetch --prune --prune-tags') + + +def main(platform: str, versions: list): + for v in versions: + tag = f'v{version}-{v}-{stage}' + logging.info(f'Generating tag "{tag}"') + update_package_json() + os.system(f'git commit -am "Bump {tag}"') + os.system( + f'git tag -a {tag} -m "Wazuh {version} for {platform} {v}"') + logging.info(f'Pushing tag "{tag}" to remote.') + os.system(f'git push origin {tag}') + # Undo latest commit + os.system(f'git reset --hard origin/{branch}') + # Save created tags to file + os.system(f'git tag | grep -P -i "^v{version}-.*-{stage}" > tags.txt') + + +if __name__ == '__main__': + logging.basicConfig( + filename='output.log', + level=logging.INFO, + format='%(asctime)s %(message)s' + ) + logging.info( + f'Wazuh version is "{version}". App revision is "{revision}". Stage is "{stage}"') + + for platform_name, platform_data in supported_versions.items(): + branch, versions = platform_data['branch'], platform_data['versions'] + setup() + main(platform_name, versions)