Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Minimum release interval policy #272

Merged
merged 6 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const schema = z.object({
rolloutDuration: z.string().refine(isValidDuration, {
message: "Invalid duration pattern",
}),
minimumReleaseInterval: z.string().refine(isValidDuration, {
message: "Invalid duration pattern",
}),
});

type RolloutAndTimingProps = {
Expand All @@ -62,10 +65,15 @@ export const RolloutAndTiming: React.FC<RolloutAndTimingProps> = ({
isLoading,
}) => {
const rolloutDuration = prettyMilliseconds(environmentPolicy.rolloutDuration);
const form = useForm({
schema,
defaultValues: { ...environmentPolicy, rolloutDuration },
});
const minimumReleaseInterval = prettyMilliseconds(
environmentPolicy.minimumReleaseInterval,
);
const defaultValues = {
...environmentPolicy,
rolloutDuration,
minimumReleaseInterval,
};
const form = useForm({ schema, defaultValues });

const { fields, append, remove } = useFieldArray({
control: form.control,
Expand All @@ -79,8 +87,10 @@ export const RolloutAndTiming: React.FC<RolloutAndTimingProps> = ({
const onSubmit = form.handleSubmit((data) => {
const { releaseWindows, rolloutDuration: durationString } = data;
const rolloutDuration = ms(durationString);
const minimumReleaseInterval = ms(data.minimumReleaseInterval);
const updates = { rolloutDuration, releaseWindows, minimumReleaseInterval };
updatePolicy
.mutateAsync({ id: policyId, data: { rolloutDuration, releaseWindows } })
.mutateAsync({ id: policyId, data: updates })
Comment on lines +97 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for non-negative duration

While the form submission logic is correct, consider adding validation to ensure that minimumReleaseInterval is non-negative. This would prevent potential issues with negative durations.

-    const minimumReleaseInterval = ms(data.minimumReleaseInterval);
+    const minimumReleaseInterval = ms(data.minimumReleaseInterval);
+    if (minimumReleaseInterval < 0) {
+      throw new Error("Minimum release interval cannot be negative");
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const minimumReleaseInterval = ms(data.minimumReleaseInterval);
const updates = { rolloutDuration, releaseWindows, minimumReleaseInterval };
updatePolicy
.mutateAsync({ id: policyId, data: { rolloutDuration, releaseWindows } })
.mutateAsync({ id: policyId, data: updates })
const minimumReleaseInterval = ms(data.minimumReleaseInterval);
if (minimumReleaseInterval < 0) {
throw new Error("Minimum release interval cannot be negative");
}
const updates = { rolloutDuration, releaseWindows, minimumReleaseInterval };
updatePolicy
.mutateAsync({ id: policyId, data: updates })

.then(() => form.reset(data))
.then(() => invalidatePolicy())
.catch((e) => toast.error(e.message));
Expand Down Expand Up @@ -222,7 +232,39 @@ export const RolloutAndTiming: React.FC<RolloutAndTimingProps> = ({
<div className="flex flex-col gap-1">
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">
Spread deployments out over
Roll deployments out over
</span>
<Input
type="string"
{...field}
placeholder="1d"
className="border-b-1 h-6 w-16 text-xs"
/>
</div>
<FormMessage />
</div>
</FormControl>
</FormItem>
)}
/>

<FormField
control={form.control}
name="minimumReleaseInterval"
render={({ field }) => (
<FormItem className="space-y-4">
<div className="flex flex-col gap-1">
<FormLabel>Minimum Release Interval</FormLabel>
<FormDescription>
Setting a minimum release interval will ensure that a certain
amount of time has passed since the last active release.
</FormDescription>
</div>
<FormControl>
<div className="flex flex-col gap-1">
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">
Minimum amount of time between active releases:
</span>
<Input
type="string"
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0048_lucky_eddie_brock.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "environment_policy" ADD COLUMN "minimum_release_interval" bigint DEFAULT 0 NOT NULL;
Loading
Loading