Skip to content

Commit

Permalink
Merge pull request #94 from bleikurr/master
Browse files Browse the repository at this point in the history
Support for gitlab self hosted servers
  • Loading branch information
jaemk committed Sep 15, 2022
2 parents 05b7f09 + 9b29a78 commit 9a78c55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [unreleased]
### Added
- Support for self hosted gitlab servers
### Changed
### Removed

Expand Down
40 changes: 30 additions & 10 deletions src/backends/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ impl Release {
/// `ReleaseList` Builder
#[derive(Clone, Debug)]
pub struct ReleaseListBuilder {
host: String,
repo_owner: Option<String>,
repo_name: Option<String>,
target: Option<String>,
auth_token: Option<String>,
}
impl ReleaseListBuilder {
/// Set the gitlab `host` url
pub fn with_host(&mut self, host: &str) -> &mut Self {
self.host = host.to_owned();
self
}

/// Set the repo owner, used to build a gitlab api url
pub fn repo_owner(&mut self, owner: &str) -> &mut Self {
self.repo_owner = Some(owner.to_owned());
Expand All @@ -89,7 +96,7 @@ impl ReleaseListBuilder {

/// Set the authorization token, used in requests to the gitlab api url
///
/// This is to support private repos where you need a GitHub auth token.
/// This is to support private repos where you need a Gitlab auth token.
/// **Make sure not to bake the token into your app**; it is recommended
/// you obtain it via another mechanism, such as environment variables
/// or prompting the user for input
Expand All @@ -101,6 +108,7 @@ impl ReleaseListBuilder {
/// Verify builder args, returning a `ReleaseList`
pub fn build(&self) -> Result<ReleaseList> {
Ok(ReleaseList {
host: self.host.clone(),
repo_owner: if let Some(ref owner) = self.repo_owner {
owner.to_owned()
} else {
Expand All @@ -117,10 +125,11 @@ impl ReleaseListBuilder {
}
}

/// `ReleaseList` provides a builder api for querying a GitHub repo,
/// `ReleaseList` provides a builder api for querying a Gitlab repo,
/// returning a `Vec` of available `Release`s
#[derive(Clone, Debug)]
pub struct ReleaseList {
host: String,
repo_owner: String,
repo_name: String,
target: Option<String>,
Expand All @@ -130,6 +139,7 @@ impl ReleaseList {
/// Initialize a ReleaseListBuilder
pub fn configure() -> ReleaseListBuilder {
ReleaseListBuilder {
host: String::from("https://gitlab.com"),
repo_owner: None,
repo_name: None,
target: None,
Expand All @@ -142,8 +152,8 @@ impl ReleaseList {
pub fn fetch(self) -> Result<Vec<Release>> {
set_ssl_vars!();
let api_url = format!(
"https://gitlab.com/api/v4/projects/{}%2F{}/releases",
self.repo_owner, self.repo_name
"{}/api/v4/projects/{}%2F{}/releases",
self.host, self.repo_owner, self.repo_name
);
let releases = self.fetch_releases(&api_url)?;
let releases = match self.target {
Expand Down Expand Up @@ -211,6 +221,7 @@ impl ReleaseList {
/// `https://gitlab.com/api/v4/projects/<repo_owner>%2F<repo_name>/releases`
#[derive(Debug)]
pub struct UpdateBuilder {
host: String,
repo_owner: Option<String>,
repo_name: Option<String>,
target: Option<String>,
Expand All @@ -233,6 +244,12 @@ impl UpdateBuilder {
Default::default()
}

/// Set the gitlab `host` url
pub fn with_host(&mut self, host: &str) -> &mut Self {
self.host = host.to_owned();
self
}

/// Set the repo owner, used to build a gitlab api url
pub fn repo_owner(&mut self, owner: &str) -> &mut Self {
self.repo_owner = Some(owner.to_owned());
Expand Down Expand Up @@ -354,7 +371,7 @@ impl UpdateBuilder {

/// Set the authorization token, used in requests to the gitlab api url
///
/// This is to support private repos where you need a GitHub auth token.
/// This is to support private repos where you need a Gitlab auth token.
/// **Make sure not to bake the token into your app**; it is recommended
/// you obtain it via another mechanism, such as environment variables
/// or prompting the user for input
Expand All @@ -375,6 +392,7 @@ impl UpdateBuilder {
};

Ok(Box::new(Update {
host: self.host.to_owned(),
repo_owner: if let Some(ref owner) = self.repo_owner {
owner.to_owned()
} else {
Expand Down Expand Up @@ -417,9 +435,10 @@ impl UpdateBuilder {
}
}

/// Updates to a specified or latest release distributed via GitHub
/// Updates to a specified or latest release distributed via Gitlab
#[derive(Debug)]
pub struct Update {
host: String,
repo_owner: String,
repo_name: String,
target: String,
Expand All @@ -446,8 +465,8 @@ impl ReleaseUpdate for Update {
fn get_latest_release(&self) -> Result<Release> {
set_ssl_vars!();
let api_url = format!(
"https://gitlab.com/api/v4/projects/{}%2F{}/releases",
self.repo_owner, self.repo_name
"{}/api/v4/projects/{}%2F{}/releases",
self.host, self.repo_owner, self.repo_name
);
let resp = reqwest::blocking::Client::new()
.get(&api_url)
Expand All @@ -468,8 +487,8 @@ impl ReleaseUpdate for Update {
fn get_release_version(&self, ver: &str) -> Result<Release> {
set_ssl_vars!();
let api_url = format!(
"https://gitlab.com/api/v4/projects/{}%2F{}/releases/{}",
self.repo_owner, self.repo_name, ver
"{}/api/v4/projects/{}%2F{}/releases/{}",
self.host, self.repo_owner, self.repo_name, ver
);
let resp = reqwest::blocking::Client::new()
.get(&api_url)
Expand Down Expand Up @@ -539,6 +558,7 @@ impl ReleaseUpdate for Update {
impl Default for UpdateBuilder {
fn default() -> Self {
Self {
host: String::from("https://gitlab.com"),
repo_owner: None,
repo_name: None,
target: None,
Expand Down

0 comments on commit 9a78c55

Please sign in to comment.