From a9c39c37868997f08f9176b5eefd032a966f2f24 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 17 Aug 2022 18:53:04 +0530 Subject: [PATCH] feat: create issues on gitea --- src/ctx.rs | 6 +++++ src/gitea.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 3 files changed, 77 insertions(+) create mode 100644 src/gitea.rs diff --git a/src/ctx.rs b/src/ctx.rs index c2ee996..c904989 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -20,7 +20,9 @@ use std::thread; use argon2_creds::{Config, ConfigBuilder, PasswordPolicy}; //use crate::errors::ServiceResult; +use crate::gitea::Gitea; use crate::settings::Settings; + /// App data pub struct Ctx { // /// database ops defined by db crates @@ -29,6 +31,7 @@ pub struct Ctx { pub creds: Config, /// app settings pub settings: Settings, + pub gitea: Gitea, pub source_code: String, } @@ -75,11 +78,14 @@ impl Ctx { base.into() }; + let gitea = Gitea::new(s.gitea.access_token.clone(), s.gitea.repository_url.clone()); + let data = Ctx { creds, // db, settings: s.clone(), source_code, + gitea, }; Arc::new(data) diff --git a/src/gitea.rs b/src/gitea.rs new file mode 100644 index 0000000..1329cca --- /dev/null +++ b/src/gitea.rs @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use reqwest::Client; +use serde::{Deserialize, Serialize}; +use url::Url; + +#[derive(Clone, Debug)] +pub struct Gitea { + token: String, + repository_url: Url, + org: String, + repo: String, + client: Client, +} + +#[derive(Debug, Deserialize, Serialize)] +struct Issue { + title: String, + body: String, +} + +impl Gitea { + pub fn new(token: String, repository_url: Url) -> Self { + let client = Client::default(); + + let mut iter = repository_url.path().split('/'); + iter.next().unwrap(); + let org = iter.next().unwrap().to_owned(); + let repo = iter.next().unwrap().to_owned(); + + Self { + token, + org, + repo, + client, + repository_url, + } + } + + pub async fn report(&self, payload: &T) { + let path = format!("/api/v1/repos/{}/{}/issues", self.org, self.repo); + let mut url = self.repository_url.clone(); + url.set_path(&path); + let issue = Issue { + body: format!("```json\n{}```", serde_json::to_string(&payload).unwrap()), + title: "new issue".to_string(), + }; + + self.client + .post(url) + .json(&issue) + .bearer_auth(&self.token) + .send() + .await + .unwrap(); + } +} diff --git a/src/main.rs b/src/main.rs index 72dac82..1905766 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ mod ctx; //mod docs; #[cfg(not(tarpaulin_include))] mod errors; +mod gitea; //#[macro_use] //mod pages; //#[macro_use]