Skip to content

Commit

Permalink
CLOUDP-263207: Use internal OAS instead of fetching from s3 bucket (#175
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Luke-Sanderson authored Aug 20, 2024
1 parent 3c44fb3 commit 96c1880
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 63 deletions.
40 changes: 7 additions & 33 deletions .github/workflows/release-postman.yml
Original file line number Diff line number Diff line change
@@ -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: |
Expand All @@ -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 }}
Expand All @@ -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 }}
1 change: 1 addition & 0 deletions tools/postman/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export OPENAPI_FOLDER=./openapi
export TMP_FOLDER=./tmp
export FULL_OPENAPI_FOLDER=../../openapi/v2/

default: build

Expand Down
37 changes: 12 additions & 25 deletions tools/postman/scripts/fetch-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions tools/postman/scripts/transform-for-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#########################################################
Expand All @@ -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}"

Expand Down
5 changes: 3 additions & 2 deletions tools/postman/scripts/upload-collection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 96c1880

Please sign in to comment.