diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6816f28..6ffb4a5 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -7,15 +7,22 @@ jobs: runs-on: ubuntu-latest name: Test diff action steps: - - name: checkout - uses: actions/checkout@v4 - - name: Running diff action - id: test_ete - uses: ./diff - with: - base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml - revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml - format: 'text' + - name: checkout + uses: actions/checkout@v4 + - name: Running diff action + id: test_ete + uses: ./diff + with: + base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml + revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml + format: 'text' + output-to-file: 'diff.txt' + - name: Test diff action output to file + run: | + if [ ! -s diff.txt ]; then + echo "Diff file doesn't exist or is empty" + exit 1 + fi oasdiff_diff_exclude_elements: runs-on: ubuntu-latest name: Test diff action with exclude-elements option @@ -79,6 +86,7 @@ jobs: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' fail-on-diff: false + output-to-file: 'breaking.txt' - name: Test breaking changes action output run: | delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-') @@ -90,6 +98,17 @@ jobs: echo "Expected output '1 breaking changes: 1 error, 0 warning' but got '$output'" >&2 exit 1 fi + - name: Test breaking changes action output to file + run: | + if [ ! -s breaking.txt ]; then + echo "Breaking changes file doesn't exist or is empty" + exit 1 + fi + output=$(cat breaking.txt | head -n 1) + if [[ "${output}" != "1 breaking changes: 1 error, 0 warning" ]]; then + echo "Expected output '1 breaking changes: 1 error, 0 warning' but got '${output}'" >&2 + exit 1 + fi oasdiff_breaking_composed: runs-on: ubuntu-latest name: Test breaking action with composed option @@ -149,6 +168,7 @@ jobs: with: base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml + output-to-file: "changelog.txt" - name: Test changelog action output run: | readonly expected_output="20 changes: 2 error, 4 warning, 14 info" @@ -156,7 +176,18 @@ jobs: if [[ "${output}" != "${expected_output}" ]]; then echo "Expected output '20 changes: 2 error, 4 warning, 14 info' but got '${output}'" >&2 exit 1 - fi + fi + - name: Test changelog action output to file + run: | + if [ ! -s changelog.txt ]; then + echo "Changelog file doesn't exist or is empty" + exit 1 + fi + output=$(cat changelog.txt | head -n 1) + if [[ "${output}" != "20 changes: 2 error, 4 warning, 14 info" ]]; then + echo "Expected output '20 changes: 2 error, 4 warning, 14 info' but got '${output}'" >&2 + exit 1 + fi oasdiff_changelog_composed: runs-on: ubuntu-latest name: Test changelog action with composed option diff --git a/README.md b/README.md index 5941ce2..fb77e3a 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,16 @@ Copy and paste the following snippet into your build .yml file: revision: 'specs/revision.yaml' ``` -This action supports additional arguments that are converted to parameters for the `oasdiff` CLI. +This action supports additional arguments. Most are converted to parameters for the `oasdiff` CLI. -| CLI | Action input | Default | -|--------|--------|--------| -| --fail-on-diff | fail-on-diff | false | -| --format | format | yaml | -| --include-path-params | include-path-params | false | -| --exclude-elements | exclude-elements | '' | -| --composed | composed | false | +| CLI | Action input | Default | +| --------------------- | ------------------- | ------- | +| --fail-on-diff | fail-on-diff | false | +| --format | format | yaml | +| --include-path-params | include-path-params | false | +| --exclude-elements | exclude-elements | '' | +| --composed | composed | false | +| N/A | output-to-file | '' | ### Check for breaking API changes, and fail if any are found Copy and paste the following snippet into your build .yml file: @@ -39,7 +40,7 @@ Copy and paste the following snippet into your build .yml file: Additional arguments: | CLI | Action input | Default | -|---------------------------|-------------------------|---------| +| ------------------------- | ----------------------- | ------- | | --fail-on WARN | fail-on-diff | true | | --include-checks | include-checks | csv | | --include-path-params | include-path-params | false | @@ -47,6 +48,7 @@ Additional arguments: | --deprecation-days-stable | deprecation-days-stable | 180 | | --exclude-elements | exclude-elements | '' | | --composed | composed | false | +| N/A | output-to-file | '' | This action delivers a summary of breaking changes, accessible as a GitHub step output named `breaking`. @@ -62,8 +64,9 @@ Copy and paste the following snippet into your build .yml file: Additional arguments: -| CLI | Action input | Default | -|--------|--------|--------| -| --include-path-params | include-path-params | false | -| --exclude-elements | exclude-elements | '' | -| --composed | composed | false | \ No newline at end of file +| CLI | Action input | Default | +| --------------------- | ------------------- | ------- | +| --include-path-params | include-path-params | false | +| --exclude-elements | exclude-elements | '' | +| --composed | composed | false | +| N/A | output-to-file | '' | diff --git a/breaking/action.yml b/breaking/action.yml index 833f107..8238bd9 100644 --- a/breaking/action.yml +++ b/breaking/action.yml @@ -32,6 +32,10 @@ inputs: description: 'Run in composed mode' required: false default: 'false' + output-to-file: + description: 'Output to a file at the given path' + required: false + default: '' outputs: breaking: description: 'Output summary of API breaking changes, encompassing both warnings and errors' @@ -48,3 +52,4 @@ runs: - ${{ inputs.deprecation-days-stable }} - ${{ inputs.exclude-elements }} - ${{ inputs.composed }} + - ${{ inputs.output-to-file }} diff --git a/breaking/entrypoint.sh b/breaking/entrypoint.sh index 18c800f..bc65028 100755 --- a/breaking/entrypoint.sh +++ b/breaking/entrypoint.sh @@ -1,6 +1,8 @@ #!/bin/sh set -e +source $GITHUB_WORKSPACE/common/common.sh + readonly base="$1" readonly revision="$2" readonly fail_on_diff="$3" @@ -10,31 +12,32 @@ readonly deprecation_days_beta="$6" readonly deprecation_days_stable="$7" readonly exclude_elements="$8" readonly composed="$9" +readonly output_to_file="${10}" -echo "running oasdiff breaking... base: $base, revision: $revision, fail_on_diff: $fail_on_diff, include_checks: $include_checks, include_path_params: $include_path_params, deprecation_days_beta: $deprecation_days_beta, deprecation_days_stable: $deprecation_days_stable, exclude_elements: $exclude_elements" +echo "running oasdiff breaking... base: $base, revision: $revision, fail_on_diff: $fail_on_diff, include_checks: $include_checks, include_path_params: $include_path_params, deprecation_days_beta: $deprecation_days_beta, deprecation_days_stable: $deprecation_days_stable, exclude_elements: $exclude_elements, composed: $composed, output_to_file: $output_to_file" # Build flags to pass in command flags="" if [ "$fail_on_diff" = "true" ]; then - flags="${flags} --fail-on WARN" + flags="$flags --fail-on WARN" fi if [ "$include_path_params" = "true" ]; then - flags="${flags} --include-path-params" + flags="$flags --include-path-params" fi if [ -n "$include_checks" ]; then - flags="${flags} --include-checks $include_checks" + flags="$flags --include-checks $include_checks" fi if [ -n "$deprecation_days_beta" ]; then - flags="${flags} --deprecation-days-beta $deprecation_days_beta" + flags="$flags --deprecation-days-beta $deprecation_days_beta" fi if [ -n "$deprecation_days_stable" ]; then - flags="${flags} --deprecation-days-stable $deprecation_days_stable" + flags="$flags --deprecation-days-stable $deprecation_days_stable" fi -if [ "$exclude_elements" != "" ]; then - flags="${flags} --exclude-elements ${exclude_elements}" +if [ -n "$exclude_elements" ]; then + flags="$flags --exclude-elements $exclude_elements" fi if [ "$composed" = "true" ]; then - flags="${flags} -c" + flags="$flags -c" fi echo "flags: $flags" @@ -46,32 +49,25 @@ echo "flags: $flags" # {delimiter} # see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-') -echo "breaking<<$delimiter" >>$GITHUB_OUTPUT +echo "breaking<<$delimiter" >>"$GITHUB_OUTPUT" if [ -n "$flags" ]; then - output=$(oasdiff breaking "$base" "$revision" $flags | head -n 1) + output=$(oasdiff breaking "$base" "$revision" $flags) else - output=$(oasdiff breaking "$base" "$revision" | head -n 1) + output=$(oasdiff breaking "$base" "$revision") fi if [ -n "$output" ]; then - # github-action limits output to 1MB - # we count bytes because unicode has multibyte characters - size=$(echo "$output" | wc -c) - if [ "$size" -ge "1000000" ]; then - echo "WARN: breaking exceeds the 1MB limit, truncating output..." >&2 - output=$(echo "$output" | head -c 1000000) - fi - echo "$output" >>$GITHUB_OUTPUT + write_output "$(echo "$output" | head -n 1)" "$output" else - echo "No breaking changes" >>$GITHUB_OUTPUT + write_output "No breaking changes" fi -echo "$delimiter" >>$GITHUB_OUTPUT +echo "$delimiter" >>"$GITHUB_OUTPUT" # *** github action step output *** # Updating GitHub Action summary with formatted output -flags="${flags} --format githubactions" +flags="$flags --format githubactions" # Writes the summary to log and updates GitHub Action summary oasdiff breaking "$base" "$revision" $flags diff --git a/changelog/action.yml b/changelog/action.yml index c445741..4fb32d3 100644 --- a/changelog/action.yml +++ b/changelog/action.yml @@ -19,6 +19,10 @@ inputs: description: 'Run in composed mode' required: false default: 'false' + output-to-file: + description: 'Output to a file at the given path' + required: false + default: '' outputs: changelog: description: 'Output summary of API changelog' @@ -31,3 +35,4 @@ runs: - ${{ inputs.include-path-params }} - ${{ inputs.exclude-elements }} - ${{ inputs.composed }} + - ${{ inputs.output-to-file }} diff --git a/changelog/entrypoint.sh b/changelog/entrypoint.sh index c0139e7..d6723ca 100755 --- a/changelog/entrypoint.sh +++ b/changelog/entrypoint.sh @@ -1,24 +1,27 @@ #!/bin/sh set -e +source $GITHUB_WORKSPACE/common/common.sh + readonly base="$1" readonly revision="$2" readonly include_path_params="$3" readonly exclude_elements="$4" readonly composed="$5" +readonly output_to_file="$6" -echo "running oasdiff changelog base: $base, revision: $revision, include_path_params: $include_path_params, exclude_elements: $exclude_elements" +echo "running oasdiff changelog base: $base, revision: $revision, include_path_params: $include_path_params, exclude_elements: $exclude_elements, composed: $composed, output_to_file: $output_to_file" # Build flags to pass in command flags="" if [ "$include_path_params" = "true" ]; then - flags="${flags} --include-path-params" + flags="$flags --include-path-params" fi -if [ "$exclude_elements" != "" ]; then - flags="${flags} --exclude-elements ${exclude_elements}" +if [ -n "$exclude_elements" ]; then + flags="$flags --exclude-elements $exclude_elements" fi if [ "$composed" = "true" ]; then - flags="${flags} -c" + flags="$flags -c" fi echo "flags: $flags" @@ -32,7 +35,7 @@ set -o pipefail # {delimiter} # see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-') -echo "changelog<<$delimiter" >>$GITHUB_OUTPUT +echo "changelog<<$delimiter" >>"$GITHUB_OUTPUT" if [ -n "$flags" ]; then output=$(oasdiff changelog "$base" "$revision" $flags) @@ -41,19 +44,12 @@ else fi if [ -n "$output" ]; then - # github-action limits output to 1MB - # we count bytes because unicode has multibyte characters - size=$(echo "$output" | wc -c) - if [ "$size" -ge "1000000" ]; then - echo "WARN: changelog exceeds the 1MB limit, truncating output..." >&2 - output=$(echo "$output" | head -c 1000000) - fi - echo "$output" >>$GITHUB_OUTPUT + write_output "$output" else - echo "No changelog changes" >>$GITHUB_OUTPUT + write_output "No changelog changes" fi -echo "$delimiter" >>$GITHUB_OUTPUT +echo "$delimiter" >>"$GITHUB_OUTPUT" # *** github action step output *** diff --git a/common/common.sh b/common/common.sh new file mode 100644 index 0000000..15d555c --- /dev/null +++ b/common/common.sh @@ -0,0 +1,18 @@ +write_output () { + local output="$1" + if [ -n "$output_to_file" ]; then + local file_output="$2" + if [ -z "$file_output" ]; then + file_output=$output + fi + echo "$file_output" >> "$output_to_file" + fi + # github-action limits output to 1MB + # we count bytes because unicode has multibyte characters + size=$(echo "$output" | wc -c) + if [ "$size" -ge "1000000" ]; then + echo "WARN: diff exceeds the 1MB limit, truncating output..." >&2 + output=$(echo "$output" | head -c 1000000) + fi + echo "$output" >>"$GITHUB_OUTPUT" +} \ No newline at end of file diff --git a/diff/action.yml b/diff/action.yml index f9bbd65..dbd1493 100644 --- a/diff/action.yml +++ b/diff/action.yml @@ -27,6 +27,10 @@ inputs: description: 'Run in composed mode' required: false default: 'false' + output-to-file: + description: 'Output to a file at the given path' + required: false + default: '' outputs: diff: description: 'Output summary of API diff' @@ -41,3 +45,4 @@ runs: - ${{ inputs.include-path-params }} - ${{ inputs.exclude-elements }} - ${{ inputs.composed }} + - ${{ inputs.output-to-file }} diff --git a/diff/entrypoint.sh b/diff/entrypoint.sh index fdcb4c1..6a75583 100755 --- a/diff/entrypoint.sh +++ b/diff/entrypoint.sh @@ -1,6 +1,8 @@ #!/bin/sh set -e +source $GITHUB_WORKSPACE/common/common.sh + readonly base="$1" readonly revision="$2" readonly format="$3" @@ -8,25 +10,26 @@ readonly fail_on_diff="$4" readonly include_path_params="$5" readonly exclude_elements="$6" readonly composed="$7" +readonly output_to_file="$8" -echo "running oasdiff diff base: $base, revision: $revision, format: $format, fail_on_diff: $fail_on_diff, include_path_params: $include_path_params, exclude_elements: $exclude_elements" +echo "running oasdiff diff base: $base, revision: $revision, format: $format, fail_on_diff: $fail_on_diff, include_path_params: $include_path_params, exclude_elements: $exclude_elements, composed: $composed, output_to_file: $output_to_file" # Build flags to pass in command flags="" if [ "$format" != "yaml" ]; then - flags="${flags} --format ${format}" + flags="$flags --format $format" fi if [ "$fail_on_diff" = "true" ]; then - flags="${flags} --fail-on-diff" + flags="$flags --fail-on-diff" fi if [ "$include_path_params" = "true" ]; then - flags="${flags} --include-path-params" + flags="$flags --include-path-params" fi -if [ "$exclude_elements" != "" ]; then - flags="${flags} --exclude-elements ${exclude_elements}" +if [ -n "$exclude_elements" ]; then + flags="$flags --exclude-elements $exclude_elements" fi if [ "$composed" = "true" ]; then - flags="${flags} -c" + flags="$flags -c" fi echo "flags: $flags" @@ -38,7 +41,7 @@ echo "flags: $flags" # {delimiter} # see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-') -echo "diff<<$delimiter" >>$GITHUB_OUTPUT +echo "diff<<$delimiter" >>"$GITHUB_OUTPUT" set -o pipefail @@ -49,16 +52,9 @@ else fi if [ -n "$output" ]; then - # github-action limits output to 1MB - # we count bytes because unicode has multibyte characters - size=$(echo "$output" | wc -c) - if [ "$size" -ge "1000000" ]; then - echo "WARN: diff exceeds the 1MB limit, truncating output..." >&2 - output=$(echo "$output" | head -c $1000000) - fi - echo "$output" >>$GITHUB_OUTPUT + write_output "$output" else - echo "No changes" >>$GITHUB_OUTPUT + write_output "No changes" fi -echo "$delimiter" >>$GITHUB_OUTPUT \ No newline at end of file +echo "$delimiter" >>"$GITHUB_OUTPUT" \ No newline at end of file