-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcheck_api_sunset_changed.go
129 lines (110 loc) · 2.96 KB
/
check_api_sunset_changed.go
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
package checker
import (
"time"
"cloud.google.com/go/civil"
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/diff"
)
const (
APISunsetDeletedId = "sunset-deleted"
APISunsetDateChangedTooSmallId = "api-sunset-date-changed-too-small"
)
func APISunsetChangedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSourcesMap, config *Config) Changes {
result := make(Changes, 0)
if diffReport.PathsDiff == nil {
return result
}
for path, pathItem := range diffReport.PathsDiff.Modified {
if pathItem.OperationsDiff == nil {
continue
}
for operation, operationDiff := range pathItem.OperationsDiff.Modified {
opRevision := pathItem.Revision.GetOperation(operation)
opBase := pathItem.Base.GetOperation(operation)
if !opRevision.Deprecated {
continue
}
if operationDiff.ExtensionsDiff == nil {
continue
}
if operationDiff.ExtensionsDiff.Deleted.Contains(diff.SunsetExtension) {
result = append(result, NewApiChange(
APISunsetDeletedId,
config,
nil,
"",
operationsSources,
opRevision,
operation,
path,
))
continue
}
if _, ok := operationDiff.ExtensionsDiff.Modified[diff.SunsetExtension]; !ok {
continue
}
date, err := getSunsetDate(opRevision.Extensions[diff.SunsetExtension])
if err != nil {
result = append(result, getAPIPathSunsetParse(config, opRevision, operationsSources, path, operation, err))
continue
}
baseDate, err := getSunsetDate(opBase.Extensions[diff.SunsetExtension])
if err != nil {
result = append(result, getAPIPathSunsetParse(config, opBase, operationsSources, path, operation, err))
continue
}
days := date.DaysSince(civil.DateOf(time.Now()))
stability, err := getStabilityLevel(opRevision.Extensions)
if err != nil {
// handled in CheckBackwardCompatibility
continue
}
deprecationDays := getDeprecationDays(config, stability)
if baseDate.After(date) && days < int(deprecationDays) {
result = append(result, NewApiChange(
APISunsetDateChangedTooSmallId,
config,
[]any{baseDate, date, baseDate, deprecationDays},
"",
operationsSources,
opRevision,
operation,
path,
))
}
}
}
return result
}
const (
STABILITY_DRAFT = "draft"
STABILITY_ALPHA = "alpha"
STABILITY_BETA = "beta"
STABILITY_STABLE = "stable"
)
func getDeprecationDays(config *Config, stability string) uint {
switch stability {
case STABILITY_DRAFT:
return 0
case STABILITY_ALPHA:
return 0
case STABILITY_BETA:
return config.MinSunsetBetaDays
case STABILITY_STABLE:
return config.MinSunsetStableDays
default:
return config.MinSunsetStableDays
}
}
func getAPIDeprecatedSunsetMissing(config *Config, operation *openapi3.Operation, operationsSources *diff.OperationsSourcesMap, method string, path string) Change {
return NewApiChange(
APIDeprecatedSunsetMissingId,
config,
nil,
"",
operationsSources,
operation,
method,
path,
)
}