This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29da598
commit 9e759f6
Showing
26 changed files
with
3,703 additions
and
106 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::api::models::github; | ||
use crate::config; | ||
use crate::errors::GitError; | ||
|
||
use log::error; | ||
use reqwest; | ||
|
||
fn headers(token: &str) -> reqwest::header::HeaderMap { | ||
let mut headers = reqwest::header::HeaderMap::new(); | ||
headers.insert( | ||
reqwest::header::AUTHORIZATION, | ||
reqwest::header::HeaderValue::from_str(&format!("token {}", token)).unwrap(), | ||
); | ||
headers.insert( | ||
reqwest::header::ACCEPT, | ||
reqwest::header::HeaderValue::from_static("application/vnd.github.v3+json"), | ||
); | ||
headers.insert( | ||
reqwest::header::ACCEPT_ENCODING, | ||
reqwest::header::HeaderValue::from_static("Accept-Encoding: deflate, gzip"), | ||
); | ||
headers | ||
} | ||
|
||
fn make_repo_url(org: &str, repo: &str) -> String { | ||
let hostname = match config::CONFIG.github.hostname.as_ref() { | ||
Some(hostname) => hostname.clone(), | ||
_ => "github.com".to_string(), | ||
}; | ||
format!("https://api.{}/repos/{}/{}", hostname, org, repo) | ||
} | ||
|
||
pub fn get_pull( | ||
client: &reqwest::Client, | ||
org: &str, | ||
repo: &str, | ||
number: i64, | ||
) -> Result<github::RepoPr, GitError> { | ||
let res: github::RepoPr = client | ||
.get(&format!("{}/pulls/{}", make_repo_url(org, repo), number)) | ||
.headers(headers(&config::CONFIG.github.api_token)) | ||
.send()? | ||
.json()?; | ||
Ok(res) | ||
} | ||
|
||
pub fn create_issue_comment( | ||
client: &reqwest::Client, | ||
org: &str, | ||
repo: &str, | ||
number: i64, | ||
body: &str, | ||
) -> Result<(), GitError> { | ||
let mut res = client | ||
.post(&format!( | ||
"{}/issues/{}/comments", | ||
make_repo_url(org, repo), | ||
number | ||
)) | ||
.headers(headers(&config::CONFIG.github.api_token)) | ||
.body(serde_json::json!({"body":body.to_string()}).to_string()) | ||
.send()?; | ||
let body = res.text()?; | ||
match res.status() { | ||
reqwest::StatusCode::CREATED => Ok(()), | ||
_ => { | ||
let msg = format!("Error creating issue comment: res={:#?} body={}", res, body); | ||
error!("{}", msg); | ||
Err(GitError { message: msg }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use crate::api::models::gitlab; | ||
use crate::config; | ||
use crate::errors::GitError; | ||
|
||
use log::error; | ||
use reqwest; | ||
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET}; | ||
|
||
fn headers(token: &str) -> reqwest::header::HeaderMap { | ||
let token_header = reqwest::header::HeaderName::from_static("private-token"); | ||
let mut headers = reqwest::header::HeaderMap::new(); | ||
headers.insert( | ||
token_header, | ||
reqwest::header::HeaderValue::from_str(token).unwrap(), | ||
); | ||
headers.insert( | ||
reqwest::header::ACCEPT, | ||
reqwest::header::HeaderValue::from_static("application/json"), | ||
); | ||
headers.insert( | ||
reqwest::header::ACCEPT_ENCODING, | ||
reqwest::header::HeaderValue::from_static("Accept-Encoding: deflate, gzip"), | ||
); | ||
headers | ||
} | ||
|
||
fn make_api_url(project: &str) -> String { | ||
let hostname = match config::CONFIG.gitlab.hostname.as_ref() { | ||
Some(hostname) => hostname.clone(), | ||
_ => "gitlab.com".to_string(), | ||
}; | ||
let project = utf8_percent_encode(project, PATH_SEGMENT_ENCODE_SET).to_string(); | ||
format!("https://{}/api/v4/projects/{}", hostname, project) | ||
} | ||
|
||
pub fn make_ext_url(project: &str) -> String { | ||
let hostname = match config::CONFIG.gitlab.hostname.as_ref() { | ||
Some(hostname) => hostname.clone(), | ||
_ => "gitlab.com".to_string(), | ||
}; | ||
format!("https://{}/{}", hostname, project) | ||
} | ||
|
||
pub fn get_pipelines( | ||
client: &reqwest::Client, | ||
project: &str, | ||
page: i64, | ||
per_page: i64, | ||
) -> Result<Vec<gitlab::Pipeline>, GitError> { | ||
let res: Vec<gitlab::Pipeline> = client | ||
.get(&format!( | ||
"{}/pipelines?page={}&per_page={}", | ||
make_api_url(project), | ||
page, | ||
per_page | ||
)) | ||
.headers(headers(&config::CONFIG.gitlab.api_token)) | ||
.send()? | ||
.json()?; | ||
Ok(res) | ||
} | ||
|
||
pub fn retry_pipeline( | ||
client: &reqwest::Client, | ||
project: &str, | ||
pipeline_id: i64, | ||
) -> Result<(), GitError> { | ||
let res = client | ||
.post(&format!( | ||
"{}/pipelines/{}/retry", | ||
make_api_url(project), | ||
pipeline_id | ||
)) | ||
.headers(headers(&config::CONFIG.gitlab.api_token)) | ||
.send()?; | ||
|
||
match res.status() { | ||
reqwest::StatusCode::CREATED => Ok(()), | ||
_ => { | ||
let msg = format!("Error retrying pipeline: {:#?}", res); | ||
error!("{}", msg); | ||
Err(GitError { message: msg }) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_make_ext_url() { | ||
assert_eq!( | ||
make_ext_url("brndnmtthws-oss/conky"), | ||
"https://gitlab.com/brndnmtthws-oss/conky" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_make_api_url() { | ||
assert_eq!( | ||
make_api_url("brndnmtthws-oss/conky"), | ||
"https://gitlab.com/api/v4/projects/brndnmtthws-oss%2Fconky" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod github_client; | ||
pub mod github_proto; | ||
pub mod github_signature; | ||
pub mod gitlab_client; | ||
pub mod models; |
Oops, something went wrong.