diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-04 20:47:05 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-04 20:47:05 +0300 |
| commit | 17962fd076e857921c374b4d705a54d5e1055178 (patch) | |
| tree | 6195e9cfdc913cd95b8f577eca3f39d41b089008 /ofborg/tickborg/src/commitstatus.rs | |
| parent | 7c7f28532f1898a81b0250f875614ad3aa494a1c (diff) | |
| download | Project-Tick-17962fd076e857921c374b4d705a54d5e1055178.tar.gz Project-Tick-17962fd076e857921c374b4d705a54d5e1055178.zip | |
NOISSUE welcome to ofborg! (tickborg)
Signed-off-by: Mehmet Samet Duman <yongdohyun@projecttick.org>
Diffstat (limited to 'ofborg/tickborg/src/commitstatus.rs')
| -rw-r--r-- | ofborg/tickborg/src/commitstatus.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/ofborg/tickborg/src/commitstatus.rs b/ofborg/tickborg/src/commitstatus.rs new file mode 100644 index 0000000000..6747f3b048 --- /dev/null +++ b/ofborg/tickborg/src/commitstatus.rs @@ -0,0 +1,103 @@ +use futures_util::future::TryFutureExt; +use tracing::warn; + +pub struct CommitStatus { + api: hubcaps::statuses::Statuses, + sha: String, + context: String, + description: String, + url: String, +} + +impl CommitStatus { + pub fn new( + api: hubcaps::statuses::Statuses, + sha: String, + context: String, + description: String, + url: Option<String>, + ) -> CommitStatus { + let mut stat = CommitStatus { + api, + sha, + context, + description, + url: "".to_owned(), + }; + + stat.set_url(url); + + stat + } + + pub fn set_url(&mut self, url: Option<String>) { + self.url = url.unwrap_or_else(|| String::from("")) + } + + pub async fn set_with_description( + &mut self, + description: &str, + state: hubcaps::statuses::State, + ) -> Result<(), CommitStatusError> { + self.set_description(description.to_owned()); + self.set(state).await + } + + pub fn set_description(&mut self, description: String) { + self.description = description; + } + + pub async fn set(&self, state: hubcaps::statuses::State) -> Result<(), CommitStatusError> { + let desc = if self.description.len() >= 140 { + warn!( + "description is over 140 char; truncating: {:?}", + &self.description + ); + self.description.chars().take(140).collect() + } else { + self.description.clone() + }; + self.api + .create( + self.sha.as_ref(), + &hubcaps::statuses::StatusOptions::builder(state) + .context(self.context.clone()) + .description(desc) + .target_url(self.url.clone()) + .build(), + ) + .map_ok(|_| ()) + .map_err(|e| CommitStatusError::from(e)) + .await?; + Ok(()) + } +} + +#[derive(Debug)] +pub enum CommitStatusError { + ExpiredCreds(hubcaps::Error), + MissingSha(hubcaps::Error), + Error(hubcaps::Error), + InternalError(String), +} + +impl From<hubcaps::Error> for CommitStatusError { + fn from(e: hubcaps::Error) -> CommitStatusError { + use http::status::StatusCode; + use hubcaps::Error; + match &e { + Error::Fault { code, error } + if code == &StatusCode::UNAUTHORIZED && error.message == "Bad credentials" => + { + CommitStatusError::ExpiredCreds(e) + } + Error::Fault { code, error } + if code == &StatusCode::UNPROCESSABLE_ENTITY + && error.message.starts_with("No commit found for SHA:") => + { + CommitStatusError::MissingSha(e) + } + _otherwise => CommitStatusError::Error(e), + } + } +} |
