This repository has been archived on 2022-08-18. You can view files and clone it, but cannot push or open issues or pull requests.
rageshake-webhook/src/gitea.rs

71 lines
2.0 KiB
Rust

/*
* 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();
}
}