agent_hub_runtime/core/platforms/
schedules.rs1#[path = "schedule_contracts.rs"]
2mod config;
3#[cfg(any(feature = "github", feature = "gitlab"))]
4use super::contracts::Provider;
5use crate::core::task_browser::{Binding, CliReader, TaskClient, TaskFailure};
6pub use config::{ScheduleConfig, ScheduleEdit};
7use serde::Serialize;
8#[cfg(any(feature = "github", feature = "gitlab"))]
9use serde_json::Value;
10use std::sync::atomic::AtomicBool;
11
12#[cfg(any(feature = "github", feature = "gitlab"))]
13pub(super) const MAX_PAGES: usize = 20;
14
15#[derive(Debug, Serialize)]
16pub struct ScheduleRule {
17 pub cron: String,
18 pub timezone: Option<String>,
19}
20
21#[derive(Debug, Serialize)]
22pub struct RepositorySchedule {
23 pub id: String,
24 pub title: String,
25 pub rules: Vec<ScheduleRule>,
26 pub target: Option<String>,
27 pub active: Option<bool>,
28 pub last_status: Option<String>,
29 pub last_run_id: Option<u64>,
30 pub last_run_url: Option<String>,
31 pub next_run_at: Option<String>,
32 pub owner: Option<String>,
33 pub url: String,
34}
35
36#[derive(Debug, Default, Serialize)]
37pub struct RepositorySchedules {
38 pub items: Vec<RepositorySchedule>,
39 pub limited: bool,
40}
41
42#[derive(Debug, Default, Serialize)]
43pub struct ScheduleProgress {
44 pub total: usize,
45 pub finished: usize,
46 pub running: usize,
47 pub waiting: usize,
48 pub status: Option<String>,
49 pub limited: bool,
50}
51
52#[cfg(any(feature = "github", feature = "gitlab"))]
53pub(super) enum JobState {
54 Finished,
55 Running,
56 Waiting,
57 Unknown,
58}
59
60#[cfg(any(feature = "github", feature = "gitlab"))]
61impl ScheduleProgress {
62 pub(super) fn record(&mut self, state: JobState) {
63 self.total += 1;
64 match state {
65 JobState::Finished => self.finished += 1,
66 JobState::Running => self.running += 1,
67 JobState::Waiting => self.waiting += 1,
68 JobState::Unknown => (),
69 }
70 }
71}
72
73#[cfg(any(feature = "github", feature = "gitlab"))]
74pub(super) fn field(value: &Value, key: &str) -> Option<String> {
75 value[key]
76 .as_str()
77 .filter(|value| !value.is_empty())
78 .map(str::to_owned)
79}
80
81impl<R: CliReader> TaskClient<R> {
82 pub fn schedules(
84 &self,
85 binding: &Binding,
86 cancel: &AtomicBool,
87 ) -> Result<RepositorySchedules, TaskFailure> {
88 binding.validate()?;
89 let _ = cancel;
90 match binding.provider {
91 #[cfg(feature = "gitlab")]
92 Provider::Gitlab => super::gitlab::schedules::list(self, binding, cancel),
93 #[cfg(feature = "github")]
94 Provider::Github => super::github::schedules::list(self, binding, cancel),
95 #[allow(unreachable_patterns)]
96 _ => Err(TaskFailure::new(
97 "feature_unavailable",
98 "The hosting platform is unavailable",
99 )),
100 }
101 }
102
103 pub fn run_schedule(
105 &self,
106 binding: &Binding,
107 schedule_id: u64,
108 cancel: &AtomicBool,
109 ) -> Result<(), TaskFailure> {
110 binding.validate()?;
111 let _ = (schedule_id, cancel);
112 match binding.provider {
113 #[cfg(feature = "gitlab")]
114 Provider::Gitlab => {
115 super::gitlab::schedule_management::run(self, binding, schedule_id, cancel)
116 }
117 #[allow(unreachable_patterns)]
118 _ => Err(TaskFailure::new(
119 "feature_unavailable",
120 "Schedule management is unavailable for this platform",
121 )),
122 }
123 }
124
125 pub fn schedule_config(
127 &self,
128 binding: &Binding,
129 schedule_id: u64,
130 cancel: &AtomicBool,
131 ) -> Result<ScheduleConfig, TaskFailure> {
132 binding.validate()?;
133 let _ = (schedule_id, cancel);
134 match binding.provider {
135 #[cfg(feature = "gitlab")]
136 Provider::Gitlab => {
137 super::gitlab::schedule_management::details(self, binding, schedule_id, cancel)
138 }
139 #[allow(unreachable_patterns)]
140 _ => Err(TaskFailure::new(
141 "feature_unavailable",
142 "Schedule management is unavailable for this platform",
143 )),
144 }
145 }
146
147 pub fn edit_schedule(
149 &self,
150 binding: &Binding,
151 schedule_id: u64,
152 edit: &ScheduleEdit,
153 cancel: &AtomicBool,
154 ) -> Result<ScheduleConfig, TaskFailure> {
155 binding.validate()?;
156 let _ = (schedule_id, cancel);
157 let _ = edit;
158 match binding.provider {
159 #[cfg(feature = "gitlab")]
160 Provider::Gitlab => {
161 super::gitlab::schedule_management::edit(self, binding, schedule_id, edit, cancel)
162 }
163 #[allow(unreachable_patterns)]
164 _ => Err(TaskFailure::new(
165 "feature_unavailable",
166 "Schedule management is unavailable for this platform",
167 )),
168 }
169 }
170
171 pub fn delete_schedule(
173 &self,
174 binding: &Binding,
175 schedule_id: u64,
176 cancel: &AtomicBool,
177 ) -> Result<(), TaskFailure> {
178 binding.validate()?;
179 let _ = (schedule_id, cancel);
180 match binding.provider {
181 #[cfg(feature = "gitlab")]
182 Provider::Gitlab => {
183 super::gitlab::schedule_management::delete(self, binding, schedule_id, cancel)
184 }
185 #[allow(unreachable_patterns)]
186 _ => Err(TaskFailure::new(
187 "feature_unavailable",
188 "Schedule management is unavailable for this platform",
189 )),
190 }
191 }
192
193 pub fn schedule_progress(
195 &self,
196 binding: &Binding,
197 run_id: u64,
198 cancel: &AtomicBool,
199 ) -> Result<ScheduleProgress, TaskFailure> {
200 binding.validate()?;
201 if run_id == 0 {
202 return Err(TaskFailure::new("invalid_input", "Invalid pipeline run ID"));
203 }
204 let _ = cancel;
205 match binding.provider {
206 #[cfg(feature = "gitlab")]
207 Provider::Gitlab => super::gitlab::schedules::progress(self, binding, run_id, cancel),
208 #[cfg(feature = "github")]
209 Provider::Github => super::github::schedules::progress(self, binding, run_id, cancel),
210 #[allow(unreachable_patterns)]
211 _ => Err(TaskFailure::new(
212 "feature_unavailable",
213 "The hosting platform is unavailable",
214 )),
215 }
216 }
217}