-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
131 lines (118 loc) · 4.2 KB
/
action.yml
File metadata and controls
131 lines (118 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: "OpenAPI Diff"
description: "Run oasdiff changelog between base and head OpenAPI specs"
author: "Framna Denmark"
inputs:
base:
description: "Path to the base OpenAPI spec file"
required: true
head:
description: "Path to the head OpenAPI spec file"
required: true
format:
description: "Output format: table (default), text, html, or json"
required: false
default: "table"
outputs:
diff:
description: "The formatted diff output"
value: ${{ steps.render.outputs.result }}
has-changes:
description: "Whether there are changes (true/false)"
value: ${{ steps.render.outputs.has_changes }}
runs:
using: composite
steps:
- name: Install oasdiff
shell: bash
run: |
if ! command -v oasdiff &> /dev/null; then
curl -sSfL https://raw.githubusercontent.com/tufin/oasdiff/main/install.sh | sh -s -- -b /usr/local/bin
fi
- name: Run oasdiff and render
id: render
shell: bash
env:
BASE_SPEC: ${{ inputs.base }}
HEAD_SPEC: ${{ inputs.head }}
OUTPUT_FORMAT: ${{ inputs.format }}
run: |
set +e
# For table format, use JSON and render with jq
if [ "$OUTPUT_FORMAT" = "table" ] || [ -z "$OUTPUT_FORMAT" ]; then
DIFF=$(oasdiff changelog "$BASE_SPEC" "$HEAD_SPEC" --format json 2>&1)
EXIT_CODE=$?
else
DIFF=$(oasdiff changelog "$BASE_SPEC" "$HEAD_SPEC" --format "$OUTPUT_FORMAT" 2>&1)
EXIT_CODE=$?
fi
set -e
# Handle missing files
if [ $EXIT_CODE -ne 0 ] && echo "$DIFF" | grep -q "no such file"; then
{
echo "result<<EOF"
echo "_Spec file not found in one of the branches_"
echo "EOF"
} >> $GITHUB_OUTPUT
echo "has_changes=false" >> $GITHUB_OUTPUT
exit 0
fi
# Handle empty output
if [ -z "$DIFF" ] || [ "$DIFF" = "null" ] || [ "$DIFF" = "[]" ]; then
{
echo "result<<EOF"
echo "_No changes_"
echo "EOF"
} >> $GITHUB_OUTPUT
echo "has_changes=false" >> $GITHUB_OUTPUT
exit 0
fi
# For non-table formats, output directly with path cleanup
if [ "$OUTPUT_FORMAT" != "table" ] && [ -n "$OUTPUT_FORMAT" ]; then
DIFF=$(echo "$DIFF" | sed 's|pr/||g; s|base/||g; s|head/||g')
{
echo "result<<EOF"
echo "$DIFF"
echo "EOF"
} >> $GITHUB_OUTPUT
echo "has_changes=true" >> $GITHUB_OUTPUT
exit 0
fi
# Render JSON as markdown table using jq
RENDERED=$(echo "$DIFF" | jq -r '
if length == 0 then
"_No changes_"
else
# Count by level
(map(select(.level == 3)) | length) as $breaking |
(map(select(.level == 2)) | length) as $warnings |
(map(select(.level == 1)) | length) as $info |
(length) as $total |
# Build summary parts
([
(if $breaking > 0 then "\($breaking) breaking" else empty end),
(if $warnings > 0 then "\($warnings) warning" else empty end),
(if $info > 0 then "\($info) info" else empty end)
] | join(", ")) as $summary |
# Output summary, table header, and rows
(if $breaking > 0 then "**\($total) changes:** \($summary)" else "\($total) changes: \($summary)" end),
"",
"| | Endpoint | Change |",
"|:---:|:---|:---|",
(sort_by(-.level, .path, .operation) | .[] |
(if .level == 3 then "🔴" elif .level == 2 then "🟡" else "🟢" end) as $emoji |
(if .path then "`\(.operation)` \(.path | gsub("pr/|base/|head/"; ""))" else "_\(.section)_" end) as $endpoint |
"| \($emoji) | \($endpoint) | \(.text) |"
)
end
')
# Output results
{
echo "result<<EOF"
echo "$RENDERED"
echo "EOF"
} >> $GITHUB_OUTPUT
if [ "$RENDERED" = "_No changes_" ]; then
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "has_changes=true" >> $GITHUB_OUTPUT
fi