Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 36 additions & 2 deletions context/src/env/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "ruby")]
use magnus::{value::ReprValue, Module, Object};
use magnus::{Module, Object, value::ReprValue};
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -224,12 +224,12 @@ impl<'a> CIInfoParser<'a> {
CIPlatform::Semaphore => self.parse_semaphore(),
CIPlatform::GitLabCI => self.parse_gitlab_ci(),
CIPlatform::Drone => self.parse_drone(),
CIPlatform::BitbucketPipelines => self.parse_bitbucket_pipelines(),
CIPlatform::Custom => self.parse_custom_info(),
CIPlatform::CircleCI
| CIPlatform::TravisCI
| CIPlatform::Webappio
| CIPlatform::AWSCodeBuild
| CIPlatform::BitbucketPipelines
| CIPlatform::AzurePipelines
| CIPlatform::Unknown => {
// TODO(TRUNK-12908): Switch to using a crate for parsing the CI platform and related env vars
Expand Down Expand Up @@ -429,6 +429,40 @@ impl<'a> CIInfoParser<'a> {
self.ci_info.job_url = self.get_env_var("DRONE_BUILD_LINK");
}

fn parse_bitbucket_pipelines(&mut self) {
// Construct job URL from workspace, repo slug, and build number
// Format: https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}
// With step: https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}/steps/{step_uuid}

if let (Some(workspace), Some(repo_slug), Some(build_number)) = (
self.get_env_var("BITBUCKET_WORKSPACE"),
self.get_env_var("BITBUCKET_REPO_SLUG"),
self.get_env_var("BITBUCKET_BUILD_NUMBER"),
) {
self.ci_info.job_url = Some({
let base_url = format!(
"https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}"
);
if let Some(step_uuid) = self.get_env_var("BITBUCKET_STEP_UUID") {
format!("{base_url}/steps/{step_uuid}")
} else {
base_url
}
});
}

self.ci_info.branch = self.get_env_var("BITBUCKET_BRANCH");
self.ci_info.pr_number = Self::parse_pr_number(self.get_env_var("BITBUCKET_PR_ID"));

// Use pipeline UUID as workflow identifier and step UUID as job identifier
self.ci_info.workflow = self.get_env_var("BITBUCKET_PIPELINE_UUID");
self.ci_info.job = self.get_env_var("BITBUCKET_STEP_UUID");

// Note: Bitbucket Pipelines doesn't provide author/committer info, commit message,
// or PR title via environment variables. These will be populated from repo info
// via apply_repo_overrides(), or users can set them via CUSTOM env vars.
}

fn get_env_var<T: AsRef<str>>(&self, env_var: T) -> Option<String> {
self.env_vars
.get(env_var.as_ref())
Expand Down
298 changes: 296 additions & 2 deletions context/tests/env.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an aside: we should probably think about splitting this test file up. We have much too many lines of code here

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use context::env::{
self,
self, EnvVars,
parser::{BranchClass, CIInfo, CIPlatform, EnvParser},
validator::{EnvValidationIssue, EnvValidationIssueSubOptimal, EnvValidationLevel},
EnvVars,
};

#[test]
Expand Down Expand Up @@ -962,6 +961,301 @@ fn test_simple_gitlab_stable_branches() {
);
}

#[test]
fn test_simple_bitbucket() {
let workspace = String::from("my-workspace");
let repo_slug = String::from("my-repo");
let build_number = String::from("42");
let branch = String::from("feature-branch");
let pipeline_uuid = String::from("{12345678-1234-1234-1234-123456789abc}");
let step_uuid = String::from("{abcdef12-3456-7890-abcd-ef1234567890}");

let env_vars = EnvVars::from_iter(vec![
(
String::from("BITBUCKET_BUILD_NUMBER"),
String::from(&build_number),
),
(
String::from("BITBUCKET_WORKSPACE"),
String::from(&workspace),
),
(
String::from("BITBUCKET_REPO_SLUG"),
String::from(&repo_slug),
),
(String::from("BITBUCKET_BRANCH"), String::from(&branch)),
(
String::from("BITBUCKET_PIPELINE_UUID"),
String::from(&pipeline_uuid),
),
(
String::from("BITBUCKET_STEP_UUID"),
String::from(&step_uuid),
),
]);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars, &[], None);

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::BitbucketPipelines,
job_url: Some(format!(
"https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}/steps/{step_uuid}"
)),
branch: Some(branch),
branch_class: Some(BranchClass::None),
pr_number: None,
actor: None,
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: Some(pipeline_uuid),
job: Some(step_uuid),
}
);

let env_validation = env::validator::validate(&ci_info);
assert_eq!(env_validation.max_level(), EnvValidationLevel::SubOptimal);
pretty_assertions::assert_eq!(
env_validation.issues(),
&[
EnvValidationIssue::SubOptimal(EnvValidationIssueSubOptimal::CIInfoActorTooShort(
String::from("")
)),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoAuthorEmailTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(EnvValidationIssueSubOptimal::CIInfoAuthorNameTooShort(
String::from(""),
),),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitMessageTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitterEmailTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitterNameTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(EnvValidationIssueSubOptimal::CIInfoTitleTooShort(
String::from(""),
),),
]
);
}

#[test]
fn test_bitbucket_pr() {
let workspace = String::from("my-workspace");
let repo_slug = String::from("my-repo");
let build_number = String::from("123");
let branch = String::from("feature/add-tests");
let pr_id = 456;
let pipeline_uuid = String::from("{pipeline-uuid-1234}");
let step_uuid = String::from("{step-uuid-5678}");

let env_vars = EnvVars::from_iter(vec![
(
String::from("BITBUCKET_BUILD_NUMBER"),
String::from(&build_number),
),
(
String::from("BITBUCKET_WORKSPACE"),
String::from(&workspace),
),
(
String::from("BITBUCKET_REPO_SLUG"),
String::from(&repo_slug),
),
(String::from("BITBUCKET_BRANCH"), String::from(&branch)),
(String::from("BITBUCKET_PR_ID"), pr_id.to_string()),
(
String::from("BITBUCKET_PIPELINE_UUID"),
String::from(&pipeline_uuid),
),
(
String::from("BITBUCKET_STEP_UUID"),
String::from(&step_uuid),
),
]);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars, &[], None);

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

// Verify that PR branch class is correctly set when BITBUCKET_PR_ID is present
pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::BitbucketPipelines,
job_url: Some(format!(
"https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}/steps/{step_uuid}"
)),
branch: Some(branch),
branch_class: Some(BranchClass::PullRequest),
pr_number: Some(pr_id),
actor: None,
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: Some(pipeline_uuid),
job: Some(step_uuid),
}
);
}

#[test]
fn test_bitbucket_without_step_uuid() {
// Test that job URL works without step UUID (no /steps/ suffix)
// and that workflow/job are None when UUIDs not provided
let workspace = String::from("my-workspace");
let repo_slug = String::from("my-repo");
let build_number = String::from("99");
let branch = String::from("develop");

let env_vars = EnvVars::from_iter(vec![
(
String::from("BITBUCKET_BUILD_NUMBER"),
String::from(&build_number),
),
(
String::from("BITBUCKET_WORKSPACE"),
String::from(&workspace),
),
(
String::from("BITBUCKET_REPO_SLUG"),
String::from(&repo_slug),
),
(String::from("BITBUCKET_BRANCH"), String::from(&branch)),
]);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars, &[], None);

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::BitbucketPipelines,
job_url: Some(format!(
"https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}"
)),
branch: Some(branch),
branch_class: Some(BranchClass::None),
pr_number: None,
actor: None,
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: None,
job: None,
}
);
}

#[test]
fn test_bitbucket_stable_branch() {
let workspace = String::from("my-workspace");
let repo_slug = String::from("my-repo");
let build_number = String::from("200");
let branch = String::from("main");

let env_vars = EnvVars::from_iter(vec![
(
String::from("BITBUCKET_BUILD_NUMBER"),
String::from(&build_number),
),
(
String::from("BITBUCKET_WORKSPACE"),
String::from(&workspace),
),
(
String::from("BITBUCKET_REPO_SLUG"),
String::from(&repo_slug),
),
(String::from("BITBUCKET_BRANCH"), String::from(&branch)),
]);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars, &["main", "master"], None);

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::BitbucketPipelines,
job_url: Some(format!(
"https://bitbucket.org/{workspace}/{repo_slug}/pipelines/results/{build_number}"
)),
branch: Some(branch),
branch_class: Some(BranchClass::ProtectedBranch),
pr_number: None,
actor: None,
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: None,
job: None,
}
);
}

#[test]
fn test_bitbucket_missing_job_url_vars() {
// Test that job_url is None when required vars are missing
let branch = String::from("feature-branch");

let env_vars = EnvVars::from_iter(vec![
(String::from("BITBUCKET_BUILD_NUMBER"), String::from("42")),
// Missing BITBUCKET_WORKSPACE and BITBUCKET_REPO_SLUG
(String::from("BITBUCKET_BRANCH"), String::from(&branch)),
]);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars, &[], None);

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::BitbucketPipelines,
job_url: None,
branch: Some(branch),
branch_class: Some(BranchClass::None),
pr_number: None,
actor: None,
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: None,
job: None,
}
);
}

#[test]
fn does_not_cross_contaminate_prioritizes_custom() {
let pr_number = 123;
Expand Down
Loading