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

Feat: add cronjob update #465

Merged
merged 30 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cfd3eea
simplify process
niristius Aug 1, 2024
1324697
Merge branch 'master' into feature/cronjob-update
martin-helmich Aug 22, 2024
1b7c620
chore: re-generate README
martin-helmich Aug 22, 2024
b4002a7
implement "inferred" type for cronjob update
niristius Aug 26, 2024
a463948
reduce duplication by defining cronjob flags centrally. currently bro…
niristius Aug 27, 2024
76b3211
centralize flags through Flag definitions and build payload aggregati…
niristius Aug 27, 2024
a2a5ec9
chore: re-generate README
niristius Aug 27, 2024
f63b8a2
Set useful summaries and descriptions for helptext generation.
niristius Aug 27, 2024
780ba66
Merge remote-tracking branch 'origin/feature/cronjob-update' into fea…
niristius Aug 27, 2024
a80471a
chore: re-generate README
niristius Aug 27, 2024
9488423
Merge remote-tracking branch 'origin/master' into feature/cronjob-update
niristius Aug 27, 2024
51d6b92
Merge remote-tracking branch 'origin/feature/cronjob-update' into fea…
niristius Aug 27, 2024
6001358
Merge branch 'master' into feature/cronjob-update
niristius Aug 28, 2024
2e98a63
split active flag into enable and disable where applicable map interp…
niristius Aug 29, 2024
83930ce
Merge remote-tracking branch 'origin/master' into feature/cronjob-update
martin-helmich Aug 29, 2024
d578838
chore: re-generate README
martin-helmich Aug 29, 2024
5045de2
handle url/command/flag overlap through flags, modularize destination…
niristius Aug 30, 2024
04cdad2
chore: re-generate README
niristius Aug 30, 2024
9887cf1
Merge branch 'master' into feature/cronjob-update
niristius Sep 3, 2024
549f1d9
reimplement Duration for timeout and throw speaking error when limit …
niristius Sep 3, 2024
17039cb
Merge branch 'master' into feature/cronjob-update
niristius Sep 3, 2024
1f304f5
chore: re-generate README
niristius Sep 3, 2024
e8855e3
remove now redundant flag
niristius Sep 3, 2024
0845dc6
Merge remote-tracking branch 'origin/feature/cronjob-update' into fea…
niristius Sep 3, 2024
a56f629
Update src/commands/cronjob/update.tsx
niristius Sep 4, 2024
87520e0
move destination parse error into the buildCronjobDestination functio…
niristius Sep 4, 2024
75f7088
Merge remote-tracking branch 'origin/feature/cronjob-update' into fea…
niristius Sep 4, 2024
32f66fc
Merge branch 'master' into feature/cronjob-update
niristius Sep 4, 2024
010c140
optimize code and remove timeout value check
niristius Sep 4, 2024
402b77c
Merge branch 'master' into feature/cronjob-update
niristius Sep 5, 2024
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
31 changes: 9 additions & 22 deletions src/commands/cronjob/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ import { appInstallationFlags } from "../../lib/resources/app/flags.js";
import { cronjobFlagDefinitions } from "../../lib/resources/cronjob/flags.js";
import { buildCronjobDestination } from "../../lib/resources/cronjob/destination.js";
import Duration from "../../lib/units/Duration.js";
import type { MittwaldAPIV2Client } from "@mittwald/api-client";
import { Flags } from "@oclif/core";
import { checkTimeout } from "../../lib/resources/cronjob/timeout.js";

type Result = {
cronjobId: string;
};

type CronjobCreationData = Parameters<
MittwaldAPIV2Client["cronjob"]["createCronjob"]
>[0]["data"];

export class Create extends ExecRenderBaseCommand<typeof Create, Result> {
static summary = "Create a new cron job";
static flags = {
Expand Down Expand Up @@ -73,8 +67,6 @@ export class Create extends ExecRenderBaseCommand<typeof Create, Result> {
timeout,
} = this.flags;

checkTimeout(timeout);

const { projectId } = await p.runStep("fetching project", async () => {
const r = await this.apiClient.app.getAppinstallation({
appInstallationId,
Expand All @@ -87,23 +79,18 @@ export class Create extends ExecRenderBaseCommand<typeof Create, Result> {
throw new Error("no project found for app installation");
}

const destination: CronjobCreationData["destination"] =
buildCronjobDestination(url, command, interpreter);

const cronjobCreationData: CronjobCreationData = {
appId: appInstallationId,
active: !disable,
description,
interval,
email,
destination,
timeout: timeout.seconds,
};

const { id: cronjobId } = await p.runStep("creating cron job", async () => {
const r = await this.apiClient.cronjob.createCronjob({
projectId,
data: cronjobCreationData,
data: {
appId: appInstallationId,
active: !disable,
description,
interval,
email,
destination: buildCronjobDestination(url, command, interpreter),
timeout: timeout.seconds,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason I was under the impression that if a key has an undefined value, currently set values for conjobs (or whichever entity is being updated) will be overwritten, I played around for a bit and it seems to not be the case wich makes this approach the most concise.

});

assertStatus(r, 201);
Expand Down
40 changes: 12 additions & 28 deletions src/commands/cronjob/update.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import {
} from "../../rendering/process/process_flags.js";
import { Success } from "../../rendering/react/components/Success.js";
import assertSuccess from "../../lib/apiutil/assert_success.js";
import type { MittwaldAPIV2Client } from "@mittwald/api-client";
import { cronjobFlagDefinitions } from "../../lib/resources/cronjob/flags.js";
import { buildCronjobDestination } from "../../lib/resources/cronjob/destination.js";
import Duration from "../../lib/units/Duration.js";
import { checkTimeout } from "../../lib/resources/cronjob/timeout.js";

type UpdateResult = void;
type CronjobUpdateData = Parameters<
MittwaldAPIV2Client["cronjob"]["updateCronjob"]
>[0]["data"];

export default class Update extends ExecRenderBaseCommand<
typeof Update,
Expand Down Expand Up @@ -86,28 +81,7 @@ export default class Update extends ExecRenderBaseCommand<
disable,
} = this.flags;

if (timeout) {
checkTimeout(timeout);
}

let destination = undefined;
if (url || command) {
destination = buildCronjobDestination(url, command, interpreter);
}

const updateCronjobPayload: CronjobUpdateData = {
...(destination !== undefined && {
destination,
}),
...(enable && { active: true }),
...(disable && { active: false }),
...(description && { description }),
...(timeout && { timeout: timeout.seconds }),
...(email && { email }),
...(interval && { interval }),
};

if (Object.keys(updateCronjobPayload).length == 0) {
if (Object.keys(this.flags).length == 0) {
await process.complete(
<Success>Nothing to change. Have a good day!</Success>,
);
Expand All @@ -117,7 +91,17 @@ export default class Update extends ExecRenderBaseCommand<
await process.runStep("Updating cron job", async () => {
const response = await this.apiClient.cronjob.updateCronjob({
cronjobId,
data: updateCronjobPayload,
data: {
destination:
url || command
? buildCronjobDestination(url, command, interpreter)
: undefined,
active: enable ? true : disable ? false : undefined,
description,
timeout: timeout?.seconds,
email,
interval,
},
});
assertSuccess(response);
});
Expand Down
10 changes: 0 additions & 10 deletions src/lib/resources/cronjob/timeout.ts

This file was deleted.