Skip to content

Commit 8c86325

Browse files
CLOUDP-277156: [API Platform] Lint exemptions file in PR check
1 parent 914ff72 commit 8c86325

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

tools/cli/internal/breakingchanges/exemptions.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package breakingchanges
1516

1617
import (
@@ -54,6 +55,29 @@ func isWithinExpirationDate(exemption Exemption) bool {
5455
return exemptUntil.After(date) || exemptUntil.Equal(date)
5556
}
5657

58+
func validateExemption(exemption Exemption) error {
59+
if _, err := time.Parse("2006-01-02", exemption.ExemptUntil); err != nil {
60+
return fmt.Errorf("validation error: %v", err)
61+
}
62+
63+
if err := validateField(exemption.Reason, "reason", exemption); err != nil {
64+
return err
65+
}
66+
67+
if err := validateField(exemption.BreakingChangeDescription, "breaking_change_description", exemption); err != nil {
68+
return err
69+
}
70+
71+
return validateField(exemption.ExemptUntil, "exempt_until", exemption)
72+
}
73+
74+
func validateField(fieldValue, fieldName string, exemption Exemption) error {
75+
if fieldValue == "" {
76+
return fmt.Errorf("validation error: empty value for the '%s' field is not allowed. Exemption: '%s'", fieldName, exemption)
77+
}
78+
return nil
79+
}
80+
5781
func transformComponentEntry(breakingChangeDescription string) string {
5882
if strings.Contains(breakingChangeDescription, "api-schema-removed") && !strings.Contains(breakingChangeDescription, "in components") {
5983
return fmt.Sprintf("in components %s", breakingChangeDescription)
@@ -85,6 +109,10 @@ func GetValidExemptionsList(exemptionsPath string, ignoreExpiration bool, fs afe
85109

86110
var validExemptions []Exemption
87111
for _, exemption := range exemptions {
112+
if err := validateExemption(exemption); err != nil {
113+
return nil, err
114+
}
115+
88116
if ignoreExpiration || isWithinExpirationDate(exemption) {
89117
validExemptions = append(validExemptions, exemption)
90118
}

tools/cli/internal/breakingchanges/exemptions_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,78 @@ func TestGenerateExemptionsFileWithFs(t *testing.T) {
118118
assert.Equal(t, expectedContent, string(data))
119119
})
120120
}
121+
122+
func TestValidateExemption(t *testing.T) {
123+
tests := []struct {
124+
name string
125+
exemption Exemption
126+
expectedError require.ErrorAssertionFunc
127+
}{
128+
{
129+
name: "Valid exemption",
130+
exemption: Exemption{
131+
Reason: "Some reason",
132+
BreakingChangeDescription: "Description of breaking change",
133+
HideFromChangelog: "false",
134+
ExemptUntil: "2024-12-11",
135+
},
136+
expectedError: require.NoError,
137+
},
138+
{
139+
name: "Invalid date format",
140+
exemption: Exemption{
141+
Reason: "Some reason",
142+
BreakingChangeDescription: "Description of breaking change",
143+
HideFromChangelog: "false",
144+
ExemptUntil: "invalid-date",
145+
},
146+
expectedError: require.Error,
147+
},
148+
{
149+
name: "Empty Reason field",
150+
exemption: Exemption{
151+
Reason: "",
152+
BreakingChangeDescription: "Description of breaking change",
153+
HideFromChangelog: "false",
154+
ExemptUntil: "2024-12-11",
155+
},
156+
expectedError: require.Error,
157+
},
158+
{
159+
name: "Empty BreakingChangeDescription field",
160+
exemption: Exemption{
161+
Reason: "Some reason",
162+
BreakingChangeDescription: "",
163+
HideFromChangelog: "false",
164+
ExemptUntil: "2024-12-11",
165+
},
166+
expectedError: require.Error,
167+
},
168+
{
169+
name: "Empty HideFromChangelog field",
170+
exemption: Exemption{
171+
Reason: "Some reason",
172+
BreakingChangeDescription: "Description of breaking change",
173+
HideFromChangelog: "",
174+
ExemptUntil: "2024-12-11",
175+
},
176+
expectedError: require.NoError,
177+
},
178+
{
179+
name: "Empty ExemptUntil field",
180+
exemption: Exemption{
181+
Reason: "Some reason",
182+
BreakingChangeDescription: "Description of breaking change",
183+
HideFromChangelog: "false",
184+
ExemptUntil: "",
185+
},
186+
expectedError: require.Error,
187+
},
188+
}
189+
190+
for _, tt := range tests {
191+
t.Run(tt.name, func(t *testing.T) {
192+
tt.expectedError(t, validateExemption(tt.exemption))
193+
})
194+
}
195+
}

tools/cli/internal/cli/breakingchanges/exemptions/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (o *Opts) PreRunE(_ []string) error {
5151
return nil
5252
}
5353

54-
// Builder builds the merge command with the following signature:
54+
// ParseBuilder builds the merge command with the following signature:
5555
// breaking-changes exemptions parse -e file_path
5656
func ParseBuilder() *cobra.Command {
5757
opts := &Opts{

tools/cli/test/data/changelog/new-api-version/exemptions.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,7 @@
17171717
"hide_from_changelog": "true"
17181718
- "breaking_change_description": "DELETE /api/atlas/v2/orgs/{orgId}/serviceAccounts/{serviceAccountId}/secrets/{secretId} api path removed without deprecation [api-path-removed-without-deprecation]"
17191719
"exempt_until": "2024-06-01"
1720+
"reason": "Spec correction"
17201721
- "breaking_change_description": "removed the schema 'DiskBackupBaseRestoreMember' [api-schema-removed]"
17211722
"exempt_until": "2024-05-30"
17221723
"reason": "Spec correction"

tools/cli/test/data/changelog/same-api-version/exemptions.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,7 @@
17171717
"hide_from_changelog": "true"
17181718
- "breaking_change_description": "DELETE /api/atlas/v2/orgs/{orgId}/serviceAccounts/{serviceAccountId}/secrets/{secretId} api path removed without deprecation [api-path-removed-without-deprecation]"
17191719
"exempt_until": "2024-06-01"
1720+
"reason": "Spec correction"
17201721
- "breaking_change_description": "removed the schema 'DiskBackupBaseRestoreMember' [api-schema-removed]"
17211722
"exempt_until": "2024-05-30"
17221723
"reason": "Spec correction"

0 commit comments

Comments
 (0)