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 Self {
160 name,
161 step_id: step
162 .value
163 .id
164 .clone()
165 .expect("Steps that produce outputs must have an ID"),
166 }
167 }
168
169 pub fn expr(&self) -> String {
170 format!("steps.{}.outputs.{}", self.step_id, self.name)
171 }
172
173 pub fn as_job_output(self, job: &NamedJob) -> JobOutput {
174 JobOutput {
175 job_name: job.name.clone(),
176 name: self.name,
177 }
178 }
179}
180
181impl serde::Serialize for StepOutput {
182 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
183 where
184 S: serde::Serializer,
185 {
186 serializer.serialize_str(&self.to_string())
187 }
188}
189
190impl std::fmt::Display for StepOutput {
191 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192 write!(f, "${{{{ {} }}}}", self.expr())
193 }
194}
195
196pub(crate) struct JobOutput {
197 job_name: String,
198 name: &'static str,
199}
200
201impl JobOutput {
202 pub fn expr(&self) -> String {
203 format!("needs.{}.outputs.{}", self.job_name, self.name)
204 }
205}
206
207impl serde::Serialize for JobOutput {
208 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
209 where
210 S: serde::Serializer,
211 {
212 serializer.serialize_str(&self.to_string())
213 }
214}
215
216impl std::fmt::Display for JobOutput {
217 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
218 write!(f, "${{{{ {} }}}}", self.expr())
219 }
220}
221
222pub struct WorkflowInput {
223 pub input_type: &'static str,
224 pub name: &'static str,
225 pub default: Option<String>,
226 pub description: Option<String>,
227}
228
229impl WorkflowInput {
230 pub fn string(name: &'static str, default: Option<String>) -> Self {
231 Self {
232 input_type: "string",
233 name,
234 default,
235 description: None,
236 }
237 }
238
239 pub fn bool(name: &'static str, default: Option<bool>) -> Self {
240 Self {
241 input_type: "boolean",
242 name,
243 default: default.as_ref().map(ToString::to_string),
244 description: None,
245 }
246 }
247
248 pub fn description(mut self, description: impl ToString) -> Self {
249 self.description = Some(description.to_string());
250 self
251 }
252
253 pub fn input(&self) -> WorkflowDispatchInput {
254 WorkflowDispatchInput {
255 description: self
256 .description
257 .clone()
258 .unwrap_or_else(|| self.name.to_owned()),
259 required: self.default.is_none(),
260 input_type: self.input_type.to_owned(),
261 default: self.default.clone(),
262 }
263 }
264
265 pub fn call_input(&self) -> WorkflowCallInput {
266 WorkflowCallInput {
267 description: self.name.to_owned(),
268 required: self.default.is_none(),
269 input_type: self.input_type.to_owned(),
270 default: self.default.clone(),
271 }
272 }
273
274 pub(crate) fn expr(&self) -> String {
275 format!("inputs.{}", self.name)
276 }
277}
278
279impl std::fmt::Display for WorkflowInput {
280 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
281 write!(f, "${{{{ {} }}}}", self.expr())
282 }
283}
284
285impl serde::Serialize for WorkflowInput {
286 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
287 where
288 S: serde::Serializer,
289 {
290 serializer.serialize_str(&self.to_string())
291 }
292}
293
294pub(crate) struct WorkflowSecret {
295 pub name: &'static str,
296 description: String,
297 required: bool,
298}
299
300impl WorkflowSecret {
301 pub fn new(name: &'static str, description: impl ToString) -> Self {
302 Self {
303 name,
304 description: description.to_string(),
305 required: true,
306 }
307 }
308
309 pub fn secret_configuration(&self) -> WorkflowCallSecret {
310 WorkflowCallSecret {
311 description: self.description.clone(),
312 required: self.required,
313 }
314 }
315}
316
317impl std::fmt::Display for WorkflowSecret {
318 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319 write!(f, "${{{{ secrets.{} }}}}", self.name)
320 }
321}
322
323impl serde::Serialize for WorkflowSecret {
324 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
325 where
326 S: serde::Serializer,
327 {
328 serializer.serialize_str(&self.to_string())
329 }
330}
331
332pub mod assets {
333 // NOTE: these asset names also exist in the zed.dev codebase.
334 pub const MAC_AARCH64: &str = "Zed-aarch64.dmg";
335 pub const MAC_X86_64: &str = "Zed-x86_64.dmg";
336 pub const LINUX_AARCH64: &str = "zed-linux-aarch64.tar.gz";
337 pub const LINUX_X86_64: &str = "zed-linux-x86_64.tar.gz";
338 pub const WINDOWS_X86_64: &str = "Zed-x86_64.exe";
339 pub const WINDOWS_AARCH64: &str = "Zed-aarch64.exe";
340
341 pub const REMOTE_SERVER_MAC_AARCH64: &str = "zed-remote-server-macos-aarch64.gz";
342 pub const REMOTE_SERVER_MAC_X86_64: &str = "zed-remote-server-macos-x86_64.gz";
343 pub const REMOTE_SERVER_LINUX_AARCH64: &str = "zed-remote-server-linux-aarch64.gz";
344 pub const REMOTE_SERVER_LINUX_X86_64: &str = "zed-remote-server-linux-x86_64.gz";
345 pub const REMOTE_SERVER_WINDOWS_AARCH64: &str = "zed-remote-server-windows-aarch64.zip";
346 pub const REMOTE_SERVER_WINDOWS_X86_64: &str = "zed-remote-server-windows-x86_64.zip";
347
348 pub fn all() -> Vec<&'static str> {
349 vec![
350 MAC_AARCH64,
351 MAC_X86_64,
352 LINUX_AARCH64,
353 LINUX_X86_64,
354 WINDOWS_X86_64,
355 WINDOWS_AARCH64,
356 REMOTE_SERVER_MAC_AARCH64,
357 REMOTE_SERVER_MAC_X86_64,
358 REMOTE_SERVER_LINUX_AARCH64,
359 REMOTE_SERVER_LINUX_X86_64,
360 REMOTE_SERVER_WINDOWS_AARCH64,
361 REMOTE_SERVER_WINDOWS_X86_64,
362 ]
363 }
364}