|
| 1 | +# TODO (workflows) - Trigger CI build: |
| 2 | +# 1. clone random repo from list (/dev/urandom) select line=(rnd num % (len of list - 1)) + 1 --> using number $ head -$line $file | tail -1 |
| 3 | +# 2. replace tmpl files with random color from list (/dev/urandom) |
| 4 | +# 3. replace commits.tmpl and select random commit message (/dev/urandom) |
| 5 | +# 4. commit changes |
| 6 | + |
| 7 | +# TODO (workflows) - Trigger CD build: |
| 8 | +# 1. workflow to wait for multiple webhooks to be true to proceed with trio app deployment pipeline |
| 9 | + |
| 10 | +# Execute relative to the script path |
| 11 | +cd "$(dirname "$0")" |
| 12 | + |
| 13 | +REPLACEMENTS_FILE=./res/colors.list |
| 14 | +COMMITS_FILE=./res/commits.tmpl |
| 15 | +REPOS_FILE=./res/repos.list |
| 16 | + |
| 17 | +function getLine() { |
| 18 | + local input_file=$1 |
| 19 | + local random=$(od -vAn -N2 -tu2 < /dev/urandom |tr -d ' '); |
| 20 | + # wc counts newlines, not lines. Each file should has a trialing new lines so modulo arithmetic will work correctly |
| 21 | + local number_of_lines=$(wc -l < ${input_file} | tr -d ' ') |
| 22 | + # Head read line starting at 1 not 0, so we add 1 to the result |
| 23 | + local selected_line=$(( ${random} % ${number_of_lines} + 1 )) |
| 24 | + local result=$(head -n${selected_line} ${input_file} | tail -n1) |
| 25 | + printf "${result}" |
| 26 | +} |
| 27 | + |
| 28 | +function splitRepoInfo() { |
| 29 | + local repo_arr=(${1}) |
| 30 | + # Set globals |
| 31 | + REPO=${repo_arr[0]} |
| 32 | + BRANCH=${repo_arr[1]} |
| 33 | + TEMPLATE_PATH=${repo_arr[2]} |
| 34 | + OUTPUT_PATH=${repo_arr[3]} |
| 35 | + SEARCH_STRING=${repo_arr[4]} |
| 36 | +} |
| 37 | + |
| 38 | +# Text to replace search string with if found in files |
| 39 | +REPLACEMENT_TEXT=$(getLine ${REPLACEMENTS_FILE}) |
| 40 | + |
| 41 | +# Repo, branch, file and search string in file to clone and replace |
| 42 | +# Quotes preserve spaces we are passing |
| 43 | +REPO_INFO=$(getLine ${REPOS_FILE}) |
| 44 | +splitRepoInfo "${REPO_INFO}" |
| 45 | + |
| 46 | +# Commit message to set for committing files changs |
| 47 | +COMMIT=$(getLine ${COMMITS_FILE} | sed "s/${SEARCH_STRING}/${REPLACEMENT_TEXT}/g") |
| 48 | + |
| 49 | + |
| 50 | +# Could also output JSON and parse it with sprig templates in workflows |
| 51 | +echo "Outputting info:" |
| 52 | +echo "Repo info: ${REPO_INFO}" |
| 53 | +echo "Commit: ${COMMIT}" |
| 54 | +echo "Replacement text: ${REPLACEMENT_TEXT}" |
| 55 | +# Output values to files to be read as outputs |
| 56 | +printf "${REPO}" > target_repo.out |
| 57 | +printf "${BRANCH}" > target_branch.out |
| 58 | +printf "${TEMPLATE_PATH}" > target_template_filepath.out |
| 59 | +printf "${OUTPUT_PATH}" > target_putput_filepath.out |
| 60 | +printf "${SEARCH_STRING}" > target_search_string.out |
| 61 | +printf "${REPLACEMENT_TEXT}" > target_replacement.out |
| 62 | +printf "${COMMIT}" > target_commit_message.out |
0 commit comments