diff --git a/.github/workflows/docs-as-code.yaml b/.github/workflows/docs-as-code.yaml index 0f8fe74..ad3ab3e 100644 --- a/.github/workflows/docs-as-code.yaml +++ b/.github/workflows/docs-as-code.yaml @@ -7,14 +7,13 @@ on: workflow_dispatch: inputs: log_level: - description: 'Log level: 0=DEBUG, 1=INFO, 2=WARNING, 3=ERROR' + description: 'Log level: 1=DEBUG, 2=INFO, 3=WARNING, 4=ERROR' required: false - default: '1' # Set the default log level to INFO + default: '2' # Set the default log level to INFO env: - PORTAL_STAGING_SUBDOMAIN: ${{ secrets.SWAGGERHUB_PORTAL_STAGING_SUBDOMAIN }} - PORTAL_SUBDOMAIN: ${{ secrets.SWAGGERHUB_PORTAL_SUBDOMAIN }} - SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }} + SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }} + LOG_LEVEL: ${{ github.event.inputs.log_level }} jobs: publish: @@ -24,26 +23,29 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 - - name: Set up Node.js - uses: actions/setup-node@v2 - with: - node-version: 14 - - - name: Install dependencies - run: npm install - - name: Iterate over product folders + shell: bash run: | for product in ./products/*; do + echo "Product: $product" if [[ -d "$product" ]]; then - manifest="./$product/manifest.json" + echo "Product is a directory" + product_name=${product#./products/} + echo "Product name: $product_name" + manifest="./products/$product_name/manifest.json" + echo "Manifest: $manifest" if [[ -f "$manifest" ]]; then - ./scripts/publish-portal-content.sh portal_product_upsert "$manifest" "$product" - ./scripts/publish-portal-content.sh load_and_process_product_manifest_content_metadata "$manifest" "$product" + echo "Manifest is a file" + . ./scripts/publish-portal-content.sh && portal_product_upsert "$manifest" "$product_name" + . ./scripts/publish-portal-content.sh && load_and_process_product_manifest_content_metadata "$manifest" "$product_name" + else + echo "Manifest is not a file" fi + else + echo "Product is not a directory" fi done env: - LOG_LEVEL: ${{ env.LOG_LEVEL }} + SWAGGERHUB_PORTAL_SUBDOMAIN: ${{ env.SWAGGERHUB_PORTAL_SUBDOMAIN }} \ No newline at end of file diff --git a/products/SwaggerHub Portal APIs/images/SwaggerHub Icon Color.png b/products/SwaggerHub Portal APIs/images/SwaggerHub.png similarity index 100% rename from products/SwaggerHub Portal APIs/images/SwaggerHub Icon Color.png rename to products/SwaggerHub Portal APIs/images/SwaggerHub.png diff --git a/products/SwaggerHub Portal APIs/manifest.json b/products/SwaggerHub Portal APIs/manifest.json index aa39ed9..cde30cc 100644 --- a/products/SwaggerHub Portal APIs/manifest.json +++ b/products/SwaggerHub Portal APIs/manifest.json @@ -4,7 +4,7 @@ "slug": "swaggerhub-portal", "public": true, "hidden": false, - "logo": "images/SwaggerHub Icon Color.png", + "logo": "images/SwaggerHub.png", "logoDark": "", "autoPublish": true }, diff --git a/scripts/publish-portal-content.sh b/scripts/publish-portal-content.sh old mode 100644 new mode 100755 index 8a7398d..1623dc8 --- a/scripts/publish-portal-content.sh +++ b/scripts/publish-portal-content.sh @@ -1,5 +1,6 @@ #!/bin/bash +# Author: @frankkilcommins # This script is part of a workflow that publishes content to the SwaggerHub Portal instance. # SwaggerHub Portal API Ref: https://app.swaggerhub.com/apis-docs/smartbear-public/swaggerhub-portal-api/0.2.0-beta @@ -9,37 +10,25 @@ # - SWAGGERHUB_API_KEY: SwaggerHub API Key # - SWAGGERHUB_PORTAL_SUBDOMAIN: SwaggerHub Portal subdomain +#trap 'echo "Error on line $LINENO"' ERR + # Log levels -DEBUG=0 -INFO=1 -WARNING=2 -ERROR=3 +DEBUG=1 +INFO=2 +WARNING=3 +ERROR=4 # Default log level -LOG_LEVEL=${LOG_LEVEL:-$INFO} - +LOG_LEVEL=${LOG_LEVEL:=$INFO} PORTAL_SUBDOMAIN="${SWAGGERHUB_PORTAL_SUBDOMAIN}" SWAGGERHUB_API_KEY="${SWAGGERHUB_API_KEY}" PORTAL_URL="https://api.portal.swaggerhub.com/v1" -log_message $INFO "Fetching portal information..." -portalsResponse=$(curl -s --request GET \ - --url "$PORTAL_URL/portals?subdomain=$PORTAL_SUBDOMAIN" \ - --header "Authorization: Bearer $SWAGGERHUB_API_KEY" \ - --header "Content-Type: application/json") - -portal_id=$(echo "$portalsResponse" | jq -r '.items[0].id') -log_message $INFO "Portal ID: $portal_id" - declare -g section_id declare -g product_id declare -g document_id -# ToDo - replace with variable set from GitHub Actions -#portal_product_upsert "../products/Adopt a Pet/manifest.json" "Adopt a Pet" -#load_and_process_product_manifest_content_metadata "../products/SwaggerHub Portal APIs/manifest.json" "SwaggerHub Portal APIs" - ## HELPER FUNCTIONS log_message() { local log_level=$1 @@ -48,13 +37,13 @@ log_message() { case $log_level in $DEBUG) - [ $LOG_LEVEL -le $DEBUG ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [DEBUG] $message" ;; + ([ $LOG_LEVEL -le $DEBUG ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [DEBUG] $message") || true ;; $INFO) - [ $LOG_LEVEL -le $INFO ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $message" ;; + ([ $LOG_LEVEL -le $INFO ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $message") || true ;; $WARNING) - [ $LOG_LEVEL -le $WARNING ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [WARNING] $message" ;; + ([ $LOG_LEVEL -le $WARNING ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [WARNING] $message") || true ;; $ERROR) - [ $LOG_LEVEL -le $ERROR ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $message" >&2 ;; + ([ $LOG_LEVEL -le $ERROR ] && echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $message" >&2) || true ;; *) echo "$(date '+%Y-%m-%d %H:%M:%S') [UNKNOWN] $message" ;; esac @@ -105,24 +94,30 @@ function portal_branding_image_post() { local portal_id=$1 local image_path=$2 local image_name=$3 - local encoded_param_value=$(url_encode "$image_name") - log_message $INFO "Uploading branding image for portal $portal_id from ../products/$image_name/$image_path" + # let's parse the image path to get the image name (everything after the last /) + image_shortname=$(basename "$image_path") + + local encoded_param_value=$(url_encode "$image_shortname") + + log_message $INFO "Uploading branding image for portal $portal_id from /products/$image_name/$image_path" + log_message $DEBUG "See if image $image_shortname already exists" # get existing branding attachments portal_branding_attachments_get "$portal_id" # Check if the image is already uploaded - local image_already_uploaded=$(echo "$existing_branding_attachments" | jq -r ".[] | select(.name == \"$image_name\") | .id") + local image_already_uploaded=$(echo "$existing_branding_attachments" | jq -r "first(.[] | select(.name == \"$image_shortname\")) | .id") if [ -n "$image_already_uploaded" ]; then log_message $INFO "Image already uploaded: $image_already_uploaded" + branding_image_id=$image_already_uploaded log_message $DEBUG "Exit portal_product_branding_image_post" return fi # get the Content-Type of the image from the image path if the file exists - local full_path="../products/$image_name/$image_path" + local full_path="./products/$image_name/$image_path" if [ -f "$full_path" ]; then local content_type=$(file --mime-type -b "$full_path") @@ -167,10 +162,10 @@ function portal_product_doc_image_post() { return fi - log_message $INFO "Uploading image for product $product_id from ../products/$product_name/images/embedded/$image_filename" + log_message $INFO "Uploading image for product $product_id from /products/$product_name/images/embedded/$image_filename" # get the Content-Type of the image from the image path - local full_path="../products/$product_name/images/embedded/$image_filename" + local full_path="./products/$product_name/images/embedded/$image_filename" local content_type=$(file --mime-type -b "$full_path") local response=$(curl -s --request POST \ @@ -222,7 +217,7 @@ function portal_product_load_documentation_images() { log_message $INFO "Loading documentation images for product $product_name ..." - local images_path="../products/$product_name/images/embedded" + local images_path="./products/$product_name/images/embedded" if [ ! -d "$images_path" ]; then log_message $WARNING "No images found in $images_path" return @@ -288,7 +283,7 @@ function load_and_process_product_manifest_content_metadata() { portal_product_toc_markdown_upsert "$name" "$slug" $order "$product_toc_id" log_message $INFO "Document ID: $document_id" - local markdown_file="../products/$product_name/$contentUrl" + local markdown_file="./products/$product_name/$contentUrl" if [ ! -f "$markdown_file" ]; then log_message $ERROR "Markdown file not found: $markdown_file" exit 1 @@ -344,7 +339,7 @@ function load_and_process_product_manifest_content_metadata() { portal_product_toc_markdown_upsert "$child_name" "$child_slug" $child_order "$parent_toc_id" log_message $INFO "Document ID for CHILD: $document_id" - local child_markdown_file="../products/$product_name/$child_contentUrl" + local child_markdown_file="./products/$product_name/$child_contentUrl" if [ ! -f "$child_markdown_file" ]; then log_message $ERROR "Markdown file not found: $child_markdown_file" exit 1 @@ -865,3 +860,14 @@ function portal_product_publish() { log_message $INFO "Done publishing product." log_message $DEBUG "Exit portal_product_publish" } + + +## Initial setup - get portal ID for the subdomain +log_message $INFO "Fetching portal information..." +portalsResponse=$(curl -s --request GET \ + --url "$PORTAL_URL/portals?subdomain=$PORTAL_SUBDOMAIN" \ + --header "Authorization: Bearer $SWAGGERHUB_API_KEY" \ + --header "Content-Type: application/json") + +portal_id=$(echo "$portalsResponse" | jq -r '.items[0].id') +log_message $INFO "Portal ID: $portal_id" \ No newline at end of file