Last active
June 2, 2022 20:58
-
-
Save justinTM/b856c285f04fa5bdb5d0fec739c60d9e to your computer and use it in GitHub Desktop.
Revisions
-
justinTM revised this gist
Jul 11, 2021 . 1 changed file with 23 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,14 +6,20 @@ # 3. write out file argument 2, $DIFF_FILE_OUT # https://gitlab.com/gitlab-org/gitlab/-/issues/15582 set -e DIFF_START='```diff\n' DIFF_HEADER="The following rule changes will be applied upon successful rules sync,\ based on rules files changes in this commit:\n" DIFF_END='\n```' DIFF_WRAPPER="${DIFF_HEADER}${DIFF_START}%s${DIFF_END}" # check input file variable, use $DIFF_FILE_IN from ./gitlab-ci.yml by default DIFF_FILE_IN="${1:-$DIFF_FILE_IN}" if [ -z "$DIFF_FILE_IN" ]; then echo "Error: input variable 1 'DIFF_FILE_IN' is not defined." exit 1 elif test -f "${DIFF_FILE_IN}"; then # print diff to pipeline echo echo @@ -34,36 +40,33 @@ if [ -z "$DIFF_FILE_OUT" ]; then fi # remove all occurences of unwanted characters from diff text DIFF_OLD=$(cat -v ${DIFF_FILE_IN}) # read in file with "show-all" characters flag DIFF_OLD=`echo -e "${DIFF_OLD//'[0m'/}"` # remove all SGR ASCII reset codes '[0m' DIFF_OLD=`echo -e "${DIFF_OLD//'$'/}"` # remove all newline characters '$' DIFF_OLD=`echo -e "${DIFF_OLD//'^['/}"` # remove all escape characters '^[' DIFF_OLD=`echo -e "${DIFF_OLD//'+'/}"` # remove all '+' DIFF_OLD=`echo -e "${DIFF_OLD//'-'/}"` # remove all '-' # replace sgr color codes with characters, eg. '[32m' with '+' declare -A MAP_SGR_TO_CHAR=( ['[32m']='+' ['[31m']='-' ['[33m']='~' ) while read -r line; do # loop each line in $diff variable # check line for each color code for sgr_code in "${!MAP_SGR_TO_CHAR[@]}"; do # if found, replace with new character from associative array if [[ $line == *"$sgr_code"* ]]; then new_char="${MAP_SGR_TO_CHAR[$sgr_code]}" printf -v line '%s' "${new_char}${line/sgr_code/}" fi done DIFF_NEW+=`echo -e "$line\n"` # append markdown newline ' ' done <<< "${DIFF_OLD}" # wrap with markdown diff code section formatting ticks printf -v DIFF_NEW "${DIFF_WRAPPER}" "${DIFF_NEW}" echo -e "${DIFF_NEW}" > "${DIFF_FILE_OUT}" # write new diff file # print diff to pipeline echo echo echo "Markdown-formatted diff below" cat "${DIFF_FILE_OUT}" -
justinTM revised this gist
Jul 11, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/bash # this script will: # 1. read in file argument 1, $DIFF_FILE_IN # 2. format text to Markdown diff code section # 3. write out file argument 2, $DIFF_FILE_OUT @@ -41,7 +41,7 @@ diff_old=`echo -e "${diff_old//'^['/}"` # remove all escape characters '^[' diff_old=`echo -e "${diff_old//'+'/}"` # remove all '+' diff_old=`echo -e "${diff_old//'-'/}"` # remove all '-' # replace sgr color codes with characters, eg. '[32m' with '+' declare -A map_sgr_to_char=( ['[32m']='+' ['[31m']='-' ['[33m']='~' ) while read -r line; do # loop each line in $diff variable # check line for each color code -
justinTM created this gist
Jul 11, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ #!/bin/bash # this script will: # 1. read in argument 1, $DIFF_FILE_IN # 2. format text to Markdown diff code section # 3. write out file argument 2, $DIFF_FILE_OUT # https://gitlab.com/gitlab-org/gitlab/-/issues/15582 set -e # check input file variable, use $DIFF_FILE_IN from ./gitlab-ci.yml by default DIFF_FILE_IN="${1:-$DIFF_FILE_IN}" if [ -z "$DIFF_FILE_IN" ]; then echo "Error: input variable 1 'DIFF_FILE_IN' is not defined." exit 1 elif test -f "$DIFF_FILE_IN"; then # print diff to pipeline echo echo echo "rules diff below" cat "${DIFF_FILE_IN}" echo echo else echo "Error: Input file \"${DIFF_FILE_IN}\" does not exist." exit 1 fi # check input variable 2, use $DIFF_FILE_OUT from ./gitlab-ci.yml by default DIFF_FILE_OUT="${2:-$DIFF_FILE_OUT}" if [ -z "$DIFF_FILE_OUT" ]; then echo "Error: input variable 2 'DIFF_FILE_OUT' is not defined." exit 1 fi # remove all occurences of unwanted characters from diff text diff_old=$(cat -v ${DIFF_FILE_IN}) # read in file with "show-all" characters flag diff_old=`echo -e "${diff_old//'[0m'/}"` # remove all SGR ASCII reset codes '[0m' diff_old=`echo -e "${diff_old//'$'/}"` # remove all newline characters '$' diff_old=`echo -e "${diff_old//'^['/}"` # remove all escape characters '^[' diff_old=`echo -e "${diff_old//'+'/}"` # remove all '+' diff_old=`echo -e "${diff_old//'-'/}"` # remove all '-' # replace sgr color codes with characters, eg. '[31m' with '+' declare -A map_sgr_to_char=( ['[32m']='+' ['[31m']='-' ['[33m']='~' ) while read -r line; do # loop each line in $diff variable # check line for each color code for sgr_code in "${!map_sgr_to_char[@]}"; do # if found, replace with new character from associative array if [[ $line == *"$sgr_code"* ]]; then new_char="${map_sgr_to_char[$sgr_code]}" printf -v line "${new_char}%s" "${line/sgr_code/}" fi done diff+=`echo -e "$line\n"` # append markdown newline ' ' done <<< "$diff_old" # wrap with markdown diff code section formatting ticks diff_start='```diff\n' && diff_end='\n```' printf -v diff "${diff_start}%s${diff_end}" "$diff" echo -e "$diff" > "${DIFF_FILE_OUT}" # write new diff file # print diff to pipeline echo echo echo "Markdown-formatted diff below" cat ${DIFF_FILE_OUT} echo echo