Skip to content

Commit

Permalink
Merge pull request #4 from AdobeDocs/update-scripts
Browse files Browse the repository at this point in the history
Update scripts
  • Loading branch information
melissag-ensemble authored Mar 6, 2025
2 parents fcf448a + ed5fc27 commit c6842aa
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 83 deletions.
46 changes: 11 additions & 35 deletions .github/scripts/get-path-prefix.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
// This script retrieves the pathPrefix from the gatsby-config.js file.
// This script retrieves the pathPrefix from the runtime-connector's devsite-paths.json file.
// It serves as an example for how to set up external javascript functions
// outside workflow .yml files when they get too big or complex to keep them inline.

// Documentation for the actions/github-script:
// https://github.com/actions/github-script#run-a-separate-file

module.exports = async ({ core }) => {
const { pathPrefix } = await require('../../gatsby-config.js');
const DEVSITE_PATHS_URL = "https://raw.githubusercontent.com/aemsites/devsite-runtime-connector/refs/heads/main/src/devsite-paths.json";

module.exports = async ({ core, owner, repo }) => {
const entries = await (await fetch(DEVSITE_PATHS_URL)).json();
const entry = entries?.find(entry => entry.owner === owner && entry.repo === repo);
const pathPrefix = entry?.pathPrefix;

if (!pathPrefix) {
core.setFailed(
`The pathPrefix in the site's gatsby-config.js file is missing.
To fix this, open your gatsby-config.js file, and add it to the config object:
module.exports = {
pathPrefix: "/commerce/frontend-core/",
...
}`
);
} else if (pathPrefix === '/') {
core.setFailed(
`The pathPrefix in the site's gatsby-config.js file is set to "/". This is not allowed.
To fix this, change the pathPrefix to include a name that starts and ends with "/":
pathPrefix: "/commerce/frontend - core/"
This name identifies the site within the developer.adobe.com domain:
https://developer.adobe.com/document-services/<PATH_TO_FILES>.
`The pathPrefix is missing from the runtime-connector's devsite-paths.json file.
To fix this, reach out to the dev-site team.
`
);
} else {
if (!pathPrefix.startsWith('/') || !pathPrefix.endsWith('/')) {
core.setFailed(
`The pathPrefix in the site's gatsby-config.js file does not start or end with "/".
To fix this, change the pathPrefix to include a name that starts and ends with "/".
For example: "/document-services/" or "/commerce/cloud-tools/".
This is required by convention because of the way we construct site URLs.
For example: https://developer.adobe.com + /document-services/ + path/to/files/.
`
);
}
}

core.setOutput('path_prefix', pathPrefix);
};
98 changes: 98 additions & 0 deletions .github/scripts/process-mds.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash

fail() {
echo "$@" 1>&2
exit 1
}

ROOT="./src/pages"
OPERATION=$1
ENV=$2
CONTENT_REPO_BRANCH=$3
PATH_PREFIX=$4

# conditional http_method
case "$OPERATION" in
cache | preview | live)
http_method="POST"
;;
*)
fail "Unknown operation"
;;
esac

# conditional site and code_repo_branch
case "$ENV" in
stage)
site="adp-devsite-stage"
code_repo_branch="stage"
;;
prod)
site="adp-devsite"
code_repo_branch="main"
;;
*)
fail "Unknown env"
;;
esac

# conditional args
if [ "$ENV" == "stage" ] && [ "$OPERATION" == "preview" ]
then
args="--header \"x-content-source-authorization: ${CONTENT_REPO_BRANCH}\""
else
args=""
fi

process()
{
filename=$1
path="${PATH_PREFIX:1}/${filename#$ROOT/}"
url="https://admin.hlx.page/${OPERATION}/adobedocs/${site}/${code_repo_branch}/${path}"
cmd="curl -X${http_method} -vi ${args} ${url}"

echo ""
echo ""
echo "--------------------------------------------------------------------------------"
echo ""
echo "${cmd}"
echo ""

# run command and extract failure string
failure=$(eval "${cmd} | grep -e \"x-error:\"")

# append to failures
if [ "$failure" != "" ]
then
failures="${failures}\n${cmd}\n${failure}\n"
fi

# write failures to stderr so it can be accessed outside this subshell later
echo $failures > 2
}

summarize() {
echo ""
echo ""
echo "================================================================================"
echo ""

# read failures from stderr to access it from this parent shell
read -r failures < 2

if [ "${failures}" == "" ]
then
echo "Success!"
else
echo "Failures:"
echo -e "${failures}"
fi

echo ""
}

# process mds in root
# TODO: may want to only process certain types of files
find "${ROOT}" -type f \( -name "*.md" -o -name "*.json" \) -exec echo "{}" \; | while read i; do process $i; done

summarize
20 changes: 0 additions & 20 deletions .github/scripts/publish-mds.sh

This file was deleted.

93 changes: 93 additions & 0 deletions .github/scripts/validate-path-prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// This script retrieves the pathPrefix from the config.md file and validates it against the pathPrefix from devsite-paths.json.
// It serves as an example for how to set up external javascript functions
// outside workflow .yml files when they get too big or complex to keep them inline.

// Documentation for the actions/github-script:
// https://github.com/actions/github-script#run-a-separate-file

const CONFIG_PATH = `./src/pages/config.md`;

module.exports = async ({ core, pathPrefixFromDevsitePaths }) => {
const fs = await require('fs');
if (!fs.existsSync(CONFIG_PATH)) {
core.setFailed(
`The site's config.md file is missing.
To fix this, either create one in ./src/pages, or auto-generate one from the site's gatsby-config.md file by building navigation file.`
);
return;
}

const string = fs.readFileSync(CONFIG_PATH).toString() ?? "";
const lines = string.split('\n');

// find the pathPrefix key
const keyIndex = lines.findIndex(line => line.includes("pathPrefix:"));

if (keyIndex < 0) {
core.setFailed(
`The pathPrefix in the site's config.md file is missing.
To fix this, open your config.md file, and add it to the config object:
- pathPrefix:
...`
);
return;
}

// find the pathPrefix value
const line = lines.slice(keyIndex + 1)?.find(line => line.trimStart().startsWith("-")) ?? "";

// remove whitespace at start, remove dash (i.e. first non-whitespace character), and remove whitespace at start and end
const pathPrefix = line.trimStart().substring(1).trim();

if (!pathPrefix) {
core.setFailed(
`The pathPrefix in the site's config.md file is missing.
To fix this, open your config.md file, and add it to the config object:
- pathPrefix:
- /commerce/frontend-core/
...`
);
} else if (pathPrefix === '/') {
core.setFailed(
`The pathPrefix in the site's config.md file is set to "/". This is not allowed.
To fix this, change the pathPrefix to include a name that starts and ends with "/".
For example: "/commerce/frontend - core/"
This name identifies the site within the developer.adobe.com domain:
https://developer.adobe.com/document-services/<PATH_TO_FILES>.
`
);
} else if (!pathPrefix.startsWith('/') || !pathPrefix.endsWith('/')) {
core.setFailed(
`The pathPrefix in the site's config.md file does not start or end with "/".
pathPrefix: "${pathPrefix}"
To fix this, change the pathPrefix to include a name that starts and ends with "/".
For example: "/document-services/" or "/commerce/cloud-tools/".
This is required by convention because of the way we construct site URLs.
For example: https://developer.adobe.com + /document-services/ + path/to/files/.
`
);
} else if(pathPrefix !== `${pathPrefixFromDevsitePaths}/`) {
core.setFailed(
`The pathPrefix in the site's config.md file doesn't match the pathPrefix in the runtime-connector's devsite-paths.json.
pathPrefix from config.md: "${pathPrefix}"
pathPrefix from devsite-paths.json: "${pathPrefixFromDevsitePaths}"
To fix this, change the pathPrefix on either file to have the same value - except with trailing slash for config.md and without trailing slash for devsite-paths.json. To change devsite-paths.json, reach out to the dev-site team.
`
);
}
}


Loading

0 comments on commit c6842aa

Please sign in to comment.