Skip to content

Commit

Permalink
add check for api removed with deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison committed Oct 12, 2024
1 parent b46cf83 commit 4e52e6c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
45 changes: 38 additions & 7 deletions checker/check_api_removed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

const (
APIPathRemovedWithoutDeprecationId = "api-path-removed-without-deprecation"
APIPathRemovedWithDeprecationId = "api-path-removed-with-deprecation"
APIPathSunsetParseId = "api-path-sunset-parse"
APIPathRemovedBeforeSunsetId = "api-path-removed-before-sunset"
APIRemovedWithoutDeprecationId = "api-removed-without-deprecation"
APIRemovedWithDeprecationId = "api-removed-with-deprecation"
APIRemovedBeforeSunsetId = "api-removed-before-sunset"
)

Expand All @@ -29,7 +31,7 @@ func APIRemovedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSo

for operation := range diffReport.PathsDiff.Base.Value(path).Operations() {
op := diffReport.PathsDiff.Base.Value(path).GetOperation(operation)
if change := checkAPIRemoval(config, APIPathRemovedWithoutDeprecationId, APIPathRemovedBeforeSunsetId, op, operationsSources, operation, path); change != nil {
if change := checkAPIRemoval(config, true, op, operationsSources, operation, path); change != nil {
result = append(result, change)
}
}
Expand All @@ -41,7 +43,7 @@ func APIRemovedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSo
}
for _, operation := range pathItem.OperationsDiff.Deleted {
op := pathItem.Base.GetOperation(operation)
if change := checkAPIRemoval(config, APIRemovedWithoutDeprecationId, APIRemovedBeforeSunsetId, op, operationsSources, operation, path); change != nil {
if change := checkAPIRemoval(config, false, op, operationsSources, operation, path); change != nil {
result = append(result, change)
}
}
Expand All @@ -50,10 +52,10 @@ func APIRemovedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSo
return result
}

func checkAPIRemoval(config *Config, deprecationId, sunsetId string, op *openapi3.Operation, operationsSources *diff.OperationsSourcesMap, method, path string) Change {
func checkAPIRemoval(config *Config, isPath bool, op *openapi3.Operation, operationsSources *diff.OperationsSourcesMap, method, path string) Change {
if !op.Deprecated {
return NewApiChange(
deprecationId,
getWithoutDeprecationId(isPath),
config,
nil,
"",
Expand All @@ -65,8 +67,16 @@ func checkAPIRemoval(config *Config, deprecationId, sunsetId string, op *openapi
}
sunset, ok := getSunset(op.Extensions)
if !ok {
// No sunset date, allow removal
return nil
return NewApiChange(
getWithDeprecationId(isPath),
config,
nil,
"",
operationsSources,
op,
method,
path,
)
}

date, err := getSunsetDate(sunset)
Expand All @@ -76,7 +86,7 @@ func checkAPIRemoval(config *Config, deprecationId, sunsetId string, op *openapi

if civil.DateOf(time.Now()).Before(date) {
return NewApiChange(
sunsetId,
getBeforeSunsetId(isPath),
config,
[]any{date},
"",
Expand All @@ -101,3 +111,24 @@ func getAPIPathSunsetParse(config *Config, operation *openapi3.Operation, operat
path,
)
}

func getWithDeprecationId(isPath bool) string {
if isPath {
return APIPathRemovedWithDeprecationId
}

Check warning on line 118 in checker/check_api_removed.go

View check run for this annotation

Codecov / codecov/patch

checker/check_api_removed.go#L117-L118

Added lines #L117 - L118 were not covered by tests
return APIRemovedWithDeprecationId
}

func getWithoutDeprecationId(isPath bool) string {
if isPath {
return APIPathRemovedWithoutDeprecationId
}
return APIRemovedWithoutDeprecationId
}

func getBeforeSunsetId(isPath bool) string {
if isPath {
return APIPathRemovedBeforeSunsetId
}
return APIRemovedBeforeSunsetId
}
2 changes: 1 addition & 1 deletion checker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
numOfChecks = 91
numOfIds = 262
numOfIds = 264
)

func TestNewConfig(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions checker/localizations/localizations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions checker/localizations_src/en/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ endpoint-added: endpoint added
endpoint-deprecated: endpoint deprecated
endpoint-reactivated: endpoint reactivated
api-path-removed-without-deprecation: api path removed without deprecation
api-path-removed-with-deprecation: api path removed with deprecation
api-path-removed-before-sunset: api path removed before the sunset date %s
api-removed-without-deprecation: api removed without deprecation
api-removed-with-deprecation: api removed with deprecation
api-removed-before-sunset: api removed before the sunset date %s
api-operation-id-removed: api operation id %s removed and replaced with %s
api-operation-id-added: api operation id %s was added
Expand Down
4 changes: 3 additions & 1 deletion checker/localizations_src/ru/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ api-sunset-date-too-small: дата API sunset date %s слишком рання
api-path-added: API path добавлено
api-path-deprecated: API path deprecated
api-path-reactivated: API path реактивирован
api-path-removed-without-deprecation: api path удалён без процедуры deprecation
api-path-removed-without-deprecation: API path удалён без процедуры deprecation
api-path-removed-with-deprecation: API path удалён с процедурой deprecation
api-path-removed-before-sunset: API path удалён до даты sunset %s
api-removed-without-deprecation: API удалён без deprecation
api-removed-with-deprecation: API удалён с процедурой deprecation
api-removed-before-sunset: API удалёг до даты sunset %s
api-operation-id-removed: Идентификатор операции API %s удален и заменен на %s
api-tag-removed: Тег API %s удален
Expand Down
2 changes: 2 additions & 0 deletions checker/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ func GetAllRules() BackwardCompatibilityRules {
newBackwardCompatibilityRule(EndpointDeprecatedId, INFO, APIDeprecationCheck, DirectionNone, LocationNone, ActionNone),
// APIRemovedCheck
newBackwardCompatibilityRule(APIPathRemovedWithoutDeprecationId, ERR, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
newBackwardCompatibilityRule(APIPathRemovedWithDeprecationId, INFO, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
newBackwardCompatibilityRule(APIPathSunsetParseId, ERR, APIRemovedCheck, DirectionNone, LocationNone, ActionNone),
newBackwardCompatibilityRule(APIPathRemovedBeforeSunsetId, ERR, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
newBackwardCompatibilityRule(APIRemovedWithoutDeprecationId, ERR, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
newBackwardCompatibilityRule(APIRemovedWithDeprecationId, INFO, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
newBackwardCompatibilityRule(APIRemovedBeforeSunsetId, ERR, APIRemovedCheck, DirectionNone, LocationNone, ActionRemove),
// APISunsetChangedCheck
newBackwardCompatibilityRule(APISunsetDeletedId, ERR, APISunsetChangedCheck, DirectionNone, LocationNone, ActionRemove),
Expand Down

0 comments on commit 4e52e6c

Please sign in to comment.