Skip to content

Commit adb7083

Browse files
CLOUDP-263846: Fetch the current number of forks for each collection (#116)
1 parent cafad01 commit adb7083

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

tools/postman/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ default: build
55

66
.PHONY: fetch_openapi
77
fetch_openapi:
8-
./scripts/fetch.sh
8+
./scripts/fetch-openapi.sh
9+
10+
.PHONY: fetch_forks
11+
fetch_forks:
12+
./scripts/fetch-forks.sh
913

1014
.PHONY: convert_to_collection
1115
convert_to_collection:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
#########################################################
7+
# Fetch number of forks of each collection
8+
# Environment variables:
9+
# OPENAPI_FOLDER - folder for saving openapi file
10+
# COLLECTIONS_LIST_FILE - file containing a list of collections in the Postman Workspace
11+
# FORKS_DATA_FILE - file containing a list fork metrics for each collection
12+
# POSTMAN_API_KEY - API Key for Postman API
13+
# WORKSPACE_ID - Identifier for current Postman Workspace
14+
#########################################################
15+
16+
OPENAPI_FOLDER=${OPENAPI_FOLDER:-"../openapi"}
17+
COLLECTIONS_LIST_FILE=${COLLECTIONS_LIST_FILE:-"collections-list.json"}
18+
FORKS_DATA_FILE=${FORKS_DATA_FILE:-"fork-data.json"}
19+
20+
pushd "${OPENAPI_FOLDER}"
21+
22+
echo "Fetching list of current collections"
23+
curl --show-error --retry 5 --fail --silent -o "${COLLECTIONS_LIST_FILE}" \
24+
--location "https://api.getpostman.com/collections?workspace=${WORKSPACE_ID}" \
25+
--header "X-API-Key: ${POSTMAN_API_KEY}"
26+
27+
collection_ids=$(jq -r '.collections[].id' "$COLLECTIONS_LIST_FILE")
28+
29+
echo '{"collections": []}' > "$FORKS_DATA_FILE"
30+
31+
for collection_id in $collection_ids; do
32+
collection_name=$(jq -r --arg id "$collection_id" '.collections[] | select(.id==$id).name' "$COLLECTIONS_LIST_FILE")
33+
34+
echo "Fetching fork data for collection: $collection_name"
35+
response=$(curl --silent --retry 5 -w "%{http_code}" -o "current-collection.json" \
36+
--location "https://api.getpostman.com/collections/${collection_id}/forks" \
37+
--header "X-API-Key: ${POSTMAN_API_KEY}")
38+
39+
http_code=${response: -3}
40+
41+
if [ "$http_code" = "200" ]; then
42+
forks=$(jq '.meta.total' current-collection.json)
43+
else
44+
forks=0
45+
fi
46+
47+
# Add the ID, Name, Forks for current collection to JSON file
48+
fork_data=$(jq -n --arg id "$collection_id" --arg name "$collection_name" --arg forks "$forks" \
49+
'{id: $id, name: $name, forks: $forks}')
50+
51+
jq --argjson data "$fork_data" '.collections += [$data]' "$FORKS_DATA_FILE" > temp-"$FORKS_DATA_FILE"
52+
mv temp-"$FORKS_DATA_FILE" "$FORKS_DATA_FILE"
53+
54+
done
55+
56+
popd -0
File renamed without changes.

tools/postman/scripts/transform-for-api.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ echo "Wrapping Collection in \"collection\" tag"
3030
jq '{"collection": .}' "$COLLECTION_FILE_NAME" > intermediateCollectionWrapped.json
3131

3232
echo "Disabling query params by default"
33-
jq '(.. | select(.request? != null).request.url.query[].disabled) = true ' intermediateCollectionWrapped.json > intermediateCollectionDisableQueryParam.json
33+
jq '(.. | select(.request? != null).request.url.query[].disabled) = true ' \
34+
intermediateCollectionWrapped.json > intermediateCollectionDisableQueryParam.json
3435

3536
# This is to be removed because it is autogenerated when a new collection is created
3637
echo "Removing _postman_id"
37-
jq 'del(.collection.info._postman_id)' intermediateCollectionDisableQueryParam.json > intermediateCollectionNoPostmanID.json
38+
jq 'del(.collection.info._postman_id)' \
39+
intermediateCollectionDisableQueryParam.json > intermediateCollectionNoPostmanID.json
3840

3941
echo "Updating name with version"
40-
jq '.collection.info.name = "MongoDB Atlas Administration API '"${current_api_revision}"'"' intermediateCollectionNoPostmanID.json > intermediateCollectionWithName.json
42+
jq --arg api_version "$current_api_revision" \
43+
'.collection.info.name = ("MongoDB Atlas Administration API " + $api_version)' \
44+
intermediateCollectionNoPostmanID.json > intermediateCollectionWithName.json
4145

4246
echo "Updating baseUrl"
43-
jq '.collection.variable[0].value = "'"${BASE_URL}"'"' intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json
47+
jq --arg base_url "$BASE_URL" \
48+
'.collection.variable[0].value = $base_url' \
49+
intermediateCollectionWithName.json > intermediateCollectionWithBaseURL.json
4450

4551
if [ "$USE_ENVIRONMENT_AUTH" = "false" ]; then
4652
echo "Adding auth variables"

0 commit comments

Comments
 (0)