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 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)

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;
#[cfg(not(tarpaulin_include))]
mod errors;
mod gitea;
//#[macro_use]
//mod pages;
//#[macro_use]