diff --git a/tools/postman/Makefile b/tools/postman/Makefile index 9118f19e6b..877d436c1d 100644 --- a/tools/postman/Makefile +++ b/tools/postman/Makefile @@ -7,9 +7,24 @@ default: build fetch_openapi: ./scripts/fetch.sh -.PHONY: convert_collection -convert_collection: +.PHONY: convert_to_collection +convert_to_collection: ./scripts/convert-to-collection.sh +.PHONY: transform_collection +transform_collection: + ./scripts/transform-for-api.sh + +.PHONY: upload_collection +upload_collection: transform_collection + ./scripts/upload-collection.sh + .PHONY: build -build: fetch_openapi convert_collection +build: fetch_openapi convert_to_collection + +.PHONY: build_and_upload +build_and_upload: build upload_collection + +.PHONY: clean +clean: + rm ./openapi/*; rm ./tmp/* diff --git a/tools/postman/scripts/convert-to-collection.sh b/tools/postman/scripts/convert-to-collection.sh index 19a65749fa..30c141c475 100755 --- a/tools/postman/scripts/convert-to-collection.sh +++ b/tools/postman/scripts/convert-to-collection.sh @@ -4,11 +4,11 @@ set -o nounset set -o pipefail ######################################################### -# Fetch openapi from remote file +# Convert from OpenAPI to PostmanV2 Collection # Environment variables: -# OPENAPI_FILE_NAME - openapi file name to use -# OPENAPI_FOLDER - folder for saving openapi file -# COLLECTION_FILE_NAME - postman collection file name to save to +# OPENAPI_FILE_NAME - name of the openapi file +# OPENAPI_FOLDER - folder where openapi file is saved +# COLLECTION_FILE_NAME - name of the postman collection file # TMP_FOLDER - folder for temporary files during transformations ######################################################### diff --git a/tools/postman/scripts/transform-for-api.sh b/tools/postman/scripts/transform-for-api.sh new file mode 100755 index 0000000000..7aced040d4 --- /dev/null +++ b/tools/postman/scripts/transform-for-api.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail + +######################################################### +# Prepare collection for Postman API +# Environment variables: +# COLLECTION_FILE_NAME - name of the postman collection file +# COLLECTION_TRANSFORMED_FILE_NAME - name of the transformed collection file +# OPENAPI_FOLDER - folder where openapi file is saved +# TMP_FOLDER - folder for temporary files during transformations +# USE_ENVIRONMENT_AUTH - bool for if auth variables are stored at the environment or collection level +# VERSIONS_FILE - name for the openapi versions file +# BASE_URL - the default base url the Postman Collection will use +######################################################### + +COLLECTION_FILE_NAME=${COLLECTION_FILE_NAME:-"collection.json"} +COLLECTION_TRANSFORMED_FILE_NAME=${COLLECTION_TRANSFORMED_FILE_NAME:-"collection-transformed.json"} +OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"} +TMP_FOLDER=${TMP_FOLDER:-"../tmp"} +USE_ENVIRONMENT_AUTH=${USE_ENVIRONMENT_AUTH:-true} +VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"} + +current_api_revision=$(jq -r '.versions."2.0" | .[-1]' < "${OPENAPI_FOLDER}/${VERSIONS_FILE}") + +pushd "${TMP_FOLDER}" + +echo "Wrapping Collection in \"collection\" tag" +jq '{"collection": .}' "$COLLECTION_FILE_NAME" > intermediateCollectionWrapped.json + +echo "Disabling query params by default" +jq '(.. | select(.request? != null).request.url.query.[].disabled) = true ' intermediateCollectionWrapped.json > intermediateCollectionDisableQueryParam.json + +# This is to be removed because it is autogenerated when a new collection is created +echo "Removing _postman_id" +jq 'del(.collection.info._postman_id)' intermediateCollectionDisableQueryParam.json > intermediateCollectionNoPostmanID.json + +echo "Updating name with version" +jq '.collection.info.name = "MongoDB Atlas Administration API '"${current_api_revision}"'"' intermediateCollectionNoPostmanID.json > intermediateCollectionWithName.json + +echo "Updating baseUrl" +jq '.collection.variable.[0].value = "'"${BASE_URL}"'"' intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json + +if [ "$USE_ENVIRONMENT_AUTH" = "false" ]; then + echo "Adding auth variables" + jq '.collection.variable += [{"key": "digestAuthUsername", "value": ""}, + {"key": "digestAuthPassword", "value": ""}, + {"key": "realm", "value": ""}]' intermediateCollectionWithBaseURL.json > "$COLLECTION_TRANSFORMED_FILE_NAME" +else + cp intermediateCollectionWithBaseURL.json "$COLLECTION_TRANSFORMED_FILE_NAME" +fi + +rm intermediateCollectionWrapped.json \ + intermediateCollectionDisableQueryParam.json \ + intermediateCollectionNoPostmanID.json \ + intermediateCollectionWithName.json \ + intermediateCollectionWithBaseURL.json + +popd -0 diff --git a/tools/postman/scripts/upload-collection.sh b/tools/postman/scripts/upload-collection.sh new file mode 100755 index 0000000000..37892fde0c --- /dev/null +++ b/tools/postman/scripts/upload-collection.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail + +######################################################### +# Upload collection to Postman +# 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 +# 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 +# WORKSPACE_ID - Identifier for current Postman Workspace +######################################################### + +OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"} +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"} + +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" +curl --show-error --fail --silent -o "${COLLECTIONS_LIST_FILE}" \ + --location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \ + --header "X-API-Key: ${POSTMAN_API_KEY}" + +collection_exists=$(jq '.collections | any(.name=="'"${current_collection_name}"'")' "${COLLECTIONS_LIST_FILE}") + +if [ "$collection_exists" = "false" ]; then + # Create new collection + echo "Creating new remote collection ${current_collection_name}" + curl --show-error --fail --retry 5 --retry-connrefused --silent \ + --location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \ + --header "Content-Type: application/json" \ + --header "X-API-Key: ${POSTMAN_API_KEY}" \ + --data "@${collection_transformed_path}" + +else + # Find collection ID and update collection + echo "Updating remote collection ${current_collection_name}" + collection_id=$(jq -r '.collections | map(select(.name=="'"${current_collection_name}"'").id).[0]' "${COLLECTIONS_LIST_FILE}") + curl --show-error --fail --retry 5 --retry-connrefused --silent --request PUT \ + --location "https://api.getpostman.com/collections/${collection_id}" \ + --header "Content-Type: application/json" \ + --header "X-API-Key: ${POSTMAN_API_KEY}" \ + --data "@${collection_transformed_path}" +fi + +popd -0