Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor code improvements #12

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zlocation-detail=none"]
rustflags = []
[target.x86_64-apple-darwin]
rustflags = ["-Zlocation-detail=none"]
rustflags = []
[target.aarch64-apple-darwin]
rustflags = ["-Zlocation-detail=none"]
rustflags = []
[target.x86_64-linux-unknown]
rustflags = ["-Zlocation-detail=none"]
rustflags = []
35 changes: 20 additions & 15 deletions src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use anyhow::Context;

use crate::types::{BundleUploadLocation, Repo};
use crate::types::{BundleUploadLocation, CreateBundleUploadRequest, Repo};

pub const TRUNK_API_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
pub const TRUNK_API_TOKEN_HEADER: &str = "x-api-token";
Expand All @@ -13,32 +13,37 @@ pub async fn get_bundle_upload_location(
org_slug: &str,
repo: &Repo,
) -> anyhow::Result<BundleUploadLocation> {
let req_body = serde_json::json!({
"repo": {
"host": repo.host.clone(),
"owner": repo.owner.clone(),
"name": repo.name.clone(),
},
"orgUrlSlug": org_slug.to_string(),
});

let client = reqwest::Client::new();
let resp = match client
.post(api_address)
.timeout(TRUNK_API_TIMEOUT)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.header(TRUNK_API_TOKEN_HEADER, api_token)
.json(&req_body)
.json(&CreateBundleUploadRequest {
org_url_slug: org_slug.to_owned(),
repo: repo.clone(),
})
.send()
.await
{
Ok(resp) => resp,
Err(e) => {
log::error!("Failed to create bundle upload. Status: {:?}", e.status());
if e.status() == Some(reqwest::StatusCode::UNAUTHORIZED) {
log::error!("Your Trunk token may be incorrect - find it on the Trunk app (Settings -> Manage Organization -> Organization API Token -> View).");
} else if e.status() == Some(reqwest::StatusCode::NOT_FOUND) {
log::error!("Your Trunk organization URL slug may be incorrect - find it on the Trunk app (Settings -> Manage Organization -> Organization Slug).");
match e.status() {
Some(reqwest::StatusCode::UNAUTHORIZED) => log::error!(
"Your Trunk token may be incorrect - \
find it on the Trunk app (Settings -> \
Manage Organization -> Organization \
API Token -> View)."
),
Some(reqwest::StatusCode::NOT_FOUND) => log::error!(
"Your Trunk organization URL \
slug may be incorrect - find \
it on the Trunk app (Settings \
-> Manage Organization -> \
Organization Slug)."
),
_ => ()
}
return Err(anyhow::anyhow!(e).context("Failed to create bundle upload"));
}
Expand Down
9 changes: 8 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ use serde::{Deserialize, Serialize};

use crate::scanner::{BundleRepo, FileSet};

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CreateBundleUploadRequest {
pub repo: Repo,
#[serde(rename = "orgUrlSlug")]
pub org_url_slug: String,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct BundleUploadLocation {
pub url: String,
pub key: String,
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Repo {
pub host: String,
pub owner: String,
Expand Down
Loading