Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add raw-output option for json output #900

Merged
merged 13 commits into from Dec 30, 2022
30 changes: 27 additions & 3 deletions .github/workflows/manual-matrix-test.yml
Expand Up @@ -12,6 +12,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
matrix-raw-format: ${{ steps.set-matrix-json-raw-format.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -23,11 +24,19 @@ jobs:
with:
json: true
- name: List all changed files
run: |
echo '${{ steps.changed-files.outputs.all_changed_files }}'

run: echo '${{ steps.changed-files.outputs.all_changed_files }}'
- id: set-matrix
run: echo "matrix={\"container\":${{ steps.changed-files.outputs.all_changed_files }}}" >> "$GITHUB_OUTPUT"
- name: Get changed files json raw format
id: changed-files-json-raw-format
uses: ./
with:
json: true
json_raw_format: true
- name: List all changed files json raw format
run: echo '${{ steps.changed-files-json-raw-format.outputs.all_changed_files }}'
- id: set-matrix-json-raw-format
run: echo "matrix={\"container\":${{ steps.changed-files.outputs.all_changed_files }}}" >> "$GITHUB_OUTPUT"

matrix-job:
name: Run Matrix Job
Expand All @@ -43,3 +52,18 @@ jobs:
- name: Test
run: |
echo ${{ matrix.container }}

matrix-job-json-raw-format:
name: Run Matrix Job json raw format
runs-on: ubuntu-latest
needs: [changed-files]
strategy:
matrix: ${{ fromJSON(needs.changed-files.outputs.matrix-raw-format) }}
max-parallel: 4
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Test
run: |
echo ${{ matrix.container }}
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -529,6 +529,18 @@ jobs:
echo '${{ toJSON(steps.changed-files-json.outputs.all_changed_files) }}'
shell:
bash
- name: Run changed-files with json raw format
id: changed-files-json-raw-format
uses: ./
with:
json: true
json_raw_format: true
- name: Show output
run: |
echo '${{ toJSON(steps.changed-files-json-raw-format.outputs) }}'
echo '${{ toJSON(steps.changed-files-json-raw-format.outputs.all_changed_files) }}'
shell:
bash
- name: Run changed-files with comma separator
id: changed-files-comma
uses: ./
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -58,7 +58,7 @@ on:
push:
branches:
- main
# Compare the last commit of main -> to the current commit of a PR branch.
# Compare the last commit of main -> to the current commit of a PR branch.
# (Note: To compare changes between the last pushed commit to the remote PR branch set `since_last_remote_commit: true`)
pull_request:
branches:
Expand Down Expand Up @@ -227,7 +227,7 @@ See [inputs](#inputs) for more information.
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35

- name: List all added files
run: |
for file in ${{ steps.changed-files.outputs.added_files }}; do
Expand Down Expand Up @@ -472,7 +472,7 @@ See [inputs](#inputs) for more information.
- uses: nrwl/nx-set-shas@v3
id: last_successful_commit_push
with:
main-branch-name: ${{ steps.branch-name.outputs.current_branch }} # Get the last successful commit for the current branch.
main-branch-name: ${{ steps.branch-name.outputs.current_branch }} # Get the last successful commit for the current branch.
workflow-id: 'test.yml'

- name: Run changed-files with the commit of the last successful test workflow run
Expand All @@ -499,7 +499,7 @@ See [inputs](#inputs) for more information.
- uses: nrwl/nx-set-shas@v3
id: last_successful_commit_pull_request
with:
main-branch-name: ${{ steps.branch-name.outputs.base_ref_branch }} # Get the last successful commit on master or main branch
main-branch-name: ${{ steps.branch-name.outputs.base_ref_branch }} # Get the last successful commit on master or main branch
workflow_id: 'test.yml'

- name: Run changed-files with the commit of the last successful test workflow run on main
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Expand Up @@ -79,6 +79,10 @@ inputs:
description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs."
required: false
default: "false"
json_raw_format:
description: "Output list of changed files in a raw format which means that the output will not be surrounded by quotes and special characters will not be escaped."
required: false
default: "false"
fetch_depth:
description: "Depth of additional branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history."
required: false
Expand Down Expand Up @@ -243,6 +247,7 @@ runs:
INPUT_DIR_NAMES_MAX_DEPTH: ${{ inputs.dir_names_max_depth }}
INPUT_JSON: ${{ inputs.json }}
INPUT_HAS_CUSTOM_PATTERNS: ${{ steps.glob.outputs.has-custom-patterns }}
INPUT_JSON_RAW_FORMAT: ${{ inputs.json_raw_format }}
- name: Generate output files
uses: tj-actions/json2file@v1
if: inputs.write_output_files == 'true'
Expand Down
64 changes: 37 additions & 27 deletions get-changed-paths.sh
Expand Up @@ -51,6 +51,16 @@ function get_dirname_max_depth() {
done < <(uniq)
}

function json_output() {
JQ_ARGS="-R"
if [[ "$INPUT_JSON_RAW_FORMAT" == "true" ]]; then
JQ_ARGS="$JQ_ARGS -r"
fi

# shellcheck disable=SC2086
jq $JQ_ARGS 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /
}

function get_diff() {
local base="$1"
local sha="$2"
Expand Down Expand Up @@ -172,19 +182,19 @@ if [[ "$INPUT_HAS_CUSTOM_PATTERNS" == "false" ]]; then
ALL_OLD_NEW_RENAMED=$(get_renames "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" | awk -v d="$INPUT_OLD_NEW_FILES_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
fi
else
ADDED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" A | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
COPIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" C | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
DELETED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" D | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" M | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
RENAMED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" R | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
TYPE_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" T | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
UNMERGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" U | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
UNKNOWN=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" X | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_CHANGED_AND_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "*ACDMRTUX" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMR" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMRD" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ADDED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" A | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
COPIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" C | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
DELETED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" D | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" M | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
RENAMED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" R | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
TYPE_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" T | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
UNMERGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" U | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
UNKNOWN=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" X | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
ALL_CHANGED_AND_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "*ACDMRTUX" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
ALL_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMR" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
ALL_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMRD" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
if [[ $INPUT_INCLUDE_ALL_OLD_NEW_RENAMED_FILES == "true" ]]; then
ALL_OLD_NEW_RENAMED=$(get_renames "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_OLD_NEW_RENAMED=$(get_renames "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}' | json_output)
fi
fi
else
Expand Down Expand Up @@ -233,7 +243,7 @@ else
if [[ "$INPUT_JSON" == "false" ]]; then
OTHER_CHANGED=$(echo "${OTHER_CHANGED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
else
OTHER_CHANGED=$(echo "${OTHER_CHANGED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
OTHER_CHANGED=$(echo "${OTHER_CHANGED}" | json_output)
fi

if [[ -n "${OTHER_CHANGED}" && "${OTHER_CHANGED}" != "[]" ]]; then
Expand Down Expand Up @@ -285,7 +295,7 @@ else
if [[ "$INPUT_JSON" == "false" ]]; then
OTHER_MODIFIED=$(echo "${OTHER_MODIFIED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
else
OTHER_MODIFIED=$(echo "${OTHER_MODIFIED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
OTHER_MODIFIED=$(echo "${OTHER_MODIFIED}" | json_output)
fi

if [[ -n "${OTHER_MODIFIED}" && "$OTHER_MODIFIED" != "[]" ]]; then
Expand Down Expand Up @@ -336,7 +346,7 @@ else
if [[ "$INPUT_JSON" == "false" ]]; then
OTHER_DELETED=$(echo "${OTHER_DELETED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
else
OTHER_DELETED=$(echo "${OTHER_DELETED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
OTHER_DELETED=$(echo "${OTHER_DELETED}" | json_output)
fi

if [[ -n "${OTHER_DELETED}" && "${OTHER_DELETED}" != "[]" ]]; then
Expand Down Expand Up @@ -368,17 +378,17 @@ else
ALL_CHANGED=$(echo "${ALL_CHANGED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
ALL_MODIFIED=$(echo "${ALL_MODIFIED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}')
else
ADDED=$(echo "${ADDED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
COPIED=$(echo "${COPIED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
DELETED=$(echo "${DELETED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
MODIFIED=$(echo "${MODIFIED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
RENAMED=$(echo "${RENAMED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
TYPE_CHANGED=$(echo "${TYPE_CHANGED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
UNMERGED=$(echo "${UNMERGED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
UNKNOWN=$(echo "${UNKNOWN}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_CHANGED_AND_MODIFIED=$(echo "${ALL_CHANGED_AND_MODIFIED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_CHANGED=$(echo "${ALL_CHANGED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ALL_MODIFIED=$(echo "${ALL_MODIFIED}" | jq -R 'split("|") | @json' | sed -r 's/^"|"$//g' | tr -s /)
ADDED=$(echo "${ADDED}" | json_output)
COPIED=$(echo "${COPIED}" | json_output)
DELETED=$(echo "${DELETED}" | json_output)
MODIFIED=$(echo "${MODIFIED}" | json_output)
RENAMED=$(echo "${RENAMED}" | json_output)
TYPE_CHANGED=$(echo "${TYPE_CHANGED}" | json_output)
UNMERGED=$(echo "${UNMERGED}" | json_output)
UNKNOWN=$(echo "${UNKNOWN}" | json_output)
ALL_CHANGED_AND_MODIFIED=$(echo "${ALL_CHANGED_AND_MODIFIED}" | json_output)
ALL_CHANGED=$(echo "${ALL_CHANGED}" | json_output)
ALL_MODIFIED=$(echo "${ALL_MODIFIED}" | json_output)
fi
fi

Expand Down Expand Up @@ -435,4 +445,4 @@ if [[ $INPUT_INCLUDE_ALL_OLD_NEW_RENAMED_FILES == "true" ]]; then
fi
fi

echo "::endgroup::"
echo "::endgroup::"