|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -o errexit |
| 3 | +set -o nounset |
| 4 | +set -o pipefail |
| 5 | + |
| 6 | +######################################################### |
| 7 | +# Prepare collection for Postman API |
| 8 | +# Environment variables: |
| 9 | +# COLLECTION_FILE_NAME - name of the postman collection file |
| 10 | +# COLLECTION_TRANSFORMED_FILE_NAME - name of the transformed collection file |
| 11 | +# OPENAPI_FOLDER - folder where openapi file is saved |
| 12 | +# TMP_FOLDER - folder for temporary files during transformations |
| 13 | +# USE_ENVIRONMENT_AUTH - bool for if auth variables are stored at the environment or collection level |
| 14 | +# VERSIONS_FILE - name for the openapi versions file |
| 15 | +# BASE_URL - the default base url the Postman Collection will use |
| 16 | +######################################################### |
| 17 | + |
| 18 | +COLLECTION_FILE_NAME=${COLLECTION_FILE_NAME:-"collection.json"} |
| 19 | +COLLECTION_TRANSFORMED_FILE_NAME=${COLLECTION_TRANSFORMED_FILE_NAME:-"collection-transformed.json"} |
| 20 | +OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"} |
| 21 | +TMP_FOLDER=${TMP_FOLDER:-"../tmp"} |
| 22 | +USE_ENVIRONMENT_AUTH=${USE_ENVIRONMENT_AUTH:-true} |
| 23 | +VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"} |
| 24 | + |
| 25 | +current_api_revision=$(jq -r '.versions."2.0" | .[-1]' < "${OPENAPI_FOLDER}/${VERSIONS_FILE}") |
| 26 | + |
| 27 | +pushd "${TMP_FOLDER}" |
| 28 | + |
| 29 | +echo "Wrapping Collection in \"collection\" tag" |
| 30 | +jq '{"collection": .}' "$COLLECTION_FILE_NAME" > intermediateCollectionWrapped.json |
| 31 | + |
| 32 | +echo "Disabling query params by default" |
| 33 | +jq '(.. | select(.request? != null).request.url.query.[].disabled) = true ' intermediateCollectionWrapped.json > intermediateCollectionDisableQueryParam.json |
| 34 | + |
| 35 | +# This is to be removed because it is autogenerated when a new collection is created |
| 36 | +echo "Removing _postman_id" |
| 37 | +jq 'del(.collection.info._postman_id)' intermediateCollectionDisableQueryParam.json > intermediateCollectionNoPostmanID.json |
| 38 | + |
| 39 | +echo "Updating name with version" |
| 40 | +jq '.collection.info.name = "MongoDB Atlas Administration API '"${current_api_revision}"'"' intermediateCollectionNoPostmanID.json > intermediateCollectionWithName.json |
| 41 | + |
| 42 | +echo "Updating baseUrl" |
| 43 | +jq '.collection.variable.[0].value = "'"${BASE_URL}"'"' intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json |
| 44 | + |
| 45 | +if [ "$USE_ENVIRONMENT_AUTH" = "false" ]; then |
| 46 | + echo "Adding auth variables" |
| 47 | + jq '.collection.variable += [{"key": "digestAuthUsername", "value": "<string>"}, |
| 48 | + {"key": "digestAuthPassword", "value": "<string>"}, |
| 49 | + {"key": "realm", "value": "<string>"}]' intermediateCollectionWithBaseURL.json > "$COLLECTION_TRANSFORMED_FILE_NAME" |
| 50 | +else |
| 51 | + cp intermediateCollectionWithBaseURL.json "$COLLECTION_TRANSFORMED_FILE_NAME" |
| 52 | +fi |
| 53 | + |
| 54 | +rm intermediateCollectionWrapped.json \ |
| 55 | + intermediateCollectionDisableQueryParam.json \ |
| 56 | + intermediateCollectionNoPostmanID.json \ |
| 57 | + intermediateCollectionWithName.json \ |
| 58 | + intermediateCollectionWithBaseURL.json |
| 59 | + |
| 60 | +popd -0 |
0 commit comments