Skip to content

Commit

Permalink
Merge pull request #79 from patriksvensson/feature/GH-67
Browse files Browse the repository at this point in the history
Duck collector: Add view support
  • Loading branch information
patriksvensson committed Apr 5, 2020
2 parents d9f6242 + eff19c6 commit 97d72b0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ pub struct DuckConfiguration {
/// # The Duck server URL
#[serde(rename = "serverUrl")]
pub server_url: String,
/// # The view to get builds from
#[serde(default)]
pub view: Option<String>,
}

///////////////////////////////////////////////////////////
Expand Down
11 changes: 10 additions & 1 deletion src/providers/collectors/duck/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use crate::DuckResult;

pub struct DuckClient {
pub server_url: String,
pub view: Option<String>,
}

impl DuckClient {
pub fn new(config: &DuckConfiguration) -> Self {
Self {
server_url: config.server_url.clone(),
view: config.view.clone(),
}
}

Expand All @@ -26,7 +28,14 @@ impl DuckClient {
}

pub fn get_builds(&self, client: &impl HttpClient) -> DuckResult<Vec<DuckBuild>> {
let url = format!("{}/api/builds", owner = self.server_url,);
let url = match &self.view {
Some(view) => format!(
"{owner}/api/builds/view/{view}",
owner = self.server_url,
view = view
),
None => format!("{owner}/api/builds", owner = self.server_url),
};

let body = self.send_get_request(client, url)?;
Ok(serde_json::from_str(&body[..])?)
Expand Down
57 changes: 54 additions & 3 deletions src/providers/collectors/duck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@ mod tests {
use crate::utils::http::{HttpMethod, MockHttpClient, MockHttpResponseBuilder};
use reqwest::StatusCode;

fn create_collector() -> DuckCollector<MockHttpClient> {
fn create_collector(view: Option<String>) -> DuckCollector<MockHttpClient> {
DuckCollector::<MockHttpClient>::new(&DuckConfiguration {
id: "duck_other".to_owned(),
enabled: Some(true),
server_url: "http://localhost:15826".to_owned(),
view,
})
}

#[test]
fn should_return_correct_provider_name() {
// Given
let github = create_collector();
let github = create_collector(None);
// When
let provider = &github.info().provider;
// Then
Expand All @@ -136,7 +137,7 @@ mod tests {
#[test]
fn should_get_correct_data() {
// Given
let duck = create_collector();
let duck = create_collector(None);
let client = duck.get_client();

client.add_response(
Expand Down Expand Up @@ -185,4 +186,54 @@ mod tests {
assert_eq!(1584846026, result[0].started_at);
assert_eq!(1584846262, result[0].finished_at.unwrap());
}

#[test]
fn should_get_correct_data_for_view() {
// Given
let duck = create_collector(Some("foo".to_owned()));
let client = duck.get_client();

client.add_response(
MockHttpResponseBuilder::new(HttpMethod::Get, "http://localhost:15826/api/server")
.returns_status(StatusCode::OK)
.returns_body(format!("{{ \"version\": \"{}\" }}", crate::utils::VERSION)),
);
client.add_response(
MockHttpResponseBuilder::new(
HttpMethod::Get,
"http://localhost:15826/api/builds/view/foo",
)
.returns_status(StatusCode::OK)
.returns_body(include_str!("test_data/view.json")),
);

let (_, listener) = waithandle::new();

// When
let mut result = Vec::<Build>::new();
duck.collect(listener, &mut |build: Build| {
// Store the results
result.push(build);
})
.unwrap();

// Then
assert_eq!(1, result.len());
assert_eq!("58880314", result[0].build_id);
assert_eq!("GitHub", result[0].provider);
assert_eq!("duck_other", result[0].collector);
assert_eq!("spectresystems/duck", result[0].project_id);
assert_eq!("spectresystems/duck", result[0].project_name);
assert_eq!("ci.yaml", result[0].definition_id);
assert_eq!("ci.yaml", result[0].definition_name);
assert_eq!("24", result[0].build_number);
assert_eq!(BuildStatus::Success, result[0].status);
assert_eq!("setup-docker-for-local-development", result[0].branch);
assert_eq!(
"https://github.com/spectresystems/duck/actions/runs/58880314",
result[0].url
);
assert_eq!(1584617069, result[0].started_at);
assert_eq!(1584617318, result[0].finished_at.unwrap());
}
}
16 changes: 16 additions & 0 deletions src/providers/collectors/duck/test_data/view.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id": 1619989489999387859,
"provider": "GitHub",
"collector": "duck_other",
"project": "spectresystems/duck",
"build": "ci.yaml",
"branch": "setup-docker-for-local-development",
"buildId": "58880314",
"buildNumber": "24",
"started": 1584617069,
"finished": 1584617318,
"url": "https://github.com/spectresystems/duck/actions/runs/58880314",
"status": "Success"
}
]

0 comments on commit 97d72b0

Please sign in to comment.