diff --git a/ci/create-revisions-file.sh b/ci/create-revisions-file.sh new file mode 100755 index 000000000..4bd2f03e5 --- /dev/null +++ b/ci/create-revisions-file.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# Look for pull-request-title and pull-request-body files in the current directory +# these files should be generated by the environment in which we find ourselves: for now either github actions/workflows or jenkins pipelines + +# expected inputs via environment variables +# REPOS - space separated names of repositories to include, if a forward slash (/) included then the format is / otherwise the default org is cfengine + +# helper functions +function rev_env_name() +{ + local repo=$1 + echo "${repo}_rev" | tr '[:lower:]' '[:upper:]' | tr '/-' '_' +} + +# process revisions +# replace numeric revisions by pull/REVISION/merge + +# blank out revisions file +echo > revisions + +# process pull-request-body file added by execution environment like Jenkinsfile or github workflow +if [ -f pull-request-body ]; then + awk 'BEGIN { RS="https://" } {print $0}' pull-request-body | while IFS= read -r line + do + org_repo_rev=$(echo "$line" | awk 'match($0,/github.com\/([a-zA-Z-]*)\/([a-zA-Z-]*)\/pull\/([0-9]*)/,a) {print a[1] ":" a[2] ":" a[3]}') + _org=$(echo "$org_repo_rev" | cut -d: -f1) + _repo=$(echo "$org_repo_rev" | cut -d: -f2) + _rev=$(echo "$org_repo_rev" | cut -d: -f3) + if [ -n "$_repo" ]; then + varname=$(rev_env_name "$_repo") + declare "$varname"="$_rev" + fi + done +fi + +for reponame in $REPOS; do + if [ "$(expr "$reponame" : ".*/.*")" = 0 ]; then + org=cfengine + repo="$reponame" + else + org="$(echo "$reponame" | cut -d/ -f1)" + repo="$(echo "$reponame" | cut -d/ -f2)" + fi + varname="$(rev_env_name "$repo")" + + # if _REV is empty the set a default from: + # PR_BASE - from jenkins ${pullRequest.base} + # BRANCH_NAME - from jenkins, only usable for non-pull requests + if [ -z "${!varname}" ]; then + if [ "$varname" = "NT_DOCS_REV" ]; then + declare "$varname"="main" + else + if [ -n "$PR_BASE" ]; then + declare "$varname"="$PR_BASE" + else + declare "$varname"="$BRANCH_NAME" # master, 3.24.x, etc + fi + fi + fi + + declare "$varname"="$(echo "${!varname}"|sed -r 's|^([0-9]+)$|pull/\1/merge|')" + # do nothing if empty, remove tag: if found, add origin/ otherwise + declare "$varname"="$(echo "${!varname}"|awk 'BEGIN {FS= ":"} /^$/ {next} /^tag:.*/ {print $2} !/^tag:/ {print "origin/"$1}')" + + # remove origin/ (if any) and add refs/ if necessary + declare "$varname"="${!varname##origin/}" + if [ -n "${!varname}" ] && [ "$(expr "${!varname}" : "pull/")" != 0 ]; then + declare "$varname"="refs/${!varname}" + fi + echo "$repo git@github.com:${org}/${repo} ${!varname}" >> revisions +done