Skip to content

Commit d6a7718

Browse files
CLOUDP-262013: Upload local Collection to Postman (#87)
1 parent 95f4875 commit d6a7718

File tree

4 files changed

+140
-7
lines changed

4 files changed

+140
-7
lines changed

tools/postman/Makefile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@ default: build
77
fetch_openapi:
88
./scripts/fetch.sh
99

10-
.PHONY: convert_collection
11-
convert_collection:
10+
.PHONY: convert_to_collection
11+
convert_to_collection:
1212
./scripts/convert-to-collection.sh
1313

14+
.PHONY: transform_collection
15+
transform_collection:
16+
./scripts/transform-for-api.sh
17+
18+
.PHONY: upload_collection
19+
upload_collection: transform_collection
20+
./scripts/upload-collection.sh
21+
1422
.PHONY: build
15-
build: fetch_openapi convert_collection
23+
build: fetch_openapi convert_to_collection
24+
25+
.PHONY: build_and_upload
26+
build_and_upload: build upload_collection
27+
28+
.PHONY: clean
29+
clean:
30+
rm ./openapi/*; rm ./tmp/*

tools/postman/scripts/convert-to-collection.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ set -o nounset
44
set -o pipefail
55

66
#########################################################
7-
# Fetch openapi from remote file
7+
# Convert from OpenAPI to PostmanV2 Collection
88
# Environment variables:
9-
# OPENAPI_FILE_NAME - openapi file name to use
10-
# OPENAPI_FOLDER - folder for saving openapi file
11-
# COLLECTION_FILE_NAME - postman collection file name to save to
9+
# OPENAPI_FILE_NAME - name of the openapi file
10+
# OPENAPI_FOLDER - folder where openapi file is saved
11+
# COLLECTION_FILE_NAME - name of the postman collection file
1212
# TMP_FOLDER - folder for temporary files during transformations
1313
#########################################################
1414

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
#########################################################
7+
# Upload collection to Postman
8+
# Environment variables:
9+
# OPENAPI_FOLDER - folder for saving openapi file
10+
# TMP_FOLDER - folder for temporary files during transformations
11+
# VERSIONS_FILE - name for the openapi versions file
12+
# COLLECTION_TRANSFORMED_FILE_NAME - transformed collection file name to save to
13+
# COLLECTIONS_LIST_FILE - file containing a list of collections in the Postman Workspace
14+
# POSTMAN_API_KEY - API Key for Postman API
15+
# WORKSPACE_ID - Identifier for current Postman Workspace
16+
#########################################################
17+
18+
OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"}
19+
TMP_FOLDER=${TMP_FOLDER:-"../tmp"}
20+
VERSIONS_FILE=${VERSIONS_FILE:-"versions.json"}
21+
COLLECTION_TRANSFORMED_FILE_NAME=${COLLECTION_TRANSFORMED_FILE_NAME:-"collection-transformed.json"}
22+
COLLECTIONS_LIST_FILE=${COLLECTIONS_LIST_FILE:-"collections-list.json"}
23+
24+
collection_transformed_path="${PWD}/${TMP_FOLDER}/${COLLECTION_TRANSFORMED_FILE_NAME}"
25+
26+
pushd "${OPENAPI_FOLDER}"
27+
28+
current_api_revision=$(jq -r '.versions."2.0" | .[-1]' < "./${VERSIONS_FILE}")
29+
current_collection_name="MongoDB Atlas Administration API ${current_api_revision}"
30+
31+
echo "Fetching list of current collections"
32+
curl --show-error --fail --silent -o "${COLLECTIONS_LIST_FILE}" \
33+
--location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \
34+
--header "X-API-Key: ${POSTMAN_API_KEY}"
35+
36+
collection_exists=$(jq '.collections | any(.name=="'"${current_collection_name}"'")' "${COLLECTIONS_LIST_FILE}")
37+
38+
if [ "$collection_exists" = "false" ]; then
39+
# Create new collection
40+
echo "Creating new remote collection ${current_collection_name}"
41+
curl --show-error --fail --retry 5 --retry-connrefused --silent \
42+
--location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \
43+
--header "Content-Type: application/json" \
44+
--header "X-API-Key: ${POSTMAN_API_KEY}" \
45+
--data "@${collection_transformed_path}"
46+
47+
else
48+
# Find collection ID and update collection
49+
echo "Updating remote collection ${current_collection_name}"
50+
collection_id=$(jq -r '.collections | map(select(.name=="'"${current_collection_name}"'").id).[0]' "${COLLECTIONS_LIST_FILE}")
51+
curl --show-error --fail --retry 5 --retry-connrefused --silent --request PUT \
52+
--location "https://api.getpostman.com/collections/${collection_id}" \
53+
--header "Content-Type: application/json" \
54+
--header "X-API-Key: ${POSTMAN_API_KEY}" \
55+
--data "@${collection_transformed_path}"
56+
fi
57+
58+
popd -0

0 commit comments

Comments
 (0)