Skip to content

Commit

Permalink
Dynamically compute remote owner and repo name stub
Browse files Browse the repository at this point in the history
For the reference model documentation develop branch, we want the owner, repo name, and branch
to be dynamically computed and not statically generated and presumed to be from the official
main repo, usnistgov/OSCAL. Resolves usnistgov#1286.
  • Loading branch information
aj-stein-nist committed May 25, 2022
1 parent d3a2b99 commit 67fdcfc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
50 changes: 48 additions & 2 deletions build/ci-cd/generate-model-documentation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,52 @@ fi

echo "BRANCH(initial)='${BRANCH}'"

# Compute owner and repo from parsing revision symbolic data to support local
# and remote automated CI/CD git clone scenarios more comprehensively. The
# remote might be origin, or might be something custom, do no rely on this.
# Out of this git rev-parse command is like owner/reponame
OWNER_REPO_NAME="$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}')"
# Use regex to group OWNER_REPO_NAME into (owner) (/) (reponame) in array at
# indices 1 and 3, drop slash at index 2.
remote_branch_filter='^([a-zA-Z0-9-]+)(\/)([a-zA-Z0-9_-]+)$'
# Set defaults if error occurs and detection fails
DEFAULT_REMOTE_NAME="origin"
DEFAULT_REMOTE_URL="[email protected]:usnistgov/OSCAL.git"

if [[ "${OWNER_REPO_NAME}" =~ $remote_branch_filter ]]; then
remote_name="${BASH_REMATCH[1]}"
remote_url="$(git remote get-url ${remote_name})"
else
echo -e "${P_ERROR}Name and URL for remote not detected, using default${P_END}"
remote_name="${DEFAULT_REMOTE_NAME}"
remote_url="${DEFAULT_REMOTE_URL}"
fi

# Use regex to group variations of a GitHub remote URL determined by resolving
# a git remote get-url remotename and consistently format it into a HTTPS URL
# stub for the correct org name or username. Each group is below with the
# numeric index of the array for BASH_REMATCH
# 0: full matching string
# 1: required SSH username, git protocol URI or HTTPS URI protocol, so git@ or
# git:// or https://
# 2: required github.com hostname to match only GitHub orgs and user forks
# 3: required colon or slash to support git SSH or GitHub HTTPS URL separators
# 4: required GitHub org or username, this can be letters, numbers, or hyphens
# 5: required slash, the separator between org/username and reponame
# 6: required repo name, letters, numbers, hyphens, underscores, and periods,
# there are no official GH docs on this, common reference stackoverflow.com/a/59082561
# 7: optional .git ending, common with git SSH URIs but also allow for HTTPS
github_url_filter='^(git@|git:\/\/|https:\/\/)(github.com)(:|\/)([a-zA-Z0-9-]+)(\/)([a-zA-Z0-9_-]+)(.git){0,1}$'
# Set defaults if error occurs and detection fails
DEFAULT_REMOTE="usnistgov/OSCAL"

if [[ "${remote_url}" =~ $github_url_filter ]]; then
REMOTE="${BASH_REMATCH[4]}/${BASH_REMATCH[6]}"
else
echo -e "${P_ERROR}No URL detected remote matching pattern, setting remote stub to default${P_END}"
REMOTE="${DEFAULT_REMOTE}"
fi

if [[ "$BRANCH" =~ ^v.* ]]; then
VERSION="${BRANCH/#"v"}"
REVISION="${VERSION}"
Expand Down Expand Up @@ -182,7 +228,7 @@ if [ ! -d "${doc_path}" ] || [ "$DISABLE_ARCHETYPE_CREATION" = "false" ]; then
rm -rf "${doc_path}"
#mkdir -p "${doc_path}"

result=$(cd ${DOCS_DIR};HUGO_REF_TYPE="${TYPE}" HUGO_REF_BRANCH="${BRANCH}" HUGO_REF_VERSION="${VERSION}" HUGO_REF_REVISION="${REVISION}" hugo new --kind reference-index "${doc_path}/_index.md" 2>&1)
result=$(cd ${DOCS_DIR};HUGO_REF_TYPE="${TYPE}" HUGO_REF_REMOTE="${REMOTE}" HUGO_REF_BRANCH="${BRANCH}" HUGO_REF_VERSION="${VERSION}" HUGO_REF_REVISION="${REVISION}" hugo new --kind reference-index "${doc_path}/_index.md" 2>&1)
cmd_exitcode=$?
if [ $cmd_exitcode -ne 0 ]; then
echo -e "${P_ERROR}Generating index page failed for revision '${P_END}${REVISION}${P_ERROR}' on branch '${P_END}${BRANCH}${P_ERROR}'.${P_END}"
Expand Down Expand Up @@ -236,7 +282,7 @@ while IFS="|" read metaschema_path archetype model_id model_name layer_id schema
# build the version folder
#if [ -d "${model_path}" ] && rm -rf "${doc_path}"

result=$(cd ${DOCS_DIR};HUGO_REF_TYPE="${TYPE}" HUGO_REF_BRANCH="${BRANCH}" HUGO_REF_VERSION="${VERSION}" HUGO_REF_REVISION="${REVISION}" HUGO_MODEL_NAME="${model_name}" HUGO_MODEL_ID="${model_id}" HUGO_SCHEMA_ID="${schema_id}" HUGO_MODEL_CONCEPTS_URL="/concepts/layer/${layer_id}/${schema_id}/" hugo new --kind ${archetype} "${model_path}" 2>&1)
result=$(cd ${DOCS_DIR};HUGO_REF_TYPE="${TYPE}" HUGO_REF_REMOTE="${REMOTE}" HUGO_REF_BRANCH="${BRANCH}" HUGO_REF_VERSION="${VERSION}" HUGO_REF_REVISION="${REVISION}" HUGO_MODEL_NAME="${model_name}" HUGO_MODEL_ID="${model_id}" HUGO_SCHEMA_ID="${schema_id}" HUGO_MODEL_CONCEPTS_URL="/concepts/layer/${layer_id}/${schema_id}/" hugo new --kind ${archetype} "${model_path}" 2>&1)
cmd_exitcode=$?
if [ $cmd_exitcode -ne 0 ]; then
echo -e "${P_ERROR}Generating '${P_END}${model_id}${P_OK}' model page failed for revision '${P_END}${REVISION}${P_ERROR}' on branch '${P_END}${BRANCH}${P_ERROR}'.${P_END}"
Expand Down
1 change: 1 addition & 0 deletions docs/archetypes/reference-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sidenav:
debug: false
oscal:
type: "{{ getenv "HUGO_REF_TYPE" }}"
remote: "{{ getenv "HUGO_REF_REMOTE" }}"
branch: "{{ getenv "HUGO_REF_BRANCH" }}"
revision: "{{ getenv "HUGO_REF_REVISION" }}"
version: "{{ getenv "HUGO_REF_VERSION" }}"
Expand Down
4 changes: 4 additions & 0 deletions docs/layouts/_default/reference-release.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
{{ if isset .Params "heading" }}<h1>{{ .Params.heading }}</h1>{{else}}<h1>{{ .Title }}</h1>{{ end }}
</header>
<p><span class="usa-tag">Release Version</span> {{ if eq .Params.oscal.version "develop" }}Latest Development Snapshot{{ else }}OSCAL v{{ .Params.oscal.version }}{{ end }}</p>
{{ if isset .Params.oscal "remote" }}
<p><span class="usa-tag">Github</span> <a href="https://github.com/{{ .Params.oscal.remote }}">{{ .Params.oscal.remote }}</a> <span class="usa-tag">{{ if eq .Params.oscal.type "tag" }}Tag{{ else }}Branch{{end}}</span> <a href="https://github.com/{{ .Params.oscal.remote }}/tree/{{ .Params.oscal.branch }}">{{ .Params.oscal.branch }}</a></p>
{{ else }}
<p><span class="usa-tag">Github</span> <a href="https://github.com/usnistgov/OSCAL">usnistgov/OSCAL</a> <span class="usa-tag">{{ if eq .Params.oscal.type "tag" }}Tag{{ else }}Branch{{end}}</span> <a href="https://github.com/usnistgov/OSCAL/tree/{{ .Params.oscal.branch }}">{{ .Params.oscal.branch }}</a></p>
{{ end }}
{{ .Content }}
{{ if gt (len .Pages) 0 }}
<p>The following documentation is provided for this release.</p>
Expand Down

0 comments on commit 67fdcfc

Please sign in to comment.