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 output to file option #44

Merged
merged 10 commits into from
Mar 7, 2024
51 changes: 41 additions & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '-')
Expand All @@ -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
Expand Down Expand Up @@ -149,14 +168,26 @@ 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"
output=$(echo "${{steps.test_changelog.outputs.changelog}}" | head -n 1)
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
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -39,14 +40,15 @@ 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 |
| --deprecation-days-beta | deprecation-days-beta | 31 |
| --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`.

Expand All @@ -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 |
| CLI | Action input | Default |
| --------------------- | ------------------- | ------- |
| --include-path-params | include-path-params | false |
| --exclude-elements | exclude-elements | '' |
| --composed | composed | false |
| N/A | output-to-file | '' |
5 changes: 5 additions & 0 deletions breaking/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -48,3 +52,4 @@ runs:
- ${{ inputs.deprecation-days-stable }}
- ${{ inputs.exclude-elements }}
- ${{ inputs.composed }}
- ${{ inputs.output-to-file }}
42 changes: 19 additions & 23 deletions breaking/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"

Expand All @@ -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
5 changes: 5 additions & 0 deletions changelog/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -31,3 +35,4 @@ runs:
- ${{ inputs.include-path-params }}
- ${{ inputs.exclude-elements }}
- ${{ inputs.composed }}
- ${{ inputs.output-to-file }}
28 changes: 12 additions & 16 deletions changelog/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
Expand All @@ -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 ***

18 changes: 18 additions & 0 deletions common/common.sh
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 5 additions & 0 deletions diff/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -41,3 +45,4 @@ runs:
- ${{ inputs.include-path-params }}
- ${{ inputs.exclude-elements }}
- ${{ inputs.composed }}
- ${{ inputs.output-to-file }}
Loading
Loading