MCPcopy Create free account
hub / github.com/CodSpeedHQ/codspeed / try_from

Method try_from

src/run_environment/github_actions/provider.rs:73–152  ·  view source on GitHub ↗
(config: &OrchestratorConfig)

Source from the content-addressed store, hash-verified

71impl TryFrom<&OrchestratorConfig> for GitHubActionsProvider {
72 type Error = Error;
73 fn try_from(config: &OrchestratorConfig) -> Result<Self> {
74 if config.repository_override.is_some() {
75 bail!("Specifying owner and repository from CLI is not supported for Github Actions");
76 }
77 let (owner, repository) = Self::get_owner_and_repository()?;
78
79 let github_event_path = get_env_variable("GITHUB_EVENT_PATH")?;
80 let github_event = fs::read_to_string(github_event_path)?;
81 let github_event: Value =
82 serde_json::from_str(&github_event).expect("GITHUB_EVENT_PATH file could not be read");
83
84 let ref_ = get_env_variable("GITHUB_REF")?;
85 let is_pr = PR_REF_REGEX.is_match(&ref_);
86
87 let is_repository_private = github_event["repository"]["private"].as_bool().unwrap();
88
89 let (head_ref, is_head_repo_fork) = if is_pr {
90 let pull_request = github_event["pull_request"].as_object().unwrap();
91
92 let head_repo = pull_request["head"]["repo"].as_object().unwrap();
93 let base_repo = pull_request["base"]["repo"].as_object().unwrap();
94
95 let is_head_repo_fork = head_repo["id"] != base_repo["id"];
96
97 let head_ref = if is_head_repo_fork {
98 format!(
99 "{}:{}",
100 head_repo["owner"]["login"].as_str().unwrap(),
101 pull_request["head"]["ref"].as_str().unwrap()
102 )
103 } else {
104 pull_request["head"]["ref"].as_str().unwrap().to_owned()
105 };
106 (Some(head_ref), is_head_repo_fork)
107 } else {
108 (None, false)
109 };
110
111 let github_event_name = get_env_variable("GITHUB_EVENT_NAME")?;
112 let event = serde_json::from_str(&format!("\"{github_event_name}\"")).context(format!(
113 "Event {github_event_name} is not supported by CodSpeed"
114 ))?;
115 let repository_root_path = match find_repository_root(&std::env::current_dir()?) {
116 Some(mut path) => {
117 // Add a trailing slash to the path
118 path.push("");
119 path.to_string_lossy().to_string()
120 }
121 None => {
122 // Fallback to GITHUB_WORKSPACE, the default repository location when using the checkout action
123 // https://docs.github.com/en/actions/reference/workflows-and-actions/variables
124 if let Ok(github_workspace) = env::var("GITHUB_WORKSPACE") {
125 format!("{github_workspace}/")
126 } else {
127 format!("/home/runner/work/{repository}/{repository}/")
128 }
129 }
130 };

Callers

nothing calls this directly

Calls 3

get_env_variableFunction · 0.85
find_repository_rootFunction · 0.85
as_strMethod · 0.80

Tested by

no test coverage detected