| 126 | } |
| 127 | |
| 128 | async fn retrieve_upload_data( |
| 129 | orchestrator: &Orchestrator, |
| 130 | api_client: &CodSpeedAPIClient, |
| 131 | upload_metadata: &UploadMetadata, |
| 132 | ) -> Result<UploadData> { |
| 133 | let mut upload_request = REQUEST_CLIENT |
| 134 | .post(orchestrator.config.upload_url.clone()) |
| 135 | .json(&upload_metadata); |
| 136 | if let Some(token) = api_client.token() { |
| 137 | upload_request = upload_request.header("Authorization", token.to_owned()); |
| 138 | } |
| 139 | |
| 140 | let response = upload_request.send().await; |
| 141 | |
| 142 | match response { |
| 143 | Ok(response) => { |
| 144 | if !response.status().is_success() { |
| 145 | let status = response.status(); |
| 146 | let text = response.text().await?; |
| 147 | let mut error_message = serde_json::from_str::<UploadError>(&text) |
| 148 | .map(|body| body.error) |
| 149 | .unwrap_or(text); |
| 150 | if status == StatusCode::UNAUTHORIZED { |
| 151 | let additional_message = match upload_metadata.run_environment { |
| 152 | RunEnvironment::GithubActions => { |
| 153 | "Check that the workflow is correctly authenticated. View more at https://codspeed.io/docs/integrations/ci/github-actions/configuration#authentication" |
| 154 | } |
| 155 | RunEnvironment::GitlabCi => { |
| 156 | "Check that the CI job is correctly authenticated. View more at https://codspeed.io/docs/integrations/ci/gitlab-ci/configuration#authentication" |
| 157 | } |
| 158 | RunEnvironment::Buildkite => { |
| 159 | "Check that CODSPEED_TOKEN is set and has the correct value" |
| 160 | } |
| 161 | RunEnvironment::Local => { |
| 162 | "Run `codspeed auth login` to authenticate the CLI" |
| 163 | } |
| 164 | }; |
| 165 | error_message.push_str(&format!("\n\n{additional_message}")); |
| 166 | } |
| 167 | |
| 168 | debug!( |
| 169 | "Check that owner and repository are correct (case-sensitive!): {}/{}", |
| 170 | upload_metadata.run_environment_metadata.owner, |
| 171 | upload_metadata.run_environment_metadata.repository |
| 172 | ); |
| 173 | |
| 174 | bail!( |
| 175 | "Failed to retrieve upload data: {}\n -> {} {}", |
| 176 | status, |
| 177 | style("Reason:").bold(), |
| 178 | // we have to manually apply the style to the error message, because nesting styles is not supported by the console crate: https://github.com/console-rs/console/issues/106 |
| 179 | style(error_message).red() |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | Ok(response.json().await?) |
| 184 | } |
| 185 | Err(err) => Err(err.into()), |