-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoGuard.gitlab-ci.yml
More file actions
147 lines (118 loc) · 4.05 KB
/
Copy pathAutoGuard.gitlab-ci.yml
File metadata and controls
147 lines (118 loc) · 4.05 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
spec:
inputs:
source_dir:
base_ref:
type: string
default: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
head_ref:
type: string
default: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
fail_on_breaking:
type: string
default: "true"
description: "If true, pipeline fails on breaking changes. If false, breaking changes are reported and then passed"
---
.autoGuard:
image: "docker:latest"
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
before_script:
- docker pull alexx882/auto-oas:1.2
- docker pull djamn/openapi-diff:latest
- apk add --no-cache git
script:
- git fetch --all
- mkdir -p autooas-base
- mkdir -p autooas-head
- chmod -R 777 autooas-base autooas-head
- git checkout $[[inputs.base_ref]]
- echo "Running Base Analysis..."
- |
if [ -d "$[[ inputs.source_dir ]]" ]; then
docker run -v "$(pwd):/project" alexx882/auto-oas:1.2 "/project/$[[ inputs.source_dir ]]" /project/autooas-base/openapi
else
echo "Directory '$[[ inputs.source_dir ]]' not found in base ref, treating as new project."
fi
- git checkout $[[inputs.head_ref]]
- echo "Running Head Analysis..."
- docker run -v "$(pwd):/project" alexx882/auto-oas:1.2 "/project/$[[ inputs.source_dir ]]" /project/autooas-head/openapi
- |
OLD_DIR=autooas-base
NEW_DIR=autooas-head
EMPTY_OAS='{ "openapi": "3.0.1", "info": { "title": "", "version": "" }, "paths": {}, "components": { "schemas": {} } }'
echo $EMPTY_OAS > empty.json
OLD_FILES=$(find "$OLD_DIR" -type f | sed "s|$OLD_DIR/||" | sort)
NEW_FILES=$(find "$NEW_DIR" -type f | sed "s|$NEW_DIR/||" | sort)
printf "%s\n" $OLD_FILES > old_files
printf "%s\n" $NEW_FILES > new_files
COMMON_FILES=$(comm -12 old_files new_files)
OTHER_FILES=$(comm -3 old_files new_files)
STATUS=0
mkdir -p oas_diff
run_diff() {
local name=$1
local old_path=$2
local new_path=$3
echo "Diffing $name..."
set +e
STATE=$(docker run --rm \
-v "$(pwd):/specs" \
openapitools/openapi-diff:latest \
--state \
--fail-on-incompatible \
--html "/specs/oas_diff/$name.html" \
--markdown "/specs/oas_diff/$name.md" \
"/specs/$old_path" "/specs/$new_path" 2>/dev/null)
EXIT_CODE=$?
STATE=$(echo "$STATE" | tr -d '\r')
if [ "$STATE" == "no_changes" ]; then
echo "No changes detected. Removing report files."
rm -f "oas_diff/$name.html"
rm -f "oas_diff/$name.md"
fi
set -e
if [ $EXIT_CODE -ne 0 ] || [ "$STATE" == "incompatible" ]; then
echo -e "\e[31mBreaking change detected in $name\e[0m"
return 1
fi
if [ "$STATE" == "compatible" ]; then
echo -e "\e[33mCompatible changes detected in $name\e[0m"
return 0
fi
return 0
}
if [ -z "$COMMON_FILES" ] && [ -z "$OTHER_FILES" ]; then
echo "ERROR: No OpenAPI files were generated. Analysis failed."
exit 1
fi
for p in $COMMON_FILES; do
run_diff "$p" "$OLD_DIR/$p" $NEW_DIR/$p || STATUS=$((STATUS + 1))
done
for p in $OTHER_FILES; do
if [ -f "$OLD_DIR/$p" ]; then
run_diff "$p" "$OLD_DIR/$p" "empty.json" || STATUS=$((STATUS + 1))
else
run_diff "$p" "empty.json" "$NEW_DIR/$p" || STATUS=$((STATUS + 1))
fi
done
if [ $STATUS -gt 0 ]; then
if [ "$[[ inputs.fail_on_breaking ]]" = "true" ]; then
echo "Fail on breaking is ENABLED. Failing job."
exit 1
else
echo "Fail on breaking is DISABLED. Job passes with warnings."
exit 0
fi
else
echo -e "\e[32mNo breaking changes found.\e[0m"
exit 0
fi
artifacts:
when: always
name: "OpenAPI specifications"
paths:
- "autooas-base/*.json"
- "autooas-head/*.json"
- "oas_diff/*"