use anyhow::Result;
use super::BitbucketClient;
use crate::models::{
Paginated, Pipeline, PipelineCache, PipelineSchedule, PipelineStep, PipelineVariable,
PipelineVariableInput, PipelinesConfig, TriggerPipelineRequest,
};
#[derive(Default)]
pub(crate) struct PipelineListFilters<'a> {
pub page: Option<u32>,
pub pagelen: Option<u32>,
pub status: Option<&'a str>,
pub target_branch: Option<&'a str>,
pub sort: Option<&'a str>,
}
impl BitbucketClient {
pub async fn list_pipelines(
&self,
workspace: &str,
repo_slug: &str,
page: Option<u32>,
pagelen: Option<u32>,
) -> Result<Paginated<Pipeline>> {
self.list_pipelines_filtered(
workspace,
repo_slug,
PipelineListFilters {
page,
pagelen,
..Default::default()
},
)
.await
}
pub(crate) async fn list_pipelines_filtered(
&self,
workspace: &str,
repo_slug: &str,
filters: PipelineListFilters<'_>,
) -> Result<Paginated<Pipeline>> {
let query = pipeline_list_params(&filters);
let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();
let path = format!("/repositories/{}/{}/pipelines", workspace, repo_slug);
self.get_with_query(&path, &query_refs).await
}
pub async fn get_pipeline(
&self,
workspace: &str,
repo_slug: &str,
pipeline_uuid: &str,
) -> Result<Pipeline> {
let path = format!(
"/repositories/{}/{}/pipelines/{}",
workspace, repo_slug, pipeline_uuid
);
self.get(&path).await
}
pub async fn trigger_pipeline(
&self,
workspace: &str,
repo_slug: &str,
request: &TriggerPipelineRequest,
) -> Result<Pipeline> {
let path = format!("/repositories/{}/{}/pipelines", workspace, repo_slug);
self.post(&path, request).await
}
pub async fn stop_pipeline(
&self,
workspace: &str,
repo_slug: &str,
pipeline_uuid: &str,
) -> Result<()> {
let path = format!(
"/repositories/{}/{}/pipelines/{}/stopPipeline",
workspace, repo_slug, pipeline_uuid
);
self.post_no_response(&path, &serde_json::json!({})).await
}
pub async fn list_pipeline_steps(
&self,
workspace: &str,
repo_slug: &str,
pipeline_uuid: &str,
) -> Result<Paginated<PipelineStep>> {
let path = format!(
"/repositories/{}/{}/pipelines/{}/steps",
workspace, repo_slug, pipeline_uuid
);
self.get(&path).await
}
pub async fn get_pipeline_step(
&self,
workspace: &str,
repo_slug: &str,
pipeline_uuid: &str,
step_uuid: &str,
) -> Result<PipelineStep> {
let path = format!(
"/repositories/{}/{}/pipelines/{}/steps/{}",
workspace, repo_slug, pipeline_uuid, step_uuid
);
self.get(&path).await
}
pub async fn get_step_log(
&self,
workspace: &str,
repo_slug: &str,
pipeline_uuid: &str,
step_uuid: &str,
) -> Result<String> {
let path = format!(
"/repositories/{}/{}/pipelines/{}/steps/{}/log",
workspace, repo_slug, pipeline_uuid, step_uuid
);
self.get_text(&path, None).await
}
pub async fn list_pipelines_for_commit(
&self,
workspace: &str,
repo_slug: &str,
commit_hash: &str,
scan_limit: u32,
) -> Result<Vec<Pipeline>> {
let pagelen = scan_limit.clamp(1, 100);
let pipelines = self
.list_pipelines(workspace, repo_slug, None, Some(pagelen))
.await?;
Ok(pipelines
.values
.into_iter()
.filter(|p| {
p.target
.commit
.as_ref()
.is_some_and(|c| commit_hashes_match(&c.hash, commit_hash))
})
.collect())
}
pub async fn get_pipeline_by_build_number(
&self,
workspace: &str,
repo_slug: &str,
build_number: u64,
) -> Result<Pipeline> {
find_pipeline_by_build_number(
|page| self.list_pipelines(workspace, repo_slug, Some(page), Some(100)),
build_number,
50,
)
.await
}
pub async fn list_pipeline_variables(
&self,
workspace: &str,
repo_slug: &str,
) -> Result<Vec<PipelineVariable>> {
let path = format!(
"/repositories/{}/{}/pipelines_config/variables/",
workspace, repo_slug
);
self.get_all_pages(&path).await
}
pub async fn create_pipeline_variable(
&self,
workspace: &str,
repo_slug: &str,
variable: &PipelineVariableInput,
) -> Result<PipelineVariable> {
let path = format!(
"/repositories/{}/{}/pipelines_config/variables/",
workspace, repo_slug
);
self.post(&path, variable).await
}
pub async fn update_pipeline_variable(
&self,
workspace: &str,
repo_slug: &str,
variable_uuid: &str,
variable: &PipelineVariableInput,
) -> Result<PipelineVariable> {
let path = format!(
"/repositories/{}/{}/pipelines_config/variables/{}",
workspace, repo_slug, variable_uuid
);
self.put(&path, variable).await
}
pub async fn delete_pipeline_variable(
&self,
workspace: &str,
repo_slug: &str,
variable_uuid: &str,
) -> Result<()> {
let path = format!(
"/repositories/{}/{}/pipelines_config/variables/{}",
workspace, repo_slug, variable_uuid
);
self.delete(&path).await
}
pub async fn list_workspace_pipeline_variables(
&self,
workspace: &str,
) -> Result<Vec<PipelineVariable>> {
let path = format!("/workspaces/{}/pipelines-config/variables/", workspace);
self.get_all_pages(&path).await
}
pub async fn create_workspace_pipeline_variable(
&self,
workspace: &str,
variable: &PipelineVariableInput,
) -> Result<PipelineVariable> {
let path = format!("/workspaces/{}/pipelines-config/variables/", workspace);
self.post(&path, variable).await
}
pub async fn update_workspace_pipeline_variable(
&self,
workspace: &str,
variable_uuid: &str,
variable: &PipelineVariableInput,
) -> Result<PipelineVariable> {
let path = format!(
"/workspaces/{}/pipelines-config/variables/{}",
workspace, variable_uuid
);
self.put(&path, variable).await
}
pub async fn delete_workspace_pipeline_variable(
&self,
workspace: &str,
variable_uuid: &str,
) -> Result<()> {
let path = format!(
"/workspaces/{}/pipelines-config/variables/{}",
workspace, variable_uuid
);
self.delete(&path).await
}
pub async fn list_pipeline_schedules(
&self,
workspace: &str,
repo_slug: &str,
) -> Result<Vec<PipelineSchedule>> {
let path = format!(
"/repositories/{}/{}/pipelines_config/schedules/",
workspace, repo_slug
);
self.get_all_pages(&path).await
}
pub async fn create_pipeline_schedule(
&self,
workspace: &str,
repo_slug: &str,
branch: &str,
cron_pattern: &str,
) -> Result<PipelineSchedule> {
let path = format!(
"/repositories/{}/{}/pipelines_config/schedules/",
workspace, repo_slug
);
let body = serde_json::json!({
"enabled": true,
"target": {
"type": "pipeline_ref_target",
"ref_type": "branch",
"ref_name": branch,
"selector": {
"type": "branches",
"pattern": branch,
},
},
"cron_pattern": cron_pattern,
});
self.post(&path, &body).await
}
pub async fn delete_pipeline_schedule(
&self,
workspace: &str,
repo_slug: &str,
schedule_uuid: &str,
) -> Result<()> {
let path = format!(
"/repositories/{}/{}/pipelines_config/schedules/{}",
workspace, repo_slug, schedule_uuid
);
self.delete(&path).await
}
pub async fn list_pipeline_caches(
&self,
workspace: &str,
repo_slug: &str,
) -> Result<Vec<PipelineCache>> {
let path = format!(
"/repositories/{}/{}/pipelines-config/caches/",
workspace, repo_slug
);
self.get_all_pages(&path).await
}
pub async fn delete_pipeline_cache(
&self,
workspace: &str,
repo_slug: &str,
cache_uuid: &str,
) -> Result<()> {
let path = format!(
"/repositories/{}/{}/pipelines-config/caches/{}",
workspace, repo_slug, cache_uuid
);
self.delete(&path).await
}
pub async fn get_pipelines_config(
&self,
workspace: &str,
repo_slug: &str,
) -> Result<PipelinesConfig> {
let path = format!("/repositories/{}/{}/pipelines_config", workspace, repo_slug);
self.get(&path).await
}
pub async fn update_pipelines_config_enabled(
&self,
workspace: &str,
repo_slug: &str,
enabled: bool,
) -> Result<PipelinesConfig> {
let path = format!("/repositories/{}/{}/pipelines_config", workspace, repo_slug);
self.put(&path, &serde_json::json!({ "enabled": enabled }))
.await
}
pub async fn set_pipelines_config_build_number(
&self,
workspace: &str,
repo_slug: &str,
next: u64,
) -> Result<()> {
let path = format!(
"/repositories/{}/{}/pipelines_config/build_number",
workspace, repo_slug
);
let _: serde_json::Value = self
.put(&path, &serde_json::json!({ "next": next }))
.await?;
Ok(())
}
}
fn pipeline_list_params(filters: &PipelineListFilters<'_>) -> Vec<(&'static str, String)> {
let mut query = Vec::new();
query.push(("sort", filters.sort.unwrap_or("-created_on").to_string()));
if let Some(status) = filters.status {
query.push(("status", status.to_uppercase()));
}
if let Some(branch) = filters.target_branch {
query.push(("target.branch", branch.to_string()));
}
if let Some(p) = filters.page {
query.push(("page", p.to_string()));
}
if let Some(len) = filters.pagelen {
query.push(("pagelen", len.to_string()));
}
query
}
async fn find_pipeline_by_build_number<F, Fut>(
mut fetch_page: F,
build_number: u64,
max_pages: u32,
) -> Result<Pipeline>
where
F: FnMut(u32) -> Fut,
Fut: std::future::Future<Output = Result<Paginated<Pipeline>>>,
{
let mut pipelines_seen: usize = 0;
for page in 1..=max_pages {
let pipelines = fetch_page(page).await?;
let has_next = pipelines.next.is_some();
pipelines_seen += pipelines.values.len();
if let Some(pipeline) = pipelines
.values
.into_iter()
.find(|p| p.build_number == build_number)
{
return Ok(pipeline);
}
if !has_next {
return Err(anyhow::anyhow!("Pipeline #{} not found", build_number));
}
}
Err(anyhow::anyhow!(
"Pipeline #{} not found (searched the {} most recent pipelines)",
build_number,
pipelines_seen
))
}
fn commit_hashes_match(a: &str, b: &str) -> bool {
let (long, short) = if a.len() >= b.len() { (a, b) } else { (b, a) };
!short.is_empty() && long.starts_with(short)
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use std::future::{Ready, ready};
use chrono::Utc;
use super::{
PipelineListFilters, commit_hashes_match, find_pipeline_by_build_number,
pipeline_list_params,
};
use crate::models::{Paginated, Pipeline, PipelineState, PipelineStateName, PipelineTarget};
fn param<'a>(params: &'a [(&'static str, String)], key: &str) -> Option<&'a str> {
params
.iter()
.find(|(k, _)| *k == key)
.map(|(_, v)| v.as_str())
}
#[test]
fn params_default_sort_is_created_on_descending() {
let params = pipeline_list_params(&PipelineListFilters::default());
assert_eq!(param(¶ms, "sort"), Some("-created_on"));
}
#[test]
fn params_sort_override_wins() {
let filters = PipelineListFilters {
sort: Some("created_on"),
..Default::default()
};
assert_eq!(
param(&pipeline_list_params(&filters), "sort"),
Some("created_on")
);
}
#[test]
fn params_status_is_uppercased() {
let filters = PipelineListFilters {
status: Some("in_progress"),
..Default::default()
};
assert_eq!(
param(&pipeline_list_params(&filters), "status"),
Some("IN_PROGRESS")
);
}
#[test]
fn params_target_branch_uses_dotted_key() {
let filters = PipelineListFilters {
target_branch: Some("main"),
..Default::default()
};
let params = pipeline_list_params(&filters);
assert_eq!(param(¶ms, "target.branch"), Some("main"));
assert_eq!(
param(
&pipeline_list_params(&PipelineListFilters::default()),
"target.branch"
),
None
);
}
fn pipeline(build_number: u64) -> Pipeline {
Pipeline {
uuid: format!("{{pipeline-{}}}", build_number),
build_number,
creator: None,
repository: None,
target: PipelineTarget {
target_type: "pipeline_ref_target".to_string(),
ref_type: None,
ref_name: None,
selector: None,
commit: None,
},
trigger: None,
state: PipelineState {
name: PipelineStateName::Completed,
state_type: "pipeline_state_completed".to_string(),
result: None,
stage: None,
},
created_on: Utc::now(),
completed_on: None,
build_seconds_used: None,
links: None,
}
}
fn page(values: Vec<Pipeline>, has_next: bool) -> Paginated<Pipeline> {
Paginated {
size: None,
page: None,
pagelen: None,
next: has_next.then(|| "https://api.bitbucket.org/next".to_string()),
previous: None,
values,
}
}
type PageFuture = Ready<anyhow::Result<Paginated<Pipeline>>>;
#[tokio::test]
async fn finds_pipeline_on_second_page_with_exactly_two_fetches() {
let calls = Cell::new(0u32);
let fetch = |page_number: u32| -> PageFuture {
calls.set(calls.get() + 1);
ready(Ok(match page_number {
1 => page(vec![pipeline(100), pipeline(99)], true),
2 => page(vec![pipeline(98), pipeline(42)], true),
other => panic!("unexpected page fetch: {}", other),
}))
};
let found = find_pipeline_by_build_number(fetch, 42, 50)
.await
.expect("pipeline should be found on page 2");
assert_eq!(found.build_number, 42);
assert_eq!(calls.get(), 2);
}
#[tokio::test]
async fn stops_after_one_fetch_when_pages_are_exhausted() {
let calls = Cell::new(0u32);
let fetch = |_page_number: u32| -> PageFuture {
calls.set(calls.get() + 1);
ready(Ok(page(vec![pipeline(100), pipeline(99)], false)))
};
let err = find_pipeline_by_build_number(fetch, 42, 50)
.await
.expect_err("pipeline should not be found");
assert_eq!(err.to_string(), "Pipeline #42 not found");
assert_eq!(calls.get(), 1);
}
#[tokio::test]
async fn enforces_page_cap_when_next_is_always_present() {
let calls = Cell::new(0u32);
let fetch = |_page_number: u32| -> PageFuture {
calls.set(calls.get() + 1);
ready(Ok(page(vec![pipeline(100), pipeline(99)], true)))
};
let err = find_pipeline_by_build_number(fetch, 42, 3)
.await
.expect_err("search should stop at the page cap");
assert_eq!(
err.to_string(),
"Pipeline #42 not found (searched the 6 most recent pipelines)"
);
assert_eq!(calls.get(), 3);
}
#[test]
fn exact_equality_matches() {
assert!(commit_hashes_match("abc123", "abc123"));
}
#[test]
fn short_pr_hash_matches_full_pipeline_hash() {
assert!(commit_hashes_match(
"975f24a99dab12345abc678def910ghi112233jk",
"975f24a99dab"
));
}
#[test]
fn full_pr_hash_matches_short_pipeline_hash() {
assert!(commit_hashes_match(
"975f24a99dab",
"975f24a99dab12345abc678def910ghi112233jk"
));
}
#[test]
fn different_commits_do_not_match() {
assert!(!commit_hashes_match("abc123def", "abc999def"));
}
#[test]
fn empty_hash_never_matches() {
assert!(!commit_hashes_match("", "abc123"));
assert!(!commit_hashes_match("abc123", ""));
assert!(!commit_hashes_match("", ""));
}
}