-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathprovider.rs
More file actions
852 lines (773 loc) · 31.9 KB
/
provider.rs
File metadata and controls
852 lines (773 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use std::sync::LazyLock;
use async_trait::async_trait;
use git2::Repository;
use regex::Regex;
use serde::Deserialize;
use serde_json::Value;
use simplelog::SharedLogger;
use std::collections::BTreeMap;
use std::{env, fs};
use crate::cli::run::helpers::{find_repository_root, get_env_variable};
use crate::executor::config::{ExecutorConfig, OrchestratorConfig};
use crate::prelude::*;
use crate::request_client::OIDC_CLIENT;
use crate::run_environment::interfaces::{
GhData, RepositoryProvider, RunEnvironmentMetadata, RunEvent, Sender,
};
use crate::run_environment::provider::{RunEnvironmentDetector, RunEnvironmentProvider};
use crate::run_environment::{RunEnvironment, RunPart};
use super::logger::GithubActionLogger;
pub struct GitHubActionsProvider {
pub owner: String,
pub repository: String,
pub ref_: String,
pub head_ref: Option<String>,
pub base_ref: Option<String>,
pub sender: Option<Sender>,
pub gh_data: GhData,
pub event: RunEvent,
pub repository_root_path: String,
/// Indicates whether the head repository is a fork of the base repository.
is_head_repo_fork: bool,
/// Indicates whether the repository is private.
is_repository_private: bool,
/// OIDC configuration data necessary to request an OIDC token.
///
/// If None, OIDC is not configured for this run.
oidc_config: Option<OIDCTokenRequestData>,
}
struct OIDCTokenRequestData {
request_url: String,
request_token: String,
}
impl GitHubActionsProvider {
fn get_owner_and_repository() -> Result<(String, String)> {
let owner_and_repository = get_env_variable("GITHUB_REPOSITORY")?;
let mut owner_and_repository = owner_and_repository.split('/');
let owner = owner_and_repository.next().unwrap();
let repository = owner_and_repository.next().unwrap();
Ok((owner.into(), repository.into()))
}
}
#[derive(Deserialize)]
struct OIDCResponse {
value: Option<String>,
}
static PR_REF_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^refs/pull/(?P<pr_number>\d+)/merge$").unwrap());
impl TryFrom<&OrchestratorConfig> for GitHubActionsProvider {
type Error = Error;
fn try_from(config: &OrchestratorConfig) -> Result<Self> {
if config.repository_override.is_some() {
bail!("Specifying owner and repository from CLI is not supported for Github Actions");
}
let (owner, repository) = Self::get_owner_and_repository()?;
let github_event_path = get_env_variable("GITHUB_EVENT_PATH")?;
let github_event = fs::read_to_string(github_event_path)?;
let github_event: Value =
serde_json::from_str(&github_event).expect("GITHUB_EVENT_PATH file could not be read");
let ref_ = get_env_variable("GITHUB_REF")?;
let is_pr = PR_REF_REGEX.is_match(&ref_);
let is_repository_private = github_event["repository"]["private"].as_bool().unwrap();
let (head_ref, is_head_repo_fork) = if is_pr {
let pull_request = github_event["pull_request"].as_object().unwrap();
let head_repo = pull_request["head"]["repo"].as_object().unwrap();
let base_repo = pull_request["base"]["repo"].as_object().unwrap();
let is_head_repo_fork = head_repo["id"] != base_repo["id"];
let head_ref = if is_head_repo_fork {
format!(
"{}:{}",
head_repo["owner"]["login"].as_str().unwrap(),
pull_request["head"]["ref"].as_str().unwrap()
)
} else {
pull_request["head"]["ref"].as_str().unwrap().to_owned()
};
(Some(head_ref), is_head_repo_fork)
} else {
(None, false)
};
let github_event_name = get_env_variable("GITHUB_EVENT_NAME")?;
let event = serde_json::from_str(&format!("\"{github_event_name}\"")).context(format!(
"Event {github_event_name} is not supported by CodSpeed"
))?;
let repository_root_path = match find_repository_root(&std::env::current_dir()?) {
Some(mut path) => {
// Add a trailing slash to the path
path.push("");
path.to_string_lossy().to_string()
}
None => {
// Fallback to GITHUB_WORKSPACE, the default repository location when using the checkout action
// https://docs.github.com/en/actions/reference/workflows-and-actions/variables
if let Ok(github_workspace) = env::var("GITHUB_WORKSPACE") {
format!("{github_workspace}/")
} else {
format!("/home/runner/work/{repository}/{repository}/")
}
}
};
Ok(Self {
owner,
repository: repository.clone(),
ref_,
head_ref,
event,
gh_data: GhData {
job: get_env_variable("GITHUB_JOB")?,
run_id: get_env_variable("GITHUB_RUN_ID")?,
},
sender: Some(Sender {
login: get_env_variable("GITHUB_ACTOR")?,
id: get_env_variable("GITHUB_ACTOR_ID")?,
}),
base_ref: get_env_variable("GITHUB_BASE_REF").ok(),
repository_root_path,
is_head_repo_fork,
is_repository_private,
oidc_config: None,
})
}
}
impl RunEnvironmentDetector for GitHubActionsProvider {
fn detect() -> bool {
// check if the GITHUB_ACTIONS environment variable is set and the value is truthy
env::var("GITHUB_ACTIONS") == Ok("true".into())
}
}
#[async_trait(?Send)]
impl RunEnvironmentProvider for GitHubActionsProvider {
fn get_repository_provider(&self) -> RepositoryProvider {
RepositoryProvider::GitHub
}
fn get_logger(&self) -> Box<dyn SharedLogger> {
Box::new(GithubActionLogger::new())
}
fn get_run_environment(&self) -> RunEnvironment {
RunEnvironment::GithubActions
}
fn get_run_environment_metadata(&self) -> Result<RunEnvironmentMetadata> {
Ok(RunEnvironmentMetadata {
base_ref: self.base_ref.clone(),
head_ref: self.head_ref.clone(),
event: self.event.clone(),
gh_data: Some(self.gh_data.clone()),
gl_data: None,
local_data: None,
sender: self.sender.clone(),
owner: self.owner.clone(),
repository: self.repository.clone(),
ref_: self.ref_.clone(),
repository_root_path: self.repository_root_path.clone(),
})
}
/// For Github, the run environment run part is the most complicated
/// since we support matrix jobs.
///
/// Computing the `run_part_id`:
/// - not in a matrix:
/// - simply take the job name
/// - in a matrix:
/// - take the job name
/// - concatenate it with key-values from `matrix` and `strategy`
///
/// `GH_MATRIX` and `GH_STRATEGY` are environment variables computed by
/// https://github.com/CodSpeedHQ/action:
/// - `GH_MATRIX`: ${{ toJson(matrix) }}
/// - `GH_STRATEGY`: ${{ toJson(strategy) }}
///
/// A note on parsing:
///
/// The issue is these variables from Github Actions are multiline.
/// As we need to use them compute an identifier, we need them as a single line.
/// Plus we are interested in the content of these objects,
/// so it makes sense to parse and re-serialize them.
fn get_run_provider_run_part(&self) -> Option<RunPart> {
let job_name = self.gh_data.job.clone();
let mut metadata = BTreeMap::new();
let gh_matrix = get_env_variable("GH_MATRIX")
.ok()
.and_then(|v| serde_json::from_str::<Value>(&v).ok());
let gh_strategy = get_env_variable("GH_STRATEGY")
.ok()
.and_then(|v| serde_json::from_str::<Value>(&v).ok());
let run_part_id = if let (Some(Value::Object(matrix)), Some(Value::Object(mut strategy))) =
(gh_matrix, gh_strategy)
{
// remove useless values from the strategy
strategy.remove("fail-fast");
strategy.remove("max-parallel");
// The re-serialization is on purpose here. We want to serialize it as a single line.
let matrix_str = serde_json::to_string(&matrix).expect("Unable to re-serialize matrix");
let strategy_str =
serde_json::to_string(&strategy).expect("Unable to re-serialize strategy");
metadata.extend(matrix);
metadata.extend(strategy);
format!("{job_name}-{matrix_str}-{strategy_str}")
} else {
job_name
};
Some(RunPart {
run_id: self.gh_data.run_id.clone(),
run_part_id,
job_name: self.gh_data.job.clone(),
metadata,
})
}
fn get_commit_hash(&self, repository_root_path: &str) -> Result<String> {
let repo = Repository::open(repository_root_path).context(format!(
"Failed to open repository at path: {repository_root_path}\n\
Make sure git is installed, and that `actions/checkout` used git to fetch the repository\n\
If necessary, install git before running `actions/checkout`.\n\
If you run into permission issues when running in Docker, you may need to also run \
`git config --global --add safe.directory $GITHUB_WORKSPACE` "
))?;
let commit_hash = repo
.head()
.and_then(|head| head.peel_to_commit())
.context("Failed to get HEAD commit")?
.id()
.to_string();
Ok(commit_hash)
}
/// Validate that the environment is correctly configured for OIDC usage.
///
/// ## Logic
/// - If the user has explicitly set a token in the configuration (i.e. "static token"), inform the user that OIDC is available but do nothing.
/// - Otherwise, check if the necessary environment variables are set to use OIDC.
///
/// For Github Actions, there are two necessary environment variables:
/// - `ACTIONS_ID_TOKEN_REQUEST_TOKEN`
/// - `ACTIONS_ID_TOKEN_REQUEST_URL`
/// If environment variables are not set, this could be because:
/// - The user has misconfigured the workflow (missing `id-token` permission)
/// - The run is from a public fork, in which case GitHub Actions does not provide these environment variables for security reasons.
///
/// ## Notes
/// Retrieving the token requires that the workflow has the `id-token` permission enabled.
///
/// Docs:
/// - https://docs.github.com/en/actions/how-tos/secure-your-work/security-harden-deployments/oidc-with-reusable-workflows
/// - https://docs.github.com/en/actions/concepts/security/openid-connect
/// - https://docs.github.com/en/actions/reference/security/oidc#methods-for-requesting-the-oidc-token
fn check_oidc_configuration(&mut self, config: &OrchestratorConfig) -> Result<()> {
// Check if a static token is already set
if config.token.is_some() {
announcement!(
"OIDC Authentication",
"You can now authenticate your CI workflows using OpenID Connect (OIDC) tokens instead of `CODSPEED_TOKEN` secrets.\n\
This makes integrating and authenticating jobs safer and simpler.\n\
Learn more at https://codspeed.io/docs/integrations/ci/github-actions/configuration#oidc-recommended\n"
);
return Ok(());
}
// The `ACTIONS_ID_TOKEN_REQUEST_TOKEN` environment variable is set when the `id-token` permission is granted, which is necessary to authenticate with OIDC.
let request_token = get_env_variable("ACTIONS_ID_TOKEN_REQUEST_TOKEN").ok();
let request_url = get_env_variable("ACTIONS_ID_TOKEN_REQUEST_URL").ok();
if request_token.is_none() || request_url.is_none() {
// If the run is from a fork, it is expected that these environment variables are not set.
// We will fall back to tokenless authentication in this case.
if self.is_head_repo_fork {
return Ok(());
}
if self.is_repository_private {
bail!(
"Unable to retrieve OIDC token for authentication.\n\
Make sure your workflow has the `id-token: write` permission set.\n\
See https://codspeed.io/docs/integrations/ci/github-actions/configuration#oidc-recommended"
)
}
announcement!(
"OIDC Authentication",
"You can now authenticate your CI workflows using OpenID Connect (OIDC).\n\
This makes integrating and authenticating jobs safer and simpler.\n\
Learn more at https://codspeed.io/docs/integrations/ci/github-actions/configuration#oidc-recommended\n"
);
return Ok(());
}
let request_url = request_url.unwrap();
let request_token = request_token.unwrap();
self.oidc_config = Some(OIDCTokenRequestData {
request_url,
request_token,
});
Ok(())
}
/// Request the OIDC token from GitHub Actions if necessary.
///
/// All the validation has already been performed in `check_oidc_configuration`.
/// So if the oidc_config is None, we simply return.
async fn set_oidc_token(&self, config: &mut ExecutorConfig) -> Result<()> {
if let Some(oidc_config) = &self.oidc_config {
let request_url = format!(
"{}&audience={}",
oidc_config.request_url,
self.get_oidc_audience()
);
let token = match OIDC_CLIENT
.get(request_url)
.header("Accept", "application/json")
.header(
"Authorization",
format!("Bearer {}", oidc_config.request_token),
)
.send()
.await
{
Ok(response) => match response.json::<OIDCResponse>().await {
Ok(oidc_response) => oidc_response.value,
Err(_) => None,
},
Err(_) => None,
};
if token.is_some() {
debug!("Successfully retrieved OIDC token for authentication.");
config.set_token(token);
} else if self.is_repository_private {
bail!(
"Unable to retrieve OIDC token for authentication. \n\
Make sure your workflow has the `id-token: write` permission set. \n\
See https://codspeed.io/docs/integrations/ci/github-actions/configuration#oidc-recommended"
)
} else {
warn!("Failed to retrieve OIDC token for authentication.");
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use insta::assert_json_snapshot;
use temp_env::{with_var, with_vars};
use crate::VERSION;
use super::*;
#[test]
fn test_detect() {
with_var("GITHUB_ACTIONS", Some("true"), || {
assert!(GitHubActionsProvider::detect());
});
}
#[test]
fn test_get_owner_and_repository() {
with_var("GITHUB_REPOSITORY", Some("owner/repository"), || {
let (owner, repository) = GitHubActionsProvider::get_owner_and_repository().unwrap();
assert_eq!(owner, "owner");
assert_eq!(repository, "repository");
});
}
#[test]
fn test_try_from_push_main() {
with_vars(
[
("GITHUB_ACTOR_ID", Some("1234567890")),
("GITHUB_ACTOR", Some("actor")),
("GITHUB_BASE_REF", Some("main")),
("GITHUB_EVENT_NAME", Some("push")),
(
"GITHUB_EVENT_PATH",
Some(
format!(
"{}/src/run_environment/github_actions/samples/push-event.json",
env!("CARGO_MANIFEST_DIR")
)
.as_str(),
),
),
("GITHUB_JOB", Some("job")),
("GITHUB_REF", Some("refs/heads/main")),
("GITHUB_REPOSITORY", Some("owner/repository")),
("GITHUB_RUN_ID", Some("1234567890")),
],
|| {
let config = OrchestratorConfig {
token: Some("token".into()),
..OrchestratorConfig::test()
};
let github_actions_provider = GitHubActionsProvider::try_from(&config).unwrap();
assert_eq!(github_actions_provider.owner, "owner");
assert_eq!(github_actions_provider.repository, "repository");
assert_eq!(github_actions_provider.ref_, "refs/heads/main");
assert_eq!(github_actions_provider.base_ref, Some("main".into()));
assert_eq!(github_actions_provider.head_ref, None);
assert_eq!(github_actions_provider.event, RunEvent::Push);
assert_eq!(github_actions_provider.gh_data.job, "job");
assert_eq!(github_actions_provider.gh_data.run_id, "1234567890");
assert_eq!(
github_actions_provider.sender.as_ref().unwrap().login,
"actor"
);
assert_eq!(
github_actions_provider.sender.as_ref().unwrap().id,
"1234567890"
);
assert!(!github_actions_provider.is_head_repo_fork);
assert!(!github_actions_provider.is_repository_private);
},
)
}
#[test]
fn test_pull_request_run_environment_metadata() {
with_vars(
[
("GITHUB_ACTIONS", Some("true")),
("GITHUB_ACTOR_ID", Some("19605940")),
("GITHUB_ACTOR", Some("adriencaccia")),
("GITHUB_BASE_REF", Some("main")),
("GITHUB_EVENT_NAME", Some("pull_request")),
(
"GITHUB_EVENT_PATH",
Some(
format!(
"{}/src/run_environment/github_actions/samples/pr-event.json",
env!("CARGO_MANIFEST_DIR")
)
.as_str(),
),
),
("GITHUB_HEAD_REF", Some("feat/codspeed-runner")),
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
],
|| {
let config = OrchestratorConfig {
token: Some("token".into()),
..OrchestratorConfig::test()
};
let github_actions_provider = GitHubActionsProvider::try_from(&config).unwrap();
assert!(!github_actions_provider.is_head_repo_fork);
assert!(github_actions_provider.is_repository_private);
let run_environment_metadata = github_actions_provider
.get_run_environment_metadata()
.unwrap();
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_json_snapshot!(run_environment_metadata, {
".runner.version" => insta::dynamic_redaction(|value,_path| {
assert_eq!(value.as_str().unwrap(), VERSION.to_string());
"[version]"
}),
});
assert_json_snapshot!(run_part);
},
);
}
#[test]
fn test_fork_pull_request_run_environment_metadata() {
with_vars(
[
("GITHUB_ACTIONS", Some("true")),
("GITHUB_ACTOR_ID", Some("19605940")),
("GITHUB_ACTOR", Some("adriencaccia")),
("GITHUB_BASE_REF", Some("main")),
("GITHUB_EVENT_NAME", Some("pull_request")),
(
"GITHUB_EVENT_PATH",
Some(
format!(
"{}/src/run_environment/github_actions/samples/fork-pr-event.json",
env!("CARGO_MANIFEST_DIR")
)
.as_str(),
),
),
("GITHUB_HEAD_REF", Some("feat/codspeed-runner")),
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
("GH_MATRIX", Some("null")),
],
|| {
let config = OrchestratorConfig {
token: Some("token".into()),
..OrchestratorConfig::test()
};
let github_actions_provider = GitHubActionsProvider::try_from(&config).unwrap();
assert!(github_actions_provider.is_head_repo_fork);
assert!(!github_actions_provider.is_repository_private);
let run_environment_metadata = github_actions_provider
.get_run_environment_metadata()
.unwrap();
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_eq!(run_environment_metadata.owner, "my-org");
assert_eq!(run_environment_metadata.repository, "adrien-python-test");
assert_eq!(run_environment_metadata.base_ref, Some("main".into()));
assert_eq!(
run_environment_metadata.head_ref,
Some("fork-owner:feat/codspeed-runner".into())
);
assert_json_snapshot!(run_environment_metadata, {
".runner.version" => insta::dynamic_redaction(|value,_path| {
assert_eq!(value.as_str().unwrap(), VERSION.to_string());
"[version]"
}),
});
assert_json_snapshot!(run_part);
},
);
}
#[test]
fn test_matrix_job_run_environment_metadata() {
with_vars(
[
("GITHUB_ACTIONS", Some("true")),
("GITHUB_ACTOR_ID", Some("19605940")),
("GITHUB_ACTOR", Some("adriencaccia")),
("GITHUB_BASE_REF", Some("main")),
("GITHUB_EVENT_NAME", Some("pull_request")),
(
"GITHUB_EVENT_PATH",
Some(
format!(
"{}/src/run_environment/github_actions/samples/pr-event.json",
env!("CARGO_MANIFEST_DIR")
)
.as_str(),
),
),
("GITHUB_HEAD_REF", Some("feat/codspeed-runner")),
("GITHUB_JOB", Some("log-env")),
("GITHUB_REF", Some("refs/pull/22/merge")),
(
"GITHUB_WORKSPACE",
Some("/home/runner/work/adrien-python-test/adrien-python-test"),
),
("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")),
("GITHUB_RUN_ID", Some("6957110437")),
("VERSION", Some("0.1.0")),
(
"GH_MATRIX",
Some(
r#"{
"runner-version":"3.2.1",
"numeric-value":123456789
}"#,
),
),
(
"GH_STRATEGY",
Some(
r#"{
"fail-fast":true,
"job-index":1,
"job-total":2,
"max-parallel":2
}"#,
),
),
],
|| {
let config = OrchestratorConfig {
token: Some("token".into()),
..OrchestratorConfig::test()
};
let github_actions_provider = GitHubActionsProvider::try_from(&config).unwrap();
assert!(!github_actions_provider.is_head_repo_fork);
assert!(github_actions_provider.is_repository_private);
let run_environment_metadata = github_actions_provider
.get_run_environment_metadata()
.unwrap();
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_json_snapshot!(run_environment_metadata, {
".runner.version" => insta::dynamic_redaction(|value,_path| {
assert_eq!(value.as_str().unwrap(), VERSION.to_string());
"[version]"
}),
});
assert_json_snapshot!(run_part);
},
);
}
#[test]
fn test_get_run_part_no_matrix() {
with_vars([("GITHUB_ACTIONS", Some("true"))], || {
let github_actions_provider = GitHubActionsProvider {
owner: "owner".into(),
repository: "repository".into(),
ref_: "refs/head/my-branch".into(),
head_ref: Some("my-branch".into()),
base_ref: None,
sender: None,
gh_data: GhData {
job: "my_job".into(),
run_id: "123789".into(),
},
event: RunEvent::Push,
repository_root_path: "/home/work/my-repo".into(),
is_head_repo_fork: false,
is_repository_private: false,
oidc_config: None,
};
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_eq!(run_part.run_id, "123789");
assert_eq!(run_part.job_name, "my_job");
assert_eq!(run_part.run_part_id, "my_job");
assert_json_snapshot!(run_part.metadata, @"{}");
})
}
#[test]
fn test_get_run_part_null_matrix() {
with_vars(
[
("GH_MATRIX", Some("null")),
(
"GH_STRATEGY",
Some(
r#"{
"fail-fast":true,
"job-index":0,
"job-total":1,
"max-parallel":1
}"#,
),
),
],
|| {
let github_actions_provider = GitHubActionsProvider {
owner: "owner".into(),
repository: "repository".into(),
ref_: "refs/head/my-branch".into(),
head_ref: Some("my-branch".into()),
base_ref: None,
sender: None,
gh_data: GhData {
job: "my_job".into(),
run_id: "123789".into(),
},
event: RunEvent::Push,
repository_root_path: "/home/work/my-repo".into(),
is_head_repo_fork: false,
is_repository_private: false,
oidc_config: None,
};
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_eq!(run_part.run_id, "123789");
assert_eq!(run_part.job_name, "my_job");
assert_eq!(run_part.run_part_id, "my_job");
assert_json_snapshot!(run_part.metadata, @"{}");
},
)
}
#[test]
fn test_get_matrix_run_part() {
with_vars(
[
(
"GH_MATRIX",
Some(
r#"{
"runner-version":"3.2.1",
"numeric-value":123456789
}"#,
),
),
(
"GH_STRATEGY",
Some(
r#"{
"fail-fast":true,
"job-index":1,
"job-total":2,
"max-parallel":2
}"#,
),
),
],
|| {
let github_actions_provider = GitHubActionsProvider {
owner: "owner".into(),
repository: "repository".into(),
ref_: "refs/head/my-branch".into(),
head_ref: Some("my-branch".into()),
base_ref: None,
sender: None,
gh_data: GhData {
job: "my_job".into(),
run_id: "123789".into(),
},
event: RunEvent::Push,
repository_root_path: "/home/work/my-repo".into(),
is_head_repo_fork: false,
is_repository_private: false,
oidc_config: None,
};
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_eq!(run_part.run_id, "123789");
assert_eq!(run_part.job_name, "my_job");
assert_eq!(
run_part.run_part_id,
"my_job-{\"runner-version\":\"3.2.1\",\"numeric-value\":123456789}-{\"job-total\":2,\"job-index\":1}"
);
assert_json_snapshot!(run_part.metadata, @r#"
{
"job-index": 1,
"job-total": 2,
"numeric-value": 123456789,
"runner-version": "3.2.1"
}
"#);
},
)
}
#[test]
fn test_get_inline_matrix_run_part() {
with_vars(
[
(
"GH_MATRIX",
Some("{\"runner-version\":\"3.2.1\",\"numeric-value\":123456789}"),
),
(
"GH_STRATEGY",
Some("{\"fail-fast\":true,\"job-index\":1,\"job-total\":2,\"max-parallel\":2}"),
),
],
|| {
let github_actions_provider = GitHubActionsProvider {
owner: "owner".into(),
repository: "repository".into(),
ref_: "refs/head/my-branch".into(),
head_ref: Some("my-branch".into()),
base_ref: None,
sender: None,
gh_data: GhData {
job: "my_job".into(),
run_id: "123789".into(),
},
event: RunEvent::Push,
repository_root_path: "/home/work/my-repo".into(),
is_head_repo_fork: false,
is_repository_private: false,
oidc_config: None,
};
let run_part = github_actions_provider.get_run_provider_run_part().unwrap();
assert_eq!(run_part.run_id, "123789");
assert_eq!(run_part.job_name, "my_job");
assert_eq!(
run_part.run_part_id,
"my_job-{\"runner-version\":\"3.2.1\",\"numeric-value\":123456789}-{\"job-total\":2,\"job-index\":1}"
);
assert_json_snapshot!(run_part.metadata, @r#"
{
"job-index": 1,
"job-total": 2,
"numeric-value": 123456789,
"runner-version": "3.2.1"
}
"#);
},
)
}
}