From d45f25c36066353ccbef9e5a7d4adae80cb331eb Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 29 Aug 2024 10:18:13 +1200 Subject: [PATCH] Improve bump-version workflow Signed-off-by: Thomas Farr (cherry picked from commit 0b48175b7e376a7b0580fa7ea6e7044395916a50) --- .ci/update-version.sh | 22 +++ .github/workflows/bump-version.yml | 209 +++++++++++++++++++++++++++++ .github/workflows/test.yml | 2 +- .github/workflows/version.yml | 61 --------- Makefile.toml | 90 +------------ opensearch/src/lib.rs | 6 +- 6 files changed, 237 insertions(+), 153 deletions(-) create mode 100644 .ci/update-version.sh create mode 100644 .github/workflows/bump-version.yml delete mode 100644 .github/workflows/version.yml diff --git a/.ci/update-version.sh b/.ci/update-version.sh new file mode 100644 index 00000000..4bdbac97 --- /dev/null +++ b/.ci/update-version.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -ex + +IFS='.' read -r -a VERSION_COMPONENTS <<< "$1" +MAJOR="${VERSION_COMPONENTS[0]}" +MINOR="${VERSION_COMPONENTS[1]}" +PATCH="${VERSION_COMPONENTS[2]}" + +if [[ -z "$MAJOR" || -z "$MINOR" || -z "$PATCH" ]]; then + echo "Usage: $0 .." + exit 1 +fi + +VERSION="$MAJOR.$MINOR.$PATCH" + +CURRENT_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "opensearch") | .version') + +cargo install cargo-edit \ + && cargo set-version "${VERSION}" + +gsed -i'' -E "s/\/\/\! opensearch =( \{ version =)? \"${CURRENT_VERSION}\"/\/\/\! opensearch =\1 \"${VERSION}\"/" opensearch/src/lib.rs \ No newline at end of file diff --git a/.github/workflows/bump-version.yml b/.github/workflows/bump-version.yml new file mode 100644 index 00000000..b6bbb3d4 --- /dev/null +++ b/.github/workflows/bump-version.yml @@ -0,0 +1,209 @@ +name: Bump Version + +on: + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+' + workflow_dispatch: + inputs: + branch: + description: 'Branch to bump version on' + required: true + version: + description: 'Version to bump to' + required: true + +jobs: + bump-version-manual: + name: Bump Version (Manual) + runs-on: ubuntu-latest + if: github.event_name == 'workflow_dispatch' + permissions: + contents: write + pull-requests: write + steps: + - name: GitHub App Token + if: github.repository == 'opensearch-project/opensearch-rs' + id: app-token + uses: actions/create-github-app-token@v1 + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.inputs.branch }} + + - name: Install latest stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + + - name: Install cargo-make + uses: davidB/rust-cargo-make@v1 + + - name: Bump Version + run: cargo make update-version "$VERSION" + env: + VERSION: ${{ github.event.inputs.version }} + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.app-token.outputs.token || secrets.GITHUB_TOKEN }} + base: ${{ github.event.inputs.branch }} + branch: "feat/${{ github.event.inputs.branch }}/bump-version" + commit-message: Bump version to ${{ github.event.inputs.version }} + signoff: true + delete-branch: true + title: '[AUTO] Bump version on `${{ github.event.inputs.branch }}` to `${{ github.event.inputs.version }}`' + labels: autocut + body: | + Bumping version on `${{ github.event.inputs.branch }}` to `${{ github.event.inputs.version }}`. + + bump-version-auto: + name: Bump Version (Auto) + runs-on: ubuntu-latest + if: github.repository == 'opensearch-project/opensearch-rs' && github.event_name == 'push' + steps: + - name: GitHub App Token + id: app-token + uses: actions/create-github-app-token@v1 + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + + - name: Checkout ${{ github.ref }} + uses: actions/checkout@v4 + with: + token: ${{ steps.app-token.outputs.token }} + + - name: Install latest stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + + - name: Install cargo-make + uses: davidB/rust-cargo-make@v1 + + - name: Fetch Version Information + run: | + echo "GITHUB_REF=${GITHUB_REF}" + VERSION=$(echo "${GITHUB_REF#refs/*/v}") + VERSION_COMPONENTS=(${VERSION//./ }) + MAJOR="${VERSION_COMPONENTS[0]}" + MINOR="${VERSION_COMPONENTS[1]}" + PATCH="${VERSION_COMPONENTS[2]}" + BASE="${MAJOR}.${MINOR}" + BASE_X="${MAJOR}.x" + + IS_MAJOR_BUMP=false + IS_MINOR_BUMP=false + + if [ "${PATCH}" = "0" ]; then + IS_MINOR_BUMP=true + if [ "${MINOR}" = "0" ]; then + IS_MAJOR_BUMP=true + fi + fi + + NEXT_MAJOR="$((MAJOR + 1)).0.0" + NEXT_MINOR="${MAJOR}.$((MINOR + 1)).0" + NEXT_PATCH="${MAJOR}.${MINOR}.$((PATCH + 1))" + + { + echo "VERSION=${VERSION}" + echo "MAJOR=${MAJOR}" + echo "MINOR=${MINOR}" + echo "PATCH=${PATCH}" + echo "BASE=${BASE}" + echo "BASE_X=${BASE_X}" + echo "IS_MAJOR_BUMP=${IS_MAJOR_BUMP}" + echo "IS_MINOR_BUMP=${IS_MINOR_BUMP}" + echo "NEXT_MAJOR=${NEXT_MAJOR}" + echo "NEXT_MINOR=${NEXT_MINOR}" + echo "NEXT_PATCH=${NEXT_PATCH}" + } | tee -a "${GITHUB_ENV}" + + - name: Create ${{ env.BASE_X }} branch + if: env.IS_MAJOR_BUMP == 'true' + run: git branch ${BASE_X} && git push origin ${BASE_X} + + - name: Create ${{ env.BASE }} branch + if: env.IS_MINOR_BUMP == 'true' + run: git branch ${BASE} && git push origin ${BASE} + + - name: Checkout ${{ env.BASE }} branch + uses: actions/checkout@v4 + with: + ref: ${{ env.BASE }} + token: ${{ steps.app-token.outputs.token }} + + - name: Bump Patch Version + run: cargo make update-version "$NEXT_PATCH" + + - name: Create Patch Version Pull Request + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.app-token.outputs.token }} + base: ${{ env.BASE }} + branch: 'feat/${{ env.BASE }}/bump-version' + commit-message: Bump version to ${{ env.NEXT_PATCH }} + signoff: true + delete-branch: true + title: '[AUTO] Bump version on `${{ env.BASE }}` to `${{ env.NEXT_PATCH }}`' + labels: autocut + body: | + Bumping version on `${{ env.BASE }}` to `${{ env.NEXT_PATCH }}`. + + - name: Checkout ${{ env.BASE_X }} branch + if: env.IS_MINOR_BUMP == 'true' + uses: actions/checkout@v4 + with: + ref: ${{ env.BASE_X }} + token: ${{ steps.app-token.outputs.token }} + + - name: Bump Minor Version + if: env.IS_MINOR_BUMP == 'true' + run: cargo make update-version "$NEXT_MINOR" + + - name: Create Minor Version Pull Request + if: env.IS_MINOR_BUMP == 'true' + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.app-token.outputs.token }} + base: ${{ env.BASE_X }} + branch: 'feat/${{ env.BASE_X }}/bump-version' + commit-message: Bump version to ${{ env.NEXT_MINOR }} + signoff: true + delete-branch: true + title: '[AUTO] Bump version on `${{ env.BASE_X }}` to `${{ env.NEXT_MINOR }}`' + labels: autocut + body: | + Bumping version on `${{ env.BASE_X }}` to `${{ env.NEXT_MINOR }}`. + + - name: Checkout main branch + if: env.IS_MAJOR_BUMP == 'true' + uses: actions/checkout@v4 + with: + ref: main + token: ${{ steps.app-token.outputs.token }} + + - name: Bump Major Version + if: env.IS_MAJOR_BUMP == 'true' + run: cargo make update-version "$NEXT_MAJOR" + + - name: Create Major Version Pull Request + if: env.IS_MAJOR_BUMP == 'true' + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.app-token.outputs.token }} + base: main + branch: 'feat/main/bump-version' + commit-message: Bump version to ${{ env.NEXT_MAJOR }} + signoff: true + delete-branch: true + title: '[AUTO] Bump version on `main` to `${{ env.NEXT_MAJOR }}`' + labels: autocut + body: | + Bumping version on `main` to `${{ env.NEXT_MAJOR }}`. \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f1b706d8..826a5941 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,7 +57,7 @@ jobs: - name: Upload Coverage Data uses: codecov/codecov-action@v4 with: - files: ./test_results/opensearch.lcov + files: ./client/test_results/opensearch.lcov flags: unit use_oidc: true diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml deleted file mode 100644 index 823715f6..00000000 --- a/.github/workflows/version.yml +++ /dev/null @@ -1,61 +0,0 @@ -name: Increment Version - -on: - push: - tags: - - 'v*.*.*' - -permissions: {} -jobs: - build: - if: github.repository == 'opensearch-project/opensearch-rs' - runs-on: ubuntu-latest - steps: - - name: GitHub App token - id: github_app_token - uses: tibdex/github-app-token@v2.1.0 - with: - app_id: ${{ secrets.APP_ID }} - private_key: ${{ secrets.APP_PRIVATE_KEY }} - installation_id: 22958780 - - - uses: actions/checkout@v4 - - name: Fetch and Update Version Information - run: | - TAG=$(echo "${GITHUB_REF#refs/*/v}") - CURRENT_VERSION_ARRAY=($(echo "$TAG" | tr . '\n')) - BASE=$(IFS=. ; echo "${CURRENT_VERSION_ARRAY[*]:0:2}") - CURRENT_VERSION=$(IFS=. ; echo "${CURRENT_VERSION_ARRAY[*]:0:3}") - CURRENT_VERSION_ARRAY[2]=$((CURRENT_VERSION_ARRAY[2]+1)) - NEXT_VERSION=$(IFS=. ; echo "${CURRENT_VERSION_ARRAY[*]:0:3}") - echo "TAG=$TAG" >> $GITHUB_ENV - echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV - echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_ENV - echo "BASE=$BASE" >> $GITHUB_ENV - - - uses: actions/checkout@v4 - with: - ref: ${{ env.BASE }} - token: ${{ steps.github_app_token.outputs.token }} - - - name: Increment Patch Version - run: | - echo Incrementing $CURRENT_VERSION to $NEXT_VERSION - sed -i'' -e "s/^version = \"$CURRENT_VERSION\"/version = \"$NEXT_VERSION\"/g" opensearch/Cargo.toml - sed -i'' -e "s/^version = \"$CURRENT_VERSION\"/version = \"$NEXT_VERSION\"/g" api_generator/Cargo.toml - sed -i'' -e "s/^version = \"$CURRENT_VERSION\"/version = \"$NEXT_VERSION\"/g" yaml_test_runner/Cargo.toml - - - name: Create Pull Request - uses: peter-evans/create-pull-request@v3 - with: - token: ${{ steps.github_app_token.outputs.token }} - base: ${{ env.BASE }} - branch: 'create-pull-request/patch- ${{ env.BASE }}' - commit-message: Increment version to ${{ env.NEXT_VERSION }} - signoff: true - delete-branch: true - labels: | - autocut - title: '[AUTO] Increment version to ${{ env.NEXT_VERSION }}.' - body: | - I've noticed that a new tag ${{ env.TAG }} was pushed, and incremented the version from ${{ env.CURRENT_VERSION }} to ${{ env.NEXT_VERSION }}. \ No newline at end of file diff --git a/Makefile.toml b/Makefile.toml index c01a1dc2..591119f2 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -232,94 +232,8 @@ end [tasks.update-version] description = "Updates the package versions and version in docs" condition = { env_set = ["NEW_VERSION"] } -script_runner = "@rust" -script = ''' -//! ```cargo -//! [dependencies] -//! envmnt = "*" -//! glob = "0.3.0" -//! semver = "0.11.0" -//! toml_edit = "0.2.0" -//! ``` - -use std::fs::{File, OpenOptions}; -use std::io::{Read, Write}; -use std::path::Path; -use glob::glob; -use semver::Version; -fn main() { - let new_version = { - let v = envmnt::get_or_panic("NEW_VERSION"); - v.parse::().expect("NEW_VERSION must be a valid semantic version") - }; - let old_version = update_cargo_toml(&new_version); - update_docs(&old_version, &new_version); -} - -fn update_docs(old_version: &str, new_version: &Version) { - for entry in glob("docs/*.asciidoc").unwrap() - .chain(glob("README.md").unwrap()) - .chain(glob("opensearch/src/lib.rs").unwrap()) { - match entry { - Ok(path) => { - let mut content = read_file(&path); - content = content.replace( - &format!("opensearch = \"{}\"", old_version), - &format!("opensearch = \"{}\"", new_version.to_string())); - write_file(&path, content); - } - Err(e) => panic!("{:?}", e), - } - } -} - -fn update_cargo_toml(new_version: &Version) -> String { - let mut old_version = String::new(); - for entry in glob("**/Cargo.toml").unwrap() { - match entry { - Ok(path) => { - // skip workspace and target tomls - if path.starts_with("target") || path.to_string_lossy() == "Cargo.toml" { - continue; - } - - let content = read_file(&path); - let mut toml = content.parse::().expect("Could not parse Cargo.toml"); - let name = toml["package"]["name"].as_str().expect("toml has name"); - - // store the version from the opensearch package to target replacement in docs - if name == "opensearch" { - old_version = toml["package"]["version"] - .as_str() - .expect("toml has version") - .to_string(); - } - - toml["package"]["version"] = toml_edit::value(new_version.to_string()); - write_file(&path, toml.to_string()); - }, - Err(e) => panic!("{:?}", e), - } - } - old_version -} - -fn read_file>(path: P) -> String { - let mut file = File::open(path).unwrap(); - let mut raw_data = String::new(); - file.read_to_string(&mut raw_data).unwrap(); - raw_data -} - -fn write_file>(path: P, content: String) { - let mut file = OpenOptions::new() - .write(true) - .truncate(true) - .open(path) - .unwrap(); - file.write_all(content.as_bytes()).unwrap(); -} -''' +command = "bash" +args = ["./.ci/update-version.sh", "@@split(CARGO_MAKE_TASK_ARGS,;)"] [tasks.default] clear = true diff --git a/opensearch/src/lib.rs b/opensearch/src/lib.rs index 226f6324..4eb047ee 100644 --- a/opensearch/src/lib.rs +++ b/opensearch/src/lib.rs @@ -74,7 +74,7 @@ //! //! ```toml,no_run //! [dependencies] -//! opensearch = "1.0.0" +//! opensearch = "3.0.0" //! ``` //! The following _optional_ dependencies may also be useful to create requests and read responses //! @@ -341,8 +341,8 @@ //! //! ```toml,no_run //! [dependencies] -//! opensearch = { version = "1", features = ["aws-auth"] } -//! aws-config = "0.10" +//! opensearch = { version = "3.0.0", features = ["aws-auth"] } +//! aws-config = "1" //! ``` //! //! ```rust,no_run