Skip to content

Commit

Permalink
CLOUDP-263846: Fetch the current number of forks for each collection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke-Sanderson authored Jul 30, 2024
1 parent cafad01 commit adb7083
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
6 changes: 5 additions & 1 deletion tools/postman/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ default: build

.PHONY: fetch_openapi
fetch_openapi:
./scripts/fetch.sh
./scripts/fetch-openapi.sh

.PHONY: fetch_forks
fetch_forks:
./scripts/fetch-forks.sh

.PHONY: convert_to_collection
convert_to_collection:
Expand Down
56 changes: 56 additions & 0 deletions tools/postman/scripts/fetch-forks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

#########################################################
# Fetch number of forks of each collection
# Environment variables:
# OPENAPI_FOLDER - folder for saving openapi file
# COLLECTIONS_LIST_FILE - file containing a list of collections in the Postman Workspace
# FORKS_DATA_FILE - file containing a list fork metrics for each collection
# POSTMAN_API_KEY - API Key for Postman API
# WORKSPACE_ID - Identifier for current Postman Workspace
#########################################################

OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"}
COLLECTIONS_LIST_FILE=${COLLECTIONS_LIST_FILE:-"collections-list.json"}
FORKS_DATA_FILE=${FORKS_DATA_FILE:-"fork-data.json"}

pushd "${OPENAPI_FOLDER}"

echo "Fetching list of current collections"
curl --show-error --retry 5 --fail --silent -o "${COLLECTIONS_LIST_FILE}" \
--location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \
--header "X-API-Key: ${POSTMAN_API_KEY}"

collection_ids=$(jq -r '.collections[].id' "$COLLECTIONS_LIST_FILE")

echo '{"collections": []}' > "$FORKS_DATA_FILE"

for collection_id in $collection_ids; do
collection_name=$(jq -r --arg id "$collection_id" '.collections[] | select(.id==$id).name' "$COLLECTIONS_LIST_FILE")

echo "Fetching fork data for collection: $collection_name"
response=$(curl --silent --retry 5 -w "%{http_code}" -o "current-collection.json" \
--location "https://api.getpostman.com/collections/${collection_id}/forks" \
--header "X-API-Key: ${POSTMAN_API_KEY}")

http_code=${response: -3}

if [ "$http_code" = "200" ]; then
forks=$(jq '.meta.total' current-collection.json)
else
forks=0
fi

# Add the ID, Name, Forks for current collection to JSON file
fork_data=$(jq -n --arg id "$collection_id" --arg name "$collection_name" --arg forks "$forks" \
'{id: $id, name: $name, forks: $forks}')

jq --argjson data "$fork_data" '.collections += [$data]' "$FORKS_DATA_FILE" > temp-"$FORKS_DATA_FILE"
mv temp-"$FORKS_DATA_FILE" "$FORKS_DATA_FILE"

done

popd -0
File renamed without changes.
14 changes: 10 additions & 4 deletions tools/postman/scripts/transform-for-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ 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
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
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
jq --arg api_version "$current_api_revision" \
'.collection.info.name = ("MongoDB Atlas Administration API " + $api_version)' \
intermediateCollectionNoPostmanID.json > intermediateCollectionWithName.json

echo "Updating baseUrl"
jq '.collection.variable[0].value = "'"${BASE_URL}"'"' intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json
jq --arg base_url "$BASE_URL" \
'.collection.variable[0].value = $base_url' \
intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json

if [ "$USE_ENVIRONMENT_AUTH" = "false" ]; then
echo "Adding auth variables"
Expand Down

0 comments on commit adb7083

Please sign in to comment.