From 96c18803579a9e37074246885916fe1d888ed59c Mon Sep 17 00:00:00 2001 From: Luke Sanderson <94322623+Luke-Sanderson@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:00:18 +0100 Subject: [PATCH] CLOUDP-263207: Use internal OAS instead of fetching from s3 bucket (#175) --- .github/workflows/release-postman.yml | 40 ++++------------------ tools/postman/Makefile | 1 + tools/postman/scripts/fetch-openapi.sh | 37 +++++++------------- tools/postman/scripts/transform-for-api.sh | 6 ++-- tools/postman/scripts/upload-collection.sh | 5 +-- 5 files changed, 26 insertions(+), 63 deletions(-) diff --git a/.github/workflows/release-postman.yml b/.github/workflows/release-postman.yml index 7fd14f0c0..43caad583 100644 --- a/.github/workflows/release-postman.yml +++ b/.github/workflows/release-postman.yml @@ -1,39 +1,28 @@ name: Postman Release on: - schedule: - - cron: '0 10 ? * *' + pull_request: + branches: + - main + types: + - closed workflow_dispatch: permissions: issues: write jobs: release-postman: + if: ${{ github.head_ref == 'api-bot-update' && github.event.pull_request.merged }} runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - ## Will be removed after CLOUDP-263207 - - name: Fetch Collection + - name: Fetch OpenAPI Specification working-directory: ./tools/postman run: | make fetch_openapi - - name: Compute checksum for current OpenAPI Specification - working-directory: ./tools/postman/openapi - run: | - checksum=$(sha256sum atlas-api.json | cut -d ' ' -f 1) - echo "checksum=$checksum" >> "$GITHUB_ENV" - - - name: Check if checksum is saved in the cache - id: check-cache - uses: actions/cache/restore@v4 - with: - path: ./tools/postman/openapi/openapi-checksum.txt - key: postman-openapi-${{ env.checksum }} - - name: Convert the OpenAPI Specification into Collection - if: steps.check-cache.outputs.cache-hit != 'true' id: convert working-directory: ./tools/postman run: | @@ -49,7 +38,6 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} - name: Transform Postman Collection - if: steps.check-cache.outputs.cache-hit != 'true' id: transform env: BASE_URL: ${{ vars.ATLAS_PROD_BASE_URL }} @@ -67,23 +55,9 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} - name: Upload Collection to Postman - if: steps.check-cache.outputs.cache-hit != 'true' env: POSTMAN_API_KEY: ${{ secrets.POSTMAN_API_KEY }} WORKSPACE_ID: ${{ secrets.WORKSPACE_ID }} working-directory: ./tools/postman run: | make upload_collection - - - name: Save new checksum to file - if: steps.check-cache.outputs.cache-hit != 'true' - working-directory: ./tools/postman/openapi - run: | - echo "${{ env.checksum }}" > openapi-checksum.txt - - - name: Cache new checksum file - if: steps.check-cache.outputs.cache-hit != 'true' - uses: actions/cache/save@v4 - with: - path: ./tools/postman/openapi/openapi-checksum.txt - key: postman-openapi-${{ env.checksum }} diff --git a/tools/postman/Makefile b/tools/postman/Makefile index 138f78b5b..c9635bf11 100644 --- a/tools/postman/Makefile +++ b/tools/postman/Makefile @@ -1,5 +1,6 @@ export OPENAPI_FOLDER=./openapi export TMP_FOLDER=./tmp +export FULL_OPENAPI_FOLDER=../../openapi/v2/ default: build diff --git a/tools/postman/scripts/fetch-openapi.sh b/tools/postman/scripts/fetch-openapi.sh index 0cb66aaf2..075badf44 100755 --- a/tools/postman/scripts/fetch-openapi.sh +++ b/tools/postman/scripts/fetch-openapi.sh @@ -6,35 +6,22 @@ set -euo pipefail # Environment variables: # OPENAPI_FILE_NAME - openapi file name to use # OPENAPI_FOLDER - folder for saving openapi file -# API_BASE_URL - base URL where the spec is hosted -# S3_BUCKET - S3 bucket where the spec is hosted -# VERSIONS_FILE - openapi versions file name to use +# FULL_OPENAPI_FOLDER - folder where openapi files are stored +# VERSION_FILE_NAME - name of the file where the current version is stored ######################################################### OPENAPI_FILE_NAME=${OPENAPI_FILE_NAME:-"atlas-api.json"} +FULL_OPENAPI_FOLDER=${FULL_OPENAPI_FOLDER:-"../../../openapi/v2/"} OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"} -API_BASE_URL=${API_BASE_URL:-"https://cloud.mongodb.com"} -S3_BUCKET=${S3_BUCKET:-"mongodb-mms-prod-build-server"} -VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"} +VERSION_FILE_NAME=${VERSION_FILE_NAME:-"version.txt"} -versions_url="${API_BASE_URL}/api/openapi/versions" +# Get the latest file by sorting lexicographically and store the full filename +echo "Fetching latest version of the OpenAPI Spec" +latest_openapi_filename=$(find "$FULL_OPENAPI_FOLDER"/openapi-????-??-??.json 2>/dev/null | sort -r | head -n 1) -pushd "${OPENAPI_FOLDER}" +# Extract the version from the filename +latest_version=$(basename "$latest_openapi_filename" .json | cut -d '-' -f2-) +echo "Latest version is $latest_version" +echo "$latest_version" > "$OPENAPI_FOLDER"/"$VERSION_FILE_NAME" -echo "Fetching versions from $versions_url" -curl --show-error --fail --silent -o "${VERSIONS_FILE}" \ - -H "Accept: application/json" "${versions_url}" - -## Dynamic Versioned API Version -CURRENT_API_REVISION=$(jq -r '.versions."2.0" | .[-1]' < "./${VERSIONS_FILE}") - -echo "Fetching OpenAPI release sha" -sha=$(curl --show-error --fail --silent -H "Accept: text/plain" "${API_BASE_URL}/api/private/unauth/version") - -echo "Fetching OAS file for ${sha}" -openapi_url="https://${S3_BUCKET}.s3.amazonaws.com/openapi/${sha}-v2-${CURRENT_API_REVISION}.json" - -echo "Fetching api from $openapi_url to $OPENAPI_FILE_NAME" -curl --show-error --fail --silent -o "$OPENAPI_FILE_NAME" "$openapi_url" - -popd -0 +cp "$latest_openapi_filename" "$OPENAPI_FOLDER"/"$OPENAPI_FILE_NAME" diff --git a/tools/postman/scripts/transform-for-api.sh b/tools/postman/scripts/transform-for-api.sh index 16f099ed7..3b96404e5 100755 --- a/tools/postman/scripts/transform-for-api.sh +++ b/tools/postman/scripts/transform-for-api.sh @@ -11,7 +11,7 @@ set -euo pipefail # TMP_FOLDER - folder for temporary files during transformations # TOGGLE_USE_ENVIRONMENT_AUTH - bool for if auth variables are stored at the environment or collection level # TOGGLE_INCLUDE_BODY - bool for if generated bodies should be removed or kept -# VERSIONS_FILE - name for the openapi versions file +# VERSION_FILE_NAME - name of the file where the current version is stored # DESCRIPTION_FILE - name for the markdown description file # BASE_URL - the default base url the Postman Collection will use ######################################################### @@ -21,14 +21,14 @@ COLLECTION_TRANSFORMED_FILE_NAME=${COLLECTION_TRANSFORMED_FILE_NAME:-"collection OPENAPI_FILE_NAME=${OPENAPI_FILE_NAME:-"atlas-api.json"} OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"} TMP_FOLDER=${TMP_FOLDER:-"../tmp"} +VERSION_FILE_NAME=${VERSION_FILE_NAME:-"version.txt"} -VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"} DESCRIPTION_FILE=${DESCRIPTION_FILE:-"../collection-description.md"} TOGGLE_USE_ENVIRONMENT_AUTH=${TOGGLE_USE_ENVIRONMENT_AUTH:-true} TOGGLE_INCLUDE_BODY=${TOGGLE_INCLUDE_BODY:-true} -current_api_revision=$(jq -r '.versions."2.0" | .[-1]' < "${OPENAPI_FOLDER}/${VERSIONS_FILE}") +current_api_revision=$(<"$OPENAPI_FOLDER/$VERSION_FILE_NAME") pushd "${TMP_FOLDER}" diff --git a/tools/postman/scripts/upload-collection.sh b/tools/postman/scripts/upload-collection.sh index 064860819..7ba3c4dfe 100755 --- a/tools/postman/scripts/upload-collection.sh +++ b/tools/postman/scripts/upload-collection.sh @@ -6,7 +6,7 @@ set -euo pipefail # Environment variables: # OPENAPI_FOLDER - folder for saving openapi file # TMP_FOLDER - folder for temporary files during transformations -# VERSIONS_FILE - name for the openapi versions file +# VERSION_FILE_NAME - name of the file where the current version is stored # COLLECTION_TRANSFORMED_FILE_NAME - transformed collection file name to save to # COLLECTIONS_LIST_FILE - file containing a list of collections in the Postman Workspace # POSTMAN_API_KEY - API Key for Postman API @@ -18,12 +18,13 @@ TMP_FOLDER=${TMP_FOLDER:-"../tmp"} VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"} COLLECTION_TRANSFORMED_FILE_NAME=${COLLECTION_TRANSFORMED_FILE_NAME:-"collection-transformed.json"} COLLECTIONS_LIST_FILE=${COLLECTIONS_LIST_FILE:-"collections-list.json"} +VERSION_FILE_NAME=${VERSION_FILE_NAME:-"version.txt"} +current_api_revision=$(<"$OPENAPI_FOLDER/$VERSION_FILE_NAME") collection_transformed_path="${PWD}/${TMP_FOLDER}/${COLLECTION_TRANSFORMED_FILE_NAME}" pushd "${OPENAPI_FOLDER}" -current_api_revision=$(jq -r '.versions."2.0" | .[-1]' < "./${VERSIONS_FILE}") current_collection_name="MongoDB Atlas Administration API ${current_api_revision}" echo "Fetching list of current collections"