+
Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Feat/reimplement changelog #475

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and `Removed`.

## [Unreleased]

### Added

- Added back inline changelogs for remote version. Clicking on the remote version
will show the changelog inline instead of opening a browser window.

### Fixed

- Parsing error causing WeakAuras to fail parsing due to missing "version" field
Expand Down
20 changes: 17 additions & 3 deletions crates/core/src/addon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
error::ParseError,
error::{ParseError, RepositoryError},
repository::{
GitKind, GlobalReleaseChannel, ReleaseChannel, RemotePackage, RepositoryIdentifiers,
RepositoryKind, RepositoryMetadata, RepositoryPackage,
Changelog, GitKind, GlobalReleaseChannel, ReleaseChannel, RemotePackage,
RepositoryIdentifiers, RepositoryKind, RepositoryMetadata, RepositoryPackage,
},
utility::strip_non_digits,
};
Expand Down Expand Up @@ -360,6 +360,20 @@ impl Addon {
}
}

pub async fn changelog(
&self,
default_release_channel: GlobalReleaseChannel,
) -> Result<Changelog, RepositoryError> {
let text = if let Some(repo) = self.repository.as_ref() {
repo.get_changelog(self.release_channel, default_release_channel)
.await?
} else {
None
};

Ok(Changelog { text })
}

/// Returns the curse id of the addon, if applicable.
pub fn curse_id(&self) -> Option<i32> {
if self.repository_kind() == Some(RepositoryKind::Curse) {
Expand Down
32 changes: 31 additions & 1 deletion crates/core/src/repository/backend/curse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::*;
use crate::config::Flavor;
use crate::error::DownloadError;
use crate::network::post_json_async;
use crate::network::{post_json_async, request_async};
use crate::repository::{ReleaseChannel, RemotePackage};
use crate::utility::{regex_html_tags_to_newline, regex_html_tags_to_space, truncate};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -43,6 +44,35 @@ impl Backend for Curse {

Ok(metadata)
}

async fn get_changelog(
&self,
file_id: Option<i64>,
_tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError> {
let file_id = file_id.ok_or(RepositoryError::CurseChangelogFileId)?;

let url = format!(
"{}/addon/{}/file/{}/changelog",
API_ENDPOINT, self.id, file_id
);

let mut resp = request_async(&url, vec![], None).await?;

if resp.status().is_success() {
let changelog: String = resp.text_async().await?;

let c = regex_html_tags_to_newline()
.replace_all(&changelog, "\n")
.to_string();
let c = regex_html_tags_to_space().replace_all(&c, "").to_string();
let c = truncate(&c, 2500).to_string();

return Ok(Some(c));
}

Ok(None)
}
}

pub(crate) fn metadata_from_curse_package(flavor: Flavor, package: Package) -> RepositoryMetadata {
Expand Down
64 changes: 64 additions & 0 deletions crates/core/src/repository/backend/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@ mod github {

Ok(metadata)
}

async fn get_changelog(
&self,
_file_id: Option<i64>,
tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError> {
let tag_name = tag_name.ok_or(RepositoryError::GitChangelogTagName)?;

let mut path = self.url.path().split('/');
// Get rid of leading slash
path.next();

let author = path.next().ok_or(RepositoryError::GitMissingAuthor {
url: self.url.to_string(),
})?;
let repo = path.next().ok_or(RepositoryError::GitMissingRepo {
url: self.url.to_string(),
})?;

let url = format!(
"https://api.github.com/repos/{}/{}/releases/tags/{}",
author, repo, tag_name
);

let mut resp = request_async(&url, vec![], None).await?;

let release: Release = resp
.json()
.map_err(|_| RepositoryError::GitMissingRelease { url })?;

Ok(Some(release.body))
}
}

fn set_remote_package(
Expand Down Expand Up @@ -312,6 +344,38 @@ mod gitlab {

Ok(metadata)
}

async fn get_changelog(
&self,
_file_id: Option<i64>,
tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError> {
let tag_name = tag_name.ok_or(RepositoryError::GitChangelogTagName)?;

let mut path = self.url.path().split('/');
// Get rid of leading slash
path.next();

let author = path.next().ok_or(RepositoryError::GitMissingAuthor {
url: self.url.to_string(),
})?;
let repo = path.next().ok_or(RepositoryError::GitMissingRepo {
url: self.url.to_string(),
})?;

let url = format!(
"https://gitlab.com/api/v4/projects/{}%2F{}/releases/{}",
author, repo, tag_name
);

let mut resp = request_async(&url, vec![], None).await?;

let release: Release = resp
.json()
.map_err(|_| RepositoryError::GitMissingRelease { url })?;

Ok(Some(release.description))
}
}

#[derive(Debug, Deserialize, Clone)]
Expand Down
6 changes: 6 additions & 0 deletions crates/core/src/repository/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub use wowi::WowI;
#[async_trait]
pub(crate) trait Backend: DynClone + Send + Sync {
async fn get_metadata(&self) -> Result<RepositoryMetadata, RepositoryError>;

async fn get_changelog(
&self,
file_id: Option<i64>,
tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError>;
}

clone_trait_object!(Backend);
48 changes: 48 additions & 0 deletions crates/core/src/repository/backend/tukui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::config::Flavor;
use crate::error::{DownloadError, RepositoryError};
use crate::network::request_async;
use crate::repository::{ReleaseChannel, RemotePackage};
use crate::utility::{regex_html_tags_to_newline, regex_html_tags_to_space, truncate};

use async_trait::async_trait;
use chrono::{NaiveDateTime, TimeZone, Utc};
Expand All @@ -26,6 +27,39 @@ impl Backend for Tukui {

Ok(metadata)
}

async fn get_changelog(
&self,
_file_id: Option<i64>,
_tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError> {
let url = changelog_endpoint(&self.id, &self.flavor);

match self.flavor {
Flavor::Retail | Flavor::RetailBeta | Flavor::RetailPTR => {
// Only TukUI and ElvUI main addons has changelog which can be fetched.
// The others is embeded into a page.
if &self.id == "-1" || &self.id == "-2" {
let mut resp = request_async(&url, vec![], None).await?;

if resp.status().is_success() {
let changelog: String = resp.text()?;

let c = regex_html_tags_to_newline()
.replace_all(&changelog, "\n")
.to_string();
let c = regex_html_tags_to_space().replace_all(&c, "").to_string();
let c = truncate(&c, 2500).to_string();

return Ok(Some(c));
}
}
}
Flavor::Classic | Flavor::ClassicPTR => {}
}

Ok(None)
}
}

pub(crate) fn metadata_from_tukui_package(package: TukuiPackage) -> RepositoryMetadata {
Expand Down Expand Up @@ -92,6 +126,20 @@ fn api_endpoint(id: &str, flavor: &Flavor) -> String {
)
}

fn changelog_endpoint(id: &str, flavor: &Flavor) -> String {
match flavor {
Flavor::Retail | Flavor::RetailPTR | Flavor::RetailBeta => match id {
"-1" => "https://www.tukui.org/ui/tukui/changelog".to_owned(),
"-2" => "https://www.tukui.org/ui/elvui/changelog".to_owned(),
_ => format!("https://www.tukui.org/addons.php?id={}&changelog", id),
},
Flavor::Classic | Flavor::ClassicPTR => format!(
"https://www.tukui.org/classic-addons.php?id={}&changelog",
id
),
}
}

/// Function to fetch a remote addon package which contains
/// information about the addon on the repository.
pub(crate) async fn fetch_remote_package(
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/repository/backend/wowi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ impl Backend for WowI {

Ok(metadata)
}

async fn get_changelog(
&self,
_file_id: Option<i64>,
_tag_name: Option<String>,
) -> Result<Option<String>, RepositoryError> {
Ok(None)
}
}

pub(crate) fn metadata_from_wowi_package(package: WowIPackage) -> RepositoryMetadata {
Expand Down
44 changes: 44 additions & 0 deletions crates/core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,45 @@ impl RepositoryPackage {

Ok(())
}

/// Get changelog from the repository
///
/// `channel` is only used for the Curse & GitHub repository since
/// we can get unique changelogs for each version
pub(crate) async fn get_changelog(
&self,
release_channel: ReleaseChannel,
default_release_channel: GlobalReleaseChannel,
) -> Result<Option<String>, RepositoryError> {
let release_channel = if release_channel == ReleaseChannel::Default {
default_release_channel.convert_to_release_channel()
} else {
release_channel
};

let file_id = if self.kind == RepositoryKind::Curse {
self.metadata
.remote_packages
.get(&release_channel)
.and_then(|p| p.file_id)
} else {
None
};

let tag_name = if self.kind.is_git() {
let remote_package = self.metadata.remote_packages.get(&release_channel).ok_or(
RepositoryError::MissingPackageChannel {
channel: release_channel,
},
)?;

Some(remote_package.version.clone())
} else {
None
};

self.backend.get_changelog(file_id, tag_name).await
}
}

/// Metadata from one of the repository APIs
Expand Down Expand Up @@ -306,3 +345,8 @@ pub struct RepositoryIdentifiers {
pub curse: Option<i32>,
pub git: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Changelog {
pub text: Option<String>,
}
15 changes: 15 additions & 0 deletions crates/core/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ where
})
}

pub(crate) fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,
Some((idx, _)) => &s[..idx],
}
}

pub(crate) fn regex_html_tags_to_newline() -> Regex {
regex::Regex::new(r"<br ?/?>|#.\s").unwrap()
}

pub(crate) fn regex_html_tags_to_space() -> Regex {
regex::Regex::new(r"<[^>]*>|&#?\w+;|[gl]t;").unwrap()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载