integrate-gitea (#2)
Reviewed-on: #2 Co-authored-by: Julien Fastré <julien.fastre@champs-libres.coop> Co-committed-by: Julien Fastré <julien.fastre@champs-libres.coop>
This commit is contained in:
56
src/gitea/issue.rs
Normal file
56
src/gitea/issue.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use crate::gitea::client::Client;
|
||||
use crate::gitea::repository::Repository;
|
||||
use serde::Deserialize;
|
||||
use url::Url;
|
||||
use crate::error::GeneralError;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Issue {
|
||||
pub id: u64,
|
||||
number: u64,
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub repository: Repository,
|
||||
pub html_url: String,
|
||||
}
|
||||
|
||||
pub trait IssueClient {
|
||||
fn get_issue(owner_or_organisation: &String, repo: &String, number: &u64) -> Option<Issue>;
|
||||
}
|
||||
|
||||
impl IssueClient for Client {
|
||||
fn get_issue(_owner_or_organisation: &String, _repo: &String, number: &u64) -> Option<Issue> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn issue_html_url_to_api(url: &Url) -> Result<Url, GeneralError> {
|
||||
let mut parts = url.path_segments().unwrap();
|
||||
let domain = parts.next().unwrap();
|
||||
let repo = parts.next().unwrap();
|
||||
let issue = parts.next().unwrap();
|
||||
let iid = parts.next().unwrap();
|
||||
|
||||
if (!issue.eq("issues")) {
|
||||
return Err(GeneralError {
|
||||
description: format!("Issue url is not valid: {}", url),
|
||||
});
|
||||
}
|
||||
|
||||
let url = Url::parse(format!("{}://{}/api/v1/repos/{}/{}/issues/{}", url.scheme(), url.host().unwrap(), domain, repo, iid).as_str()).unwrap();
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_issue_html_url_to_api() {
|
||||
let url = Url::parse("https://gitea.champs-libres.be/champs-libres/test/issues/1").unwrap();
|
||||
let result = issue_html_url_to_api(&url).unwrap();
|
||||
|
||||
assert_eq!(result.as_str(), "https://gitea.champs-libres.be/api/v1/repos/champs-libres/test/issues/1");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user