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

Method try_from

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

Source from the content-addressed store, hash-verified

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

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