Skip to content

Commit b9a6a7c

Browse files
authored
Add output to file option (#44)
1 parent 673c40f commit b9a6a7c

File tree

9 files changed

+136
-81
lines changed

9 files changed

+136
-81
lines changed

.github/workflows/test.yaml

+41-10
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ jobs:
77
runs-on: ubuntu-latest
88
name: Test diff action
99
steps:
10-
- name: checkout
11-
uses: actions/checkout@v4
12-
- name: Running diff action
13-
id: test_ete
14-
uses: ./diff
15-
with:
16-
base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml
17-
revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml
18-
format: 'text'
10+
- name: checkout
11+
uses: actions/checkout@v4
12+
- name: Running diff action
13+
id: test_ete
14+
uses: ./diff
15+
with:
16+
base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml
17+
revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml
18+
format: 'text'
19+
output-to-file: 'diff.txt'
20+
- name: Test diff action output to file
21+
run: |
22+
if [ ! -s diff.txt ]; then
23+
echo "Diff file doesn't exist or is empty"
24+
exit 1
25+
fi
1926
oasdiff_diff_exclude_elements:
2027
runs-on: ubuntu-latest
2128
name: Test diff action with exclude-elements option
@@ -79,6 +86,7 @@ jobs:
7986
base: 'specs/base.yaml'
8087
revision: 'specs/revision-breaking.yaml'
8188
fail-on-diff: false
89+
output-to-file: 'breaking.txt'
8290
- name: Test breaking changes action output
8391
run: |
8492
delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-')
@@ -90,6 +98,17 @@ jobs:
9098
echo "Expected output '1 breaking changes: 1 error, 0 warning' but got '$output'" >&2
9199
exit 1
92100
fi
101+
- name: Test breaking changes action output to file
102+
run: |
103+
if [ ! -s breaking.txt ]; then
104+
echo "Breaking changes file doesn't exist or is empty"
105+
exit 1
106+
fi
107+
output=$(cat breaking.txt | head -n 1)
108+
if [[ "${output}" != "1 breaking changes: 1 error, 0 warning" ]]; then
109+
echo "Expected output '1 breaking changes: 1 error, 0 warning' but got '${output}'" >&2
110+
exit 1
111+
fi
93112
oasdiff_breaking_composed:
94113
runs-on: ubuntu-latest
95114
name: Test breaking action with composed option
@@ -149,14 +168,26 @@ jobs:
149168
with:
150169
base: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test1.yaml
151170
revision: https://raw.githubusercontent.com/Tufin/oasdiff/main/data/openapi-test3.yaml
171+
output-to-file: "changelog.txt"
152172
- name: Test changelog action output
153173
run: |
154174
readonly expected_output="20 changes: 2 error, 4 warning, 14 info"
155175
output=$(echo "${{steps.test_changelog.outputs.changelog}}" | head -n 1)
156176
if [[ "${output}" != "${expected_output}" ]]; then
157177
echo "Expected output '20 changes: 2 error, 4 warning, 14 info' but got '${output}'" >&2
158178
exit 1
159-
fi
179+
fi
180+
- name: Test changelog action output to file
181+
run: |
182+
if [ ! -s changelog.txt ]; then
183+
echo "Changelog file doesn't exist or is empty"
184+
exit 1
185+
fi
186+
output=$(cat changelog.txt | head -n 1)
187+
if [[ "${output}" != "20 changes: 2 error, 4 warning, 14 info" ]]; then
188+
echo "Expected output '20 changes: 2 error, 4 warning, 14 info' but got '${output}'" >&2
189+
exit 1
190+
fi
160191
oasdiff_changelog_composed:
161192
runs-on: ubuntu-latest
162193
name: Test changelog action with composed option

README.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ Copy and paste the following snippet into your build .yml file:
1616
revision: 'specs/revision.yaml'
1717
```
1818

19-
This action supports additional arguments that are converted to parameters for the `oasdiff` CLI.
19+
This action supports additional arguments. Most are converted to parameters for the `oasdiff` CLI.
2020

21-
| CLI | Action input | Default |
22-
|--------|--------|--------|
23-
| --fail-on-diff | fail-on-diff | false |
24-
| --format | format | yaml |
25-
| --include-path-params | include-path-params | false |
26-
| --exclude-elements | exclude-elements | '' |
27-
| --composed | composed | false |
21+
| CLI | Action input | Default |
22+
| --------------------- | ------------------- | ------- |
23+
| --fail-on-diff | fail-on-diff | false |
24+
| --format | format | yaml |
25+
| --include-path-params | include-path-params | false |
26+
| --exclude-elements | exclude-elements | '' |
27+
| --composed | composed | false |
28+
| N/A | output-to-file | '' |
2829

2930
### Check for breaking API changes, and fail if any are found
3031
Copy and paste the following snippet into your build .yml file:
@@ -39,14 +40,15 @@ Copy and paste the following snippet into your build .yml file:
3940
Additional arguments:
4041

4142
| CLI | Action input | Default |
42-
|---------------------------|-------------------------|---------|
43+
| ------------------------- | ----------------------- | ------- |
4344
| --fail-on WARN | fail-on-diff | true |
4445
| --include-checks | include-checks | csv |
4546
| --include-path-params | include-path-params | false |
4647
| --deprecation-days-beta | deprecation-days-beta | 31 |
4748
| --deprecation-days-stable | deprecation-days-stable | 180 |
4849
| --exclude-elements | exclude-elements | '' |
4950
| --composed | composed | false |
51+
| N/A | output-to-file | '' |
5052

5153
This action delivers a summary of breaking changes, accessible as a GitHub step output named `breaking`.
5254

@@ -62,8 +64,9 @@ Copy and paste the following snippet into your build .yml file:
6264

6365
Additional arguments:
6466

65-
| CLI | Action input | Default |
66-
|--------|--------|--------|
67-
| --include-path-params | include-path-params | false |
68-
| --exclude-elements | exclude-elements | '' |
69-
| --composed | composed | false |
67+
| CLI | Action input | Default |
68+
| --------------------- | ------------------- | ------- |
69+
| --include-path-params | include-path-params | false |
70+
| --exclude-elements | exclude-elements | '' |
71+
| --composed | composed | false |
72+
| N/A | output-to-file | '' |

breaking/action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ inputs:
3232
description: 'Run in composed mode'
3333
required: false
3434
default: 'false'
35+
output-to-file:
36+
description: 'Output to a file at the given path'
37+
required: false
38+
default: ''
3539
outputs:
3640
breaking:
3741
description: 'Output summary of API breaking changes, encompassing both warnings and errors'
@@ -48,3 +52,4 @@ runs:
4852
- ${{ inputs.deprecation-days-stable }}
4953
- ${{ inputs.exclude-elements }}
5054
- ${{ inputs.composed }}
55+
- ${{ inputs.output-to-file }}

breaking/entrypoint.sh

+19-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22
set -e
33

4+
source $GITHUB_WORKSPACE/common/common.sh
5+
46
readonly base="$1"
57
readonly revision="$2"
68
readonly fail_on_diff="$3"
@@ -10,31 +12,32 @@ readonly deprecation_days_beta="$6"
1012
readonly deprecation_days_stable="$7"
1113
readonly exclude_elements="$8"
1214
readonly composed="$9"
15+
readonly output_to_file="${10}"
1316

14-
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"
17+
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"
1518

1619
# Build flags to pass in command
1720
flags=""
1821
if [ "$fail_on_diff" = "true" ]; then
19-
flags="${flags} --fail-on WARN"
22+
flags="$flags --fail-on WARN"
2023
fi
2124
if [ "$include_path_params" = "true" ]; then
22-
flags="${flags} --include-path-params"
25+
flags="$flags --include-path-params"
2326
fi
2427
if [ -n "$include_checks" ]; then
25-
flags="${flags} --include-checks $include_checks"
28+
flags="$flags --include-checks $include_checks"
2629
fi
2730
if [ -n "$deprecation_days_beta" ]; then
28-
flags="${flags} --deprecation-days-beta $deprecation_days_beta"
31+
flags="$flags --deprecation-days-beta $deprecation_days_beta"
2932
fi
3033
if [ -n "$deprecation_days_stable" ]; then
31-
flags="${flags} --deprecation-days-stable $deprecation_days_stable"
34+
flags="$flags --deprecation-days-stable $deprecation_days_stable"
3235
fi
33-
if [ "$exclude_elements" != "" ]; then
34-
flags="${flags} --exclude-elements ${exclude_elements}"
36+
if [ -n "$exclude_elements" ]; then
37+
flags="$flags --exclude-elements $exclude_elements"
3538
fi
3639
if [ "$composed" = "true" ]; then
37-
flags="${flags} -c"
40+
flags="$flags -c"
3841
fi
3942
echo "flags: $flags"
4043

@@ -46,32 +49,25 @@ echo "flags: $flags"
4649
# {delimiter}
4750
# see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
4851
delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-')
49-
echo "breaking<<$delimiter" >>$GITHUB_OUTPUT
52+
echo "breaking<<$delimiter" >>"$GITHUB_OUTPUT"
5053

5154
if [ -n "$flags" ]; then
52-
output=$(oasdiff breaking "$base" "$revision" $flags | head -n 1)
55+
output=$(oasdiff breaking "$base" "$revision" $flags)
5356
else
54-
output=$(oasdiff breaking "$base" "$revision" | head -n 1)
57+
output=$(oasdiff breaking "$base" "$revision")
5558
fi
5659

5760
if [ -n "$output" ]; then
58-
# github-action limits output to 1MB
59-
# we count bytes because unicode has multibyte characters
60-
size=$(echo "$output" | wc -c)
61-
if [ "$size" -ge "1000000" ]; then
62-
echo "WARN: breaking exceeds the 1MB limit, truncating output..." >&2
63-
output=$(echo "$output" | head -c 1000000)
64-
fi
65-
echo "$output" >>$GITHUB_OUTPUT
61+
write_output "$(echo "$output" | head -n 1)" "$output"
6662
else
67-
echo "No breaking changes" >>$GITHUB_OUTPUT
63+
write_output "No breaking changes"
6864
fi
6965

70-
echo "$delimiter" >>$GITHUB_OUTPUT
66+
echo "$delimiter" >>"$GITHUB_OUTPUT"
7167

7268
# *** github action step output ***
7369

7470
# Updating GitHub Action summary with formatted output
75-
flags="${flags} --format githubactions"
71+
flags="$flags --format githubactions"
7672
# Writes the summary to log and updates GitHub Action summary
7773
oasdiff breaking "$base" "$revision" $flags

changelog/action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ inputs:
1919
description: 'Run in composed mode'
2020
required: false
2121
default: 'false'
22+
output-to-file:
23+
description: 'Output to a file at the given path'
24+
required: false
25+
default: ''
2226
outputs:
2327
changelog:
2428
description: 'Output summary of API changelog'
@@ -31,3 +35,4 @@ runs:
3135
- ${{ inputs.include-path-params }}
3236
- ${{ inputs.exclude-elements }}
3337
- ${{ inputs.composed }}
38+
- ${{ inputs.output-to-file }}

changelog/entrypoint.sh

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
#!/bin/sh
22
set -e
33

4+
source $GITHUB_WORKSPACE/common/common.sh
5+
46
readonly base="$1"
57
readonly revision="$2"
68
readonly include_path_params="$3"
79
readonly exclude_elements="$4"
810
readonly composed="$5"
11+
readonly output_to_file="$6"
912

10-
echo "running oasdiff changelog base: $base, revision: $revision, include_path_params: $include_path_params, exclude_elements: $exclude_elements"
13+
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"
1114

1215
# Build flags to pass in command
1316
flags=""
1417
if [ "$include_path_params" = "true" ]; then
15-
flags="${flags} --include-path-params"
18+
flags="$flags --include-path-params"
1619
fi
17-
if [ "$exclude_elements" != "" ]; then
18-
flags="${flags} --exclude-elements ${exclude_elements}"
20+
if [ -n "$exclude_elements" ]; then
21+
flags="$flags --exclude-elements $exclude_elements"
1922
fi
2023
if [ "$composed" = "true" ]; then
21-
flags="${flags} -c"
24+
flags="$flags -c"
2225
fi
2326
echo "flags: $flags"
2427

@@ -32,7 +35,7 @@ set -o pipefail
3235
# {delimiter}
3336
# see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
3437
delimiter=$(cat /proc/sys/kernel/random/uuid | tr -d '-')
35-
echo "changelog<<$delimiter" >>$GITHUB_OUTPUT
38+
echo "changelog<<$delimiter" >>"$GITHUB_OUTPUT"
3639

3740
if [ -n "$flags" ]; then
3841
output=$(oasdiff changelog "$base" "$revision" $flags)
@@ -41,19 +44,12 @@ else
4144
fi
4245

4346
if [ -n "$output" ]; then
44-
# github-action limits output to 1MB
45-
# we count bytes because unicode has multibyte characters
46-
size=$(echo "$output" | wc -c)
47-
if [ "$size" -ge "1000000" ]; then
48-
echo "WARN: changelog exceeds the 1MB limit, truncating output..." >&2
49-
output=$(echo "$output" | head -c 1000000)
50-
fi
51-
echo "$output" >>$GITHUB_OUTPUT
47+
write_output "$output"
5248
else
53-
echo "No changelog changes" >>$GITHUB_OUTPUT
49+
write_output "No changelog changes"
5450
fi
5551

56-
echo "$delimiter" >>$GITHUB_OUTPUT
52+
echo "$delimiter" >>"$GITHUB_OUTPUT"
5753

5854
# *** github action step output ***
5955

common/common.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
write_output () {
2+
local output="$1"
3+
if [ -n "$output_to_file" ]; then
4+
local file_output="$2"
5+
if [ -z "$file_output" ]; then
6+
file_output=$output
7+
fi
8+
echo "$file_output" >> "$output_to_file"
9+
fi
10+
# github-action limits output to 1MB
11+
# we count bytes because unicode has multibyte characters
12+
size=$(echo "$output" | wc -c)
13+
if [ "$size" -ge "1000000" ]; then
14+
echo "WARN: diff exceeds the 1MB limit, truncating output..." >&2
15+
output=$(echo "$output" | head -c 1000000)
16+
fi
17+
echo "$output" >>"$GITHUB_OUTPUT"
18+
}

diff/action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ inputs:
2727
description: 'Run in composed mode'
2828
required: false
2929
default: 'false'
30+
output-to-file:
31+
description: 'Output to a file at the given path'
32+
required: false
33+
default: ''
3034
outputs:
3135
diff:
3236
description: 'Output summary of API diff'
@@ -41,3 +45,4 @@ runs:
4145
- ${{ inputs.include-path-params }}
4246
- ${{ inputs.exclude-elements }}
4347
- ${{ inputs.composed }}
48+
- ${{ inputs.output-to-file }}

0 commit comments

Comments
 (0)