Add git forge integration
This commit is contained in:
144
src/provider/github.rs
Normal file
144
src/provider/github.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::ApiErrorResponse;
|
||||
use super::Filter;
|
||||
use super::JsonError;
|
||||
use super::Project;
|
||||
use super::Provider;
|
||||
use super::SecretToken;
|
||||
|
||||
const PROVIDER_NAME: &str = "github";
|
||||
const ACCEPT_HEADER_JSON: &str = "application/vnd.github.v3+json";
|
||||
const GITHUB_API_BASEURL: &str =
|
||||
option_env!("GITHUB_API_BASEURL").unwrap_or("https://api.github.com");
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GithubProject {
|
||||
pub name: String,
|
||||
pub full_name: String,
|
||||
pub clone_url: String,
|
||||
pub ssh_url: String,
|
||||
pub private: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GithubUser {
|
||||
#[serde(rename = "login")]
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
impl Project for GithubProject {
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn namespace(&self) -> String {
|
||||
self.full_name
|
||||
.rsplit_once('/')
|
||||
.expect("Github project name did not include a namespace")
|
||||
.0
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn ssh_url(&self) -> String {
|
||||
self.ssh_url.clone()
|
||||
}
|
||||
|
||||
fn http_url(&self) -> String {
|
||||
self.clone_url.clone()
|
||||
}
|
||||
|
||||
fn private(&self) -> bool {
|
||||
self.private
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GithubApiErrorResponse {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl JsonError for GithubApiErrorResponse {
|
||||
fn to_string(self) -> String {
|
||||
self.message
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Github {
|
||||
filter: Filter,
|
||||
secret_token: SecretToken,
|
||||
}
|
||||
|
||||
impl Provider for Github {
|
||||
type Project = GithubProject;
|
||||
type Error = GithubApiErrorResponse;
|
||||
|
||||
fn new(
|
||||
filter: Filter,
|
||||
secret_token: SecretToken,
|
||||
api_url_override: Option<String>,
|
||||
) -> Result<Self, String> {
|
||||
if api_url_override.is_some() {
|
||||
return Err("API URL overriding is not supported for Github".to_string());
|
||||
}
|
||||
Ok(Self {
|
||||
filter,
|
||||
secret_token,
|
||||
})
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
String::from(PROVIDER_NAME)
|
||||
}
|
||||
|
||||
fn filter(&self) -> Filter {
|
||||
self.filter.clone()
|
||||
}
|
||||
|
||||
fn secret_token(&self) -> SecretToken {
|
||||
self.secret_token.clone()
|
||||
}
|
||||
|
||||
fn auth_header_key() -> String {
|
||||
"token".to_string()
|
||||
}
|
||||
|
||||
fn get_user_projects(
|
||||
&self,
|
||||
user: &str,
|
||||
) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!("{GITHUB_API_BASEURL}/users/{user}/repos"),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_group_projects(
|
||||
&self,
|
||||
group: &str,
|
||||
) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!("{GITHUB_API_BASEURL}/orgs/{group}/repos?type=all"),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_accessible_projects(
|
||||
&self,
|
||||
) -> Result<Vec<GithubProject>, ApiErrorResponse<GithubApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!("{GITHUB_API_BASEURL}/user/repos"),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_current_user(&self) -> Result<String, ApiErrorResponse<GithubApiErrorResponse>> {
|
||||
Ok(super::call::<GithubUser, GithubApiErrorResponse>(
|
||||
&format!("{GITHUB_API_BASEURL}/user"),
|
||||
&Self::auth_header_key(),
|
||||
&self.secret_token(),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)?
|
||||
.username)
|
||||
}
|
||||
}
|
||||
165
src/provider/gitlab.rs
Normal file
165
src/provider/gitlab.rs
Normal file
@@ -0,0 +1,165 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::ApiErrorResponse;
|
||||
use super::Filter;
|
||||
use super::JsonError;
|
||||
use super::Project;
|
||||
use super::Provider;
|
||||
use super::SecretToken;
|
||||
|
||||
const PROVIDER_NAME: &str = "gitlab";
|
||||
const ACCEPT_HEADER_JSON: &str = "application/json";
|
||||
const GITLAB_API_BASEURL: &str = option_env!("GITLAB_API_BASEURL").unwrap_or("https://gitlab.com");
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum GitlabVisibility {
|
||||
Private,
|
||||
Internal,
|
||||
Public,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GitlabProject {
|
||||
#[serde(rename = "path")]
|
||||
pub name: String,
|
||||
pub path_with_namespace: String,
|
||||
pub http_url_to_repo: String,
|
||||
pub ssh_url_to_repo: String,
|
||||
pub visibility: GitlabVisibility,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GitlabUser {
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
impl Project for GitlabProject {
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn namespace(&self) -> String {
|
||||
self.path_with_namespace
|
||||
.rsplit_once('/')
|
||||
.expect("Gitlab project name did not include a namespace")
|
||||
.0
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn ssh_url(&self) -> String {
|
||||
self.ssh_url_to_repo.clone()
|
||||
}
|
||||
|
||||
fn http_url(&self) -> String {
|
||||
self.http_url_to_repo.clone()
|
||||
}
|
||||
|
||||
fn private(&self) -> bool {
|
||||
matches!(self.visibility, GitlabVisibility::Private)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GitlabApiErrorResponse {
|
||||
#[serde(alias = "error_description")]
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl JsonError for GitlabApiErrorResponse {
|
||||
fn to_string(self) -> String {
|
||||
self.message
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Gitlab {
|
||||
filter: Filter,
|
||||
secret_token: SecretToken,
|
||||
api_url_override: Option<String>,
|
||||
}
|
||||
|
||||
impl Gitlab {
|
||||
fn api_url(&self) -> String {
|
||||
self.api_url_override
|
||||
.as_ref()
|
||||
.unwrap_or(&GITLAB_API_BASEURL.to_string())
|
||||
.trim_end_matches('/')
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Provider for Gitlab {
|
||||
type Project = GitlabProject;
|
||||
type Error = GitlabApiErrorResponse;
|
||||
|
||||
fn new(
|
||||
filter: Filter,
|
||||
secret_token: SecretToken,
|
||||
api_url_override: Option<String>,
|
||||
) -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
filter,
|
||||
secret_token,
|
||||
api_url_override,
|
||||
})
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
String::from(PROVIDER_NAME)
|
||||
}
|
||||
|
||||
fn filter(&self) -> Filter {
|
||||
self.filter.clone()
|
||||
}
|
||||
|
||||
fn secret_token(&self) -> SecretToken {
|
||||
self.secret_token.clone()
|
||||
}
|
||||
|
||||
fn auth_header_key() -> String {
|
||||
"bearer".to_string()
|
||||
}
|
||||
|
||||
fn get_user_projects(
|
||||
&self,
|
||||
user: &str,
|
||||
) -> Result<Vec<GitlabProject>, ApiErrorResponse<GitlabApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!("{}/api/v4/users/{}/projects", self.api_url(), user),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_group_projects(
|
||||
&self,
|
||||
group: &str,
|
||||
) -> Result<Vec<GitlabProject>, ApiErrorResponse<GitlabApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!(
|
||||
"{}/api/v4/groups/{}/projects?include_subgroups=true&archived=false",
|
||||
self.api_url(),
|
||||
group
|
||||
),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_accessible_projects(
|
||||
&self,
|
||||
) -> Result<Vec<GitlabProject>, ApiErrorResponse<GitlabApiErrorResponse>> {
|
||||
self.call_list(
|
||||
&format!("{}/api/v4/projects", self.api_url(),),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_current_user(&self) -> Result<String, ApiErrorResponse<GitlabApiErrorResponse>> {
|
||||
Ok(super::call::<GitlabUser, GitlabApiErrorResponse>(
|
||||
&format!("{}/api/v4/user", self.api_url()),
|
||||
&Self::auth_header_key(),
|
||||
&self.secret_token(),
|
||||
Some(ACCEPT_HEADER_JSON),
|
||||
)?
|
||||
.username)
|
||||
}
|
||||
}
|
||||
340
src/provider/mod.rs
Normal file
340
src/provider/mod.rs
Normal file
@@ -0,0 +1,340 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// Required to use the `json()` method from the trait
|
||||
use isahc::ReadResponseExt;
|
||||
|
||||
pub mod github;
|
||||
pub mod gitlab;
|
||||
|
||||
pub use github::Github;
|
||||
pub use gitlab::Gitlab;
|
||||
|
||||
use crate::{Remote, RemoteType, RepoConfig};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, clap::ArgEnum, Clone)]
|
||||
pub enum RemoteProvider {
|
||||
#[serde(alias = "github", alias = "GitHub")]
|
||||
Github,
|
||||
#[serde(alias = "gitlab", alias = "GitLab")]
|
||||
Gitlab,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum ProjectResponse<T, U> {
|
||||
Success(Vec<T>),
|
||||
Failure(U),
|
||||
}
|
||||
|
||||
pub trait Project {
|
||||
fn into_repo_config(
|
||||
self,
|
||||
provider_name: &str,
|
||||
worktree_setup: bool,
|
||||
force_ssh: bool,
|
||||
) -> RepoConfig
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
RepoConfig {
|
||||
name: self.name(),
|
||||
worktree_setup,
|
||||
remotes: Some(vec![Remote {
|
||||
name: String::from(provider_name),
|
||||
url: if force_ssh || self.private() {
|
||||
self.ssh_url()
|
||||
} else {
|
||||
self.http_url()
|
||||
},
|
||||
remote_type: if force_ssh || self.private() {
|
||||
RemoteType::Ssh
|
||||
} else {
|
||||
RemoteType::Https
|
||||
},
|
||||
}]),
|
||||
}
|
||||
}
|
||||
|
||||
fn name(&self) -> String;
|
||||
fn namespace(&self) -> String;
|
||||
fn ssh_url(&self) -> String;
|
||||
fn http_url(&self) -> String;
|
||||
fn private(&self) -> bool;
|
||||
}
|
||||
|
||||
type SecretToken = String;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Filter {
|
||||
users: Vec<String>,
|
||||
groups: Vec<String>,
|
||||
owner: bool,
|
||||
access: bool,
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
pub fn new(users: Vec<String>, groups: Vec<String>, owner: bool, access: bool) -> Self {
|
||||
Filter {
|
||||
users,
|
||||
groups,
|
||||
owner,
|
||||
access,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ApiErrorResponse<T>
|
||||
where
|
||||
T: JsonError,
|
||||
{
|
||||
Json(T),
|
||||
String(String),
|
||||
}
|
||||
|
||||
impl<T> From<String> for ApiErrorResponse<T>
|
||||
where
|
||||
T: JsonError,
|
||||
{
|
||||
fn from(s: String) -> ApiErrorResponse<T> {
|
||||
ApiErrorResponse::String(s)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait JsonError {
|
||||
fn to_string(self) -> String;
|
||||
}
|
||||
|
||||
pub trait Provider {
|
||||
type Project: serde::de::DeserializeOwned + Project;
|
||||
type Error: serde::de::DeserializeOwned + JsonError;
|
||||
|
||||
fn new(
|
||||
filter: Filter,
|
||||
secret_token: SecretToken,
|
||||
api_url_override: Option<String>,
|
||||
) -> Result<Self, String>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn name(&self) -> String;
|
||||
fn filter(&self) -> Filter;
|
||||
fn secret_token(&self) -> SecretToken;
|
||||
fn auth_header_key() -> String;
|
||||
|
||||
fn get_user_projects(
|
||||
&self,
|
||||
user: &str,
|
||||
) -> Result<Vec<Self::Project>, ApiErrorResponse<Self::Error>>;
|
||||
|
||||
fn get_group_projects(
|
||||
&self,
|
||||
group: &str,
|
||||
) -> Result<Vec<Self::Project>, ApiErrorResponse<Self::Error>>;
|
||||
|
||||
fn get_own_projects(&self) -> Result<Vec<Self::Project>, ApiErrorResponse<Self::Error>> {
|
||||
self.get_user_projects(&self.get_current_user()?)
|
||||
}
|
||||
|
||||
fn get_accessible_projects(&self) -> Result<Vec<Self::Project>, ApiErrorResponse<Self::Error>>;
|
||||
|
||||
fn get_current_user(&self) -> Result<String, ApiErrorResponse<Self::Error>>;
|
||||
|
||||
///
|
||||
/// Calls the API at specific uri and expects a successful response of Vec<T> back, or an error
|
||||
/// response U
|
||||
///
|
||||
/// Handles paging with "link" HTTP headers properly and reads all pages to
|
||||
/// the end.
|
||||
fn call_list(
|
||||
&self,
|
||||
uri: &str,
|
||||
accept_header: Option<&str>,
|
||||
) -> Result<Vec<Self::Project>, ApiErrorResponse<Self::Error>> {
|
||||
let mut results = vec![];
|
||||
|
||||
let client = isahc::HttpClient::new().map_err(|error| error.to_string())?;
|
||||
|
||||
let request = isahc::Request::builder()
|
||||
.uri(uri)
|
||||
.method("GET")
|
||||
.header("accept", accept_header.unwrap_or("application/json"))
|
||||
.header(
|
||||
"authorization",
|
||||
format!("{} {}", Self::auth_header_key(), &self.secret_token()),
|
||||
)
|
||||
.body(())
|
||||
.map_err(|error| error.to_string())?;
|
||||
|
||||
let mut response = client
|
||||
.send(request)
|
||||
.map_err(|error| ApiErrorResponse::String(error.to_string()))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let r: Self::Error = response
|
||||
.json()
|
||||
.map_err(|error| format!("Failed deserializing error response: {}", error))?;
|
||||
return Err(ApiErrorResponse::Json(r));
|
||||
}
|
||||
|
||||
let result: Vec<Self::Project> = response
|
||||
.json()
|
||||
.map_err(|error| format!("Failed deserializing response: {}", error))?;
|
||||
|
||||
results.extend(result);
|
||||
|
||||
if let Some(link_header) = response.headers().get("link") {
|
||||
let link_header = link_header.to_str().map_err(|error| error.to_string())?;
|
||||
|
||||
let link_header =
|
||||
parse_link_header::parse(link_header).map_err(|error| error.to_string())?;
|
||||
|
||||
let next_page = link_header.get(&Some(String::from("next")));
|
||||
|
||||
if let Some(page) = next_page {
|
||||
let following_repos = self.call_list(&page.raw_uri, accept_header)?;
|
||||
results.extend(following_repos);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
fn get_repos(
|
||||
&self,
|
||||
worktree_setup: bool,
|
||||
force_ssh: bool,
|
||||
) -> Result<HashMap<String, Vec<RepoConfig>>, String> {
|
||||
let mut repos = vec![];
|
||||
|
||||
if self.filter().owner {
|
||||
repos.extend(self.get_own_projects().map_err(|error| match error {
|
||||
ApiErrorResponse::Json(x) => x.to_string(),
|
||||
ApiErrorResponse::String(s) => s,
|
||||
})?);
|
||||
}
|
||||
|
||||
if self.filter().access {
|
||||
let accessible_projects =
|
||||
self.get_accessible_projects()
|
||||
.map_err(|error| match error {
|
||||
ApiErrorResponse::Json(x) => x.to_string(),
|
||||
ApiErrorResponse::String(s) => s,
|
||||
})?;
|
||||
|
||||
for accessible_project in accessible_projects {
|
||||
let mut already_present = false;
|
||||
for repo in &repos {
|
||||
if repo.name() == accessible_project.name()
|
||||
&& repo.namespace() == accessible_project.namespace()
|
||||
{
|
||||
already_present = true;
|
||||
}
|
||||
}
|
||||
if !already_present {
|
||||
repos.push(accessible_project);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for user in &self.filter().users {
|
||||
let user_projects = self.get_user_projects(user).map_err(|error| match error {
|
||||
ApiErrorResponse::Json(x) => x.to_string(),
|
||||
ApiErrorResponse::String(s) => s,
|
||||
})?;
|
||||
|
||||
for user_project in user_projects {
|
||||
let mut already_present = false;
|
||||
for repo in &repos {
|
||||
if repo.name() == user_project.name()
|
||||
&& repo.namespace() == user_project.namespace()
|
||||
{
|
||||
already_present = true;
|
||||
}
|
||||
}
|
||||
if !already_present {
|
||||
repos.push(user_project);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for group in &self.filter().groups {
|
||||
let group_projects = self
|
||||
.get_group_projects(group)
|
||||
.map_err(|error| match error {
|
||||
ApiErrorResponse::Json(x) => x.to_string(),
|
||||
ApiErrorResponse::String(s) => s,
|
||||
})?;
|
||||
for group_project in group_projects {
|
||||
let mut already_present = false;
|
||||
for repo in &repos {
|
||||
if repo.name() == group_project.name()
|
||||
&& repo.namespace() == group_project.namespace()
|
||||
{
|
||||
already_present = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !already_present {
|
||||
repos.push(group_project);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut ret: HashMap<String, Vec<RepoConfig>> = HashMap::new();
|
||||
|
||||
for repo in repos {
|
||||
let namespace = repo.namespace().clone();
|
||||
|
||||
let repo = repo.into_repo_config(&self.name(), worktree_setup, force_ssh);
|
||||
|
||||
ret.entry(namespace).or_insert(vec![]).push(repo);
|
||||
}
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
|
||||
fn call<T, U>(
|
||||
uri: &str,
|
||||
auth_header_key: &str,
|
||||
secret_token: &str,
|
||||
accept_header: Option<&str>,
|
||||
) -> Result<T, ApiErrorResponse<U>>
|
||||
where
|
||||
T: serde::de::DeserializeOwned,
|
||||
U: serde::de::DeserializeOwned + JsonError,
|
||||
{
|
||||
let client = isahc::HttpClient::new().map_err(|error| error.to_string())?;
|
||||
|
||||
let request = isahc::Request::builder()
|
||||
.uri(uri)
|
||||
.header("accept", accept_header.unwrap_or("application/json"))
|
||||
.header(
|
||||
"authorization",
|
||||
format!("{} {}", &auth_header_key, &secret_token),
|
||||
)
|
||||
.body(())
|
||||
.map_err(|error| ApiErrorResponse::String(error.to_string()))?;
|
||||
|
||||
let mut response = client
|
||||
.send(request)
|
||||
.map_err(|error| ApiErrorResponse::String(error.to_string()))?;
|
||||
|
||||
let success = response.status().is_success();
|
||||
|
||||
if !success {
|
||||
let response: U = response
|
||||
.json()
|
||||
.map_err(|error| format!("Failed deserializing error response: {}", error))?;
|
||||
|
||||
return Err(ApiErrorResponse::Json(response));
|
||||
}
|
||||
|
||||
let response: T = response
|
||||
.json()
|
||||
.map_err(|error| format!("Failed deserializing response: {}", error))?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
Reference in New Issue
Block a user