Skip to content

Commit

Permalink
feat(schedule): update form and table
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jun 30, 2024
1 parent f3f64f6 commit 586a037
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 22 deletions.
4 changes: 2 additions & 2 deletions api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ definitions:
type: integer
name:
type: string
disabled:
active:
type: boolean

Schedule:
Expand All @@ -792,7 +792,7 @@ definitions:
type: integer
name:
type: string
disabled:
active:
type: boolean

ViewRequest:
Expand Down
30 changes: 30 additions & 0 deletions api/projects/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}

func SetScheduleActive(w http.ResponseWriter, r *http.Request) {
oldSchedule := context.Get(r, "schedule").(db.Schedule)

var schedule struct {
Active bool `json:"active"`
}

if !helpers.Bind(w, r, &schedule) {
return
}

err := helpers.Store(r).SetScheduleActive(oldSchedule.ProjectID, oldSchedule.ID, schedule.Active)
if err != nil {
helpers.WriteError(w, err)
return
}

helpers.EventLog(r, helpers.EventLogUpdate, helpers.EventLogItem{
UserID: helpers.UserFromContext(r).ID,
ProjectID: oldSchedule.ProjectID,
ObjectType: db.EventSchedule,
ObjectID: oldSchedule.ID,
Description: fmt.Sprintf("Schedule ID %d updated", oldSchedule.ID),
})

refreshSchedulePool(r)

w.WriteHeader(http.StatusNoContent)
}

// RemoveSchedule deletes a schedule from the database
func RemoveSchedule(w http.ResponseWriter, r *http.Request) {
schedule := context.Get(r, "schedule").(db.Schedule)
Expand Down
1 change: 1 addition & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func Route() *mux.Router {
projectScheduleManagement.Use(projects.SchedulesMiddleware)
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.GetSchedule).Methods("GET", "HEAD")
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.UpdateSchedule).Methods("PUT")
projectScheduleManagement.HandleFunc("/{schedule_id}/active", projects.SetScheduleActive).Methods("PUT")
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.RemoveSchedule).Methods("DELETE")

projectViewManagement := projectUserAPI.PathPrefix("/views").Subrouter()
Expand Down
1 change: 1 addition & 0 deletions db/Store.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type Store interface {
CreateSchedule(schedule Schedule) (Schedule, error)
UpdateSchedule(schedule Schedule) error
SetScheduleCommitHash(projectID int, scheduleID int, hash string) error
SetScheduleActive(projectID int, scheduleID int, active bool) error
GetSchedule(projectID int, scheduleID int) (Schedule, error)
DeleteSchedule(projectID int, scheduleID int) error

Expand Down
9 changes: 9 additions & 0 deletions db/bolt/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ func (d *BoltDb) DeleteSchedule(projectID int, scheduleID int) error {
})
}

func (d *BoltDb) SetScheduleActive(projectID int, scheduleID int, active bool) error {
schedule, err := d.GetSchedule(projectID, scheduleID)
if err != nil {
return err
}
schedule.Active = active
return d.updateObject(projectID, db.ScheduleProps, schedule)
}

func (d *BoltDb) SetScheduleCommitHash(projectID int, scheduleID int, hash string) error {
schedule, err := d.GetSchedule(projectID, scheduleID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion db/sql/migrations/v2.10.12.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
alter table `project__template` add `tasks` int not null default 0;
alter table `project__schedule` add `name` varchar(100);
alter table `project__schedule` add `name` varchar(100) not null default '';
alter table `project__schedule` add `active` boolean not null default true;
22 changes: 19 additions & 3 deletions db/sql/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
func (d *SqlDb) CreateSchedule(schedule db.Schedule) (newSchedule db.Schedule, err error) {
insertID, err := d.insert(
"id",
"insert into project__schedule (project_id, template_id, cron_format, repository_id)"+
"values (?, ?, ?, ?)",
"insert into project__schedule (project_id, template_id, cron_format, repository_id, `name`, `active`)"+
"values (?, ?, ?, ?, ?, ?)",
schedule.ProjectID,
schedule.TemplateID,
schedule.CronFormat,
schedule.RepositoryID)
schedule.RepositoryID,
schedule.Name,
schedule.Active)

if err != nil {
return
Expand All @@ -39,10 +41,16 @@ func (d *SqlDb) UpdateSchedule(schedule db.Schedule) error {
_, err := d.exec("update project__schedule set "+
"cron_format=?, "+
"repository_id=?, "+
"template_id=?, "+
"`name`=?, "+
"`active`=?, "+
"last_commit_hash = NULL "+
"where project_id=? and id=?",
schedule.CronFormat,
schedule.RepositoryID,
schedule.TemplateID,
schedule.Name,
schedule.Active,
schedule.ProjectID,
schedule.ID)
return err
Expand Down Expand Up @@ -89,6 +97,14 @@ func (d *SqlDb) GetTemplateSchedules(projectID int, templateID int) (schedules [
return
}

func (d *SqlDb) SetScheduleActive(projectID int, scheduleID int, active bool) error {
_, err := d.exec("update project__schedule set `active`=? where project_id=? and id=?",
active,
projectID,
scheduleID)
return err
}

func (d *SqlDb) SetScheduleCommitHash(projectID int, scheduleID int, hash string) error {
_, err := d.exec("update project__schedule set last_commit_hash=? where project_id=? and id=?",
hash,
Expand Down
46 changes: 32 additions & 14 deletions web/src/components/ScheduleForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
>{{ formError }}
</v-alert>

<!-- <v-text-field-->
<!-- v-model="item.name"-->
<!-- :label="$t('Name')"-->
<!-- :rules="[v => !!v || $t('name_required')]"-->
<!-- required-->
<!-- :disabled="formSaving"-->
<!-- class="mb-4"-->
<!-- ></v-text-field>-->
<v-text-field
v-model="item.name"
:label="$t('Name')"
:rules="[v => !!v || $t('name_required')]"
required
:disabled="formSaving"
class="mb-4"
></v-text-field>

<v-select
v-model="item.template_id"
Expand All @@ -33,7 +33,13 @@
:disabled="formSaving"
/>

<v-switch
v-model="rawCron"
label="Show cron format"
/>

<v-text-field
v-if="rawCron"
v-model="item.cron_format"
:label="$t('Cron')"
:rules="[v => !!v || $t('Cron required')]"
Expand All @@ -42,11 +48,7 @@
@input="refreshCheckboxes()"
></v-text-field>

<div class="mb-4" style="color: limegreen; font-weight: bold;">
Next run {{ nextRunTime() | formatDate }}.
</div>

<div>
<div v-if="!rawCron">
<v-select
v-model="timing"
:label="$t('Timing')"
Expand Down Expand Up @@ -143,6 +145,21 @@
</div>
</div>

<v-checkbox
v-model="item.active"
>
<template v-slot:label>
Enabled
<span
v-if="item.active"
class="ml-3"
style="color: limegreen; font-weight: bold;"
>
Next run {{ nextRunTime() | formatDate }}.
</span>
</template>
</v-checkbox>

</v-form>
</template>

Expand Down Expand Up @@ -176,6 +193,7 @@
.v-input__slot {
background: #4caf50 !important;
}
.v-label {
color: white;
}
Expand Down Expand Up @@ -298,6 +316,7 @@ export default {
days: [],
months: [],
weekdays: [],
rawCron: false,
};
},
Expand All @@ -310,7 +329,6 @@ export default {
},
methods: {
nextRunTime() {
return parser.parseExpression(this.item.cron_format).next();
},
Expand Down
32 changes: 30 additions & 2 deletions web/src/views/project/Schedule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
class="mt-4"
:items-per-page="Number.MAX_VALUE"
>
<template v-slot:item.active="{ item }">

<v-switch
v-model="item.active"
inset
@change="setActive(item.id, item.active)"
></v-switch>
</template>

<template v-slot:item.tpl_name="{ item }">
<div class="d-flex">
<router-link :to="
Expand Down Expand Up @@ -103,6 +112,7 @@
import ItemListPageBase from '@/components/ItemListPageBase';
import ScheduleForm from '@/components/ScheduleForm.vue';
import TaskList from '@/components/TaskList.vue';
import axios from 'axios';
export default {
components: { TaskList, ScheduleForm },
Expand All @@ -113,13 +123,31 @@ export default {
};
},
methods: {
async setActive(scheduleId, active) {
await axios({
method: 'put',
url: `/api/project/${this.projectId}/schedules/${scheduleId}/active`,
responseType: 'json',
data: {
active,
},
});
},
getHeaders() {
return [{
text: this.$i18n.t('Template'),
value: 'tpl_name',
text: this.$i18n.t('Active'),
value: 'active',
sortable: false,
}, {
text: this.$i18n.t('Name'),
value: 'name',
}, {
text: this.$i18n.t('Cron'),
value: 'cron_format',
}, {
text: this.$i18n.t('Template'),
value: 'tpl_name',
width: '100%',
}, {
text: this.$i18n.t('actions'),
Expand Down

0 comments on commit 586a037

Please sign in to comment.