1use std::cell::RefCell;
2
3use gh_workflow::{
4 Concurrency, Env, Expression, Step, WorkflowCallInput, WorkflowCallSecret,
5 WorkflowDispatchInput,
6};
7
8use crate::tasks::workflows::{runners::Platform, steps::NamedJob};
9
10macro_rules! secret {
11 ($secret_name:ident) => {
12 pub const $secret_name: &str = concat!("${{ secrets.", stringify!($secret_name), " }}");
13 };
14}
15
16macro_rules! var {
17 ($var_name:ident) => {
18 pub const $var_name: &str = concat!("${{ vars.", stringify!($var_name), " }}");
19 };
20}
21
22secret!(ANTHROPIC_API_KEY);
23secret!(OPENAI_API_KEY);
24secret!(GOOGLE_AI_API_KEY);
25secret!(GOOGLE_CLOUD_PROJECT);
26secret!(APPLE_NOTARIZATION_ISSUER_ID);
27secret!(APPLE_NOTARIZATION_KEY);
28secret!(APPLE_NOTARIZATION_KEY_ID);
29secret!(AZURE_SIGNING_CLIENT_ID);
30secret!(AZURE_SIGNING_CLIENT_SECRET);
31secret!(AZURE_SIGNING_TENANT_ID);
32secret!(CACHIX_AUTH_TOKEN);
33secret!(CLUSTER_NAME);
34secret!(DIGITALOCEAN_ACCESS_TOKEN);
35secret!(DIGITALOCEAN_SPACES_ACCESS_KEY);
36secret!(DIGITALOCEAN_SPACES_SECRET_KEY);
37secret!(GITHUB_TOKEN);
38secret!(MACOS_CERTIFICATE);
39secret!(MACOS_CERTIFICATE_PASSWORD);
40secret!(SENTRY_AUTH_TOKEN);
41secret!(ZED_CLIENT_CHECKSUM_SEED);
42secret!(ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON);
43secret!(ZED_SENTRY_MINIDUMP_ENDPOINT);
44secret!(SLACK_APP_ZED_UNIT_EVALS_BOT_TOKEN);
45secret!(ZED_ZIPPY_APP_ID);
46secret!(ZED_ZIPPY_APP_PRIVATE_KEY);
47secret!(DISCORD_WEBHOOK_RELEASE_NOTES);
48secret!(WINGET_TOKEN);
49secret!(VERCEL_TOKEN);
50secret!(SLACK_WEBHOOK_WORKFLOW_FAILURES);
51secret!(R2_ACCOUNT_ID);
52secret!(R2_ACCESS_KEY_ID);
53secret!(R2_SECRET_ACCESS_KEY);
54
55// todo(ci) make these secrets too...
56var!(AZURE_SIGNING_ACCOUNT_NAME);
57var!(AZURE_SIGNING_CERT_PROFILE_NAME);
58var!(AZURE_SIGNING_ENDPOINT);
59
60pub fn bundle_envs(platform: Platform) -> Env {
61 let env = Env::default()
62 .add("CARGO_INCREMENTAL", 0)
63 .add("ZED_CLIENT_CHECKSUM_SEED", ZED_CLIENT_CHECKSUM_SEED)
64 .add("ZED_MINIDUMP_ENDPOINT", ZED_SENTRY_MINIDUMP_ENDPOINT);
65
66 match platform {
67 Platform::Linux => env,
68 Platform::Mac => env
69 .add("MACOS_CERTIFICATE", MACOS_CERTIFICATE)
70 .add("MACOS_CERTIFICATE_PASSWORD", MACOS_CERTIFICATE_PASSWORD)
71 .add("APPLE_NOTARIZATION_KEY", APPLE_NOTARIZATION_KEY)
72 .add("APPLE_NOTARIZATION_KEY_ID", APPLE_NOTARIZATION_KEY_ID)
73 .add("APPLE_NOTARIZATION_ISSUER_ID", APPLE_NOTARIZATION_ISSUER_ID),
74 Platform::Windows => env
75 .add("AZURE_TENANT_ID", AZURE_SIGNING_TENANT_ID)
76 .add("AZURE_CLIENT_ID", AZURE_SIGNING_CLIENT_ID)
77 .add("AZURE_CLIENT_SECRET", AZURE_SIGNING_CLIENT_SECRET)
78 .add("ACCOUNT_NAME", AZURE_SIGNING_ACCOUNT_NAME)
79 .add("CERT_PROFILE_NAME", AZURE_SIGNING_CERT_PROFILE_NAME)
80 .add("ENDPOINT", AZURE_SIGNING_ENDPOINT)
81 .add("FILE_DIGEST", "SHA256")
82 .add("TIMESTAMP_DIGEST", "SHA256")
83 .add("TIMESTAMP_SERVER", "http://timestamp.acs.microsoft.com"),
84 }
85}
86
87pub fn one_workflow_per_non_main_branch() -> Concurrency {
88 one_workflow_per_non_main_branch_and_token("")
89}
90
91pub fn one_workflow_per_non_main_branch_and_token<T: AsRef<str>>(token: T) -> Concurrency {
92 Concurrency::default()
93 .group(format!(
94 concat!(
95 "${{{{ github.workflow }}}}-${{{{ github.ref_name }}}}-",
96 "${{{{ github.ref_name == 'main' && github.sha || 'anysha' }}}}{}"
97 ),
98 token.as_ref()
99 ))
100 .cancel_in_progress(true)
101}
102
103pub(crate) fn allow_concurrent_runs() -> Concurrency {
104 Concurrency::default()
105 .group("${{ github.workflow }}-${{ github.ref_name }}-${{ github.run_id }}")
106 .cancel_in_progress(true)
107}
108
109// Represents a pattern to check for changed files and corresponding output variable
110pub struct PathCondition {
111 pub name: &'static str,
112 pub pattern: &'static str,
113 pub invert: bool,
114 pub set_by_step: RefCell<Option<String>>,
115}
116impl PathCondition {
117 pub fn new(name: &'static str, pattern: &'static str) -> Self {
118 Self {
119 name,
120 pattern,
121 invert: false,
122 set_by_step: Default::default(),
123 }
124 }
125 pub fn inverted(name: &'static str, pattern: &'static str) -> Self {
126 Self {
127 name,
128 pattern,
129 invert: true,
130 set_by_step: Default::default(),
131 }
132 }
133 pub fn guard(&self, job: NamedJob) -> NamedJob {
134 let set_by_step = self
135 .set_by_step
136 .borrow()
137 .clone()
138 .unwrap_or_else(|| panic!("condition {},is never set", self.name));
139 NamedJob {
140 name: job.name,
141 job: job
142 .job
143 .add_need(set_by_step.clone())
144 .cond(Expression::new(format!(
145 "needs.{}.outputs.{} == 'true'",
146 &set_by_step, self.name
147 ))),
148 }
149 }
150}
151
152pub(crate) struct StepOutput {
153 pub name: &'static str,
154 step_id: String,
155}
156
157impl StepOutput {
158 pub fn new<T>(step: &Step<T>, name: &'static str) -> Self {
159 let step_id = step
160 .value
161 .id
162 .clone()
163 .expect("Steps that produce outputs must have an ID");
164
165 assert!(
166 step.value
167 .run
168 .as_ref()
169 .is_none_or(|run_command| run_command.contains(name)),
170 "Step Output name {name} must occur at least once in run command with ID {step_id}!"
171 );
172
173 Self { name, step_id }
174 }
175
176 pub fn new_unchecked<T>(step: &Step<T>, name: &'static str) -> Self {
177 let step_id = step
178 .value
179 .id
180 .clone()
181 .expect("Steps that produce outputs must have an ID");
182
183 Self { name, step_id }
184 }
185
186 pub fn expr(&self) -> String {
187 format!("steps.{}.outputs.{}", self.step_id, self.name)
188 }
189
190 pub fn as_job_output(self, job: &NamedJob) -> JobOutput {
191 JobOutput {
192 job_name: job.name.clone(),
193 name: self.name,
194 }
195 }
196}
197
198impl serde::Serialize for StepOutput {
199 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
200 where
201 S: serde::Serializer,
202 {
203 serializer.serialize_str(&self.to_string())
204 }
205}
206
207impl std::fmt::Display for StepOutput {
208 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
209 write!(f, "${{{{ {} }}}}", self.expr())
210 }
211}
212
213pub(crate) struct JobOutput {
214 job_name: String,
215 name: &'static str,
216}
217
218impl JobOutput {
219 pub fn expr(&self) -> String {
220 format!("needs.{}.outputs.{}", self.job_name, self.name)
221 }
222}
223
224impl serde::Serialize for JobOutput {
225 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
226 where
227 S: serde::Serializer,
228 {
229 serializer.serialize_str(&self.to_string())
230 }
231}
232
233impl std::fmt::Display for JobOutput {
234 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235 write!(f, "${{{{ {} }}}}", self.expr())
236 }
237}
238
239pub struct WorkflowInput {
240 pub input_type: &'static str,
241 pub name: &'static str,
242 pub default: Option<String>,
243 pub description: Option<String>,
244}
245
246impl WorkflowInput {
247 pub fn string(name: &'static str, default: Option<String>) -> Self {
248 Self {
249 input_type: "string",
250 name,
251 default,
252 description: None,
253 }
254 }
255
256 pub fn bool(name: &'static str, default: Option<bool>) -> Self {
257 Self {
258 input_type: "boolean",
259 name,
260 default: default.as_ref().map(ToString::to_string),
261 description: None,
262 }
263 }
264
265 pub fn description(mut self, description: impl ToString) -> Self {
266 self.description = Some(description.to_string());
267 self
268 }
269
270 pub fn input(&self) -> WorkflowDispatchInput {
271 WorkflowDispatchInput {
272 description: self
273 .description
274 .clone()
275 .unwrap_or_else(|| self.name.to_owned()),
276 required: self.default.is_none(),
277 input_type: self.input_type.to_owned(),
278 default: self.default.clone(),
279 }
280 }
281
282 pub fn call_input(&self) -> WorkflowCallInput {
283 WorkflowCallInput {
284 description: self.name.to_owned(),
285 required: self.default.is_none(),
286 input_type: self.input_type.to_owned(),
287 default: self.default.clone(),
288 }
289 }
290
291 pub(crate) fn expr(&self) -> String {
292 format!("inputs.{}", self.name)
293 }
294}
295
296impl std::fmt::Display for WorkflowInput {
297 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
298 write!(f, "${{{{ {} }}}}", self.expr())
299 }
300}
301
302impl serde::Serialize for WorkflowInput {
303 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
304 where
305 S: serde::Serializer,
306 {
307 serializer.serialize_str(&self.to_string())
308 }
309}
310
311pub(crate) struct WorkflowSecret {
312 pub name: &'static str,
313 description: String,
314 required: bool,
315}
316
317impl WorkflowSecret {
318 pub fn new(name: &'static str, description: impl ToString) -> Self {
319 Self {
320 name,
321 description: description.to_string(),
322 required: true,
323 }
324 }
325
326 pub fn secret_configuration(&self) -> WorkflowCallSecret {
327 WorkflowCallSecret {
328 description: self.description.clone(),
329 required: self.required,
330 }
331 }
332}
333
334impl std::fmt::Display for WorkflowSecret {
335 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
336 write!(f, "${{{{ secrets.{} }}}}", self.name)
337 }
338}
339
340impl serde::Serialize for WorkflowSecret {
341 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
342 where
343 S: serde::Serializer,
344 {
345 serializer.serialize_str(&self.to_string())
346 }
347}
348
349pub mod assets {
350 // NOTE: these asset names also exist in the zed.dev codebase.
351 pub const MAC_AARCH64: &str = "Zed-aarch64.dmg";
352 pub const MAC_X86_64: &str = "Zed-x86_64.dmg";
353 pub const LINUX_AARCH64: &str = "zed-linux-aarch64.tar.gz";
354 pub const LINUX_X86_64: &str = "zed-linux-x86_64.tar.gz";
355 pub const WINDOWS_X86_64: &str = "Zed-x86_64.exe";
356 pub const WINDOWS_AARCH64: &str = "Zed-aarch64.exe";
357
358 pub const REMOTE_SERVER_MAC_AARCH64: &str = "zed-remote-server-macos-aarch64.gz";
359 pub const REMOTE_SERVER_MAC_X86_64: &str = "zed-remote-server-macos-x86_64.gz";
360 pub const REMOTE_SERVER_LINUX_AARCH64: &str = "zed-remote-server-linux-aarch64.gz";
361 pub const REMOTE_SERVER_LINUX_X86_64: &str = "zed-remote-server-linux-x86_64.gz";
362 pub const REMOTE_SERVER_WINDOWS_AARCH64: &str = "zed-remote-server-windows-aarch64.zip";
363 pub const REMOTE_SERVER_WINDOWS_X86_64: &str = "zed-remote-server-windows-x86_64.zip";
364
365 pub fn all() -> Vec<&'static str> {
366 vec![
367 MAC_AARCH64,
368 MAC_X86_64,
369 LINUX_AARCH64,
370 LINUX_X86_64,
371 WINDOWS_X86_64,
372 WINDOWS_AARCH64,
373 REMOTE_SERVER_MAC_AARCH64,
374 REMOTE_SERVER_MAC_X86_64,
375 REMOTE_SERVER_LINUX_AARCH64,
376 REMOTE_SERVER_LINUX_X86_64,
377 REMOTE_SERVER_WINDOWS_AARCH64,
378 REMOTE_SERVER_WINDOWS_X86_64,
379 ]
380 }
381}