(
orchestrator: &Orchestrator,
api_client: &CodSpeedAPIClient,
upload_metadata: &UploadMetadata,
)
| 98 | } |
| 99 | |
| 100 | async fn retrieve_upload_data( |
| 101 | orchestrator: &Orchestrator, |
| 102 | api_client: &CodSpeedAPIClient, |
| 103 | upload_metadata: &UploadMetadata, |
| 104 | ) -> Result<UploadData> { |
| 105 | let mut upload_request = REQUEST_CLIENT |
| 106 | .post(orchestrator.config.upload_url.clone()) |
| 107 | .json(&upload_metadata); |
| 108 | if let Some(token) = api_client.token() { |
| 109 | upload_request = upload_request.header("Authorization", token.to_owned()); |
| 110 | } |
| 111 | |
| 112 | let response = upload_request.send().await; |
| 113 | |
| 114 | match response { |
| 115 | Ok(response) => { |
| 116 | if !response.status().is_success() { |
| 117 | let status = response.status(); |
| 118 | let text = response.text().await?; |
| 119 | let mut error_message = serde_json::from_str::<UploadError>(&text) |
| 120 | .map(|body| body.error) |
| 121 | .unwrap_or(text); |
| 122 | if status == StatusCode::UNAUTHORIZED { |
| 123 | let run_environment = &upload_metadata.run_environment; |
| 124 | let additional_message = match run_environment { |
| 125 | RunEnvironment::GithubActions => { |
| 126 | "Check that the workflow is correctly authenticated." |
| 127 | } |
| 128 | RunEnvironment::GitlabCi | RunEnvironment::Circleci => { |
| 129 | "Check that the CI job is correctly authenticated." |
| 130 | } |
| 131 | RunEnvironment::Buildkite => { |
| 132 | "Check that CODSPEED_TOKEN is set and has the correct value." |
| 133 | } |
| 134 | RunEnvironment::Local => { |
| 135 | "Run `codspeed auth login` to authenticate the CLI." |
| 136 | } |
| 137 | }; |
| 138 | error_message.push_str(&format!("\n\n{additional_message}")); |
| 139 | if let Some(url) = run_environment.authentication_docs_url() { |
| 140 | error_message.push_str(&format!(" View more at {url}")); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | debug!( |
| 145 | "Check that owner and repository are correct (case-sensitive!): {}/{}", |
| 146 | upload_metadata.run_environment_metadata.owner, |
| 147 | upload_metadata.run_environment_metadata.repository |
| 148 | ); |
| 149 | |
| 150 | bail!( |
| 151 | "Failed to retrieve upload data: {}\n -> {} {}", |
| 152 | status, |
| 153 | style("Reason:").bold(), |
| 154 | // 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 |
| 155 | style(error_message).red() |
| 156 | ); |
| 157 | } |
no test coverage detected