Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/initial-automation #5

Merged
merged 23 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
21d74f7
feat(portal automation): WIP partial scripts to support automation
frankkilcommins Jun 12, 2024
1ac63af
feat(automation): content publishing - WIP
frankkilcommins Jun 13, 2024
d97bab0
feat(initial-automation): Publishing script and simple GitHub Action
frankkilcommins Jun 17, 2024
f441505
chore: adjust the action to leverage variable for subdomain
frankkilcommins Jun 17, 2024
c614c8a
chore: enable script execution
frankkilcommins Jun 17, 2024
f650285
chore: fix env vars
frankkilcommins Jun 17, 2024
dbe6b7e
chore: move setup to end of script
frankkilcommins Jun 17, 2024
4a3e409
chore: adjust how action invokes scripts
frankkilcommins Jun 17, 2024
922be64
chore: fix action iteration
frankkilcommins Jun 17, 2024
182c133
chore: fix glob pattern for product name
frankkilcommins Jun 17, 2024
bb5d548
chore: fix manifest path
frankkilcommins Jun 17, 2024
a0987d6
chore: adjust function calling from action
frankkilcommins Jun 17, 2024
160fea2
chore: action update
frankkilcommins Jun 17, 2024
0a6d761
chore: env var config
frankkilcommins Jun 17, 2024
4218242
chore: adjust manifest path
frankkilcommins Jun 17, 2024
b32e89d
chore: adjust paths based on action execution
frankkilcommins Jun 17, 2024
9f446c9
chore: add trap and output logging
frankkilcommins Jun 18, 2024
a74530d
chore: improve logging
frankkilcommins Jun 18, 2024
eb6dc1f
chore: tidy up simple action
frankkilcommins Jun 18, 2024
2aab38b
chore: fix existing branding image setting
frankkilcommins Jun 18, 2024
8ef025c
Merge branch 'main' into feat/initial-automation
frankkilcommins Jun 18, 2024
8c09546
chore: adjust image names due to bad image
frankkilcommins Jun 18, 2024
deb21cc
chore: adjust image check for existence
frankkilcommins Jun 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions .github/workflows/docs-as-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}


2 changes: 1 addition & 1 deletion products/SwaggerHub Portal APIs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"slug": "swaggerhub-portal",
"public": true,
"hidden": false,
"logo": "images/SwaggerHub Icon Color.png",
"logo": "images/SwaggerHub.png",
"logoDark": "",
"autoPublish": true
},
Expand Down
70 changes: 38 additions & 32 deletions scripts/publish-portal-content.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Loading