feat: create issues on gitea

This commit is contained in:
Aravinth Manivannan 2022-08-17 18:53:04 +05:30
parent 08bef13509
commit a9c39c3786
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 77 additions and 0 deletions

View file

@ -20,7 +20,9 @@ use std::thread;
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy}; use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
//use crate::errors::ServiceResult; //use crate::errors::ServiceResult;
use crate::gitea::Gitea;
use crate::settings::Settings; use crate::settings::Settings;
/// App data /// App data
pub struct Ctx { pub struct Ctx {
// /// database ops defined by db crates // /// database ops defined by db crates
@ -29,6 +31,7 @@ pub struct Ctx {
pub creds: Config, pub creds: Config,
/// app settings /// app settings
pub settings: Settings, pub settings: Settings,
pub gitea: Gitea,
pub source_code: String, pub source_code: String,
} }
@ -75,11 +78,14 @@ impl Ctx {
base.into() base.into()
}; };
let gitea = Gitea::new(s.gitea.access_token.clone(), s.gitea.repository_url.clone());
let data = Ctx { let data = Ctx {
creds, creds,
// db, // db,
settings: s.clone(), settings: s.clone(),
source_code, source_code,
gitea,
}; };
Arc::new(data) Arc::new(data)

70
src/gitea.rs Normal file
View file

@ -0,0 +1,70 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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<T: Serialize>(&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();
}
}

View file

@ -29,6 +29,7 @@ mod ctx;
//mod docs; //mod docs;
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
mod errors; mod errors;
mod gitea;
//#[macro_use] //#[macro_use]
//mod pages; //mod pages;
//#[macro_use] //#[macro_use]