Skip to content
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
6 changes: 3 additions & 3 deletions .github/actions/perform_gitlab_smoke_test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ runs:
run: |
./${{ inputs.cli-binary-location }} test \
--org-url-slug trunk-staging-org \
--test-collection-id z2dzjLHm \
--junit-paths "${{ github.workspace }}/target/**/*junit.xml" \
--token ${{ inputs.staging-api-token }} \
--repo-url https://gitlab.com/trunk-staging-org/analytics-cli-fake-gitlab/ \
--test-collection-id z2dzjLHm \
cargo nextest run -p smoke-test --profile ci

- name: Run tests on production
Expand Down Expand Up @@ -95,10 +95,10 @@ runs:
run: |
./${{ inputs.cli-binary-location }} upload \
--org-url-slug trunk-staging-org \
--test-collection-id z2dzjLHm \
--junit-paths "${{ github.workspace }}/target/**/*junit.xml" \
--token ${{ inputs.staging-api-token }} \
--repo-url https://gitlab.com/trunk-staging-org/analytics-cli-fake-gitlab/
--repo-url https://gitlab.com/trunk-staging-org/analytics-cli-fake-gitlab/ \
--test-collection-id z2dzjLHm

- name: Upload to production
if: inputs.environment-type == 'production' || inputs.environment-type == 'both'
Expand Down
26 changes: 13 additions & 13 deletions .github/actions/perform_smoke_test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ runs:
run: |
./${{ inputs.cli-binary-location }} test \
--org-url-slug trunk-staging-org \
--test-collection-id z2dzjLHm \
--junit-paths "${{ github.workspace }}/target/**/*junit.xml" \
--token ${{ inputs.staging-api-token }} \
--test-collection-id z2dzjLHm \
cargo nextest run -p smoke-test --profile ci

- name: Run Swift tests
Expand Down Expand Up @@ -123,23 +123,23 @@ runs:
--token ${{ inputs.staging-api-token }} \
--test-collection-id Gtnr7BWl # analytics-cli-repo-wide: https://app.trunk-staging.io/trunk-staging-org/flaky-tests/collections/Gtnr7BWl/monitors

- name: Upload to staging with public repo id
id: staging-upload-public-repo-id
- name: Upload to staging from a forked pull request
id: staging-upload-forked-pr
if: always() && steps.staging-test-run.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')
env:
TRUNK_PUBLIC_API_ADDRESS: https://api.trunk-staging.io
shell: bash
run: |
./${{ inputs.cli-binary-location }} upload \
--org-url-slug trunk-staging-org \
--test-collection-id z2dzjLHm \
--junit-paths "${{ github.workspace }}/target/**/*junit.xml" \
--public-repo-id NNSvcFXb \
--allow-forked-pr-uploads \
--use-uncloned-repo \
--repo-url https://github.com/trunk-io/flake-farm-staging-fork-prs.git \
--repo-head-sha ${{ github.sha }} \
--repo-head-branch refs/heads/main \
--repo-head-author-name "Trunk Smoke Test"
--repo-head-author-name "Trunk Smoke Test" \
--test-collection-id z2dzjLHm

# upload with variant should fail because we treat variant as a separate test
- name: Upload to staging with variant
Expand Down Expand Up @@ -171,9 +171,9 @@ runs:
shell: bash
run: |
if [ "${{ inputs.test-variants }}" == "true" ]; then
echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-public-repo-id.outcome == 'success' && steps.staging-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT
echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-forked-pr.outcome == 'success' && steps.staging-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT
else
echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-public-repo-id.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT
echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-forked-pr.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT
fi

- name: Run tests on production
Expand Down Expand Up @@ -230,8 +230,8 @@ runs:
--token ${{ inputs.production-api-token }} \
--test-collection-id 4B2wfoEU

- name: Upload to production with public repo id
id: production-upload-public-repo-id
- name: Upload to production from a forked pull request
id: production-upload-forked-pr
if: always() && steps.production-test-run.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')
env:
TRUNK_PUBLIC_API_ADDRESS: https://api.trunk.io
Expand All @@ -240,7 +240,7 @@ runs:
./${{ inputs.cli-binary-location }} upload \
--org-url-slug trunk \
--junit-paths "${{ github.workspace }}/target/**/*junit.xml" \
--public-repo-id ltT0aeMC \
--allow-forked-pr-uploads \
--use-uncloned-repo \
--repo-url https://github.com/trunk-io/flake-farm-staging-fork-prs.git \
--repo-head-sha ${{ github.sha }} \
Expand Down Expand Up @@ -278,7 +278,7 @@ runs:
shell: bash
run: |
if [ "${{ inputs.test-variants }}" == "true" ]; then
echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-public-repo-id.outcome == 'success' && steps.production-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT
echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-forked-pr.outcome == 'success' && steps.production-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT
else
echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-public-repo-id.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT
echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-forked-pr.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT
fi
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ For detailed build instructions for each supported package, see their respective

```bash
cargo build
./target/debug/trunk-analytics-cli upload --org-url-slug=trunk-io --token=${API_TOKEN} --junit-paths=junit.xml
./target/debug/trunk-analytics-cli upload --org-url-slug=trunk-io --token=${API_TOKEN} --test-collection-id=${TEST_COLLECTION_ID} --junit-paths=junit.xml
```

You can generate sample junit files by running
Expand Down
164 changes: 20 additions & 144 deletions api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct ApiClient {
/// Holds the run's v2 credential: an API token from the start, or a fork pull request's Trunk
/// login once it has one.
trunk_api_v2_client: Option<Client>,
/// `--allow-forked-pr-uploads` with no token and no public repo id.
/// `--allow-forked-pr-uploads` with no token.
credential_free: bool,
/// A credential-free GitHub Actions fork PR run, which on v2 swaps its `GITHUB_TOKEN` for a login.
github_fork_pull_request: Option<GithubForkPullRequest>,
Expand Down Expand Up @@ -130,27 +130,22 @@ impl ApiClient {
// This should always be fast
const TRUNK_TELEMETRY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);
const TRUNK_API_TOKEN_HEADER: &'static str = "x-api-token";
const TRUNK_PUBLIC_REPO_ID_HEADER: &'static str = "x-trunk-public-repo-id";
const TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER: &'static str = "x-trunk-allow-forked-pr-uploads";

pub fn new(
api_token: Option<String>,
public_repo_id: Option<String>,
allow_forked_pr_uploads: bool,
org_url_slug: impl AsRef<str>,
render_sender: Option<Sender<DisplayMessage>>,
) -> anyhow::Result<ApiClient> {
let org_url_slug = String::from(org_url_slug.as_ref());

// Either a secret API token or a non-secret public repo id authenticates the
// request. Fork PRs can't read repo secrets, so they rely on the public repo id.
let api_token = api_token.filter(|token| !token.trim().is_empty());
let public_repo_id = public_repo_id.filter(|id| !id.trim().is_empty());
// The forked-PR lane presents no credential at all on v1: the collection id already in
// the request authorizes it, gated on that collection's opt-in server-side.
if api_token.is_none() && public_repo_id.is_none() && !allow_forked_pr_uploads {
if api_token.is_none() && !allow_forked_pr_uploads {
return Err(anyhow::anyhow!(
"Either a Trunk API token, a public repo id, or --allow-forked-pr-uploads is required."
"Either a Trunk API token or --allow-forked-pr-uploads is required."
));
}

Expand All @@ -161,13 +156,6 @@ impl ApiClient {
.map_err(|_| anyhow::Error::msg("Trunk API token is not ASCII"))
})
.transpose()?;
let public_repo_id_header_value = public_repo_id
.as_deref()
.map(|id| {
HeaderValue::from_str(id)
.map_err(|_| anyhow::Error::msg("Trunk public repo id is not ASCII"))
})
.transpose()?;

let api_host = get_api_host();
tracing::debug!("Using public api address {}", api_host);
Expand All @@ -193,12 +181,6 @@ impl ApiClient {
trunk_api_client_default_headers
.append(Self::TRUNK_API_TOKEN_HEADER, api_token_header_value);
}
if let Some(public_repo_id_header_value) = public_repo_id_header_value {
trunk_api_client_default_headers.append(
Self::TRUNK_PUBLIC_REPO_ID_HEADER,
public_repo_id_header_value,
);
}
if allow_forked_pr_uploads {
trunk_api_client_default_headers.append(
Self::TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER,
Expand All @@ -215,8 +197,7 @@ impl ApiClient {
.clone()
.map(|token| V2Credential::ApiKey(token).client())
.transpose()?;
let credential_free =
allow_forked_pr_uploads && api_token.is_none() && public_repo_id.is_none();
let credential_free = allow_forked_pr_uploads && api_token.is_none();

let mut telemetry_client_default_headers = HeaderMap::new();
telemetry_client_default_headers.append(
Expand Down Expand Up @@ -751,14 +732,8 @@ mod tests {

let state = mock_server_builder.spawn_mock_server().await;

let mut api_client = ApiClient::new(
Some(String::from("mock-token")),
None,
false,
"mock-org",
None,
)
.unwrap();
let mut api_client =
ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap();
api_client.api_host.clone_from(&state.host);

assert!(
Expand Down Expand Up @@ -806,14 +781,8 @@ mod tests {

let state = mock_server_builder.spawn_mock_server().await;

let mut api_client = ApiClient::new(
Some(String::from("mock-token")),
None,
false,
"mock-org",
None,
)
.unwrap();
let mut api_client =
ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap();
api_client.api_host.clone_from(&state.host);

assert!(
Expand Down Expand Up @@ -856,14 +825,8 @@ mod tests {

let state = mock_server_builder.spawn_mock_server().await;

let mut api_client = ApiClient::new(
Some(String::from("mock-token")),
None,
false,
"mock-org",
None,
)
.unwrap();
let mut api_client =
ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap();
api_client.api_host.clone_from(&state.host);

assert!(
Expand All @@ -889,103 +852,19 @@ mod tests {
}

#[test]
fn requires_a_token_or_public_repo_id() {
// Neither credential is an error.
let err = ApiClient::new(None, None, false, "mock-org", None)
fn requires_a_token_or_allow_forked_pr_uploads() {
let err = ApiClient::new(None, false, "mock-org", None)
.err()
.expect("expected an error when no credentials are provided");
assert!(err.to_string().contains(
"Either a Trunk API token, a public repo id, or --allow-forked-pr-uploads is required"
));

// Blank values are treated as absent.
assert!(
ApiClient::new(
Some(String::from(" ")),
Some(String::from(" ")),
false,
"mock-org",
None
)
.is_err()
err.to_string()
.contains("Either a Trunk API token or --allow-forked-pr-uploads is required")
);

// A token alone is sufficient - no public repo id required.
assert!(ApiClient::new(Some(String::from("token")), None, false, "mock-org", None).is_ok());

// A public repo id alone is sufficient - no token required.
assert!(
ApiClient::new(
None,
Some(String::from("public-repo-id")),
false,
"mock-org",
None
)
.is_ok()
);
}

#[tokio::test(start_paused = true)]
async fn sends_public_repo_id_header_in_place_of_token() {
let mut mock_server_builder = MockServerBuilder::new();

lazy_static! {
static ref CAPTURED_HEADERS: Arc<Mutex<HeaderMap>> =
Arc::new(Mutex::new(HeaderMap::new()));
}

let quarantining_config_handler = move |headers: HeaderMap| async move {
*CAPTURED_HEADERS.lock().unwrap() = headers;
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(String::from(
r#"{ "status_code": 404, "error": "not found" }"#,
))
.unwrap()
};
mock_server_builder.set_get_quarantining_config_handler(quarantining_config_handler);

let state = mock_server_builder.spawn_mock_server().await;

let mut api_client = ApiClient::new(
None,
Some(String::from("public-repo-id-123")),
false,
"mock-org",
None,
)
.unwrap();
api_client.api_host.clone_from(&state.host);

// The call itself errors (404) - we only care about the headers it sent.
let _ = api_client
.get_quarantining_config(&message::GetQuarantineConfigRequest {
repo: context::repo::RepoUrlParts {
host: String::from("host"),
owner: String::from("owner"),
name: String::from("name"),
},
org_url_slug: String::from("org_url_slug"),
test_identifiers: vec![],
remote_urls: vec![],
test_collection_short_id: None,
})
.await;
// A blank token is treated as absent.
assert!(ApiClient::new(Some(String::from(" ")), false, "mock-org", None).is_err());

let headers = CAPTURED_HEADERS.lock().unwrap();
assert_eq!(
headers
.get(super::ApiClient::TRUNK_PUBLIC_REPO_ID_HEADER)
.and_then(|value| value.to_str().ok()),
Some("public-repo-id-123")
);
assert!(
headers
.get(super::ApiClient::TRUNK_API_TOKEN_HEADER)
.is_none(),
"token header should be absent when only a public repo id is provided"
);
assert!(ApiClient::new(Some(String::from("token")), false, "mock-org", None).is_ok());
}

#[tokio::test]
Expand All @@ -1010,7 +889,7 @@ mod tests {

let state = mock_server_builder.spawn_mock_server().await;

let mut api_client = ApiClient::new(None, None, true, "mock-org", None).unwrap();
let mut api_client = ApiClient::new(None, true, "mock-org", None).unwrap();
api_client.api_host.clone_from(&state.host);

let _ = api_client
Expand Down Expand Up @@ -1039,16 +918,13 @@ mod tests {
assert!(
headers
.get(super::ApiClient::TRUNK_API_TOKEN_HEADER)
.is_none()
&& headers
.get(super::ApiClient::TRUNK_PUBLIC_REPO_ID_HEADER)
.is_none(),
.is_none(),
"no credential header should be sent on the forked-PR lane"
);
}

#[test]
fn allow_forked_pr_uploads_satisfies_the_credential_requirement() {
assert!(ApiClient::new(None, None, true, "mock-org", None).is_ok());
assert!(ApiClient::new(None, true, "mock-org", None).is_ok());
}
}
Loading
Loading