Skip to content

Commit

Permalink
feat(sources): add gitea
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Perkowski <[email protected]>
Signed-off-by: Adam Perkowski <[email protected]>
  • Loading branch information
pisquaredover6 and adamperkowski committed Dec 29, 2024
1 parent 9e115ac commit 4b015d5
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ include = [

[features]
nvrs_cli = ["clap", "colored", "futures"]
default = ["aur", "crates-io", "github", "gitlab", "regex"]
default = ["aur", "crates-io", "gitea", "github", "gitlab", "regex"]
aur = []
crates-io = []
gitea = []
github = []
gitlab = []
regex = ["dep:regex"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ check the [release notes](https://github.com/adamperkowski/nvrs/releases) and [c
### Sources
- `aur`
- `cratesio`
- `gitea`
- `github`
- `gitlab` (with custom hosts)
- `website` (regex)
Expand Down
16 changes: 11 additions & 5 deletions nvrs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ oldver = "oldver.json"
newver = "newver.json"
keyfile = "n_keyfile.toml"

[hyprgui]
[julec]
source = "github"
github = "hyprutils/hyprgui"
prefix = "v"
github = "julelang/jule"
prefix = "jule"

[hyprwall]
[comlink]
source = "aur"
aur = "hyprwall"
aur = "comlink"

[mkinitcpio]
source = "gitlab"
Expand Down Expand Up @@ -41,3 +41,9 @@ regex = '<td><strong>([\d.]+)</strong></td>'
[nvrs]
source = "cratesio"
cratesio = "nvrs"

[libkeccak]
source = "gitea"
host = "codeberg.org"
gitea = "maandree/libkeccak"
use_max_tag = true
92 changes: 92 additions & 0 deletions src/api/gitea.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::{api, error};
use reqwest::{header::HeaderValue, Response};

#[derive(serde::Deserialize)]
#[serde(transparent)]
struct GiteaTagResponse {
tags: Vec<GiteaTag>,
}

#[derive(serde::Deserialize)]
struct GiteaTag {
name: String,
}

#[derive(serde::Deserialize)]
#[serde(transparent)]
struct GiteaReleaseResponse {
releases: Vec<GiteaRelease>,
}

#[derive(serde::Deserialize)]
struct GiteaRelease {
tag_name: String,
html_url: String,
}

/// get the latest version of a package from Gitea
pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
Box::pin(async move {
let host = if !args.args[1].is_empty() {
&args.args[1]
} else {
"gitea.com"
};
let repo_url = format!("https://{}/api/v1/repos/{}", host, args.args[0]);

if args.use_max_tag.is_some_and(|x| x) {
let url = format!("{}/tags", repo_url);

let result = request(url, &args).await?;
let json: &GiteaTag = &result.json::<GiteaTagResponse>().await?.tags[0];

Ok(api::Release {
name: json.name.clone(),
tag: Some(json.name.clone()),
url: format!("{}/releases/tag/{}", repo_url, json.name),
})
} else {
let url = format!("{}/releases", repo_url);
let result = request(url, &args).await?;
let json: &GiteaRelease = &result.json::<GiteaReleaseResponse>().await?.releases[0];

let tag = json.tag_name.to_owned();

Ok(api::Release {
name: tag.clone(),
tag: Some(tag),
url: json.html_url.clone(),
})
}
})
}

async fn request(url: String, args: &api::ApiArgs) -> error::Result<Response> {
let mut headers = api::setup_headers();
if !args.api_key.is_empty() {
headers.insert(
"PRIVATE-TOKEN",
HeaderValue::from_str(&args.api_key).unwrap(),
);
};
let client = &args.request_client;

let result = client.get(url).headers(headers).send().await?;
api::match_statuscode(&result.status(), args.package.clone())?;

Ok(result)
}

#[tokio::test]
async fn request_test() {
let package = "maandree/libkeccak".to_string();
let args = api::ApiArgs {
request_client: reqwest::Client::new(),
package: package.clone(),
use_max_tag: Some(true),
args: vec![package, "codeberg.org".to_string()],
api_key: String::new(),
};

assert!(get_latest(args).await.is_ok());
}
7 changes: 7 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
mod aur;
#[cfg(feature = "crates-io")]
mod crates_io;
#[cfg(feature = "gitea")]
mod gitea;
#[cfg(feature = "github")]
mod github;
#[cfg(feature = "gitlab")]
Expand Down Expand Up @@ -77,6 +79,11 @@ pub const API_LIST: &[Api] = &[
name: "cratesio",
func: crates_io::get_latest,
},
#[cfg(feature = "gitea")]
Api {
name: "gitea",
func: gitea::get_latest,
},
#[cfg(feature = "github")]
Api {
name: "github",
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct Package {
#[serde(default)]
#[serde(skip_serializing_if = "is_empty_string")]
cratesio: String,
#[cfg(feature = "gitea")]
#[serde(default)]
#[serde(skip_serializing_if = "is_empty_string")]
gitea: String,
#[cfg(feature = "github")]
#[serde(default)]
#[serde(skip_serializing_if = "is_empty_string")]
Expand Down Expand Up @@ -100,6 +104,11 @@ impl Package {
package.cratesio = target;
Ok(())
}
#[cfg(feature = "gitea")]
"gitea" => {
package.gitea = target;
Ok(())
}
#[cfg(feature = "github")]
"github" => {
package.github = target;
Expand Down Expand Up @@ -133,6 +142,8 @@ impl Package {
aur: String::new(),
#[cfg(feature = "crates-io")]
cratesio: String::new(),
#[cfg(feature = "gitea")]
gitea: String::new(),
#[cfg(feature = "github")]
github: String::new(),
#[cfg(feature = "gitlab")]
Expand Down Expand Up @@ -161,6 +172,8 @@ impl Package {
"aur" => vec![self.aur.clone()],
#[cfg(feature = "crates-io")]
"cratesio" => vec![self.cratesio.clone()],
#[cfg(feature = "gitea")]
"gitea" => vec![self.gitea.clone(), self.host.clone()],
#[cfg(feature = "github")]
"github" => vec![self.github.clone()],
#[cfg(feature = "gitlab")]
Expand Down

0 comments on commit 4b015d5

Please sign in to comment.