From f35827ca1ac7779d6d71241b1910fb7091b94467 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Sat, 5 Aug 2023 01:52:15 -0400 Subject: [PATCH 1/7] Added create check run --- src/api/checks.rs | 216 ++++++++++++++++++++++++++++++++++++++++++- src/models/checks.rs | 4 +- 2 files changed, 217 insertions(+), 3 deletions(-) diff --git a/src/api/checks.rs b/src/api/checks.rs index 4b8f8a9c..9359df4b 100644 --- a/src/api/checks.rs +++ b/src/api/checks.rs @@ -1,4 +1,5 @@ -use crate::models::CheckSuiteId; +use chrono::{DateTime, Utc}; +use crate::models::{CheckRunId, CheckSuiteId}; use crate::{models, Octocrab, Result}; /// Handler for GitHub's Checks API. @@ -10,6 +11,177 @@ pub struct ChecksHandler<'octo> { repo: String, } +#[derive(serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub enum CheckRunStatus { + Queued, + InProgress, + Completed, +} + +#[derive(serde::Serialize)] +pub struct CreateCheckRunBuilder<'octo, 'r> { + #[serde(skip)] + handler: &'r ChecksHandler<'octo>, + name: String, + head_sha: String, + #[serde(skip_serializing_if = "Option::is_none")] + details_url: Option, + #[serde(skip_serializing_if = "Option::is_none")] + external_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + status: Option +} + +impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> { + pub(crate) fn new(handler: &'r ChecksHandler<'octo>, name: String, head_sha: String) -> Self { + Self { + handler, + name, + head_sha, + details_url: None, + external_id: None, + status: None + } + } + + /// The URL of the integrator's site that has the full details of the check. + /// If the integrator does not provide this, then the homepage of the GitHub app is used. + pub fn details_url(mut self, details_url: impl Into) -> Self { + self.details_url = Some(details_url.into()); + self + } + + /// A reference for the run on the integrator's system. + pub fn external_url(mut self, external_id: impl Into) -> Self { + self.external_id = Some(external_id.into()); + self + } + + /// The current status. + /// Can be one of `queued`, `in_progress`, or `completed`. + pub fn status(mut self, status: CheckRunStatus) -> Self { + self.status = Some(status); + self + } + + /// Sends the actual request. + pub async fn send(self) -> Result { + let route = format!( + "/repos/{owner}/{repo}/check-runs", + owner = self.handler.owner, + repo = self.handler.repo + ); + self.handler.crab.post(route, Some(&self)).await + } +} + +#[derive(serde::Serialize)] +pub struct UpdateCheckRunBuilder<'octo, 'r> { + #[serde(skip)] + handler: &'r ChecksHandler<'octo>, + check_run_id: CheckRunId, + #[serde(skip_serializing_if = "Option::is_none")] + name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + details_url: Option, + #[serde(skip_serializing_if = "Option::is_none")] + external_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + started_at: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + status: Option, + #[serde(skip_serializing_if = "Option::is_none")] + conclusion: Option, + #[serde(skip_serializing_if = "Option::is_none")] + completed_at: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + output: Option +} + +impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> { + pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_run_id: CheckRunId) -> Self { + Self { + handler, + check_run_id, + name: None, + details_url: None, + external_id: None, + started_at: None, + status: None, + conclusion: None, + completed_at: None, + output: None + } + } + + /// The name of the check. For example, "code-coverage". + pub fn name(mut self, name: impl Into) -> Self { + self.name = Some(name.into()); + self + } + + /// The URL of the integrator's site that has the full details of the check. + /// If the integrator does not provide this, then the homepage of the GitHub app is used. + pub fn details_url(mut self, details_url: impl Into) -> Self { + self.details_url = Some(details_url.into()); + self + } + + /// A reference for the run on the integrator's system. + pub fn external_url(mut self, external_id: impl Into) -> Self { + self.external_id = Some(external_id.into()); + self + } + + /// The time that the check run began. + pub fn started_at(mut self, started_at: DateTime) -> Self { + self.started_at = Some(started_at); + self + } + + /// The current status. + /// Can be one of `queued`, `in_progress`, or `completed`. + pub fn status(mut self, status: CheckRunStatus) -> Self { + self.status = Some(status); + self + } + + /// The final conclusion of the check. + /// Can be one of `success`, `failure`, `neutral`, `cancelled`, `timed_out`, + /// `skipped`, `stale` or `action_required`. + pub fn conclusion(mut self, conclusion: impl Into) -> Self { + self.conclusion = Some(conclusion.into()); + self + } + + /// The time that the check run completed. + pub fn completed_at(mut self, completed_at: DateTime) -> Self { + self.completed_at = Some(completed_at); + self + } + + /// Check runs can accept a variety of data in the output object, + /// including a title and summary and can optionally provide + /// descriptive details about the run. + /// TODO: Make this a better type than String + pub fn output(mut self, output: impl Into) -> Self { + self.output = Some(output.into()); + self + } + + /// Sends the actual request. + pub async fn send(self) -> Result { + let route = format!( + "/repos/{owner}/{repo}/check-runs/{check_run_id}", + owner = self.handler.owner, + repo = self.handler.repo, + check_run_id = self.check_run_id + ); + self.handler.crab.patch(route, Some(&self)).await + } +} + #[derive(serde::Serialize)] pub struct ListCheckRunsinCheckSuiteBuilder<'octo, 'r> { #[serde(skip)] @@ -77,4 +249,46 @@ impl<'octo> ChecksHandler<'octo> { ) -> ListCheckRunsinCheckSuiteBuilder<'_, '_> { ListCheckRunsinCheckSuiteBuilder::new(self, suite_id) } + + /// ```no_run + /// # async fn run() -> octocrab::Result<()> { + /// let check_run = octocrab::instance() + /// .checks("owner", "repo") + /// .create_check_run("name", "head_sha") + /// .details_url("https://example.com") + /// .external_url("external_id") + /// .status(octocrab::checks::CheckRunStatus::InProgress) + /// .send() + /// .await?; + /// # Ok(()) + /// # } + /// ``` + pub fn create_check_run( + &self, + name: impl Into, + head_sha: impl Into, + ) -> CreateCheckRunBuilder<'_, '_> { + CreateCheckRunBuilder::new(self, name.into(), head_sha.into()) + } + + /// ```no_run + /// # async fn run() -> octocrab::Result<()> { + /// let check_run = octocrab::instance() + /// .checks("owner", "repo") + /// .update_check_run(123456.into()) + /// .name("name") + /// .details_url("https://example.com") + /// .external_url("external_id") + /// .status(octocrab::checks::CheckRunStatus::InProgress) + /// .send() + /// .await?; + /// # Ok(()) + /// # } + /// ``` + pub fn update_check_run( + &self, + check_run_id: CheckRunId, + ) -> UpdateCheckRunBuilder<'_, '_> { + UpdateCheckRunBuilder::new(self, check_run_id) + } } diff --git a/src/models/checks.rs b/src/models/checks.rs index 948bf27d..c83a6540 100644 --- a/src/models/checks.rs +++ b/src/models/checks.rs @@ -10,8 +10,8 @@ pub struct CheckRun { pub url: String, pub html_url: Option, pub conclusion: Option, - pub started_at: Option>, - pub completed_at: Option>, + pub started_at: Option>, + pub completed_at: Option>, pub name: String, } From d90515f60f7494be57259089a2c7b46cb01baec9 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Fri, 11 Aug 2023 01:30:07 -0400 Subject: [PATCH 2/7] Adjusted checks output type to be serde_json::Value --- src/api/checks.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/api/checks.rs b/src/api/checks.rs index 9359df4b..fe2cad34 100644 --- a/src/api/checks.rs +++ b/src/api/checks.rs @@ -1,6 +1,6 @@ -use chrono::{DateTime, Utc}; use crate::models::{CheckRunId, CheckSuiteId}; use crate::{models, Octocrab, Result}; +use chrono::{DateTime, Utc}; /// Handler for GitHub's Checks API. /// @@ -30,7 +30,7 @@ pub struct CreateCheckRunBuilder<'octo, 'r> { #[serde(skip_serializing_if = "Option::is_none")] external_id: Option, #[serde(skip_serializing_if = "Option::is_none")] - status: Option + status: Option, } impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> { @@ -41,7 +41,7 @@ impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> { head_sha, details_url: None, external_id: None, - status: None + status: None, } } @@ -96,7 +96,7 @@ pub struct UpdateCheckRunBuilder<'octo, 'r> { #[serde(skip_serializing_if = "Option::is_none")] completed_at: Option>, #[serde(skip_serializing_if = "Option::is_none")] - output: Option + output: Option, } impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> { @@ -111,7 +111,7 @@ impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> { status: None, conclusion: None, completed_at: None, - output: None + output: None, } } @@ -164,9 +164,8 @@ impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> { /// Check runs can accept a variety of data in the output object, /// including a title and summary and can optionally provide /// descriptive details about the run. - /// TODO: Make this a better type than String - pub fn output(mut self, output: impl Into) -> Self { - self.output = Some(output.into()); + pub fn output(mut self, output: serde_json::Value) -> Self { + self.output = Some(output); self } @@ -285,10 +284,7 @@ impl<'octo> ChecksHandler<'octo> { /// # Ok(()) /// # } /// ``` - pub fn update_check_run( - &self, - check_run_id: CheckRunId, - ) -> UpdateCheckRunBuilder<'_, '_> { + pub fn update_check_run(&self, check_run_id: CheckRunId) -> UpdateCheckRunBuilder<'_, '_> { UpdateCheckRunBuilder::new(self, check_run_id) } } From 96c9d870d0e2f4ce3bcb9c3a3af9d7b950976d4d Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Sun, 13 Aug 2023 13:49:41 -0400 Subject: [PATCH 3/7] Added an auth state getter --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 6e157e00..89980f25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1468,6 +1468,11 @@ impl Octocrab { } } + /// A convenience method to get the current authentication state. + pub fn get_auth_state(&self) -> &AuthState { + &self.auth_state + } + /// A convenience method to get all the results starting at a given /// page. pub async fn all_pages( From 29fe31d5a8a35b120b1659ac6181cace0e1b0ae7 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Mon, 21 Aug 2023 16:28:23 -0400 Subject: [PATCH 4/7] Added ListChecksForGitRefBuilder --- src/api/checks.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/api/checks.rs b/src/api/checks.rs index fe2cad34..04e25752 100644 --- a/src/api/checks.rs +++ b/src/api/checks.rs @@ -1,4 +1,5 @@ use crate::models::{CheckRunId, CheckSuiteId}; +use crate::params::repos::Commitish; use crate::{models, Octocrab, Result}; use chrono::{DateTime, Utc}; @@ -182,7 +183,7 @@ impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> { } #[derive(serde::Serialize)] -pub struct ListCheckRunsinCheckSuiteBuilder<'octo, 'r> { +pub struct ListCheckRunsInCheckSuiteBuilder<'octo, 'r> { #[serde(skip)] handler: &'r ChecksHandler<'octo>, check_suite_id: CheckSuiteId, @@ -192,7 +193,7 @@ pub struct ListCheckRunsinCheckSuiteBuilder<'octo, 'r> { page: Option, } -impl<'octo, 'r> ListCheckRunsinCheckSuiteBuilder<'octo, 'r> { +impl<'octo, 'r> ListCheckRunsInCheckSuiteBuilder<'octo, 'r> { pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_suite_id: CheckSuiteId) -> Self { Self { handler, @@ -227,6 +228,53 @@ impl<'octo, 'r> ListCheckRunsinCheckSuiteBuilder<'octo, 'r> { } } +#[derive(serde::Serialize)] +pub struct ListCheckRunsForGitRefBuilder<'octo, 'r> { + #[serde(skip)] + handler: &'r ChecksHandler<'octo>, + #[serde(skip)] + git_ref: Commitish, + #[serde(skip_serializing_if = "Option::is_none")] + per_page: Option, + #[serde(skip_serializing_if = "Option::is_none")] + page: Option, +} + +impl<'octo, 'r> ListCheckRunsForGitRefBuilder<'octo, 'r> { + pub(crate) fn new(handler: &'r ChecksHandler<'octo>, git_ref: Commitish) -> Self { + Self { + handler, + git_ref, + per_page: None, + page: None, + } + } + + /// Results per page (max 100). + pub fn per_page(mut self, per_page: impl Into) -> Self { + self.per_page = Some(per_page.into()); + self + } + + /// Page number of the results to fetch. + pub fn page(mut self, page: impl Into) -> Self { + self.page = Some(page.into()); + self + } + + /// Send the actual request. + pub async fn send(self) -> Result { + let route = format!( + "/repos/{owner}/{repo}/commits/{ref}/check-runs", + owner = self.handler.owner, + repo = self.handler.repo, + ref = self.git_ref, + ); + + self.handler.crab.get(route, Some(&self)).await + } +} + impl<'octo> ChecksHandler<'octo> { pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self { Self { crab, owner, repo } @@ -245,8 +293,24 @@ impl<'octo> ChecksHandler<'octo> { pub fn list_check_runs_in_a_check_suite( &self, suite_id: CheckSuiteId, - ) -> ListCheckRunsinCheckSuiteBuilder<'_, '_> { - ListCheckRunsinCheckSuiteBuilder::new(self, suite_id) + ) -> ListCheckRunsInCheckSuiteBuilder<'_, '_> { + ListCheckRunsInCheckSuiteBuilder::new(self, suite_id) + } + + /// ```no_run + /// # use octocrab::params::repos::Commitish; + /// async fn run() -> octocrab::Result<()> { + /// let check_runs = octocrab::instance() + /// .checks("owner", "repo") + /// .list_check_runs_for_git_ref(Commitish("ref".to_string())) + /// .send() + /// .await?; + /// # Ok(()) + pub fn list_check_runs_for_git_ref( + &self, + git_ref: Commitish, + ) -> ListCheckRunsForGitRefBuilder<'_, '_> { + ListCheckRunsForGitRefBuilder::new(self, git_ref) } /// ```no_run From 326a89a77a9db12a33957696e00f95af71fcc544 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Mon, 21 Aug 2023 17:53:56 -0400 Subject: [PATCH 5/7] Fix test --- src/api/checks.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/checks.rs b/src/api/checks.rs index 04e25752..ff061bab 100644 --- a/src/api/checks.rs +++ b/src/api/checks.rs @@ -306,6 +306,7 @@ impl<'octo> ChecksHandler<'octo> { /// .send() /// .await?; /// # Ok(()) + /// # } pub fn list_check_runs_for_git_ref( &self, git_ref: Commitish, From d0db7d17c157798b6e9de9d48ef3d72a00491bb0 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Fri, 25 Aug 2023 13:47:10 -0400 Subject: [PATCH 6/7] external_url -> external_id --- src/api/checks.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/checks.rs b/src/api/checks.rs index ff061bab..8960d1ad 100644 --- a/src/api/checks.rs +++ b/src/api/checks.rs @@ -54,7 +54,7 @@ impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> { } /// A reference for the run on the integrator's system. - pub fn external_url(mut self, external_id: impl Into) -> Self { + pub fn external_id(mut self, external_id: impl Into) -> Self { self.external_id = Some(external_id.into()); self } @@ -320,7 +320,7 @@ impl<'octo> ChecksHandler<'octo> { /// .checks("owner", "repo") /// .create_check_run("name", "head_sha") /// .details_url("https://example.com") - /// .external_url("external_id") + /// .external_id("external_id") /// .status(octocrab::checks::CheckRunStatus::InProgress) /// .send() /// .await?; From 7a786d9fb1e2892266ac46ec245912d965c32def Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Sun, 24 Sep 2023 18:57:37 -0400 Subject: [PATCH 7/7] Cleaning up --- src/lib.rs | 5 ----- src/models/checks.rs | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 89980f25..6e157e00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1468,11 +1468,6 @@ impl Octocrab { } } - /// A convenience method to get the current authentication state. - pub fn get_auth_state(&self) -> &AuthState { - &self.auth_state - } - /// A convenience method to get all the results starting at a given /// page. pub async fn all_pages( diff --git a/src/models/checks.rs b/src/models/checks.rs index c83a6540..948bf27d 100644 --- a/src/models/checks.rs +++ b/src/models/checks.rs @@ -10,8 +10,8 @@ pub struct CheckRun { pub url: String, pub html_url: Option, pub conclusion: Option, - pub started_at: Option>, - pub completed_at: Option>, + pub started_at: Option>, + pub completed_at: Option>, pub name: String, }